/** * 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, TemplateRef, ElementRef, ViewChild, AfterViewInit, inject } from '@angular/core'; import { MatPaginator } from '@angular/material/paginator'; import { MatSort, MatSortHeader } from '@angular/material/sort'; import { MatTableDataSource, MatTable, MatColumnDef, MatHeaderCellDef, MatHeaderCell, MatCellDef, MatCell, MatHeaderRowDef, MatHeaderRow, MatRowDef, MatRow } from '@angular/material/table'; import { ActivatedRoute, Router, RouterLink } from '@angular/router'; /** Custom Services */ import { PopoverService } from '../../configuration-wizard/popover/popover.service'; import { ConfigurationWizardService } from '../../configuration-wizard/configuration-wizard.service'; import { FaIconComponent } from '@fortawesome/angular-fontawesome'; import { MatTooltip } from '@angular/material/tooltip'; import { STANDALONE_SHARED_IMPORTS } from 'app/standalone-shared.module'; @Component({ selector: 'mifosx-codes', templateUrl: './codes.component.html', styleUrls: ['./codes.component.scss'], imports: [ ...STANDALONE_SHARED_IMPORTS, FaIconComponent, MatTable, MatSort, MatColumnDef, MatHeaderCellDef, MatHeaderCell, MatSortHeader, MatCellDef, MatCell, MatTooltip, MatHeaderRowDef, MatHeaderRow, MatRowDef, MatRow, MatPaginator ] }) export class CodesComponent implements OnInit, AfterViewInit { private route = inject(ActivatedRoute); private router = inject(Router); private configurationWizardService = inject(ConfigurationWizardService); private popoverService = inject(PopoverService); /** Codes data. */ codesData: any; /** Columns to be displayed in codes table. */ displayedColumns: string[] = [ 'name', 'systemDefined' ]; /** Data source for codes table. */ dataSource: MatTableDataSource; /** Paginator for codes table. */ @ViewChild(MatPaginator, { static: true }) paginator: MatPaginator; /** Sorter for codes table. */ @ViewChild(MatSort, { static: true }) sort: MatSort; /* Reference of create code button */ @ViewChild('buttonCreateCode') buttonCreateCode: ElementRef; /* Template for popover on create code button */ @ViewChild('templateButtonCreateCode') templateButtonCreateCode: TemplateRef; /* Reference of codes table */ @ViewChild('tableCodes') tableCodes: ElementRef; /* Template for popover on codes table */ @ViewChild('templateTableCodes') templateTableCodes: TemplateRef; /** * Retrieves the codes data from `resolve`. * @param {ActivatedRoute} route Activated Route. * @param {Router} router Router. * @param {ConfigurationWizardService} configurationWizardService ConfigurationWizard Service. * @param {PopoverService} popoverService PopoverService. */ constructor() { this.route.data.subscribe((data: { codes: any }) => { this.codesData = data.codes; }); } /** * Sets the codes table. */ ngOnInit() { this.setCodes(); } /** * Initializes the data source, paginator and sorter for codes table. */ setCodes() { this.dataSource = new MatTableDataSource(this.codesData); this.dataSource.paginator = this.paginator; this.dataSource.sort = this.sort; } /** * Filters data in codes table based on passed value. * @param {string} filterValue Value to filter data. */ applyFilter(filterValue: string) { this.dataSource.filter = filterValue.trim().toLowerCase(); } /** * Popover function * @param template TemplateRef. * @param target HTMLElement | ElementRef. * @param position String. * @param backdrop Boolean. */ showPopover( template: TemplateRef, target: HTMLElement | ElementRef, position: string, backdrop: boolean ): void { setTimeout(() => this.popoverService.open(template, target, position, backdrop, {}), 200); } /** * To show popover. */ ngAfterViewInit() { if (this.configurationWizardService.showSystemCodesPage) { setTimeout(() => { this.showPopover(this.templateButtonCreateCode, this.buttonCreateCode.nativeElement, 'bottom', true); }); } if (this.configurationWizardService.showSystemCodesList) { setTimeout(() => { this.showPopover(this.templateTableCodes, this.tableCodes.nativeElement, 'top', true); }); } } /** * Next Step (Create Codes Page) Configuration Wizard. */ nextStep() { this.configurationWizardService.showSystemCodesPage = false; this.configurationWizardService.showSystemCodesList = false; this.configurationWizardService.showSystemCodesForm = true; this.router.navigate(['/system/codes/create']); } /** * Previous Step (manage Codes System Page) Configuration Wizard. */ previousStep() { this.configurationWizardService.showSystemCodesPage = false; this.configurationWizardService.showSystemCodesList = false; this.configurationWizardService.showSystemCodes = true; this.router.navigate(['/system']); } }