]>
Commit | Line | Data |
---|---|---|
e79df4ee | 1 | import { ChangeDetectorRef, Component, EventEmitter, forwardRef, Input, OnInit, Output } from '@angular/core' |
f0a39880 | 2 | import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms' |
15a7eafb | 3 | import { secondsToTime, timeToInt } from '@shared/core-utils' |
f0a39880 C |
4 | |
5 | @Component({ | |
6 | selector: 'my-timestamp-input', | |
7 | styleUrls: [ './timestamp-input.component.scss' ], | |
8 | templateUrl: './timestamp-input.component.html', | |
9 | providers: [ | |
10 | { | |
11 | provide: NG_VALUE_ACCESSOR, | |
12 | useExisting: forwardRef(() => TimestampInputComponent), | |
13 | multi: true | |
14 | } | |
15 | ] | |
16 | }) | |
17 | export class TimestampInputComponent implements ControlValueAccessor, OnInit { | |
18 | @Input() maxTimestamp: number | |
19 | @Input() timestamp: number | |
20 | @Input() disabled = false | |
c729caf6 C |
21 | @Input() inputName: string |
22 | @Input() disableBorder = true | |
f0a39880 | 23 | |
e79df4ee C |
24 | @Output() inputBlur = new EventEmitter() |
25 | ||
f0a39880 C |
26 | timestampString: string |
27 | ||
28 | constructor (private changeDetector: ChangeDetectorRef) {} | |
29 | ||
30 | ngOnInit () { | |
31 | this.writeValue(this.timestamp || 0) | |
32 | } | |
33 | ||
34 | propagateChange = (_: any) => { /* empty */ } | |
35 | ||
36 | writeValue (timestamp: number) { | |
37 | this.timestamp = timestamp | |
38 | ||
39 | this.timestampString = secondsToTime(this.timestamp, true, ':') | |
40 | } | |
41 | ||
42 | registerOnChange (fn: (_: any) => void) { | |
43 | this.propagateChange = fn | |
44 | } | |
45 | ||
46 | registerOnTouched () { | |
47 | // Unused | |
48 | } | |
49 | ||
50 | onModelChange () { | |
51 | this.timestamp = timeToInt(this.timestampString) | |
52 | ||
53 | this.propagateChange(this.timestamp) | |
54 | } | |
55 | ||
56 | onBlur () { | |
57 | if (this.maxTimestamp && this.timestamp > this.maxTimestamp) { | |
58 | this.writeValue(this.maxTimestamp) | |
59 | ||
60 | this.changeDetector.detectChanges() | |
61 | ||
62 | this.propagateChange(this.timestamp) | |
63 | } | |
e79df4ee C |
64 | |
65 | this.inputBlur.emit() | |
f0a39880 C |
66 | } |
67 | } |