Skip to content
This repository was archived by the owner on Mar 2, 2018. It is now read-only.

Commit d10894a

Browse files
committed
issue #163, added a year selector
1 parent f5cba0e commit d10894a

File tree

1 file changed

+100
-32
lines changed

1 file changed

+100
-32
lines changed

src/ng2-datetime-picker.component.ts

Lines changed: 100 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,22 @@ import {Ng2Datetime} from './ng2-datetime';
1212

1313
declare var moment: any;
1414

15-
//@TODO
16-
// . display currently selected day
17-
18-
/**
19-
* show a selected date in monthly calendar
20-
*/
21-
@Component({
22-
providers : [Ng2Datetime],
23-
selector : 'ng2-datetime-picker',
24-
template : `
25-
<div class="closing-layer" (click)="close()" *ngIf="showCloseLayer" ></div>
26-
<div class="ng2-datetime-picker">
27-
<div class="close-button" *ngIf="showCloseButton" (click)="close()"></div>
28-
29-
<!-- Month - Year -->
15+
let monthYearHTML: string = `
3016
<div class="month" *ngIf="!timeOnly">
3117
<b class="prev_next prev year" (click)="updateMonthData(-12)">&laquo;</b>
3218
<b class="prev_next prev month" (click)="updateMonthData(-1)">&lsaquo;</b>
3319
<span title="{{monthData?.fullName}}">
34-
{{monthData?.shortName}}
20+
{{monthData?.shortName}}
21+
</span>
22+
<span (click)="showYearSelector = true">
23+
{{monthData.year}}
3524
</span>
36-
{{monthData.year}}
3725
<b class="prev_next next year" (click)="updateMonthData(+12)">&raquo;</b>
3826
<b class="prev_next next month" (click)="updateMonthData(+1)">&rsaquo;</b>
3927
</div>
28+
`;
4029

30+
let daysHTML: string = `
4131
<div class="week-numbers-and-days"
4232
[ngClass]="{'show-week-numbers': !timeOnly && showWeekNumbers}">
4333
<!-- Week -->
@@ -90,12 +80,9 @@ declare var moment: any;
9080
</div>
9181
</div>
9282
</div>
83+
`;
9384

94-
<div class="shortcuts" *ngIf="showTodayShortcut">
95-
<a href="#" (click)="selectToday()">Today</a>
96-
</div>
97-
98-
<!-- Time -->
85+
let timeHTML: string = `
9986
<div class="time" id="time" *ngIf="!dateOnly">
10087
<div class="select-current-time" (click)="selectCurrentTime()">{{locale.currentTime}}</div>
10188
<label class="timeLabel">{{locale.time}}</label>
@@ -121,6 +108,50 @@ declare var moment: any;
121108
type="range" min="0" max="59" range="10" [(ngModel)]="minute"/>
122109
</div>
123110
</div>
111+
`;
112+
113+
let yearSelectorHTML: string = `
114+
<div class="year-selector" *ngIf="showYearSelector">
115+
<div class="locale">
116+
<b>{{locale.year}}</b>
117+
</div>
118+
<span class="year"
119+
*ngFor="let year of yearsSelectable"
120+
(click)="selectYear(year)">
121+
{{year}}
122+
</span>
123+
</div>
124+
`;
125+
126+
//@TODO
127+
// . display currently selected day
128+
129+
/**
130+
* show a selected date in monthly calendar
131+
*/
132+
@Component({
133+
providers : [Ng2Datetime],
134+
selector : 'ng2-datetime-picker',
135+
template : `
136+
<div class="closing-layer" (click)="close()" *ngIf="showCloseLayer" ></div>
137+
<div class="ng2-datetime-picker">
138+
<div class="close-button" *ngIf="showCloseButton" (click)="close()"></div>
139+
140+
<!-- Month - Year -->
141+
${monthYearHTML}
142+
143+
<!-- Week number / Days -->
144+
${daysHTML}
145+
146+
<div class="shortcuts" *ngIf="showTodayShortcut">
147+
<a href="#" (click)="selectToday()">Today</a>
148+
</div>
149+
150+
<!-- Hour Minute -->
151+
${timeHTML}
152+
153+
<!-- Year Selector -->
154+
${yearSelectorHTML}
124155
</div>
125156
`,
126157
styles : [
@@ -270,6 +301,27 @@ declare var moment: any;
270301
padding: 10px;
271302
text-transform: Capitalize;
272303
}
304+
.ng2-datetime-picker .year-selector {
305+
position: absolute;
306+
top: 0;
307+
left: 0;
308+
background: #fff;
309+
height: 100%;
310+
overflow: auto;
311+
padding: 5px;
312+
z-index: 2;
313+
}
314+
.ng2-datetime-picker .year-selector .locale{
315+
text-align: center;
316+
}
317+
.ng2-datetime-picker .year-selector .year {
318+
display: inline-block;
319+
cursor: pointer;
320+
padding: 2px 5px;
321+
}
322+
.ng2-datetime-picker .year-selector .year:hover {
323+
background-color: #ddd;
324+
}
273325
.ng2-datetime-picker .select-current-time {
274326
position: absolute;
275327
top: 1em;
@@ -362,6 +414,8 @@ export class Ng2DatetimePickerComponent {
362414
public el:HTMLElement; // this component element
363415
public disabledDatesInTime: number[];
364416
public locale = Ng2Datetime.locale;
417+
public showYearSelector = false;
418+
365419
private _monthData: any;
366420

367421
public constructor (
@@ -372,6 +426,16 @@ export class Ng2DatetimePickerComponent {
372426
this.el = elementRef.nativeElement;
373427
}
374428

429+
public get yearsSelectable(): number[] {
430+
let startYear = this.year - 100;
431+
let endYear = this.year + 50;
432+
let years: number[] = [];
433+
for (let year = startYear; year < endYear; year++) {
434+
years.push(year);
435+
}
436+
return years;
437+
}
438+
375439
public get year(): number {
376440
return this.selectedDate.getFullYear();
377441
}
@@ -397,21 +461,11 @@ export class Ng2DatetimePickerComponent {
397461
return dt;
398462
}
399463

400-
public isWeekend(dayNum: number, month?: number): boolean {
401-
if (typeof month === 'undefined') {
402-
return Ng2Datetime.weekends.indexOf(dayNum % 7) !== -1; //weekday index
403-
} else {
404-
let weekday = this.toDate(dayNum, month).getDay();
405-
return Ng2Datetime.weekends.indexOf(weekday) !== -1;
406-
}
407-
}
408-
409464
public set year (year) {}
410465
public set month (month) {}
411466
public set day (day) {}
412467
public set today (today) {}
413468

414-
415469
public ngOnInit() {
416470
if(!this.defaultValue || isNaN(this.defaultValue.getTime())) {
417471
this.defaultValue = new Date();
@@ -431,6 +485,20 @@ export class Ng2DatetimePickerComponent {
431485
this._monthData = this.ng2Datetime.getMonthData(this.year, this.month);
432486
}
433487

488+
public isWeekend(dayNum: number, month?: number): boolean {
489+
if (typeof month === 'undefined') {
490+
return Ng2Datetime.weekends.indexOf(dayNum % 7) !== -1; //weekday index
491+
} else {
492+
let weekday = this.toDate(dayNum, month).getDay();
493+
return Ng2Datetime.weekends.indexOf(weekday) !== -1;
494+
}
495+
}
496+
497+
public selectYear(year) {
498+
this._monthData = this.ng2Datetime.getMonthData(year, this._monthData.month);
499+
this.showYearSelector = false;
500+
}
501+
434502
public toDate (day:number, month?: number):Date {
435503
return new Date(this._monthData.year, month || this._monthData.month, day);
436504
}

0 commit comments

Comments
 (0)