100 lines
2.8 KiB
TypeScript
100 lines
2.8 KiB
TypeScript
import { Component, Input, OnInit } from '@angular/core';
|
|
import { UntypedFormArray } from '@angular/forms';
|
|
import { MatChipEditedEvent, MatChipInputEvent } from '@angular/material/chips';
|
|
import { EnumUtils } from '@app/core/services/utilities/enum-utils.service';
|
|
import { BaseComponent } from '@common/base/base.component';
|
|
import { ValidationErrorModel } from '@common/forms/validation/error-model/validation-error-model';
|
|
import { TenantDepositConfigEditorModel, TenantEditorModel, TenantSourceEditorModel } from '../tenant-editor.model';
|
|
|
|
@Component({
|
|
selector: 'app-tenant-source-component',
|
|
templateUrl: 'tenant-source.component.html',
|
|
styleUrls: ['./tenant-source.component.scss']
|
|
})
|
|
export class TenantSourceComponent extends BaseComponent implements OnInit {
|
|
|
|
@Input() form;
|
|
@Input() validationErrorModel: ValidationErrorModel = null;
|
|
@Input() validationRootPath: string = null;
|
|
@Input() codes: Map<number, string[]>;
|
|
@Input() label: string = null;
|
|
|
|
constructor(
|
|
public enumUtils: EnumUtils,
|
|
) { super(); }
|
|
|
|
ngOnInit() {
|
|
}
|
|
|
|
//
|
|
// source
|
|
//
|
|
addSource(): void {
|
|
const formArray = this.form.get('sources') as UntypedFormArray;
|
|
this.codes.set(formArray.length, []);
|
|
const source: TenantSourceEditorModel = new TenantSourceEditorModel(this.validationErrorModel);
|
|
formArray.push(source.buildForm({ rootPath: this.validationRootPath + 'sources[' + formArray.length + '].' }));
|
|
}
|
|
|
|
removeSource(sourceIndex: number): void {
|
|
this.codes.delete((this.form.get('sources') as UntypedFormArray).length);
|
|
(this.form.get('sources') as UntypedFormArray).removeAt(sourceIndex);
|
|
|
|
// Reapply validators
|
|
TenantDepositConfigEditorModel.reapplySourcesFieldsValidators(
|
|
{
|
|
formArray: this.form.get('sources') as UntypedFormArray,
|
|
validationErrorModel: this.validationErrorModel,
|
|
rootPath: this.validationRootPath
|
|
}
|
|
)
|
|
this.form.get('sources').markAsDirty();
|
|
}
|
|
|
|
// source codes
|
|
|
|
addCode(event: MatChipInputEvent, key: number): void {
|
|
const value = (event.value || '').trim();
|
|
|
|
if (value){
|
|
const values = this.codes.get(key);
|
|
values.push(value);
|
|
this.codes.set(key, values);
|
|
}
|
|
event.chipInput!.clear();
|
|
}
|
|
|
|
removeCode(code: string, key: number): void {
|
|
const values = this.codes.get(key);
|
|
if (values){
|
|
const index = values.indexOf(code);
|
|
|
|
if (index >= 0) {
|
|
values.splice(index, 1);
|
|
this.codes.set(key, values);
|
|
}
|
|
}
|
|
}
|
|
|
|
editCode(code: string, event: MatChipEditedEvent, key: number) {
|
|
const values = this.codes.get(key);
|
|
if (values){
|
|
const value = event.value.trim();
|
|
|
|
// Remove code if it no longer has a value
|
|
if (!value) {
|
|
this.removeCode(code, key);
|
|
return;
|
|
}
|
|
|
|
const index = values.indexOf(code);
|
|
if (index >= 0) {
|
|
values[index] = value;
|
|
this.codes.set(key, values);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
}
|