1 import { ChangeDetectorRef, Component, EventEmitter, forwardRef, Input, OnInit, Output } from '@angular/core'
2 import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
3 import { secondsToTime, timeToInt } from '@shared/core-utils'
6 selector: 'my-timestamp-input',
7 styleUrls: [ './timestamp-input.component.scss' ],
8 templateUrl: './timestamp-input.component.html',
11 provide: NG_VALUE_ACCESSOR,
12 useExisting: forwardRef(() => TimestampInputComponent),
17 export class TimestampInputComponent implements ControlValueAccessor, OnInit {
18 @Input() maxTimestamp: number
19 @Input() timestamp: number
20 @Input() disabled = false
21 @Input() inputName: string
22 @Input() disableBorder = true
24 @Output() inputBlur = new EventEmitter()
26 timestampString: string
28 constructor (private changeDetector: ChangeDetectorRef) {}
31 this.writeValue(this.timestamp || 0)
34 propagateChange = (_: any) => { /* empty */ }
36 writeValue (timestamp: number) {
37 this.timestamp = timestamp
39 this.timestampString = secondsToTime(this.timestamp, true, ':')
42 registerOnChange (fn: (_: any) => void) {
43 this.propagateChange = fn
46 registerOnTouched () {
51 this.timestamp = timeToInt(this.timestampString)
53 this.propagateChange(this.timestamp)
57 if (this.maxTimestamp && this.timestamp > this.maxTimestamp) {
58 this.writeValue(this.maxTimestamp)
60 this.changeDetector.detectChanges()
62 this.propagateChange(this.timestamp)