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 '../../../assets/player/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
22 @Output() inputBlur = new EventEmitter()
24 timestampString: string
26 constructor (private changeDetector: ChangeDetectorRef) {}
29 this.writeValue(this.timestamp || 0)
32 propagateChange = (_: any) => { /* empty */ }
34 writeValue (timestamp: number) {
35 this.timestamp = timestamp
37 this.timestampString = secondsToTime(this.timestamp, true, ':')
40 registerOnChange (fn: (_: any) => void) {
41 this.propagateChange = fn
44 registerOnTouched () {
49 this.timestamp = timeToInt(this.timestampString)
51 this.propagateChange(this.timestamp)
55 if (this.maxTimestamp && this.timestamp > this.maxTimestamp) {
56 this.writeValue(this.maxTimestamp)
58 this.changeDetector.detectChanges()
60 this.propagateChange(this.timestamp)