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
|
//renderer.listen("document", "click", (event: any) => {
|
122
|
console.info("listen global");
|
123
|
if (this.showSelector && event.target && this.elem.nativeElement !== event.target && !this.elem.nativeElement.contains(event.target)) {
|
124
|
this.showSelector = false;
|
125
|
this.calendarToggle.emit(4);
|
126
|
}
|
127
|
if (this.opts.editableMonthAndYear && event.target && this.elem.nativeElement.contains(event.target)) {
|
128
|
this.resetMonthYearEdit();
|
129
|
}
|
130
|
});
|
131
|
}
|
132
|
|
133
|
setLocaleOptions(): void {
|
134
|
let opts: IMyOptions = this.localeService.getLocaleOptions(this.locale);
|
135
|
Object.keys(opts).forEach((k) => {
|
136
|
(<IMyOptions>this.opts)[k] = opts[k];
|
137
|
});
|
138
|
}
|
139
|
|
140
|
setOptions(): void {
|
141
|
if (this.options !== undefined) {
|
142
|
Object.keys(this.options).forEach((k) => {
|
143
|
(<IMyOptions>this.opts)[k] = this.options[k];
|
144
|
});
|
145
|
}
|
146
|
if (this.opts.minYear < this.MIN_YEAR) {
|
147
|
this.opts.minYear = this.MIN_YEAR;
|
148
|
}
|
149
|
if (this.opts.maxYear > this.MAX_YEAR) {
|
150
|
this.opts.maxYear = this.MAX_YEAR;
|
151
|
}
|
152
|
|
153
|
let separator: string = this.utilService.getDateFormatSeparator(this.opts.dateFormat);
|
154
|
this.autoFillOpts = {separator: separator, formatParts: this.opts.dateFormat.split(separator), enabled: this.opts.inputAutoFill};
|
155
|
}
|
156
|
|
157
|
getComponentWidth(): string {
|
158
|
if (this.opts.showInputField) {
|
159
|
return this.opts.width;
|
160
|
}
|
161
|
else if (this.selectionDayTxt.length > 0 && this.opts.showClearDateBtn) {
|
162
|
return "60px";
|
163
|
}
|
164
|
else {
|
165
|
return "30px";
|
166
|
}
|
167
|
}
|
168
|
|
169
|
getSelectorTopPosition(): string {
|
170
|
if (this.opts.openSelectorTopOfInput) {
|
171
|
return this.elem.nativeElement.children[0].offsetHeight + "px";
|
172
|
}
|
173
|
}
|
174
|
|
175
|
resetMonthYearEdit(): void {
|
176
|
this.editMonth = false;
|
177
|
this.editYear = false;
|
178
|
this.invalidMonth = false;
|
179
|
this.invalidYear = false;
|
180
|
}
|
181
|
|
182
|
editMonthClicked(event: any): void {
|
183
|
event.stopPropagation();
|
184
|
if (this.opts.editableMonthAndYear) {
|
185
|
this.editMonth = true;
|
186
|
}
|
187
|
}
|
188
|
|
189
|
editYearClicked(event: any): void {
|
190
|
event.stopPropagation();
|
191
|
if (this.opts.editableMonthAndYear) {
|
192
|
this.editYear = true;
|
193
|
}
|
194
|
}
|
195
|
|
196
|
userDateInput(event: any): void {
|
197
|
this.invalidDate = false;
|
198
|
if (event.target.value.length === 0) {
|
199
|
this.clearDate();
|
200
|
}
|
201
|
else {
|
202
|
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);
|
203
|
if (date.day !== 0 && date.month !== 0 && date.year !== 0) {
|
204
|
this.selectDate(date);
|
205
|
}
|
206
|
else {
|
207
|
this.invalidDate = true;
|
208
|
}
|
209
|
}
|
210
|
if (this.invalidDate) {
|
211
|
this.inputFieldChanged.emit({value: event.target.value, dateFormat: this.opts.dateFormat, valid: !(event.target.value.length === 0 || this.invalidDate)});
|
212
|
this.onChangeCb("");
|
213
|
this.onTouchedCb();
|
214
|
}
|
215
|
}
|
216
|
|
217
|
onFocusInput(event: any): void {
|
218
|
this.inputFocusBlur.emit({reason: 1, value: event.target.value});
|
219
|
}
|
220
|
|
221
|
lostFocusInput(event: any): void {
|
222
|
this.selectionDayTxt = event.target.value;
|
223
|
this.onTouchedCb();
|
224
|
this.inputFocusBlur.emit({reason: 2, value: event.target.value});
|
225
|
}
|
226
|
|
227
|
userMonthInput(event: any): void {
|
228
|
if (event.keyCode === 13 || event.keyCode === 37 || event.keyCode === 39) {
|
229
|
return;
|
230
|
}
|
231
|
|
232
|
this.invalidMonth = false;
|
233
|
|
234
|
let m: number = this.utilService.isMonthLabelValid(event.target.value, this.opts.monthLabels);
|
235
|
if (m !== -1) {
|
236
|
this.editMonth = false;
|
237
|
if (m !== this.visibleMonth.monthNbr) {
|
238
|
this.visibleMonth = {monthTxt: this.monthText(m), monthNbr: m, year: this.visibleMonth.year};
|
239
|
this.generateCalendar(m, this.visibleMonth.year, true);
|
240
|
}
|
241
|
}
|
242
|
else {
|
243
|
this.invalidMonth = true;
|
244
|
}
|
245
|
}
|
246
|
|
247
|
userYearInput(event: any): void {
|
248
|
if (event.keyCode === 13 || event.keyCode === 37 || event.keyCode === 39) {
|
249
|
return;
|
250
|
}
|
251
|
|
252
|
this.invalidYear = false;
|
253
|
|
254
|
let y: number = this.utilService.isYearLabelValid(Number(event.target.value), this.opts.minYear, this.opts.maxYear);
|
255
|
if (y !== -1) {
|
256
|
this.editYear = false;
|
257
|
if (y !== this.visibleMonth.year) {
|
258
|
this.visibleMonth = {monthTxt: this.visibleMonth.monthTxt, monthNbr: this.visibleMonth.monthNbr, year: y};
|
259
|
this.generateCalendar(this.visibleMonth.monthNbr, y, true);
|
260
|
}
|
261
|
}
|
262
|
else {
|
263
|
this.invalidYear = true;
|
264
|
}
|
265
|
}
|
266
|
|
267
|
isTodayDisabled(): void {
|
268
|
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);
|
269
|
}
|
270
|
|
271
|
parseOptions(): void {
|
272
|
if (this.locale) {
|
273
|
this.setLocaleOptions();
|
274
|
}
|
275
|
this.setOptions();
|
276
|
this.isTodayDisabled();
|
277
|
this.dayIdx = this.weekDayOpts.indexOf(this.opts.firstDayOfWeek);
|
278
|
if (this.dayIdx !== -1) {
|
279
|
let idx: number = this.dayIdx;
|
280
|
for (let i = 0; i < this.weekDayOpts.length; i++) {
|
281
|
this.weekDays.push(this.opts.dayLabels[this.weekDayOpts[idx]]);
|
282
|
idx = this.weekDayOpts[idx] === "sa" ? 0 : idx + 1;
|
283
|
}
|
284
|
}
|
285
|
}
|
286
|
|
287
|
writeValue(value: Object): void {
|
288
|
if (value && value["date"]) {
|
289
|
this.updateDateValue(this.parseSelectedDate(value["date"]), false);
|
290
|
}
|
291
|
else if (value === "") {
|
292
|
this.updateDateValue({year: 0, month: 0, day: 0}, true);
|
293
|
}
|
294
|
}
|
295
|
|
296
|
registerOnChange(fn: any): void {
|
297
|
this.onChangeCb = fn;
|
298
|
}
|
299
|
|
300
|
registerOnTouched(fn: any): void {
|
301
|
this.onTouchedCb = fn;
|
302
|
}
|
303
|
|
304
|
ngOnChanges(changes: SimpleChanges): void {
|
305
|
if (changes.hasOwnProperty("selector") && changes["selector"].currentValue > 0) {
|
306
|
this.openBtnClicked();
|
307
|
}
|
308
|
|
309
|
if (changes.hasOwnProperty("placeholder")) {
|
310
|
this.placeholder = changes["placeholder"].currentValue;
|
311
|
}
|
312
|
|
313
|
if (changes.hasOwnProperty("locale")) {
|
314
|
this.locale = changes["locale"].currentValue;
|
315
|
}
|
316
|
|
317
|
if (changes.hasOwnProperty("options")) {
|
318
|
this.options = changes["options"].currentValue;
|
319
|
}
|
320
|
|
321
|
this.weekDays.length = 0;
|
322
|
this.parseOptions();
|
323
|
|
324
|
if (changes.hasOwnProperty("defaultMonth")) {
|
325
|
let dm: string = changes["defaultMonth"].currentValue;
|
326
|
if (dm !== null && dm !== undefined && dm !== "") {
|
327
|
this.selectedMonth = this.parseSelectedMonth(dm);
|
328
|
}
|
329
|
else {
|
330
|
this.selectedMonth = {monthTxt: "", monthNbr: 0, year: 0};
|
331
|
}
|
332
|
}
|
333
|
|
334
|
if (changes.hasOwnProperty("selDate")) {
|
335
|
let sd: any = changes["selDate"];
|
336
|
if (sd.currentValue !== null && sd.currentValue !== undefined && sd.currentValue !== "" && Object.keys(sd.currentValue).length !== 0) {
|
337
|
this.selectedDate = this.parseSelectedDate(sd.currentValue);
|
338
|
setTimeout(() => {
|
339
|
this.onChangeCb(this.getDateModel(this.selectedDate));
|
340
|
});
|
341
|
}
|
342
|
else {
|
343
|
// Do not clear on init
|
344
|
if (!sd.isFirstChange()) {
|
345
|
this.clearDate();
|
346
|
}
|
347
|
}
|
348
|
}
|
349
|
if (this.opts.inline) {
|
350
|
this.setVisibleMonth();
|
351
|
}
|
352
|
else if (this.showSelector) {
|
353
|
this.generateCalendar(this.visibleMonth.monthNbr, this.visibleMonth.year, false);
|
354
|
}
|
355
|
}
|
356
|
|
357
|
removeBtnClicked(): void {
|
358
|
// Remove date button clicked
|
359
|
this.clearDate();
|
360
|
if (this.showSelector) {
|
361
|
this.calendarToggle.emit(3);
|
362
|
}
|
363
|
this.showSelector = false;
|
364
|
}
|
365
|
|
366
|
openBtnClicked(): void {
|
367
|
// Open selector button clicked
|
368
|
this.showSelector = !this.showSelector;
|
369
|
if (this.showSelector) {
|
370
|
this.setVisibleMonth();
|
371
|
this.calendarToggle.emit(1);
|
372
|
}
|
373
|
else {
|
374
|
this.calendarToggle.emit(3);
|
375
|
}
|
376
|
}
|
377
|
|
378
|
setVisibleMonth(): void {
|
379
|
// Sets visible month of calendar
|
380
|
let y: number = 0, m: number = 0;
|
381
|
if (!this.utilService.isInitializedDate(this.selectedDate)) {
|
382
|
if (this.selectedMonth.year === 0 && this.selectedMonth.monthNbr === 0) {
|
383
|
let today: IMyDate = this.getToday();
|
384
|
y = today.year;
|
385
|
m = today.month;
|
386
|
} else {
|
387
|
y = this.selectedMonth.year;
|
388
|
m = this.selectedMonth.monthNbr;
|
389
|
}
|
390
|
}
|
391
|
else {
|
392
|
y = this.selectedDate.year;
|
393
|
m = this.selectedDate.month;
|
394
|
}
|
395
|
this.visibleMonth = {monthTxt: this.opts.monthLabels[m], monthNbr: m, year: y};
|
396
|
|
397
|
// Create current month
|
398
|
this.generateCalendar(m, y, true);
|
399
|
}
|
400
|
|
401
|
prevMonth(): void {
|
402
|
// Previous month from calendar
|
403
|
let d: Date = this.getDate(this.visibleMonth.year, this.visibleMonth.monthNbr, 1);
|
404
|
d.setMonth(d.getMonth() - 1);
|
405
|
|
406
|
let y: number = d.getFullYear();
|
407
|
let m: number = d.getMonth() + 1;
|
408
|
|
409
|
this.visibleMonth = {monthTxt: this.monthText(m), monthNbr: m, year: y};
|
410
|
this.generateCalendar(m, y, true);
|
411
|
}
|
412
|
|
413
|
nextMonth(): void {
|
414
|
// Next month from calendar
|
415
|
let d: Date = this.getDate(this.visibleMonth.year, this.visibleMonth.monthNbr, 1);
|
416
|
d.setMonth(d.getMonth() + 1);
|
417
|
|
418
|
let y: number = d.getFullYear();
|
419
|
let m: number = d.getMonth() + 1;
|
420
|
|
421
|
this.visibleMonth = {monthTxt: this.monthText(m), monthNbr: m, year: y};
|
422
|
this.generateCalendar(m, y, true);
|
423
|
}
|
424
|
|
425
|
prevYear(): void {
|
426
|
// Previous year from calendar
|
427
|
this.visibleMonth.year--;
|
428
|
this.generateCalendar(this.visibleMonth.monthNbr, this.visibleMonth.year, true);
|
429
|
}
|
430
|
|
431
|
nextYear(): void {
|
432
|
// Next year from calendar
|
433
|
this.visibleMonth.year++;
|
434
|
this.generateCalendar(this.visibleMonth.monthNbr, this.visibleMonth.year, true);
|
435
|
}
|
436
|
|
437
|
todayClicked(): void {
|
438
|
// Today button clicked
|
439
|
let today: IMyDate = this.getToday();
|
440
|
this.selectDate(today);
|
441
|
if (this.opts.inline && today.year !== this.visibleMonth.year || today.month !== this.visibleMonth.monthNbr) {
|
442
|
this.visibleMonth = {monthTxt: this.opts.monthLabels[today.month], monthNbr: today.month, year: today.year};
|
443
|
this.generateCalendar(today.month, today.year, true);
|
444
|
}
|
445
|
}
|
446
|
|
447
|
cellClicked(cell: any): void {
|
448
|
// Cell clicked on the calendar
|
449
|
if (cell.cmo === this.PREV_MONTH) {
|
450
|
// Previous month day
|
451
|
this.prevMonth();
|
452
|
}
|
453
|
else if (cell.cmo === this.CURR_MONTH) {
|
454
|
// Current month day - if date is already selected clear it
|
455
|
if (cell.dateObj.year === this.selectedDate.year && cell.dateObj.month === this.selectedDate.month && cell.dateObj.day === this.selectedDate.day) {
|
456
|
this.clearDate();
|
457
|
}
|
458
|
else {
|
459
|
this.selectDate(cell.dateObj);
|
460
|
}
|
461
|
}
|
462
|
else if (cell.cmo === this.NEXT_MONTH) {
|
463
|
// Next month day
|
464
|
this.nextMonth();
|
465
|
}
|
466
|
this.resetMonthYearEdit();
|
467
|
}
|
468
|
|
469
|
cellKeyDown(event: any, cell: any) {
|
470
|
// Cell keyboard handling
|
471
|
if ((event.keyCode === 13 || event.keyCode === 32) && !cell.disabled) {
|
472
|
event.preventDefault();
|
473
|
this.cellClicked(cell);
|
474
|
}
|
475
|
}
|
476
|
|
477
|
clearDate(): void {
|
478
|
// Clears the date and notifies parent using callbacks and value accessor
|
479
|
let date: IMyDate = {year: 0, month: 0, day: 0};
|
480
|
this.dateChanged.emit({date: date, jsdate: null, formatted: "", epoc: 0});
|
481
|
this.onChangeCb("");
|
482
|
this.onTouchedCb();
|
483
|
this.updateDateValue(date, true);
|
484
|
}
|
485
|
|
486
|
selectDate(date: IMyDate): void {
|
487
|
// Date selected, notifies parent using callbacks and value accessor
|
488
|
let dateModel: IMyDateModel = this.getDateModel(date);
|
489
|
this.dateChanged.emit(dateModel);
|
490
|
this.onChangeCb(dateModel);
|
491
|
this.onTouchedCb();
|
492
|
this.updateDateValue(date, false);
|
493
|
if (this.showSelector) {
|
494
|
this.calendarToggle.emit(2);
|
495
|
}
|
496
|
this.showSelector = false;
|
497
|
}
|
498
|
|
499
|
updateDateValue(date: IMyDate, clear: boolean): void {
|
500
|
// Updates date values
|
501
|
this.selectedDate = date;
|
502
|
this.selectionDayTxt = clear ? "" : this.formatDate(date);
|
503
|
this.inputFieldChanged.emit({value: this.selectionDayTxt, dateFormat: this.opts.dateFormat, valid: !clear});
|
504
|
this.invalidDate = false;
|
505
|
}
|
506
|
|
507
|
getDateModel(date: IMyDate): IMyDateModel {
|
508
|
// Creates a date model object from the given parameter
|
509
|
return {date: date, jsdate: this.getDate(date.year, date.month, date.day), formatted: this.formatDate(date), epoc: Math.round(this.getTimeInMilliseconds(date) / 1000.0)};
|
510
|
}
|
511
|
|
512
|
preZero(val: string): string {
|
513
|
// Prepend zero if smaller than 10
|
514
|
return parseInt(val) < 10 ? "0" + val : val;
|
515
|
}
|
516
|
|
517
|
formatDate(val: any): string {
|
518
|
// Returns formatted date string, if mmm is part of dateFormat returns month as a string
|
519
|
let formatted: string = this.opts.dateFormat.replace("yyyy", val.year).replace("dd", this.preZero(val.day));
|
520
|
return this.opts.dateFormat.indexOf("mmm") !== -1 ? formatted.replace("mmm", this.monthText(val.month)) : formatted.replace("mm", this.preZero(val.month));
|
521
|
}
|
522
|
|
523
|
monthText(m: number): string {
|
524
|
// Returns month as a text
|
525
|
return this.opts.monthLabels[m];
|
526
|
}
|
527
|
|
528
|
monthStartIdx(y: number, m: number): number {
|
529
|
// Month start index
|
530
|
let d = new Date();
|
531
|
d.setDate(1);
|
532
|
d.setMonth(m - 1);
|
533
|
d.setFullYear(y);
|
534
|
let idx = d.getDay() + this.sundayIdx();
|
535
|
return idx >= 7 ? idx - 7 : idx;
|
536
|
}
|
537
|
|
538
|
daysInMonth(m: number, y: number): number {
|
539
|
// Return number of days of current month
|
540
|
return new Date(y, m, 0).getDate();
|
541
|
}
|
542
|
|
543
|
daysInPrevMonth(m: number, y: number): number {
|
544
|
// Return number of days of the previous month
|
545
|
let d: Date = this.getDate(y, m, 1);
|
546
|
d.setMonth(d.getMonth() - 1);
|
547
|
return this.daysInMonth(d.getMonth() + 1, d.getFullYear());
|
548
|
}
|
549
|
|
550
|
isCurrDay(d: number, m: number, y: number, cmo: number, today: IMyDate): boolean {
|
551
|
// Check is a given date the today
|
552
|
return d === today.day && m === today.month && y === today.year && cmo === this.CURR_MONTH;
|
553
|
}
|
554
|
|
555
|
getToday(): IMyDate {
|
556
|
let date: Date = new Date();
|
557
|
return {year: date.getFullYear(), month: date.getMonth() + 1, day: date.getDate()};
|
558
|
}
|
559
|
|
560
|
getTimeInMilliseconds(date: IMyDate): number {
|
561
|
return this.getDate(date.year, date.month, date.day).getTime();
|
562
|
}
|
563
|
|
564
|
getDayNumber(date: IMyDate): number {
|
565
|
// Get day number: su=0, mo=1, tu=2, we=3 ...
|
566
|
let d: Date = this.getDate(date.year, date.month, date.day);
|
567
|
return d.getDay();
|
568
|
}
|
569
|
|
570
|
getWeekday(date: IMyDate): string {
|
571
|
// Get weekday: su, mo, tu, we ...
|
572
|
return this.weekDayOpts[this.getDayNumber(date)];
|
573
|
}
|
574
|
|
575
|
getDate(year: number, month: number, day: number): Date {
|
576
|
// Creates a date object from given year, month and day
|
577
|
return new Date(year, month - 1, day, 0, 0, 0, 0);
|
578
|
}
|
579
|
|
580
|
sundayIdx(): number {
|
581
|
// Index of Sunday day
|
582
|
return this.dayIdx > 0 ? 7 - this.dayIdx : 0;
|
583
|
}
|
584
|
|
585
|
generateCalendar(m: number, y: number, notifyChange: boolean): void {
|
586
|
this.dates.length = 0;
|
587
|
let today: IMyDate = this.getToday();
|
588
|
let monthStart: number = this.monthStartIdx(y, m);
|
589
|
let dInThisM: number = this.daysInMonth(m, y);
|
590
|
let dInPrevM: number = this.daysInPrevMonth(m, y);
|
591
|
|
592
|
let dayNbr: number = 1;
|
593
|
let cmo: number = this.PREV_MONTH;
|
594
|
for (let i = 1; i < 7; i++) {
|
595
|
let week: Array<IMyCalendarDay> = [];
|
596
|
if (i === 1) {
|
597
|
// First week
|
598
|
let pm = dInPrevM - monthStart + 1;
|
599
|
// Previous month
|
600
|
for (let j = pm; j <= dInPrevM; j++) {
|
601
|
let date: IMyDate = {year: y, month: m - 1, day: j};
|
602
|
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)});
|
603
|
}
|
604
|
|
605
|
cmo = this.CURR_MONTH;
|
606
|
// Current month
|
607
|
let daysLeft: number = 7 - week.length;
|
608
|
for (let j = 0; j < daysLeft; j++) {
|
609
|
let date: IMyDate = {year: y, month: m, day: dayNbr};
|
610
|
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)});
|
611
|
dayNbr++;
|
612
|
}
|
613
|
}
|
614
|
else {
|
615
|
// Rest of the weeks
|
616
|
for (let j = 1; j < 8; j++) {
|
617
|
if (dayNbr > dInThisM) {
|
618
|
// Next month
|
619
|
dayNbr = 1;
|
620
|
cmo = this.NEXT_MONTH;
|
621
|
}
|
622
|
let date: IMyDate = {year: y, month: cmo === this.CURR_MONTH ? m : m + 1, day: dayNbr};
|
623
|
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)});
|
624
|
dayNbr++;
|
625
|
}
|
626
|
}
|
627
|
let weekNbr: number = this.opts.showWeekNumbers && this.opts.firstDayOfWeek === "mo" ? this.utilService.getWeekNumber(week[0].dateObj) : 0;
|
628
|
this.dates.push({week: week, weekNbr: weekNbr});
|
629
|
}
|
630
|
|
631
|
this.setHeaderBtnDisabledState(m, y);
|
632
|
|
633
|
if (notifyChange) {
|
634
|
// Notify parent
|
635
|
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})}});
|
636
|
}
|
637
|
}
|
638
|
|
639
|
parseSelectedDate(selDate: any): IMyDate {
|
640
|
// Parse selDate value - it can be string or IMyDate object
|
641
|
let date: IMyDate = {day: 0, month: 0, year: 0};
|
642
|
if (typeof selDate === "string") {
|
643
|
let sd: string = <string>selDate;
|
644
|
date.day = this.utilService.parseDatePartNumber(this.opts.dateFormat, sd, "dd");
|
645
|
|
646
|
date.month = this.opts.dateFormat.indexOf("mmm") !== -1
|
647
|
? this.utilService.parseDatePartMonthName(this.opts.dateFormat, sd, "mmm", this.opts.monthLabels)
|
648
|
: this.utilService.parseDatePartNumber(this.opts.dateFormat, sd, "mm");
|
649
|
|
650
|
date.year = this.utilService.parseDatePartNumber(this.opts.dateFormat, sd, "yyyy");
|
651
|
}
|
652
|
else if (typeof selDate === "object") {
|
653
|
date = selDate;
|
654
|
}
|
655
|
this.selectionDayTxt = this.formatDate(date);
|
656
|
return date;
|
657
|
}
|
658
|
|
659
|
parseSelectedMonth(ms: string): IMyMonth {
|
660
|
return this.utilService.parseDefaultMonth(ms);
|
661
|
}
|
662
|
|
663
|
setHeaderBtnDisabledState(m: number, y: number): void {
|
664
|
let dpm: boolean = false;
|
665
|
let dpy: boolean = false;
|
666
|
let dnm: boolean = false;
|
667
|
let dny: boolean = false;
|
668
|
if (this.opts.disableHeaderButtons) {
|
669
|
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);
|
670
|
dpy = this.utilService.isMonthDisabledByDisableUntil({year: y - 1, month: m, day: this.daysInMonth(m, y - 1)}, this.opts.disableUntil);
|
671
|
dnm = this.utilService.isMonthDisabledByDisableSince({year: m === 12 ? y + 1 : y, month: m === 12 ? 1 : m + 1, day: 1}, this.opts.disableSince);
|
672
|
dny = this.utilService.isMonthDisabledByDisableSince({year: y + 1, month: m, day: 1}, this.opts.disableSince);
|
673
|
}
|
674
|
this.prevMonthDisabled = m === 1 && y === this.opts.minYear || dpm;
|
675
|
this.prevYearDisabled = y - 1 < this.opts.minYear || dpy;
|
676
|
this.nextMonthDisabled = m === 12 && y === this.opts.maxYear || dnm;
|
677
|
this.nextYearDisabled = y + 1 > this.opts.maxYear || dny;
|
678
|
}
|
679
|
}
|