import { remove } from '@syncfusion/ej2-base' ; import { isNullOrUndefined } from '@syncfusion/ej2-base' ; import { NumberFormatOptions , DateFormatOptions } from '@syncfusion/ej2-base' ; import { IAction , IGrid , NotifyArgs , ICellRenderer } from '../base/interface' ; import { CellType } from '../base/enum' ; import { ServiceLocator } from '../services/service-locator' ; import { CellRendererFactory } from '../services/cell-render-factory' ; import { uiUpdate , initialEnd , dataReady , modelChanged , refreshAggregates , refreshFooterRenderer , groupAggregates } from '../base/constant' ; import { FooterRenderer } from '../renderer/footer-renderer' ; import { SummaryCellRenderer } from '../renderer/summary-cell-renderer' ; import { AggregateRowModel , ColumnModel } from '../models/models' ; import { AggregateColumn } from '../models/aggregate' ; import { GroupSummaryModelGenerator , CaptionSummaryModelGenerator } from '../services/summary-model-generator' ; /**
 * Summary Action controller.
 */ export class Aggregate implements IAction { private parent : IGrid ; private locator : ServiceLocator ; private footerRenderer : FooterRenderer ; constructor ( parent , locator ? ) { this . parent = parent ; this . locator = locator ; this . addEventListener ( ) ; } public getModuleName ( ) { return 'aggregate' ; } private initiateRender ( ) { let cellFac = this . locator . getService < CellRendererFactory > ( 'cellRendererFactory' ) ; let instance = new SummaryCellRenderer ( this . parent , this . locator ) ; [ CellType . Summary , CellType . CaptionSummary , CellType . GroupSummary ] . forEach ( ( type ) => cellFac . addCellRenderer ( type , instance ) ) ; this . footerRenderer = new FooterRenderer ( this . parent , this . locator ) ; this . footerRenderer . renderPanel ( ) ; this . footerRenderer . renderTable ( ) ; this . locator . register ( 'footerRenderer' , this . footerRenderer ) ; let fn = ( ) => { this . prepareSummaryInfo ( ) ; this . parent . off ( dataReady , fn ) ; } ; this . parent . on ( dataReady , fn , this ) ; this . parent . on ( dataReady , this . footerRenderer . refresh , this . footerRenderer ) ; } private prepareSummaryInfo ( ) { summaryIterator ( this . parent . aggregates , ( column ) => { let dataColumn = this . parent . getColumnByField ( column . field ) || { } ; let type = dataColumn . type ; column . setPropertiesSilent ( { format : this . getFormatFromType ( column . format , type ) } ) ; column . setFormatter ( this . parent . locale ) ; column . setPropertiesSilent ( { columnName : column . columnName || column . field } ) ; } ) ; } private getFormatFromType ( format , type ) { if ( isNullOrUndefined ( type ) || typeof format !== 'string' ) { return format ; } let obj ; switch ( type ) { case 'number' : obj = { format : format } ; break ; case 'date' : obj = { type : type , skeleton : format } ; break ; case 'datetime' : obj = { type : 'dateTime' , skeleton : format } ; break ; } return obj ; } public onPropertyChanged ( e ) { if ( e . module !== this . getModuleName ( ) ) { return ; } if ( isNullOrUndefined ( this . footerRenderer ) ) { this . initiateRender ( ) ; } this . prepareSummaryInfo ( ) ; this . footerRenderer . refresh ( ) ; let cModel = new CaptionSummaryModelGenerator ( this . parent ) ; let gModel = new GroupSummaryModelGenerator ( this . parent ) ; if ( ( < Object [ ] > gModel . getData ( ) ) . length !== 0 || ! cModel . isEmpty ( ) ) { this . parent . notify ( modelChanged , { } ) ; } } public addEventListener ( ) { if ( this . parent . isDestroyed ) { return ; } this . parent . on ( initialEnd , this . initiateRender , this ) ; this . parent . on ( uiUpdate , this . onPropertyChanged , this ) ; this . parent . on ( refreshAggregates , this . refresh , this ) ; } public removeEventListener ( ) { if ( this . parent . isDestroyed ) { return ; } this . footerRenderer . removeEventListener ( ) ; this . parent . off ( initialEnd , this . initiateRender ) ; this . parent . off ( dataReady , this . footerRenderer . refresh ) ; this . parent . off ( uiUpdate , this . onPropertyChanged ) ; this . parent . off ( refreshAggregates , this . refresh ) ; } public destroy ( ) { this . removeEventListener ( ) ; remove ( this . parent . element . querySelector ( '.e-gridfooter' ) ) ; } public refresh ( data ) { let editedData = data instanceof Array ? data : [ data ] ; this . parent . notify ( refreshFooterRenderer , editedData ) ; if ( this . parent . groupSettings . columns . length > 0 ) { this . parent . notify ( groupAggregates , editedData ) ; } } } private export function summaryIterator ( aggregates , callback ) { aggregates . forEach ( ( row ) => { row . columns . forEach ( ( column ) => { callback ( column , row ) ; } ) ; } ) ; }