Project

General

Profile

1
import { Component, Input, Output, EventEmitter, OnChanges, SimpleChanges, ElementRef, ViewEncapsulation, ChangeDetectorRef, Renderer, forwardRef } from "@angular/core";
2
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from "@angular/forms";
3
import { IMyDate, IMyDateRange, IMyMonth, IMyCalendarDay, IMyWeek, IMyDayLabels, IMyMonthLabels, IMyOptions, IMyDateModel, IMyInputAutoFill, IMyInputFieldChanged, IMyCalendarViewChanged, IMyInputFocusBlur } from "./interfaces/index";
4
import { LocaleService } from "./services/my-date-picker.locale.service";
5
import { UtilService } from "./services/my-date-picker.util.service";
6

    
7
// webpack1_
8
declare var require: any;
9
// declare var myDpStyles: string = require("./my-date-picker.component.css");
10
// declare var myDpTpl: string = require("./my-date-picker.component.html");
11
// webpack2_
12

    
13
export const MYDP_VALUE_ACCESSOR: any = {
14
    provide: NG_VALUE_ACCESSOR,
15
    useExisting: forwardRef(() => MyDatePicker),
16
    multi: true
17
};
18

    
19
@Component({
20
    selector: "my-date-picker",
21
    styleUrls: ['my-date-picker.component.css'],
22
    templateUrl: 'my-date-picker.component.html',
23
    providers: [LocaleService, UtilService, MYDP_VALUE_ACCESSOR],
24
    encapsulation: ViewEncapsulation.None
25
})
26

    
27
export class MyDatePicker implements OnChanges, ControlValueAccessor {
28
    @Input() options: any;
29
    @Input() locale: string;
30
    @Input() defaultMonth: string;
31
    @Input() selDate: string;
32
    @Input() placeholder: string;
33
    @Input() selector: number;
34
    @Output() dateChanged: EventEmitter<IMyDateModel> = new EventEmitter<IMyDateModel>();
35
    @Output() inputFieldChanged: EventEmitter<IMyInputFieldChanged> = new EventEmitter<IMyInputFieldChanged>();
36
    @Output() calendarViewChanged: EventEmitter<IMyCalendarViewChanged> = new EventEmitter<IMyCalendarViewChanged>();
37
    @Output() calendarToggle: EventEmitter<number> = new EventEmitter<number>();
38
    @Output() inputFocusBlur: EventEmitter<IMyInputFocusBlur> = new EventEmitter<IMyInputFocusBlur>();
39

    
40
    onChangeCb: (_: any) => void = () => { };
41
    onTouchedCb: () => void = () => { };
42

    
43
    showSelector: boolean = false;
44
    visibleMonth: IMyMonth = {monthTxt: "", monthNbr: 0, year: 0};
45
    selectedMonth: IMyMonth = {monthTxt: "", monthNbr: 0, year: 0};
46
    selectedDate: IMyDate = {year: 0, month: 0, day: 0};
47
    weekDays: Array<string> = [];
48
    dates: Array<IMyWeek> = [];
49
    selectionDayTxt: string = "";
50
    invalidDate: boolean = false;
51
    disableTodayBtn: boolean = false;
52
    dayIdx: number = 0;
53
    weekDayOpts: Array<string> = ["su", "mo", "tu", "we", "th", "fr", "sa"];
54
    autoFillOpts: IMyInputAutoFill = {separator: "", formatParts: [], enabled: true};
55

    
56
    editMonth: boolean = false;
57
    invalidMonth: boolean = false;
58
    editYear: boolean = false;
59
    invalidYear: boolean = false;
60

    
61
    prevMonthDisabled: boolean = false;
62
    nextMonthDisabled: boolean = false;
63
    prevYearDisabled: boolean = false;
64
    nextYearDisabled: boolean = false;
65

    
66
    PREV_MONTH: number = 1;
67
    CURR_MONTH: number = 2;
68
    NEXT_MONTH: number = 3;
69

    
70
    MIN_YEAR: number = 1000;
71
    MAX_YEAR: number = 9999;
72

    
73
    // Default options
74
    opts: IMyOptions = {
75
        dayLabels: <IMyDayLabels> {},
76
        monthLabels: <IMyMonthLabels> {},
77
        dateFormat: <string> "",
78
        showTodayBtn: <boolean> true,
79
        todayBtnTxt: <string> "",
80
        firstDayOfWeek: <string> "",
81
        sunHighlight: <boolean> true,
82
        markCurrentDay: <boolean> true,
83
        disableUntil: <IMyDate> {year: 0, month: 0, day: 0},
84
        disableSince: <IMyDate> {year: 0, month: 0, day: 0},
85
        disableDays: <Array<IMyDate>> [],
86
        enableDays: <Array<IMyDate>> [],
87
        disableDateRange: <IMyDateRange> {begin: <IMyDate> {year: 0, month: 0, day: 0}, end: <IMyDate> {year: 0, month: 0, day: 0}},
88
        disableWeekends: <boolean> false,
89
        showWeekNumbers: <boolean> false,
90
        height: <string> "34px",
91
        width: <string> "100%",
92
        selectionTxtFontSize: <string> "18px",
93
        inline: <boolean> false,
94
        showClearDateBtn: <boolean> true,
95
        alignSelectorRight: <boolean> false,
96
        openSelectorTopOfInput: <boolean> false,
97
        indicateInvalidDate: <boolean> true,
98
        editableDateField: <boolean> true,
99
        editableMonthAndYear: <boolean> true,
100
        disableHeaderButtons: <boolean> true,
101
        minYear: <number> this.MIN_YEAR,
102
        maxYear: <number> this.MAX_YEAR,
103
        componentDisabled: <boolean> false,
104
        inputValueRequired: <boolean> false,
105
        showSelectorArrow: <boolean> true,
106
        showInputField: <boolean> true,
107
        openSelectorOnInputClick: <boolean> false,
108
        inputAutoFill: <boolean> true,
109
        ariaLabelInputField: <string> "Date input field",
110
        ariaLabelClearDate: <string> "Clear Date",
111
        ariaLabelOpenCalendar: <string> "Open Calendar",
112
        ariaLabelPrevMonth: <string> "Previous Month",
113
        ariaLabelNextMonth: <string> "Next Month",
114
        ariaLabelPrevYear: <string> "Previous Year",
115
        ariaLabelNextYear: <string> "Next Year"
116
    };
117

    
118
    constructor(public elem: ElementRef, private renderer: Renderer, private cdr: ChangeDetectorRef, private localeService: LocaleService, private utilService: UtilService) {
119
        this.setLocaleOptions();
120
        renderer.listenGlobal("document", "click", (event: any) => {
121
            if (this.showSelector && event.target && this.elem.nativeElement !== event.target && !this.elem.nativeElement.contains(event.target)) {
122
                this.showSelector = false;
123
                this.calendarToggle.emit(4);
124
            }
125
            if (this.opts.editableMonthAndYear && event.target && this.elem.nativeElement.contains(event.target)) {
126
                this.resetMonthYearEdit();
127
            }
128
        });
129
    }
130

    
131
    setLocaleOptions(): void {
132
        let opts: IMyOptions = this.localeService.getLocaleOptions(this.locale);
133
        Object.keys(opts).forEach((k) => {
134
            (<IMyOptions>this.opts)[k] = opts[k];
135
        });
136
    }
137

    
138
    setOptions(): void {
139
        if (this.options !== undefined) {
140
            Object.keys(this.options).forEach((k) => {
141
                (<IMyOptions>this.opts)[k] = this.options[k];
142
            });
143
        }
144
        if (this.opts.minYear < this.MIN_YEAR) {
145
            this.opts.minYear = this.MIN_YEAR;
146
        }
147
        if (this.opts.maxYear > this.MAX_YEAR) {
148
            this.opts.maxYear = this.MAX_YEAR;
149
        }
150

    
151
        let separator: string = this.utilService.getDateFormatSeparator(this.opts.dateFormat);
152
        this.autoFillOpts = {separator: separator, formatParts: this.opts.dateFormat.split(separator), enabled: this.opts.inputAutoFill};
153
    }
154

    
155
    getComponentWidth(): string {
156
        if (this.opts.showInputField) {
157
            return this.opts.width;
158
        }
159
        else if (this.selectionDayTxt.length > 0 && this.opts.showClearDateBtn) {
160
            return "60px";
161
        }
162
        else {
163
            return "30px";
164
        }
165
    }
166

    
167
    getSelectorTopPosition(): string {
168
        if (this.opts.openSelectorTopOfInput) {
169
            return this.elem.nativeElement.children[0].offsetHeight + "px";
170
        }
171
    }
172

    
173
    resetMonthYearEdit(): void {
174
        this.editMonth = false;
175
        this.editYear = false;
176
        this.invalidMonth = false;
177
        this.invalidYear = false;
178
    }
179

    
180
    editMonthClicked(event: any): void {
181
        event.stopPropagation();
182
        if (this.opts.editableMonthAndYear) {
183
            this.editMonth = true;
184
        }
185
    }
186

    
187
    editYearClicked(event: any): void {
188
        event.stopPropagation();
189
        if (this.opts.editableMonthAndYear) {
190
            this.editYear = true;
191
        }
192
    }
193

    
194
    userDateInput(event: any): void {
195
        this.invalidDate = false;
196
        if (event.target.value.length === 0) {
197
            this.clearDate();
198
        }
199
        else {
200
            let date: IMyDate = this.utilService.isDateValid(event.target.value, this.opts.dateFormat, this.opts.minYear, this.opts.maxYear, this.opts.disableUntil, this.opts.disableSince, this.opts.disableWeekends, this.opts.disableDays, this.opts.disableDateRange, this.opts.monthLabels, this.opts.enableDays);
201
            if (date.day !== 0 && date.month !== 0 && date.year !== 0) {
202
                this.selectDate(date);
203
            }
204
            else {
205
                this.invalidDate = true;
206
            }
207
        }
208
        if (this.invalidDate) {
209
            this.inputFieldChanged.emit({value: event.target.value, dateFormat: this.opts.dateFormat, valid: !(event.target.value.length === 0 || this.invalidDate)});
210
            this.onChangeCb("");
211
            this.onTouchedCb();
212
        }
213
    }
214

    
215
    onFocusInput(event: any): void {
216
        this.inputFocusBlur.emit({reason: 1, value: event.target.value});
217
    }
218

    
219
    lostFocusInput(event: any): void {
220
        this.selectionDayTxt = event.target.value;
221
        this.onTouchedCb();
222
        this.inputFocusBlur.emit({reason: 2, value: event.target.value});
223
    }
224

    
225
    userMonthInput(event: any): void {
226
        if (event.keyCode === 13 || event.keyCode === 37 || event.keyCode === 39) {
227
            return;
228
        }
229

    
230
        this.invalidMonth = false;
231

    
232
        let m: number = this.utilService.isMonthLabelValid(event.target.value, this.opts.monthLabels);
233
        if (m !== -1) {
234
            this.editMonth = false;
235
            if (m !== this.visibleMonth.monthNbr) {
236
                this.visibleMonth = {monthTxt: this.monthText(m), monthNbr: m, year: this.visibleMonth.year};
237
                this.generateCalendar(m, this.visibleMonth.year, true);
238
            }
239
        }
240
        else {
241
            this.invalidMonth = true;
242
        }
243
    }
244

    
245
    userYearInput(event: any): void {
246
        if (event.keyCode === 13 || event.keyCode === 37 || event.keyCode === 39) {
247
            return;
248
        }
249

    
250
        this.invalidYear = false;
251

    
252
        let y: number = this.utilService.isYearLabelValid(Number(event.target.value), this.opts.minYear, this.opts.maxYear);
253
        if (y !== -1) {
254
            this.editYear = false;
255
            if (y !== this.visibleMonth.year) {
256
                this.visibleMonth = {monthTxt: this.visibleMonth.monthTxt, monthNbr: this.visibleMonth.monthNbr, year: y};
257
                this.generateCalendar(this.visibleMonth.monthNbr, y, true);
258
            }
259
        }
260
        else {
261
            this.invalidYear = true;
262
        }
263
    }
264

    
265
    isTodayDisabled(): void {
266
        this.disableTodayBtn = this.utilService.isDisabledDay(this.getToday(), this.opts.disableUntil, this.opts.disableSince, this.opts.disableWeekends, this.opts.disableDays, this.opts.disableDateRange, this.opts.enableDays);
267
    }
268

    
269
    parseOptions(): void {
270
        if (this.locale) {
271
            this.setLocaleOptions();
272
        }
273
        this.setOptions();
274
        this.isTodayDisabled();
275
        this.dayIdx = this.weekDayOpts.indexOf(this.opts.firstDayOfWeek);
276
        if (this.dayIdx !== -1) {
277
            let idx: number = this.dayIdx;
278
            for (let i = 0; i < this.weekDayOpts.length; i++) {
279
                this.weekDays.push(this.opts.dayLabels[this.weekDayOpts[idx]]);
280
                idx = this.weekDayOpts[idx] === "sa" ? 0 : idx + 1;
281
            }
282
        }
283
    }
284

    
285
    writeValue(value: Object): void {
286
        if (value && value["date"]) {
287
            this.updateDateValue(this.parseSelectedDate(value["date"]), false);
288
        }
289
        else if (value === "") {
290
            this.updateDateValue({year: 0, month: 0, day: 0}, true);
291
        }
292
    }
293

    
294
    registerOnChange(fn: any): void {
295
        this.onChangeCb = fn;
296
    }
297

    
298
    registerOnTouched(fn: any): void {
299
        this.onTouchedCb = fn;
300
    }
301

    
302
    ngOnChanges(changes: SimpleChanges): void {
303
        if (changes.hasOwnProperty("selector") && changes["selector"].currentValue > 0) {
304
            this.openBtnClicked();
305
        }
306

    
307
        if (changes.hasOwnProperty("placeholder")) {
308
            this.placeholder = changes["placeholder"].currentValue;
309
        }
310

    
311
        if (changes.hasOwnProperty("locale")) {
312
            this.locale = changes["locale"].currentValue;
313
        }
314

    
315
        if (changes.hasOwnProperty("options")) {
316
            this.options = changes["options"].currentValue;
317
        }
318

    
319
        this.weekDays.length = 0;
320
        this.parseOptions();
321

    
322
        if (changes.hasOwnProperty("defaultMonth")) {
323
            let dm: string = changes["defaultMonth"].currentValue;
324
            if (dm !== null && dm !== undefined && dm !== "") {
325
                this.selectedMonth = this.parseSelectedMonth(dm);
326
            }
327
            else {
328
                this.selectedMonth = {monthTxt: "", monthNbr: 0, year: 0};
329
            }
330
        }
331

    
332
        if (changes.hasOwnProperty("selDate")) {
333
            let sd: any = changes["selDate"];
334
            if (sd.currentValue !== null && sd.currentValue !== undefined && sd.currentValue !== "" && Object.keys(sd.currentValue).length !== 0) {
335
                this.selectedDate = this.parseSelectedDate(sd.currentValue);
336
                setTimeout(() => {
337
                    this.onChangeCb(this.getDateModel(this.selectedDate));
338
                });
339
            }
340
            else {
341
                // Do not clear on init
342
                if (!sd.isFirstChange()) {
343
                    this.clearDate();
344
                }
345
            }
346
        }
347
        if (this.opts.inline) {
348
            this.setVisibleMonth();
349
        }
350
        else if (this.showSelector) {
351
            this.generateCalendar(this.visibleMonth.monthNbr, this.visibleMonth.year, false);
352
        }
353
    }
354

    
355
    removeBtnClicked(): void {
356
        // Remove date button clicked
357
        this.clearDate();
358
        if (this.showSelector) {
359
            this.calendarToggle.emit(3);
360
        }
361
        this.showSelector = false;
362
    }
363

    
364
    openBtnClicked(): void {
365
        // Open selector button clicked
366
        this.showSelector = !this.showSelector;
367
        if (this.showSelector) {
368
            this.setVisibleMonth();
369
            this.calendarToggle.emit(1);
370
        }
371
        else {
372
            this.calendarToggle.emit(3);
373
        }
374
    }
375

    
376
    setVisibleMonth(): void {
377
        // Sets visible month of calendar
378
        let y: number = 0, m: number = 0;
379
        if (!this.utilService.isInitializedDate(this.selectedDate)) {
380
            if (this.selectedMonth.year === 0 && this.selectedMonth.monthNbr === 0) {
381
                let today: IMyDate = this.getToday();
382
                y = today.year;
383
                m = today.month;
384
            } else {
385
                y = this.selectedMonth.year;
386
                m = this.selectedMonth.monthNbr;
387
            }
388
        }
389
        else {
390
            y = this.selectedDate.year;
391
            m = this.selectedDate.month;
392
        }
393
        this.visibleMonth = {monthTxt: this.opts.monthLabels[m], monthNbr: m, year: y};
394

    
395
        // Create current month
396
        this.generateCalendar(m, y, true);
397
    }
398

    
399
    prevMonth(): void {
400
        // Previous month from calendar
401
        let d: Date = this.getDate(this.visibleMonth.year, this.visibleMonth.monthNbr, 1);
402
        d.setMonth(d.getMonth() - 1);
403

    
404
        let y: number = d.getFullYear();
405
        let m: number = d.getMonth() + 1;
406

    
407
        this.visibleMonth = {monthTxt: this.monthText(m), monthNbr: m, year: y};
408
        this.generateCalendar(m, y, true);
409
    }
410

    
411
    nextMonth(): void {
412
        // Next month from calendar
413
        let d: Date = this.getDate(this.visibleMonth.year, this.visibleMonth.monthNbr, 1);
414
        d.setMonth(d.getMonth() + 1);
415

    
416
        let y: number = d.getFullYear();
417
        let m: number = d.getMonth() + 1;
418

    
419
        this.visibleMonth = {monthTxt: this.monthText(m), monthNbr: m, year: y};
420
        this.generateCalendar(m, y, true);
421
    }
422

    
423
    prevYear(): void {
424
        // Previous year from calendar
425
        this.visibleMonth.year--;
426
        this.generateCalendar(this.visibleMonth.monthNbr, this.visibleMonth.year, true);
427
    }
428

    
429
    nextYear(): void {
430
        // Next year from calendar
431
        this.visibleMonth.year++;
432
        this.generateCalendar(this.visibleMonth.monthNbr, this.visibleMonth.year, true);
433
    }
434

    
435
    todayClicked(): void {
436
        // Today button clicked
437
        let today: IMyDate = this.getToday();
438
        this.selectDate(today);
439
        if (this.opts.inline && today.year !== this.visibleMonth.year || today.month !== this.visibleMonth.monthNbr) {
440
            this.visibleMonth = {monthTxt: this.opts.monthLabels[today.month], monthNbr: today.month, year: today.year};
441
            this.generateCalendar(today.month, today.year, true);
442
        }
443
    }
444

    
445
    cellClicked(cell: any): void {
446
        // Cell clicked on the calendar
447
        if (cell.cmo === this.PREV_MONTH) {
448
            // Previous month day
449
            this.prevMonth();
450
        }
451
        else if (cell.cmo === this.CURR_MONTH) {
452
            // Current month day - if date is already selected clear it
453
            if (cell.dateObj.year === this.selectedDate.year && cell.dateObj.month === this.selectedDate.month && cell.dateObj.day === this.selectedDate.day) {
454
                this.clearDate();
455
            }
456
            else {
457
                this.selectDate(cell.dateObj);
458
            }
459
        }
460
        else if (cell.cmo === this.NEXT_MONTH) {
461
            // Next month day
462
            this.nextMonth();
463
        }
464
        this.resetMonthYearEdit();
465
    }
466

    
467
    cellKeyDown(event: any, cell: any) {
468
        // Cell keyboard handling
469
        if ((event.keyCode === 13 || event.keyCode === 32) && !cell.disabled) {
470
            event.preventDefault();
471
            this.cellClicked(cell);
472
        }
473
    }
474

    
475
    clearDate(): void {
476
        // Clears the date and notifies parent using callbacks and value accessor
477
        let date: IMyDate = {year: 0, month: 0, day: 0};
478
        this.dateChanged.emit({date: date, jsdate: null, formatted: "", epoc: 0});
479
        this.onChangeCb("");
480
        this.onTouchedCb();
481
        this.updateDateValue(date, true);
482
    }
483

    
484
    selectDate(date: IMyDate): void {
485
        // Date selected, notifies parent using callbacks and value accessor
486
        let dateModel: IMyDateModel = this.getDateModel(date);
487
        this.dateChanged.emit(dateModel);
488
        this.onChangeCb(dateModel);
489
        this.onTouchedCb();
490
        this.updateDateValue(date, false);
491
        if (this.showSelector) {
492
            this.calendarToggle.emit(2);
493
        }
494
        this.showSelector = false;
495
    }
496

    
497
    updateDateValue(date: IMyDate, clear: boolean): void {
498
        // Updates date values
499
        this.selectedDate = date;
500
        this.selectionDayTxt = clear ? "" : this.formatDate(date);
501
        this.inputFieldChanged.emit({value: this.selectionDayTxt, dateFormat: this.opts.dateFormat, valid: !clear});
502
        this.invalidDate = false;
503
    }
504

    
505
    getDateModel(date: IMyDate): IMyDateModel {
506
        // Creates a date model object from the given parameter
507
        return {date: date, jsdate: this.getDate(date.year, date.month, date.day), formatted: this.formatDate(date), epoc: Math.round(this.getTimeInMilliseconds(date) / 1000.0)};
508
    }
509

    
510
    preZero(val: string): string {
511
        // Prepend zero if smaller than 10
512
        return parseInt(val) < 10 ? "0" + val : val;
513
    }
514

    
515
    formatDate(val: any): string {
516
        // Returns formatted date string, if mmm is part of dateFormat returns month as a string
517
        let formatted: string = this.opts.dateFormat.replace("yyyy", val.year).replace("dd", this.preZero(val.day));
518
        return this.opts.dateFormat.indexOf("mmm") !== -1 ? formatted.replace("mmm", this.monthText(val.month)) : formatted.replace("mm", this.preZero(val.month));
519
    }
520

    
521
    monthText(m: number): string {
522
        // Returns month as a text
523
        return this.opts.monthLabels[m];
524
    }
525

    
526
    monthStartIdx(y: number, m: number): number {
527
        // Month start index
528
        let d = new Date();
529
        d.setDate(1);
530
        d.setMonth(m - 1);
531
        d.setFullYear(y);
532
        let idx = d.getDay() + this.sundayIdx();
533
        return idx >= 7 ? idx - 7 : idx;
534
    }
535

    
536
    daysInMonth(m: number, y: number): number {
537
        // Return number of days of current month
538
        return new Date(y, m, 0).getDate();
539
    }
540

    
541
    daysInPrevMonth(m: number, y: number): number {
542
        // Return number of days of the previous month
543
        let d: Date = this.getDate(y, m, 1);
544
        d.setMonth(d.getMonth() - 1);
545
        return this.daysInMonth(d.getMonth() + 1, d.getFullYear());
546
    }
547

    
548
    isCurrDay(d: number, m: number, y: number, cmo: number, today: IMyDate): boolean {
549
        // Check is a given date the today
550
        return d === today.day && m === today.month && y === today.year && cmo === this.CURR_MONTH;
551
    }
552

    
553
    getToday(): IMyDate {
554
        let date: Date = new Date();
555
        return {year: date.getFullYear(), month: date.getMonth() + 1, day: date.getDate()};
556
    }
557

    
558
    getTimeInMilliseconds(date: IMyDate): number {
559
        return this.getDate(date.year, date.month, date.day).getTime();
560
    }
561

    
562
    getDayNumber(date: IMyDate): number {
563
        // Get day number: su=0, mo=1, tu=2, we=3 ...
564
        let d: Date = this.getDate(date.year, date.month, date.day);
565
        return d.getDay();
566
    }
567

    
568
    getWeekday(date: IMyDate): string {
569
        // Get weekday: su, mo, tu, we ...
570
        return this.weekDayOpts[this.getDayNumber(date)];
571
    }
572

    
573
    getDate(year: number, month: number, day: number): Date {
574
        // Creates a date object from given year, month and day
575
        return new Date(year, month - 1, day, 0, 0, 0, 0);
576
    }
577

    
578
    sundayIdx(): number {
579
        // Index of Sunday day
580
        return this.dayIdx > 0 ? 7 - this.dayIdx : 0;
581
    }
582

    
583
    generateCalendar(m: number, y: number, notifyChange: boolean): void {
584
        this.dates.length = 0;
585
        let today: IMyDate = this.getToday();
586
        let monthStart: number = this.monthStartIdx(y, m);
587
        let dInThisM: number = this.daysInMonth(m, y);
588
        let dInPrevM: number = this.daysInPrevMonth(m, y);
589

    
590
        let dayNbr: number = 1;
591
        let cmo: number = this.PREV_MONTH;
592
        for (let i = 1; i < 7; i++) {
593
            let week: Array<IMyCalendarDay> = [];
594
            if (i === 1) {
595
                // First week
596
                let pm = dInPrevM - monthStart + 1;
597
                // Previous month
598
                for (let j = pm; j <= dInPrevM; j++) {
599
                    let date: IMyDate = {year: y, month: m - 1, day: j};
600
                    week.push({dateObj: date, cmo: cmo, currDay: this.isCurrDay(j, m, y, cmo, today), dayNbr: this.getDayNumber(date), disabled: this.utilService.isDisabledDay(date, this.opts.disableUntil, this.opts.disableSince, this.opts.disableWeekends, this.opts.disableDays, this.opts.disableDateRange, this.opts.enableDays)});
601
                }
602

    
603
                cmo = this.CURR_MONTH;
604
                // Current month
605
                let daysLeft: number = 7 - week.length;
606
                for (let j = 0; j < daysLeft; j++) {
607
                    let date: IMyDate = {year: y, month: m, day: dayNbr};
608
                    week.push({dateObj: date, cmo: cmo, currDay: this.isCurrDay(dayNbr, m, y, cmo, today), dayNbr: this.getDayNumber(date), disabled: this.utilService.isDisabledDay(date, this.opts.disableUntil, this.opts.disableSince, this.opts.disableWeekends, this.opts.disableDays, this.opts.disableDateRange, this.opts.enableDays)});
609
                    dayNbr++;
610
                }
611
            }
612
            else {
613
                // Rest of the weeks
614
                for (let j = 1; j < 8; j++) {
615
                    if (dayNbr > dInThisM) {
616
                        // Next month
617
                        dayNbr = 1;
618
                        cmo = this.NEXT_MONTH;
619
                    }
620
                    let date: IMyDate = {year: y, month: cmo === this.CURR_MONTH ? m : m + 1, day: dayNbr};
621
                    week.push({dateObj: date, cmo: cmo, currDay: this.isCurrDay(dayNbr, m, y, cmo, today), dayNbr: this.getDayNumber(date), disabled: this.utilService.isDisabledDay(date, this.opts.disableUntil, this.opts.disableSince, this.opts.disableWeekends, this.opts.disableDays, this.opts.disableDateRange, this.opts.enableDays)});
622
                    dayNbr++;
623
                }
624
            }
625
            let weekNbr: number = this.opts.showWeekNumbers  && this.opts.firstDayOfWeek === "mo" ? this.utilService.getWeekNumber(week[0].dateObj) : 0;
626
            this.dates.push({week: week, weekNbr: weekNbr});
627
        }
628

    
629
        this.setHeaderBtnDisabledState(m, y);
630

    
631
        if (notifyChange) {
632
            // Notify parent
633
            this.calendarViewChanged.emit({year: y, month: m, first: {number: 1, weekday: this.getWeekday({year: y, month: m, day: 1})}, last: {number: dInThisM, weekday: this.getWeekday({year: y, month: m, day: dInThisM})}});
634
        }
635
    }
636

    
637
    parseSelectedDate(selDate: any): IMyDate {
638
        // Parse selDate value - it can be string or IMyDate object
639
        let date: IMyDate = {day: 0, month: 0, year: 0};
640
        if (typeof selDate === "string") {
641
            let sd: string = <string>selDate;
642
            date.day = this.utilService.parseDatePartNumber(this.opts.dateFormat, sd, "dd");
643

    
644
            date.month = this.opts.dateFormat.indexOf("mmm") !== -1
645
                ? this.utilService.parseDatePartMonthName(this.opts.dateFormat, sd, "mmm", this.opts.monthLabels)
646
                : this.utilService.parseDatePartNumber(this.opts.dateFormat, sd, "mm");
647

    
648
            date.year = this.utilService.parseDatePartNumber(this.opts.dateFormat, sd, "yyyy");
649
        }
650
        else if (typeof selDate === "object") {
651
            date = selDate;
652
        }
653
        this.selectionDayTxt = this.formatDate(date);
654
        return date;
655
    }
656

    
657
    parseSelectedMonth(ms: string): IMyMonth {
658
        return this.utilService.parseDefaultMonth(ms);
659
    }
660

    
661
    setHeaderBtnDisabledState(m: number, y: number): void {
662
        let dpm: boolean = false;
663
        let dpy: boolean = false;
664
        let dnm: boolean = false;
665
        let dny: boolean = false;
666
        if (this.opts.disableHeaderButtons) {
667
            dpm = this.utilService.isMonthDisabledByDisableUntil({year: m === 1 ? y - 1 : y, month: m === 1 ? 12 : m - 1, day: this.daysInMonth(m === 1 ? 12 : m - 1, m === 1 ? y - 1 : y)}, this.opts.disableUntil);
668
            dpy = this.utilService.isMonthDisabledByDisableUntil({year: y - 1, month: m, day: this.daysInMonth(m, y - 1)}, this.opts.disableUntil);
669
            dnm = this.utilService.isMonthDisabledByDisableSince({year: m === 12 ? y + 1 : y, month: m === 12 ? 1 : m + 1, day: 1}, this.opts.disableSince);
670
            dny = this.utilService.isMonthDisabledByDisableSince({year: y + 1, month: m, day: 1}, this.opts.disableSince);
671
        }
672
        this.prevMonthDisabled = m === 1 && y === this.opts.minYear || dpm;
673
        this.prevYearDisabled = y - 1 < this.opts.minYear || dpy;
674
        this.nextMonthDisabled = m === 12 && y === this.opts.maxYear || dnm;
675
        this.nextYearDisabled = y + 1 > this.opts.maxYear || dny;
676
    }
677
}
(5-5/6)