1 import { ChangeDetectorRef, Component, forwardRef, Input, OnInit } 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 timestampString: string
24 constructor (private changeDetector: ChangeDetectorRef) {}
27 this.writeValue(this.timestamp || 0)
30 propagateChange = (_: any) => { /* empty */ }
32 writeValue (timestamp: number) {
33 this.timestamp = timestamp
35 this.timestampString = secondsToTime(this.timestamp, true, ':')
38 registerOnChange (fn: (_: any) => void) {
39 this.propagateChange = fn
42 registerOnTouched () {
47 this.timestamp = timeToInt(this.timestampString)
49 this.propagateChange(this.timestamp)
53 if (this.maxTimestamp && this.timestamp > this.maxTimestamp) {
54 this.writeValue(this.maxTimestamp)
56 this.changeDetector.detectChanges()
58 this.propagateChange(this.timestamp)