Files
cbsnexora/src/app/system/manage-data-tables/column-dialog/column-dialog.component.ts
T

152 lines
4.2 KiB
TypeScript

/**
* Copyright since 2025 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/** Angular Imports */
import { Component, OnInit, inject } from '@angular/core';
import {
MAT_DIALOG_DATA,
MatDialogRef,
MatDialogTitle,
MatDialogContent,
MatDialogActions,
MatDialogClose
} from '@angular/material/dialog';
import { UntypedFormGroup, UntypedFormBuilder, Validators, ReactiveFormsModule } from '@angular/forms';
/** Data Imports */
import { columnTypeData } from '../column-type-data';
import { CdkScrollable } from '@angular/cdk/scrolling';
import { MatCheckbox } from '@angular/material/checkbox';
import { STANDALONE_SHARED_IMPORTS } from 'app/standalone-shared.module';
/**
* Column Dialog Component.
*/
@Component({
selector: 'mifosx-column-dialog',
templateUrl: './column-dialog.component.html',
styleUrls: ['./column-dialog.component.scss'],
imports: [
...STANDALONE_SHARED_IMPORTS,
MatDialogTitle,
CdkScrollable,
MatDialogContent,
MatCheckbox,
MatDialogActions,
MatDialogClose
]
})
export class ColumnDialogComponent implements OnInit {
dialogRef = inject<MatDialogRef<ColumnDialogComponent>>(MatDialogRef);
formBuilder = inject(UntypedFormBuilder);
data = inject(MAT_DIALOG_DATA);
/** Column Form. */
columnForm: UntypedFormGroup;
/** Column Type Data */
columnTypeData = columnTypeData;
/**
* Creates the add column form.
*/
ngOnInit() {
this.columnForm = this.formBuilder.group({
name: [
this.data ? this.data.columnName : '',
Validators.required
],
type: [
{
value: this.data
? this.data.columnDisplayType === ''
? ''
: this.getColumnType(this.data.columnDisplayType)
: '',
disabled: this.data.type === 'existing'
},
Validators.required
],
length: [
{
value: this.data ? +this.data.columnLength : '',
disabled: this.getColumnType(this.data.columnDisplayType) !== 'String' || this.data.type === 'existing'
},
[
Validators.required,
Validators.min(1)
]
],
mandatory: [{ value: this.data.isColumnNullable, disabled: this.data.type === 'existing' }],
unique: [
{ value: this.data.isColumnUnique, disabled: this.data.isColumnNullable || this.data.type === 'existing' }],
indexed: [{ value: this.data.isColumnIndexed, disabled: this.data.type === 'existing' }],
code: [
{
value: this.data ? this.data.columnCode : '',
disabled: this.getColumnType(this.data.columnDisplayType) !== 'Dropdown' || this.data.type === 'existing'
},
Validators.required
]
});
this.onColumnTypeChanges();
}
/**
* Returns the modified Column Type.
* @param {string} columnDisplayType Column Display Type.
* @returns {string} Column Type.
*/
getColumnType(columnDisplayType: string): string {
switch (columnDisplayType) {
case undefined: {
return '';
}
case 'INTEGER': {
return 'Number';
}
case 'CODELOOKUP': {
return 'Dropdown';
}
default: {
return columnDisplayType[0] + columnDisplayType.substring(1).toLowerCase();
}
}
}
/**
* Watches on Column Type field to enable/diable certain fields.
*/
onColumnTypeChanges() {
this.columnForm.get('type').valueChanges.subscribe((type) => {
switch (type) {
case 'String': {
this.columnForm.get('length').enable();
this.columnForm.get('code').disable();
break;
}
case 'Dropdown': {
this.columnForm.get('code').enable();
this.columnForm.get('length').disable();
break;
}
default: {
this.columnForm.get('code').disable();
this.columnForm.get('length').disable();
}
}
});
}
/**
* Closes the dialog and returns value of the form.
*/
submit() {
this.dialogRef.close(this.columnForm.value);
}
}