import { Column } from './../models/column' ; import { Row } from './../models/row' ; import { IGrid , ICell } from '../base/interface' ; import { CellType } from '../base/enum' ; import { isNullOrUndefined , DateFormatOptions , Internationalization , getValue } from '@syncfusion/ej2-base' ; import { Cell } from '../models/cell' ; import { ValueFormatter } from './../services/value-formatter' ; import { Query , DataManager } from '@syncfusion/ej2-data' ; import { Data } from '../actions/data' ; import { getForeignData } from '../base/util' ; import { ReturnType } from '../base/type' ; import { Grid } from '../base/grid' ; hidden export class ExportHelper { public parent : IGrid ; private colDepth : number ; private hideColumnInclude : boolean = false ; private foreignKeyData : { [ key ] : Object [ ] ; } = { } ; public constructor ( parent ) { this . parent = parent ; } public static getQuery ( parent , data ) { return data . isRemote ( ) ? data . generateQuery ( true ) . requiresCount ( ) . take ( parent . pageSettings . totalRecordsCount ) : data . generateQuery ( true ) . requiresCount ( ) ; } public getFData ( value , column ) { let foreignKeyData = getForeignData ( column , { } , value , this . foreignKeyData [ column . field ] ) [ 0 ] ; return foreignKeyData ; } public getColumnData ( gridObj ) { let columnPromise = [ ] ; let promise ; let fColumns = gridObj . getForeignKeyColumns ( ) ; if ( fColumns . length ) { fColumns . forEach ( ( col ) => { columnPromise . push ( ( < DataManager > col . dataSource ) . executeQuery ( new Query ( ) ) ) ; } ) ; promise = Promise . all ( columnPromise ) . then ( ( e ) => { fColumns . forEach ( ( col , index ) => { this . foreignKeyData [ col . field ] = e [ index ] . result ; } ) ; } ) as any ; } return promise ; } public getHeaders ( column , isHideColumnInclude ? ) { if ( isHideColumnInclude ) { this . hideColumnInclude = true ; } else { this . hideColumnInclude = false ; } let cols = column ; this . colDepth = this . measureColumnDepth ( cols ) ; let rows = [ ] ; let actualColumns = [ ] ; for ( let i = 0 ; i < this . colDepth ; i ++ ) { rows [ i ] = new Row < Column > ( { } ) ; rows [ i ] . cells = [ ] ; } rows = this . processColumns ( rows ) ; rows = this . processHeaderCells ( rows ) ; for ( let row of rows ) { for ( let i = 0 ; i < row . cells . length ; i ++ ) { let cell = row . cells [ i ] ; if ( cell . visible === undefined && cell . cellType !== CellType . StackedHeader ) { row . cells = this . removeCellFromRow ( row . cells , i ) ; i = i - 1 ; } if ( ( ! isHideColumnInclude ) && cell . visible !== undefined && ( ! cell . visible ) ) { row . cells = this . removeCellFromRow ( row . cells , i ) ; i = i - 1 ; } } } for ( let i = 0 ; i < cols . length ; i ++ ) { this . generateActualColumns ( cols [ i ] , actualColumns ) ; } return { rows : rows , columns : actualColumns } ; } public getConvertedWidth ( input ) { let value = parseFloat ( input ) ; return ( input . indexOf ( '%' ) !== - 1 ) ? ( this . parent . element . getBoundingClientRect ( ) . width * value / 100 ) : value ; } private generateActualColumns ( column , actualColumns ) { if ( column . commands ) { return ; } if ( ! column . columns ) { if ( column . visible || this . hideColumnInclude ) { actualColumns . push ( column ) ; } } else { if ( column . visible || this . hideColumnInclude ) { let colSpan = this . getCellCount ( column , 0 ) ; if ( colSpan !== 0 ) { for ( let i = 0 ; i < column . columns . length ; i ++ ) { this . generateActualColumns ( column . columns [ i ] , actualColumns ) ; } } } } } private removeCellFromRow ( cells , cellIndex ) { let resultCells = [ ] ; for ( let i = 0 ; i < cellIndex ; i ++ ) { resultCells . push ( cells [ i ] ) ; } for ( let i = ( cellIndex + 1 ) ; i < cells . length ; i ++ ) { resultCells . push ( cells [ i ] ) ; } return resultCells ; } private processHeaderCells ( rows ) { let columns = this . parent . enableColumnVirtualization ? this . parent . getColumns ( ) : this . parent . columns as Column [ ] ; for ( let i = 0 ; i < columns . length ; i ++ ) { if ( ! columns [ i ] . commands ) { rows = this . appendGridCells ( columns [ i ] , rows , 0 , i === 0 , false , i === ( columns . length - 1 ) ) ; } } return rows ; } private appendGridCells ( cols , gridRows , index , isFirstObj , isFirstColumn , isLastColumn ) { let lastCol = isLastColumn ? 'e-lastcell' : '' ; if ( ! cols . columns ) { gridRows [ index ] . cells . push ( this . generateCell ( cols , CellType . Header , this . colDepth - index , ( isFirstObj ? '' : ( isFirstColumn ? 'e-firstcell' : '' ) ) + lastCol , index , this . parent . getColumnIndexByUid ( cols . uid ) ) ) ; } else { let colSpan = this . getCellCount ( cols , 0 ) ; if ( colSpan ) { gridRows [ index ] . cells . push ( new Cell < Column > ( < { [ x ] : Object ; } > { cellType : CellType . StackedHeader , column : cols , colSpan : colSpan } ) ) ; } let isFirstCell ; let isIgnoreFirstCell ; for ( let i = 0 , len = cols . columns . length ; i < len ; i ++ ) { isFirstCell = false ; if ( ( cols . columns as Column [ ] ) [ i ] . visible && ! isIgnoreFirstCell ) { isFirstCell = true ; isIgnoreFirstCell = true ; } gridRows = this . appendGridCells ( ( cols . columns as Column [ ] ) [ i ] , gridRows , index + 1 , isFirstObj , i === 0 , i === ( len - 1 ) && isLastColumn ) ; } } return gridRows ; } private generateCell ( gridColumn , cellType ? , rowSpan ? , className ? , rowIndex ? , columnIndex ? ) { let option = { 'visible' : gridColumn . visible , 'isDataCell' : false , 'isTemplate' : ! isNullOrUndefined ( gridColumn . headerTemplate ) , 'rowID' : '' , 'column' : gridColumn , 'cellType' : cellType , 'rowSpan' : rowSpan , 'className' : className , 'index' : rowIndex , 'colIndex' : columnIndex } ; if ( ! option . rowSpan || option . rowSpan < 2 ) { delete option . rowSpan ; } return new Cell < Column > ( < { [ x ] : Object ; } > option ) ; } private processColumns ( rows ) { let gridObj = this . parent ; let columnIndexes = this . parent . getColumnIndexesInView ( ) ; for ( let i = 0 , len = rows . length ; i < len ; i ++ ) { if ( gridObj . allowGrouping ) { for ( let j = 0 , len = gridObj . groupSettings . columns . length ; j < len ; j ++ ) { if ( this . parent . enableColumnVirtualization && columnIndexes . indexOf ( j ) === - 1 ) { continue ; } rows [ i ] . cells . push ( this . generateCell ( { } as Column , CellType . HeaderIndent ) ) ; } } if ( gridObj . detailTemplate || gridObj . childGrid ) { rows [ i ] . cells . push ( this . generateCell ( { } as Column , CellType . DetailHeader ) ) ; } } return rows ; } private getCellCount ( column , count ) { if ( column . columns ) { for ( let i = 0 ; i < column . columns . length ; i ++ ) { count = this . getCellCount ( column . columns [ i ] , count ) ; } } else { if ( column . visible || this . hideColumnInclude ) { count ++ ; } } return count ; } private measureColumnDepth ( column ) { let max = 0 ; for ( let i = 0 ; i < column . length ; i ++ ) { let depth = this . checkDepth ( column [ i ] , 0 ) ; if ( max < depth ) { max = depth ; } } return max + 1 ; } private checkDepth ( col , index ) { let max = index ; let indices = [ ] ; if ( col . columns ) { index ++ ; for ( let i = 0 ; i < col . columns . length ; i ++ ) { indices [ i ] = this . checkDepth ( col . columns [ i ] , index ) ; } for ( let j = 0 ; j < indices . length ; j ++ ) { if ( max < indices [ j ] ) { max = indices [ j ] ; } } index = max ; } return index ; } ; } hidden export class ExportValueFormatter { private internationalization : Internationalization ; private valueFormatter : ValueFormatter ; public constructor ( culture ) { this . valueFormatter = new ValueFormatter ( culture ) ; this . internationalization = new Internationalization ( culture ) ; } private returnFormattedValue ( args , customFormat ) { if ( ! isNullOrUndefined ( args . value ) && args . value ) { return this . valueFormatter . getFormatFunction ( customFormat ) ( args . value ) ; } else { return '' ; } } public formatCellValue ( args ) { if ( args . isForeignKey ) { args . value = getValue ( args . column . foreignKeyValue , getForeignData ( args . column , { } , args . value ) [ 0 ] ) ; } if ( args . column . type === 'number' && args . column . format !== undefined && args . column . format !== '' ) { return args . value ? this . internationalization . getNumberFormat ( { format : args . column . format } ) ( args . value ) : '' ; } else if ( args . column . type === 'boolean' ) { return args . value ? 'true' : 'false' ; } else if ( ( args . column . type === 'date' || args . column . type === 'datetime' || args . column . type === 'time' ) && args . column . format !== undefined ) { if ( typeof args . column . format === 'string' ) { let format ; if ( args . column . type === 'date' ) { format = { type : 'date' , skeleton : args . column . format } ; } else if ( args . column . type === 'time' ) { format = { type : 'time' , skeleton : args . column . format } ; } else { format = { type : 'dateTime' , skeleton : args . column . format } ; } return this . returnFormattedValue ( args , format ) ; } else { if ( args . column . format instanceof Object && args . column . format . type === undefined ) { return ( args . value . toString ( ) ) ; } else { let customFormat ; if ( args . column . type === 'date' ) { customFormat = { type : args . column . format . type , format : args . column . format . format , skeleton : args . column . format . skeleton } ; } else if ( args . column . type === 'time' ) { customFormat = { type : 'time' , format : args . column . format . format , skeleton : args . column . format . skeleton } ; } else { customFormat = { type : 'dateTime' , format : args . column . format . format , skeleton : args . column . format . skeleton } ; } return this . returnFormattedValue ( args , customFormat ) ; } } } else { if ( ( ! isNullOrUndefined ( args . column . type ) && ! isNullOrUndefined ( args . value ) ) || ! isNullOrUndefined ( args . value ) ) { return ( args . value ) . toString ( ) ; } else { return '' ; } } } }