/** Angular Imports */ import { Component , OnInit , AfterViewInit } from '@angular/core' ; import { MatTableDataSource } from '@angular/material/table' ; import { ActivatedRoute , Router } from '@angular/router' ; import { FormControl } from '@angular/forms' ; /** Custom Services */ import { ReportsService } from '../reports.service' ; /**
 * XBRL Component
 */ @ Component ( { selector : 'mifosx-xbrl' , templateUrl : './xbrl.component.html' , styleUrls : [ './xbrl.component.scss' ] } ) export class XBRLComponent implements OnInit , AfterViewInit { /** Mix Taxonomy Array */ mixtaxonomyArray : any = [ ] ; /** Mix Mappings */ mixMappingJson : any ; /** GL Accounts data */ glAccounts : any ; /** Filtered GL Accounts */ filteredGlAccounts : any ; /** Columns to be displayed in reports table. */ displayedColumns : string [ ] = [ 'info' , 'name' , 'dimension' , 'mapping' ] ; /** Data source for reports table. */ dataSource = new MatTableDataSource ( ) ; /** XBRL save status */ XBRLSuccess = false ; param { ActivatedRoute } route param { Router } router param { ReportService } reportService constructor ( private route , private router , private reportService ) { this . route . data . subscribe ( ( data ) => { this . mixtaxonomyArray = data . mixtaxonomy ; this . mixMappingJson = data . mixmapping . config || JSON . stringify ( { } ) ; this . glAccounts = data . glAccounts ; } ) ; } /**
     * Creates Mappings, sets the dataSource and datasource filterPredicate.
     */ ngOnInit ( ) { if ( localStorage . getItem ( 'XbrlReportSaveSuccess' ) ) { localStorage . removeItem ( 'XbrlReportSaveSuccess' ) ; this . XBRLSuccess = true ; setTimeout ( ( ) => { this . XBRLSuccess = false ; } , 3000 ) ; } this . createMappings ( ) ; this . dataSource = new MatTableDataSource ( this . mixtaxonomyArray ) ; this . setDatasourceFilter ( ) ; } /**
     * Subscribe to taxonomy's value changes and filters options accordingly.
     */ ngAfterViewInit ( ) { this . mixtaxonomyArray . forEach ( ( taxonomy ) => { taxonomy . mapping ?. valueChanges . subscribe ( ( value ) => { this . filteredGlAccounts = this . applyFilter ( value ) ; } ) ; } ) ; } /**
     * Maps Taxonomy to it's value and add's formcontrol.
     */ createMappings ( ) { this . mixtaxonomyArray . forEach ( ( taxonomy ) => { const mapping = JSON . parse ( this . mixMappingJson ) [ taxonomy . id ] ; taxonomy . mapping = new FormControl ( '' ) ; taxonomy . mapping . value = mapping || '' ; } ) ; } /**
     * Sets custom datasource filter.
     */ setDatasourceFilter ( ) { this . dataSource . filterPredicate = ( data , filter ) => { return data . type . toString ( ) === filter ; } ; this . dataSource . filter = '0' ; } param { string } value applyFilter ( value ) { const filterValue = value . toLowerCase ( ) ; return this . glAccounts . filter ( ( entry ) => entry . name . toLowerCase ( ) . includes ( filterValue ) || entry . glCode . includes ( filterValue ) ) ; } param { number } index filterTaxonomiesBy ( index ) { this . dataSource . filter = index . toString ( ) ; } param { any } glAccount returns { string } displayGLAccount ( glAccount ? ) { if ( typeof ( glAccount ) === 'object' ) { return glAccount ? glAccount . name + ' (' + glAccount . glCode + ')' : undefined ; } else { return glAccount ; } } /**
     * Reloads the component post saving changes.
     */ reloadComponent ( ) { this . router . routeReuseStrategy . shouldReuseRoute = ( ) => false ; this . router . onSameUrlNavigation = 'reload' ; this . router . navigate ( [ './xbrl' ] ) ; } /**
     * Posts Taxonomy Data onto the API.
     */ submit ( ) { const config = { } ; const serialObject = { } ; this . mixtaxonomyArray . forEach ( ( taxonomy ) => { config [ taxonomy . id ] = taxonomy . mapping . value ; } ) ; serialObject [ 'config' ] = JSON . stringify ( config ) ; serialObject [ 'identifier' ] = 'default;' ; this . reportService . editMixMappings ( serialObject ) . subscribe ( ( response ) => { localStorage . setItem ( 'XbrlReportSaveSuccess' , 'true' ) ; this . reloadComponent ( ) ; } ) ; } }