import {Component, forwardRef, Input} from '@angular/core'; import {SaveRegularObservationsRequest, VersionedSection} from '@nspop/gm-web-facade-api'; import {ControlContainer, ControlValueAccessor, NG_VALUE_ACCESSOR, NgForm} from '@angular/forms'; import deepEqual from 'deep-equal'; export const APP_REGULAR_MODAL_SECTION_CONTROL_VALUE_ACCESSOR: any = { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => RegularModalSectionComponent), multi: true }; @Component({ selector: 'app-regular-modal-section', templateUrl: './regular-modal-section.component.html', styleUrls: ['./regular-modal-section.component.scss'], viewProviders: [ { provide: ControlContainer, useExisting: NgForm } ], providers: [APP_REGULAR_MODAL_SECTION_CONTROL_VALUE_ACCESSOR], }) export class RegularModalSectionComponent implements ControlValueAccessor { private observationDataRegular: SaveRegularObservationsRequest | undefined; private dirtyAble = false; @Input() public readOnlyMode = false; public onChangeCallback: (newValue: SaveRegularObservationsRequest | undefined) => any = () => {}; private onTouchedCallback: () => any = () => {}; constructor() { } get value(): SaveRegularObservationsRequest | undefined { return this.observationDataRegular; } set value(value: SaveRegularObservationsRequest | undefined) { if (!deepEqual(value, this.observationDataRegular)) { this.observationDataRegular = value; if (this.dirtyAble) { this.onChangeCallback(value); } if (!this.dirtyAble && this.observationDataRegular) { // We don't want to mark the field as dirty while data gets initialized this.dirtyAble = true; } if (!this.observationDataRegular) { // reset dirty when data is reset. this.dirtyAble = false; } } } registerOnChange(fn: any): void { this.onChangeCallback = fn; } registerOnTouched(fn: any): void { this.onTouchedCallback = fn; } writeValue(newValue: SaveRegularObservationsRequest): void { this.value = newValue; } markAsDirty(observationField?: VersionedSection): VersionedSection | undefined { if (observationField?.dirty === undefined) { return; } observationField.dirty = true; return observationField; } public urineContains(searchElements: string[]): boolean { if (this.value?.urine) { const intersection = this.value.urine.filter(item => searchElements.includes(item)).length > 0; return intersection !== undefined ? intersection : false ; } return false; } albuminTypesWithout(type: string): string[] { return ['spor A', '+A', '++A', 'max A', '-A'].filter(aType => aType !== type); } }