| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619 |
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
9×
2×
64×
9×
1×
1×
2×
14×
14×
26×
2×
26×
1×
1×
1×
9×
9×
2×
26×
9×
9×
9×
9×
1×
27×
9×
9×
9×
9×
9×
9×
1×
9×
9×
9×
9×
9×
9×
9×
2×
13×
13×
13×
13×
13×
1×
52×
2×
26×
1×
64×
9×
9×
9×
9×
9×
9×
9×
9×
9×
9×
9×
26×
9×
13×
9×
9×
9×
4×
4×
4×
4×
20×
12×
8×
4×
9×
4×
4×
4×
4×
4×
9×
9×
1×
9×
9×
9×
1×
11×
11×
9×
2×
1×
1×
1×
1×
32×
1×
1×
1×
26×
2×
6×
6×
6×
81×
2×
6×
6×
52×
2×
5×
5×
5×
47×
2×
6×
6×
3×
6×
6×
1×
1×
1×
1×
1×
4×
4×
4×
4×
66×
1×
4×
4×
1×
1×
1×
1×
9×
1×
1×
1×
1×
1×
1×
1×
1×
3×
3×
3×
3×
3×
3×
3×
1×
1×
1×
1×
| /*
* @license
* Copyright Hôpitaux Universitaires de Genève. All Rights Reserved.
*
* Use of this source code is governed by an Apache-2.0 license that can be
* found in the LICENSE file at https://github.com/DSI-HUG/dejajs-components/blob/master/LICENSE
*/
import { FocusMonitor } from '@angular/cdk/a11y';
import { coerceBooleanProperty } from '@angular/cdk/coercion';
import { AfterContentInit, ChangeDetectionStrategy, ChangeDetectorRef, Component, DoCheck, ElementRef, EventEmitter, HostBinding, Input, OnDestroy, OnInit, Optional, Output, Self, ViewChild, ViewEncapsulation } from '@angular/core';
import { ControlValueAccessor, FormGroupDirective, NgControl, NgForm } from '@angular/forms';
import { _MatInputMixinBase, CanUpdateErrorState, ErrorStateMatcher, MatFormFieldControl } from '@angular/material';
import * as moment_ from 'moment';
import { combineLatest as observableCombineLatest, from as observableFrom, fromEvent as observableFromEvent, merge as observableMerge, ReplaySubject, Subject } from 'rxjs';
import { delay, filter, first, map, switchMap, takeWhile, tap } from 'rxjs/operators';
import { KeyCodes } from '../../common/core/keycodes.enum';
import { DejaConnectionPositionPair } from '../../common/core/overlay/connection-position-pair';
import { DejaChildValidatorDirective } from '../../common/core/validation/child-validator.directive';
import { DateComponentLayout, DaysOfWeek, DejaDateSelectorComponent } from '../date-selector/date-selector.component';
import { formatToMask, formatToUnitOfTime } from './format-to-mask';
const moment: (value?: any, format?: string, strict?: boolean) => moment_.Moment = (<any>moment_).default || moment_;
const noop = () => { };
/**
* Date-picker component for Angular
*/
@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
providers: [{provide: MatFormFieldControl, useExisting: DejaDatePickerComponent}],
selector: 'deja-date-picker',
styles: [require('./date-picker.component.scss')],
template: require('./date-picker.component.html'),
})
export class DejaDatePickerComponent extends _MatInputMixinBase implements OnInit, ControlValueAccessor, AfterContentInit, DoCheck, OnDestroy, MatFormFieldControl<Date | string>, CanUpdateErrorState {
public static nextId = 0;
private static formattingTokens = new RegExp(`(\\[[^\\[]*\\])|(\\\\)?([Hh]mm(ss)?|Mo|MM?M?M?|Do|DDDo|DD?D?D?|ddd?d?|do?|w[o|w]?|W[o|W]?|Qo?|YYYYYY|YYYYY|YYYY|YY|gg(ggg?)?|GG(GGG?)?|e|E|a|A|hh?|HH?|kk?|mm?|ss?|S{1,9}|x|X|zz?|ZZ?|.)`, 'g');
@HostBinding() public id = `my-tel-input-${DejaDatePickerComponent.nextId++}`;
@HostBinding('class.floating') public get shouldLabelFloat() {
return this.focused || !this.empty || !!this.mask;
}
@HostBinding('attr.aria-describedby') public describedBy = '';
public _layout: number | string;
/** Maximum date avaliable inside date-picker */
@Input() public dateMax: Date;
/** Minimum date avaliable inside date-picker */
@Input() public dateMin: Date;
/** Date format. If unset, format will be 'YYYY-MM-DD' + ' HH:mm' it's a date-time selector */
@Input() public set format(format: string) {
this._format = format;
this.formatChanged$.next(format);
}
public get format(): string {
return this._format;
}
/** Placeholder for input */
@Input() public get placeholder(): string {
return this._placeholder;
}
public set placeholder(plh: string) {
this._placeholder = plh;
this.stateChanges.next();
}
@Input() public label: string;
/** Disabled dates. It's an array of DaysOfWeek (number between 0 and 6) or a date. */
@Input() public disableDates: Array<DaysOfWeek | Date>; // | ((d: Date) => boolean);
/** Reference to DejaDateSelectorComponent inside thic control */
@ViewChild(DejaDateSelectorComponent) public dateSelectorComponent: DejaDateSelectorComponent;
/** Offset de position horizontal de la zone de dropdown */
@Input() public overlayOffsetX = 0;
/** Offset de position verticale de la zone de dropdown */
@Input() public overlayOffsetY = 6;
/** Afficher un bouton raccourcis permettant de sélectionner la date courante */
@Input() public set showCurrentDateButton(value: boolean) {
this._showCurrentDateButton = coerceBooleanProperty(value);
}
public get showCurrentDateButton() {
return this._showCurrentDateButton;
}
/** Permettre la saisie de texte libre */
@Input() public allowFreeEntry = false;
@Output() public dateChange = new EventEmitter();
@Output() public timeChange = new EventEmitter();
/** matFormField implementation */
public stateChanges = new Subject<void>();
/** Mask for input */
public _mask: any[];
public get mask() {
return this._mask;
}
public onTouchedCallback: () => void = noop;
public onChangeCallback: (_: any) => void = noop;
/** Internal use */
public overlayOwnerElement: HTMLElement;
public date: Date | string = new Date();
/** matFormField focus implementation */
public focused = false;
/** This property indicates whether the associated NgControl is in an error state. */
public errorState = false;
/** This property allows us to specify a unique string for the type of control in form field.
* The <mat-form-field> will add an additional class based on this type that can be used to
* easily apply special styles to a <mat-form-field> that contains a specific type of control.
* In this example we'll use deja-date-picker as our control type which will result in the form field
* adding the class mat-form-field-deja-date-picker.
*/
public controlType = 'deja-date-picker';
@ViewChild(DejaChildValidatorDirective) private inputValidatorDirective: DejaChildValidatorDirective;
/** Default placeholder for input */
private _placeholder: string;
private _showCurrentDateButton: boolean;
private isAlive = true;
private _disabled: boolean;
private _required: boolean;
private _time: boolean;
private _format: string;
private inputElement$ = new ReplaySubject<HTMLInputElement>(1);
private inputElement: HTMLInputElement;
private focus$ = new Subject();
private _showDropDown = false;
private _positions = DejaConnectionPositionPair.default;
private _inputModel: string;
private cursorPosition: number;
private formatChanged$ = new Subject<string>();
private dateChanged$ = new Subject<Date | string>();
@ViewChild('inputelement')
public set inputElementRef(element: ElementRef) {
Eif (element) {
this.inputElement = element.nativeElement;
Eif (this.inputElement) {
this.overlayOwnerElement = this.inputElement;
} else {
this.overlayOwnerElement = this.elementRef.nativeElement;
}
this.inputElement$.next(this.inputElement);
} else {
this.overlayOwnerElement = this.elementRef.nativeElement;
}
}
public get inputModel() {
return this._inputModel;
}
@Input()
public set positions(value: DejaConnectionPositionPair[] | string) {
this._positions = typeof value === 'string' ? DejaConnectionPositionPair.parse(value) : value;
}
public get positions() {
return this._positions;
}
public get empty() {
return !this.value;
}
/**
* Constructor
* subscribe on different events needed inside this component
*/
constructor(
private elementRef: ElementRef,
private changeDetectorRef: ChangeDetectorRef,
@Self() @Optional() public ngControl: NgControl,
@Optional() _parentForm: NgForm,
@Optional() _parentFormGroup: FormGroupDirective,
_defaultErrorStateMatcher: ErrorStateMatcher,
private fm: FocusMonitor,
) {
super(_defaultErrorStateMatcher, _parentForm, _parentFormGroup, ngControl);
Iif (this.ngControl) {
this.ngControl.valueAccessor = this;
}
this.overlayOwnerElement = this.elementRef.nativeElement;
Iif (this._parentForm) {
this._parentForm.ngSubmit.subscribe(() => {
this.changeDetectorRef.markForCheck();
});
}
Iif (this._parentFormGroup) {
this._parentFormGroup.ngSubmit.subscribe(() => {
this.changeDetectorRef.markForCheck();
});
}
fm.monitor(this.elementRef.nativeElement, true)
.subscribe(origin => {
this.focused = !!origin;
if (!this.focused) {
this.onTouchedCallback();
}
this.stateChanges.next();
});
const keydown$ = observableFrom(this.inputElement$).pipe(
switchMap((element) => observableFromEvent(element, 'keydown')));
const cursorChanged$ = observableFrom(this.inputElement$).pipe(
switchMap((element: HTMLInputElement) => {
return observableMerge(observableFromEvent(element, 'mouseup'), observableFromEvent(element, 'focus'), observableFromEvent(element, 'keyup')).pipe(
map(() => {
return element.selectionStart;
}));
}));
cursorChanged$.pipe(takeWhile(() => this.isAlive))
.subscribe((position: number) => {
this.cursorPosition = position;
});
keydown$.pipe(takeWhile(() => this.isAlive),
filter((event: KeyboardEvent) => !this.showDropDown && (event.keyCode === KeyCodes.KeyD || event.keyCode === KeyCodes.UpArrow || event.keyCode === KeyCodes.DownArrow)))
.subscribe((event: KeyboardEvent) => {
switch (event.keyCode) {
case (KeyCodes.KeyD):
if (!this.allowFreeEntry) {
event.preventDefault();
this.setToCurrentDate();
}
break;
case (KeyCodes.UpArrow):
event.preventDefault();
if (event.altKey) {
this.open();
} else if (this.date) {
// If cursor is on number, we can update it
if (!isNaN(+this._inputModel[this.cursorPosition - 1])) {
// We get an array of all sections of the date format
const format = this._format.match(DejaDatePickerComponent.formattingTokens);
// We check the letter of the format at cursor position
const f = this._format[this.cursorPosition - 1];
// With this letter we determinate the format by checking on format array
let unitOfTime = format.find((str) => str.indexOf(f) !== -1);
// If this format has a corresponding value inside formatToUnitOfTime object we can increment its value with moment.add() method
unitOfTime = (unitOfTime && formatToUnitOfTime[unitOfTime]) ? formatToUnitOfTime[unitOfTime] : undefined;
if (unitOfTime) {
this.updateModel(moment(this.value).add(1, unitOfTime as moment_.unitOfTime.DurationConstructor).toDate());
}
}
}
break;
case (KeyCodes.DownArrow):
event.preventDefault();
if (event.altKey) {
this.open();
} else if (this.date) {
// Same as arrowUp
if (!isNaN(+this._inputModel[this.cursorPosition - 1])) {
const format = this._format.match(DejaDatePickerComponent.formattingTokens);
const f = this._format[this.cursorPosition - 1];
let unitOfTime = format.find((str) => str.indexOf(f) !== -1);
unitOfTime = (unitOfTime && formatToUnitOfTime[unitOfTime]) ? formatToUnitOfTime[unitOfTime] : undefined;
if (unitOfTime) {
this.updateModel(moment(this.value).subtract(1, unitOfTime as moment_.unitOfTime.DurationConstructor).toDate());
}
}
}
break;
default:
break;
}
});
const valueUpdated$ = observableCombineLatest(this.formatChanged$, this.dateChanged$).pipe(
tap(([format]) => {
let mask = [] as string[];
const array = format.match(DejaDatePickerComponent.formattingTokens);
array.forEach((val: string) => {
if (formatToMask[val]) {
mask = [...mask, ...formatToMask[val]];
} else {
mask.push(val);
}
});
this._mask = mask;
}));
valueUpdated$.pipe(takeWhile(() => this.isAlive))
.subscribe(([format, value]) => {
this.date = value;
this._inputModel = (this.date) ? (this.date instanceof Date ? moment(this.date).format(format) : this.date) : null;
// si la position du curseur était stockée, on la restaure apres avoir changé la valeur
Iif (this.cursorPosition && !this.allowFreeEntry) {
this.inputElement$.pipe(
delay(1),
first())
.subscribe((elem: HTMLInputElement) => elem.setSelectionRange(this.cursorPosition, this.cursorPosition));
}
this.changeDetectorRef.markForCheck();
});
keydown$.pipe(takeWhile(() => this.isAlive),
filter(() => this.showDropDown))
.subscribe((event: KeyboardEvent) => {
switch (event.keyCode) {
case (KeyCodes.Escape):
this.close();
break;
default:
this.dateSelectorComponent.keyDown(event);
}
});
observableCombineLatest(this.inputElement$, this.focus$).pipe(
takeWhile(() => this.isAlive))
.subscribe(([element]) => element.focus());
}
/** unsubscribe to all Observable when component is destroyed */
public ngOnDestroy() {
this.isAlive = false;
this.stateChanges.complete();
this.fm.stopMonitoring(this.elementRef.nativeElement);
}
/** Init mask */
public ngOnInit() {
Eif (!this._format) {
if (!this.layout || this.layout === DateComponentLayout.dateonly || this.layout === 'dateonly') {
this.format = 'YYYY-MM-DD';
} else if (this.layout === DateComponentLayout.datetime || this.layout === 'datetime') {
this.format = 'YYYY-MM-DD HH:mm';
} else Eif (this.layout === DateComponentLayout.timeonly || this.layout === 'timeonly') {
this.format = 'HH:mm';
} else {
this.format = 'YYYY-MM-DD';
}
}
}
public ngDoCheck() {
Iif (this.ngControl) {
// We need to re-evaluate this on every change detection cycle, because there are some
// error triggers that we can't subscribe to (e.g. parent form submissions). This means
// that whatever logic is in here has to be super lean or we risk destroying the performance.
this.updateErrorState();
}
}
/** This method is used by the <mat-form-field> to specify the IDs that should be used for the aria-describedby attribute of your component.
* The method has one parameter, the list of IDs, we just need to apply the given IDs to our host element.
*/
public setDescribedByIds(ids: string[]) {
this.describedBy = ids.join(' ');
}
/** This method will be called when the form field is clicked on.
* It allows your component to hook in and handle that click however it wants.
* The method has one parameter, the MouseEvent for the click.
* In our case we'll just focus the first <input> if the user isn't about to click an <input> anyways.
*/
public onContainerClick(event: MouseEvent) {
if ((event.target as Element).tagName.toLowerCase() !== 'input') {
this.elementRef.nativeElement.querySelector('input').focus();
}
}
public get showDropDown() {
return this._showDropDown;
}
/** disabled property setter. Can be string or empty so you can use it like : <deja-date-picker disabled></deja-date-picker> */
@Input()
public set disabled(value: boolean) {
this._disabled = coerceBooleanProperty(value) ? true : null;
this.stateChanges.next();
this.changeDetectorRef.markForCheck();
}
/** disabled property getter. */
public get disabled() {
return this._disabled;
}
/** required property setter. Can be string or empty so you can use it like : <deja-date-picker required></deja-date-picker> */
@Input()
public set required(value: boolean) {
this._required = coerceBooleanProperty(value) ? true : null;
this.stateChanges.next();
}
/** required property getter. */
public get required() {
return this._required;
}
/**
* Component Layout
*/
@Input()
public set layout(value: DateComponentLayout | string) {
Eif (value) {
this._layout = value;
}
this.changeDetectorRef.markForCheck();
}
public get layout() {
return this._layout;
}
/**
* Time property setter. Can be string or empty so you can use it like : <deja-date-picker time></deja-date-picker>
* Used to add time selector next to calendar
*/
@Input()
public set time(value: boolean | string) {
this._time = coerceBooleanProperty(value) ? true : null;
if (this._time) {
this.layout = DateComponentLayout.datetime;
}
this.changeDetectorRef.markForCheck();
}
/** Time property getter */
public get time() {
return this._time;
}
/** Method to close date-picker dialog */
public close() {
this._showDropDown = false;
this.changeDetectorRef.markForCheck();
return false;
}
/** Method to open date-picker dialog */
public open() {
this._showDropDown = true;
this.changeDetectorRef.markForCheck();
}
/** set accessor including call the onchange callback */
public set value(v: Date | string) {
Eif (v !== this.date) {
this.writeValue(v);
this.onChangeCallback(v);
this.stateChanges.next();
}
}
// ************* ControlValueAccessor Implementation **************
/** get accessor */
public get value(): Date | string {
return this.date;
}
/** From ControlValueAccessor interface */
public writeValue(value: Date | string) {
Eif (value !== this.date) {
this.dateChanged$.next(value);
}
}
/** From ControlValueAccessor interface */
public registerOnChange(fn: any) {
this.onChangeCallback = fn;
}
/** From ControlValueAccessor interface */
public registerOnTouched(fn: any) {
this.onTouchedCallback = fn;
}
public setDisabledState(isDisabled: boolean) {
this.disabled = isDisabled;
}
// ************* End of ControlValueAccessor Implementation **************
/** For reactive form. */
public ngAfterContentInit() {
Iif (this.inputValidatorDirective) {
this.inputValidatorDirective.parentControl = this.ngControl;
}
}
/** Give focus to this component */
public setFocus() {
this.focus$.next();
}
/**
* Called when user click on the input of this component.
* If click is located on icon 'calendar' who is in the matPrefix of mat-form-field-container, the picker show off.
*
* @param event
*/
public toggleDateSelector(event: Event) {
Iif (this.disabled) {
return undefined;
}
const target = event.currentTarget as HTMLElement;
Iif (target.id !== 'calendar-button') {
return undefined;
}
this.open();
return false;
}
/**
* Called when input change. If it's a string it's because user set the date manually. So we need to convert it into date with MomentJs.
*
* @param date new value of this model
*/
public updateModel(date: string | Date) {
Iif (typeof date === 'string' && !this.allowFreeEntry) { // && date.replace(/_/g, '').length === this._format.length) {
if (date.replace(/_/g, '').length === this._format.length) { // If mask is fully filled
let d = moment(date, this._format).toDate();
if (!moment(d).isValid()) {
console.warn('[DatePicker]: Invalid Date');
d = null;
this.ngControl.control.setErrors({ invalidMask: true });
this.changeDetectorRef.markForCheck();
}
date = d;
} else if (!date.match(/[0-9]/)) { // if mask is empty - do nothing
return;
} else { // If mask is partially filled
date = null;
console.warn('[DatePicker]: Invalid Date');
this.ngControl.control.setErrors({ invalidMask: true });
this.changeDetectorRef.markForCheck();
}
}
Iif (date instanceof Date) {
if (this.value instanceof Date && this.value && this.value.getTime() === date.getTime()) {
this.close();
return;
}
let event: EventEmitter<any>;
// now we check if it's date or time who is updated to raise correct event
if (this.value instanceof Date && this.value && (date.getFullYear() !== this.value.getFullYear() || date.getMonth() !== this.value.getMonth() || date.getDate() !== this.value.getDate())) {
event = this.dateChange;
} else if (this.value instanceof Date && this.value && (date.getHours() !== this.value.getHours() || date.getMinutes() !== this.value.getMinutes() || date.getSeconds() !== this.value.getSeconds() || date.getMilliseconds() !== this.value.getMilliseconds())) {
event = this.timeChange;
} else {
event = this.dateChange;
}
this.value = date;
this.onTouchedCallback();
event.emit(date);
if (!this._layout || this._layout === DateComponentLayout.dateonly) {
this.close();
} else {
this.changeDetectorRef.markForCheck();
}
} else Eif (this.allowFreeEntry) {
this.value = date;
this.onTouchedCallback();
this.dateChange.emit(date);
this.changeDetectorRef.markForCheck();
}
}
/** Reset date-picker values. */
public reset() {
setTimeout(() => { // To prevent "ExpressionChangedAfterItHasBeenCheckedError"
this.value = undefined;
delete this._inputModel;
});
this.close();
}
public setToCurrentDate(): void {
this.value = new Date();
this.selectHours();
}
private selectHours() {
if (this.layout === DateComponentLayout.datetime) {
setTimeout(() => {
const hoursTemplate = moment(this.date).format('HH:mm');
const stringDate = this.inputElement.value;
const hoursPosition = stringDate.indexOf(hoursTemplate);
this.inputElement.setSelectionRange(hoursPosition, stringDate.length);
});
}
}
}
|