import { merge , isNullOrUndefined , extend } from '@syncfusion/ej2-base' ; import { NumberFormatOptions , DateFormatOptions } from '@syncfusion/ej2-base' ; import { DataManager , Query , DataUtil } from '@syncfusion/ej2-data' ; import { ICellFormatter , IFilterUI , IEditCell , CommandModel , IFilter } from '../base/interface' ; import { TextAlign , ClipMode } from '../base/enum' ; import { ValueFormatter } from '../services/value-formatter' ; import { ValueAccessor , SortComparer } from '../base/type' ; import { getUid , templateCompiler , getForeignData , getObject } from '../base/util' ; /**
 * Represents Grid `Column` model class.
 */ export class Column { default public field : string ; default public uid : string ; default public headerText : string ; default public width : string | number ; default public minWidth : string | number ; default public maxWidth : string | number ; default public textAlign : TextAlign ; default public clipMode : ClipMode ; default public headerTextAlign : TextAlign ; default public disableHtmlEncode : boolean ; default public type : string ; default aspType public format : string | NumberFormatOptions | DateFormatOptions ; default public visible : boolean ; default public template : string ; default public headerTemplate : string ; default public isFrozen : boolean ; default public allowSorting : boolean = true ; default public allowResizing : boolean = true ; default public allowFiltering : boolean = true ; default public allowGrouping : boolean = true ; default public allowReordering : boolean = true ; default public showColumnMenu : boolean = true ; default public enableGroupByFormat : boolean = false ; default public allowEditing : boolean = true ; default public customAttributes : { [ x ] : Object ; } ; default public displayAsCheckBox : boolean ; default public dataSource : Object [ ] | DataManager ; default public formatter : { new ( ) : ICellFormatter ; } | ICellFormatter | Function ; default public valueAccessor : ValueAccessor | string ; default public filterBarTemplate : IFilterUI ; default public filter : IFilter = { } ; default public columns : Column [ ] | string [ ] | ColumnModel [ ] ; default hidden public toolTip : string ; default public isPrimaryKey : boolean ; default public hideAtMedia ? : string ; default public showInColumnChooser ? : boolean = true ; default public editType : string ; default public validationRules : Object ; default public defaultValue : string ; default public edit : IEditCell = { } ; default public isIdentity : boolean ; default public foreignKeyValue : string ; default public foreignKeyField : string ; hidden public commandsTemplate : string ; default public commands : CommandModel [ ] ; hidden default public columnData : Object [ ] ; default aspIgnore public editTemplate : string ; default aspIgnore public filterTemplate : string ; constructor ( options ) { merge ( this , options ) ; this . uid = getUid ( 'grid-column' ) ; let valueFormatter = new ValueFormatter ( ) ; if ( options . format && ( ( < DateFormatOptions > options . format ) . skeleton || ( < DateFormatOptions > options . format ) . format ) ) { this . setFormatter ( valueFormatter . getFormatFunction ( options . format as DateFormatOptions ) ) ; this . setParser ( valueFormatter . getParserFunction ( options . format as DateFormatOptions ) ) ; } if ( ! this . field ) { this . allowFiltering = false ; this . allowGrouping = false ; this . allowSorting = false ; } if ( this . commands && ! this . textAlign ) { this . textAlign = 'Right' ; } if ( this . template || this . commandsTemplate ) { this . templateFn = templateCompiler ( this . template || this . commandsTemplate ) ; } if ( this . headerTemplate ) { this . headerTemplateFn = templateCompiler ( this . headerTemplate ) ; } if ( this . filter . itemTemplate ) { this . fltrTemplateFn = templateCompiler ( this . filter . itemTemplate ) ; } if ( this . editTemplate ) { this . editTemplateFn = templateCompiler ( this . editTemplate ) ; } if ( this . filterTemplate ) { this . filterTemplateFn = templateCompiler ( this . filterTemplate ) ; } if ( this . isForeignColumn ( ) && ( isNullOrUndefined ( this . editType ) || this . editType === 'dropdownedit' ) ) { this . editType = 'dropdownedit' ; this . edit . params = extend ( { dataSource : < DataManager > this . dataSource , query : new Query ( ) , fields : { value : this . foreignKeyField || this . field , text : this . foreignKeyValue } } , this . edit . params ) ; } if ( this . sortComparer ) { let a = this . sortComparer as Function ; this . sortComparer = function comparer ( x , y ) : number { if ( typeof a === 'string' ) { a = getObject ( a , window ) ; } if ( this . sortDirection === 'Descending' ) { let z = x ; x = y ; y = z ; } return a ( x , y ) ; } ; } if ( ! this . sortComparer && this . isForeignColumn ( ) ) { this . sortComparer = ( x , y ) => { x = getObject ( this . foreignKeyValue , getForeignData ( this , { } , < string > x ) [ 0 ] ) ; y = getObject ( this . foreignKeyValue , getForeignData ( this , { } , < string > y ) [ 0 ] ) ; return this . sortDirection === 'Descending' ? DataUtil . fnDescending ( x , y ) : DataUtil . fnAscending ( x , y ) ; } ; } } private formatFn : Function ; private parserFn : Function ; private templateFn : Function ; private fltrTemplateFn : Function ; private headerTemplateFn : Function ; private editTemplateFn : Function ; private filterTemplateFn : Function ; private sortDirection : string = 'Descending' ; hidden public getEditTemplate : Function = ( ) => this . editTemplateFn ; hidden public getFilterTemplate : Function = ( ) => this . filterTemplateFn ; hidden public getSortDirection ( ) { return this . sortDirection ; } hidden public setSortDirection ( direction ) { this . sortDirection = direction ; } hidden public setProperties ( column ) { let keys = Object . keys ( column ) ; for ( let i = 0 ; i < keys . length ; i ++ ) { this [ keys [ i ] ] = column [ keys [ i ] ] ; } } /**
     * Defines the custom sort comparer function.
     * The sort comparer function has the same functionality like
     * [`Array.sort`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) sort comparer.
     * {% codeBlock src="grid/sort-comparer-api/index.ts" %}{% endcodeBlock %}
     */ public sortComparer : SortComparer | string ; hidden public isForeignColumn ( ) { return ! ! ( this . dataSource && this . foreignKeyValue ) ; } hidden public getFormatter ( ) { return this . formatFn ; } hidden public setFormatter ( value ) { this . formatFn = value ; } hidden public getParser ( ) { return this . parserFn ; } hidden public setParser ( value ) { this . parserFn = value ; } hidden public getColumnTemplate ( ) { return this . templateFn ; } hidden public getHeaderTemplate ( ) { return this . headerTemplateFn ; } hidden public getFilterItemTemplate ( ) { return this . fltrTemplateFn ; } hidden public getDomSetter ( ) { return this . disableHtmlEncode ? 'textContent' : 'innerHTML' ; } } /**
 * Interface for a class Column
 */ export interface ColumnModel { default field ? : string ; default uid ? : string ; default headerText ? : string ; default width ? : string | number ; default minWidth ? : string | number ; default maxWidth ? : string | number ; default textAlign ? : TextAlign ; default clipMode ? : ClipMode ; aspdefaultvalueignore default headerTextAlign ? : TextAlign ; default disableHtmlEncode ? : boolean ; default type ? : string ; default aspType format ? : string | NumberFormatOptions | DateFormatOptions ; default visible ? : boolean ; default template ? : string ; default headerTemplate ? : string ; default isFrozen ? : boolean ; default allowSorting ? : boolean ; default allowResizing ? : boolean ; default showColumnMenu ? : boolean ; default allowFiltering ? : boolean ; default allowGrouping ? : boolean ; default allowReordering ? : boolean ; default enableGroupByFormat ? : boolean ; default allowEditing ? : boolean ; default customAttributes ? : { [ x ] : Object ; } ; default displayAsCheckBox ? : boolean ; default dataSource ? : Object [ ] | DataManager ; default formatter ? : { new ( ) : ICellFormatter ; } | ICellFormatter | Function ; default valueAccessor ? : ValueAccessor | string ; default filterBarTemplate ? : IFilterUI ; default filter ? : IFilter ; default columns ? : Column [ ] | string [ ] | ColumnModel [ ] ; hidden default toolTip ? : string ; default isPrimaryKey ? : boolean ; default editType ? : string ; default validationRules ? : Object ; default defaultValue ? : string ; default edit ? : IEditCell ; default isIdentity ? : boolean ; default foreignKeyField ? : string ; default foreignKeyValue ? : string ; default hideAtMedia ? : string ; default showInColumnChooser ? : boolean ; hidden commandsTemplate ? : string ; default commands ? : CommandModel [ ] ; /**
     * It defines the custom sort comparer function.
     */ sortComparer ? : SortComparer | string ; hidden isForeignColumn ? : ( ) => boolean ; aspIgnore editTemplate ? : string ; aspIgnore filterTemplate ? : string ; }