// Angular imports // import { Component, OnInit, Input, Inject, DoCheck } from '@angular/core'; // Components // // Interfaces // // Services // // Directives // // Other // import { FbFormBase } from '../fbFormBase'; import * as statics from '@fb/statics'; import * as moment from 'moment'; /** * Visar en input för tid * * Syntax: * * * @param model Modell. Måste vara av typen ChangeTrack med en tidsstämpel (YYY-MM-ddTHH:mm) * @param label Label att visa * @param disabled Disabled om satt till true * @param disableReason Används som tooltip vid disabled */ @Component({ selector: 'fb-form-input-time', templateUrl: './fb-form-input-time.component.html' }) export class FbFormInputTimeComponent extends FbFormBase implements OnInit, DoCheck { @Input() noLabel: boolean = undefined; // override inputTime: string = ''; readonly inputId: string = statics.Guid.new(); private ignoreInputTimeCleared: boolean; constructor( @Inject('CommonService') private readonly commonService: fb.ICommonService ) { super(); } ngOnInit(): void { super.ngOnInit(); this.checkAttributesBeforeInit(); this.modelValueChanged(this.model.value as string); } ngDoCheck(): void { if (this.oldModelValue !== this.model.value) { this.modelValueChanged(this.model.value); this.oldModelValue = this.model.value; } } inputTimeChanged(): void { if (this.ignoreInputTimeCleared) { this.ignoreInputTimeCleared = false; return; } // Append sekund-del om giltig tid, annars tomsträng const timeString: string = moment(this.inputTime, 'HH:mm', true).isValid() ? (this.inputTime + ':00') : ''; const dateTimeString: string = this.commonService.date.setTimePart(this.model.value || '', timeString); if (dateTimeString !== this.model.value) { this.updateModel(dateTimeString); } } onblur(): void { switch (this.inputTime.length) { case 1: this.inputTime = '0' + this.inputTime + ':00'; break; case 2: this.inputTime = this.inputTime + ':00'; break; case 3: this.inputTime = '0' + this.inputTime.slice(0, 1) + ':' + this.inputTime.slice(1, 3); break; case 4: this.inputTime = this.inputTime.indexOf(':') === -1 ? this.inputTime.slice(0, 2) + ':' + this.inputTime.slice(2, 4) : '0' + this.inputTime; break; } const timeMom: Moment = moment(this.inputTime, 'HH:mm', true); if (!timeMom.isValid()) { this.inputTime = ''; } this.inputTimeChanged(); } private checkAttributesBeforeInit(): void { let errorString: string = ''; if (typeof this.noLabel !== 'undefined') { errorString += 'Har inte implementerat "noLabel" för denna komponent'; } if (errorString !== '') { if (!!this.model && !!this.model.propertyReference) { errorString = 'Fel för ' + this.getStringFromModel() + ': ' + errorString; } throw new Error(errorString); } } private modelValueChanged(value: string): void { const timeString: string = this.commonService.date.timePart(value || ''); const timeMom: Moment = moment(timeString, 'HH:mm:ss', true); if (timeString === '00:00:00') { return; } if (timeMom.isValid()) { this.inputTime = timeMom.format('HH:mm'); } else { const currTimeMom: Moment = moment(this.inputTime, 'HH:mm', true); if (currTimeMom.isValid()) { this.ignoreInputTimeCleared = true; this.inputTime = ''; } } } }