import { isNullOrUndefined , getValue , setValue } from '@syncfusion/ej2-base' ; import { IModelGenerator , ICell , IRow , IGrid } from '../base/interface' ; import { Row } from '../models/row' ; import { CellType } from '../base/enum' ; import { Column } from '../models/column' ; import { Cell } from '../models/cell' ; import { getUid } from '../base/util' ; import { getForeignData } from '../../grid/base/util' ; hidden export class RowModelGenerator implements IModelGenerator < Column > { protected parent : IGrid ; /**
     * Constructor for header renderer module
     */ constructor ( parent ? ) { this . parent = parent ; } public generateRows ( data , args ? ) { let rows = [ ] ; let startIndex = this . parent . enableVirtualization ? args . startIndex : 0 ; for ( let i = 0 , len = Object . keys ( data ) . length ; i < len ; i ++ , startIndex ++ ) { rows [ i ] = this . generateRow ( data [ i ] , startIndex ) ; } return rows ; } protected ensureColumns ( ) { let cols = [ ] ; if ( this . parent . detailTemplate || this . parent . childGrid ) { cols . push ( this . generateCell ( { } as Column , null , CellType . DetailExpand ) ) ; } return cols ; } protected generateRow ( data , index , cssClass ? , indent ? ) { let options = { } ; options . foreignKeyData = { } ; options . uid = getUid ( 'grid-row' ) ; options . data = data ; options . index = index ; options . indent = indent ; options . isDataRow = true ; options . isExpand = false ; options . cssClass = cssClass ; options . isAltRow = this . parent . enableAltRow ? index % 2 !== 0 : false ; options . isSelected = this . parent . getSelectedRowIndexes ( ) . indexOf ( index ) > - 1 ; this . refreshForeignKeyRow ( options ) ; let cells = this . ensureColumns ( ) ; let row = new Row < Column > ( < { [ x ] : Object ; } > options ) ; row . cells = cells . concat ( this . generateCells ( options ) ) ; return row ; } protected refreshForeignKeyRow ( options ) { this . parent . getForeignKeyColumns ( ) . forEach ( ( col ) => { setValue ( col . field , getForeignData ( col , options . data ) , options . foreignKeyData ) ; } ) ; } protected generateCells ( options ) { let dummies = this . parent . getColumns ( ) as Column [ ] ; let tmp = [ ] ; dummies . forEach ( ( dummy , index ) => tmp . push ( this . generateCell ( dummy , < string > options . uid , isNullOrUndefined ( dummy . commands ) ? undefined : CellType . CommandColumn , null , index , options . foreignKeyData ) ) ) ; return tmp ; } protected generateCell ( column , rowId ? , cellType ? , colSpan ? , oIndex ? , foreignKeyData ? ) { let opt = { 'visible' : column . visible , 'isDataCell' : ! isNullOrUndefined ( column . field || column . template ) , 'isTemplate' : ! isNullOrUndefined ( column . template ) , 'rowID' : rowId , 'column' : column , 'cellType' : ! isNullOrUndefined ( cellType ) ? cellType : CellType . Data , 'colSpan' : colSpan , 'commands' : column . commands , 'isForeignKey' : column . isForeignColumn && column . isForeignColumn ( ) , 'foreignKeyData' : column . isForeignColumn && column . isForeignColumn ( ) && getValue ( column . field , foreignKeyData ) } ; if ( opt . isDataCell || opt . column . type === 'checkbox' ) { opt . index = oIndex ; } return new Cell < Column > ( < { [ x ] : Object ; } > opt ) ; } public refreshRows ( input ? ) { input . forEach ( ( row ) => { this . refreshForeignKeyRow ( row ) ; row . cells = this . generateCells ( row ) ; } ) ; return input ; } }