-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
datepicker-input.ts
306 lines (261 loc) · 9.72 KB
/
datepicker-input.ts
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
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {
AfterContentInit,
Directive,
ElementRef,
EventEmitter,
forwardRef,
Inject,
Input,
OnDestroy,
Optional,
Output,
Renderer2
} from '@angular/core';
import {MdDatepicker} from './datepicker';
import {
AbstractControl,
ControlValueAccessor,
NG_VALIDATORS,
NG_VALUE_ACCESSOR,
ValidationErrors,
Validator,
ValidatorFn,
Validators
} from '@angular/forms';
import {Subscription} from 'rxjs/Subscription';
import {MdFormField} from '../form-field/index';
import {DOWN_ARROW} from '../core/keyboard/keycodes';
import {DateAdapter} from '../core/datetime/index';
import {createMissingDateImplError} from './datepicker-errors';
import {MD_DATE_FORMATS, MdDateFormats} from '../core/datetime/date-formats';
import {coerceBooleanProperty} from '@angular/cdk/coercion';
export const MD_DATEPICKER_VALUE_ACCESSOR: any = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => MdDatepickerInput),
multi: true
};
export const MD_DATEPICKER_VALIDATORS: any = {
provide: NG_VALIDATORS,
useExisting: forwardRef(() => MdDatepickerInput),
multi: true
};
/**
* An event used for datepicker input and change events. We don't always have access to a native
* input or change event because the event may have been triggered by the user clicking on the
* calendar popup. For consistency, we always use MdDatepickerInputEvent instead.
*/
export class MdDatepickerInputEvent<D> {
/** The new value for the target datepicker input. */
value: D | null;
constructor(public target: MdDatepickerInput<D>, public targetElement: HTMLElement) {
this.value = this.target.value;
}
}
/** Directive used to connect an input to a MdDatepicker. */
@Directive({
selector: 'input[mdDatepicker], input[matDatepicker]',
providers: [MD_DATEPICKER_VALUE_ACCESSOR, MD_DATEPICKER_VALIDATORS],
host: {
'[attr.aria-haspopup]': 'true',
'[attr.aria-owns]': '(_datepicker?.opened && _datepicker.id) || null',
'[attr.min]': 'min ? _dateAdapter.getISODateString(min) : null',
'[attr.max]': 'max ? _dateAdapter.getISODateString(max) : null',
'[disabled]': 'disabled',
'(input)': '_onInput($event.target.value)',
'(change)': '_onChange()',
'(blur)': '_onTouched()',
'(keydown)': '_onKeydown($event)',
},
exportAs: 'mdDatepickerInput',
})
export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAccessor, OnDestroy,
Validator {
/** The datepicker that this input is associated with. */
@Input()
set mdDatepicker(value: MdDatepicker<D>) {
if (value) {
this._datepicker = value;
this._datepicker._registerInput(this);
}
}
_datepicker: MdDatepicker<D>;
@Input() set matDatepicker(value: MdDatepicker<D>) { this.mdDatepicker = value; }
@Input() set mdDatepickerFilter(filter: (date: D | null) => boolean) {
this._dateFilter = filter;
this._validatorOnChange();
}
_dateFilter: (date: D | null) => boolean;
@Input() set matDatepickerFilter(filter: (date: D | null) => boolean) {
this.mdDatepickerFilter = filter;
}
/** The value of the input. */
@Input()
get value(): D | null {
return this._getValidDateOrNull(this._dateAdapter.parse(
this._elementRef.nativeElement.value, this._dateFormats.parse.dateInput));
}
set value(value: D | null) {
if (value != null && !this._dateAdapter.isDateInstance(value)) {
throw Error('Datepicker: value not recognized as a date object by DateAdapter.');
}
this._lastValueValid = !value || this._dateAdapter.isValid(value);
value = this._getValidDateOrNull(value);
let oldDate = this.value;
this._renderer.setProperty(this._elementRef.nativeElement, 'value',
value ? this._dateAdapter.format(value, this._dateFormats.display.dateInput) : '');
if (!this._dateAdapter.sameDate(oldDate, value)) {
this._valueChange.emit(value);
}
}
/** The minimum valid date. */
@Input()
get min(): D | null { return this._min; }
set min(value: D | null) {
this._min = value;
this._validatorOnChange();
}
private _min: D | null;
/** The maximum valid date. */
@Input()
get max(): D | null { return this._max; }
set max(value: D | null) {
this._max = value;
this._validatorOnChange();
}
private _max: D | null;
/** Whether the datepicker-input is disabled. */
@Input()
get disabled() { return this._disabled; }
set disabled(value: any) {
this._disabled = coerceBooleanProperty(value);
}
private _disabled: boolean;
/** Emits when a `change` event is fired on this `<input>`. */
@Output() dateChange = new EventEmitter<MdDatepickerInputEvent<D>>();
/** Emits when an `input` event is fired on this `<input>`. */
@Output() dateInput = new EventEmitter<MdDatepickerInputEvent<D>>();
/** Emits when the value changes (either due to user input or programmatic change). */
_valueChange = new EventEmitter<D|null>();
_onTouched = () => {};
private _cvaOnChange: (value: any) => void = () => {};
private _validatorOnChange = () => {};
private _datepickerSubscription: Subscription;
/** The form control validator for whether the input parses. */
private _parseValidator: ValidatorFn = (): ValidationErrors | null => {
return this._lastValueValid ?
null : {'mdDatepickerParse': {'text': this._elementRef.nativeElement.value}};
}
/** The form control validator for the min date. */
private _minValidator: ValidatorFn = (control: AbstractControl): ValidationErrors | null => {
return (!this.min || !control.value ||
this._dateAdapter.compareDate(this.min, control.value) <= 0) ?
null : {'mdDatepickerMin': {'min': this.min, 'actual': control.value}};
}
/** The form control validator for the max date. */
private _maxValidator: ValidatorFn = (control: AbstractControl): ValidationErrors | null => {
return (!this.max || !control.value ||
this._dateAdapter.compareDate(this.max, control.value) >= 0) ?
null : {'mdDatepickerMax': {'max': this.max, 'actual': control.value}};
}
/** The form control validator for the date filter. */
private _filterValidator: ValidatorFn = (control: AbstractControl): ValidationErrors | null => {
return !this._dateFilter || !control.value || this._dateFilter(control.value) ?
null : {'mdDatepickerFilter': true};
}
/** The combined form control validator for this input. */
private _validator: ValidatorFn | null =
Validators.compose(
[this._parseValidator, this._minValidator, this._maxValidator, this._filterValidator]);
/** Whether the last value set on the input was valid. */
private _lastValueValid = false;
constructor(
private _elementRef: ElementRef,
private _renderer: Renderer2,
@Optional() private _dateAdapter: DateAdapter<D>,
@Optional() @Inject(MD_DATE_FORMATS) private _dateFormats: MdDateFormats,
@Optional() private _mdFormField: MdFormField) {
if (!this._dateAdapter) {
throw createMissingDateImplError('DateAdapter');
}
if (!this._dateFormats) {
throw createMissingDateImplError('MD_DATE_FORMATS');
}
}
ngAfterContentInit() {
if (this._datepicker) {
this._datepickerSubscription =
this._datepicker.selectedChanged.subscribe((selected: D) => {
this.value = selected;
this._cvaOnChange(selected);
this._onTouched();
this.dateInput.emit(new MdDatepickerInputEvent(this, this._elementRef.nativeElement));
this.dateChange.emit(new MdDatepickerInputEvent(this, this._elementRef.nativeElement));
});
}
}
ngOnDestroy() {
if (this._datepickerSubscription) {
this._datepickerSubscription.unsubscribe();
}
}
registerOnValidatorChange(fn: () => void): void {
this._validatorOnChange = fn;
}
validate(c: AbstractControl): ValidationErrors | null {
return this._validator ? this._validator(c) : null;
}
/**
* Gets the element that the datepicker popup should be connected to.
* @return The element to connect the popup to.
*/
getPopupConnectionElementRef(): ElementRef {
return this._mdFormField ? this._mdFormField.underlineRef : this._elementRef;
}
// Implemented as part of ControlValueAccessor
writeValue(value: D): void {
this.value = value;
}
// Implemented as part of ControlValueAccessor
registerOnChange(fn: (value: any) => void): void {
this._cvaOnChange = fn;
}
// Implemented as part of ControlValueAccessor
registerOnTouched(fn: () => void): void {
this._onTouched = fn;
}
// Implemented as part of ControlValueAccessor
setDisabledState(disabled: boolean): void {
this._renderer.setProperty(this._elementRef.nativeElement, 'disabled', disabled);
}
_onKeydown(event: KeyboardEvent) {
if (event.altKey && event.keyCode === DOWN_ARROW) {
this._datepicker.open();
event.preventDefault();
}
}
_onInput(value: string) {
let date = this._dateAdapter.parse(value, this._dateFormats.parse.dateInput);
this._lastValueValid = !date || this._dateAdapter.isValid(date);
date = this._getValidDateOrNull(date);
this._cvaOnChange(date);
this._valueChange.emit(date);
this.dateInput.emit(new MdDatepickerInputEvent(this, this._elementRef.nativeElement));
}
_onChange() {
this.dateChange.emit(new MdDatepickerInputEvent(this, this._elementRef.nativeElement));
}
/**
* @param obj The object to check.
* @returns The given object if it is both a date instance and valid, otherwise null.
*/
private _getValidDateOrNull(obj: any): D | null {
return (this._dateAdapter.isDateInstance(obj) && this._dateAdapter.isValid(obj)) ? obj : null;
}
}