// Angular imports // import { OnInit, Input, Output, EventEmitter } from '@angular/core'; // Components // // Interfaces // import { IUpdateModel } from './forms.interface'; // Services // // Directives // // Other // export class FbFormBase implements OnInit { @Input() model: fb.ChangeTrack; @Input() label: string; @Input() disabled: boolean = false; // OBS! Har bytt namn från disable @Input() disableReason: string; @Input() noLabel: boolean = false; @Output() onValueChange: EventEmitter = new EventEmitter(); protected oldModelValue: any; ngOnInit(): void { if (this.model === undefined) { const errorString: string = `Missing required parameter 'model' for ${this.getStringFromModel()}`; throw new Error(errorString); } this.oldModelValue = this.model.value; } updateModel(data: T): void { if (typeof this.model === 'undefined' || this.model === null || typeof this.model !== 'object') { console.log('Felaktig typ att bevaka'); return; } if (data !== this.model.value && !((data as any) === '' && this.model.value === null)) { this.model.setValue(data); const lastOldModelValue: T = this.oldModelValue; this.oldModelValue = data; const emitVal: IUpdateModel = { newVal: data, oldVal: lastOldModelValue }; this.onValueChange.emit(emitVal); } } // TODO frha: Se till att denna används genomgående vid kastande av fel protected getStringFromModel(): string { const str: string = `fbForm-component${ this.model && this.model.propertyReference ? ` with propertyReference ${this.model.propertyReference}` : ''}${ this.constructor.name ? ` of type ${this.constructor.name}` : ''}${ this.label ? ` with label ${this.label}` : ''}`; return str; } }