-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathtime-value-accessor.ts
46 lines (41 loc) · 1.61 KB
/
time-value-accessor.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
import {Directive, ElementRef, Renderer, Self, forwardRef, provide} from '@angular/core';
import {NG_VALUE_ACCESSOR} from '@angular/common/src/forms-deprecated/directives/control_value_accessor';
import {isBlank, isDate} from '@angular/core/src/facade/lang';
import {BaseValueAccessor} from './base-value-accessor';
import {TimePicker} from "ui/time-picker";
const TIME_VALUE_ACCESSOR = provide(NG_VALUE_ACCESSOR, { useExisting: forwardRef(() => TimeValueAccessor), multi: true });
/**
* The accessor for setting a time and listening to changes that is used by the
* {@link NgModel} directives.
*
* ### Example
* ```
* <TimePicker [(ngModel)]='model.test'>
* ```
*/
@Directive({
selector: 'TimePicker[ngModel]',
host: { '(timeChange)': 'onChange($event.value)' },
providers: [TIME_VALUE_ACCESSOR]
})
export class TimeValueAccessor extends BaseValueAccessor<TimePicker> {
onTouched = () => { };
constructor(elementRef: ElementRef) {
super(elementRef.nativeElement);
}
writeValue(value: any): void {
var normalizedValue = isBlank(value) ? new Date() : value;
if (!isDate(normalizedValue)) {
if (typeof normalizedValue === 'string') {
normalizedValue = new Date(normalizedValue);
} else if (typeof normalizedValue === 'number') {
normalizedValue = new Date(normalizedValue);
}
if (!isDate(normalizedValue)) {
normalizedValue = new Date();
}
}
this.view.time = normalizedValue;
}
registerOnTouched(fn: () => void): void { this.onTouched = fn; }
}