module Sfx { export class ListSettings { public search : string ; public sortPropertyPaths : string [ ] = [ ] ; public sortReverse : boolean = false ; private _currentPage : number = 1 ; private _itemCount : number = 0 ; public get count ( ) : number { return this . _itemCount ; } public set count ( itemCount ) { this . _itemCount = itemCount ; if ( this . currentPage > this . pageCount ) { this . currentPage = this . pageCount ; } } public get currentPage ( ) : number { return this . _currentPage ; } public get hasEnabledFilters ( ) : boolean { return _ . some ( this . columnSettings , cs => cs . enableFilter ) ; } public set currentPage ( page ) { if ( page < 1 ) { this . _currentPage = 1 ; } else if ( page > this . pageCount ) { if ( this . pageCount > 0 ) { this . _currentPage = this . pageCount ; } else { this . _currentPage = 1 ; } } else { this . _currentPage = page ; } } public get begin ( ) : number { return ( this . currentPage - 1 ) * this . limit ; } public get pageCount ( ) : number { return Math . ceil ( this . count / this . limit ) ; } public setPageWithIndex ( index ) { this . currentPage = Math . floor ( index / this . limit ) + 1 ; } param limit param defaultSortPropertyPaths param columnSettings param secondRowColumnSettings param secondRowCollapsible public constructor ( public limit , public defaultSortPropertyPaths , public columnSettings , public secondRowColumnSettings = [ ] , public secondRowCollapsible = false , public showSecondRow = ( item ) => true , public searchable = true ) { this . sortPropertyPaths = defaultSortPropertyPaths ; } public sort ( sortPropertyPaths ) { this . sortPropertyPaths = sortPropertyPaths ; this . sortReverse = ! this . sortReverse ; } public isSortedByColumn ( columnSetting ) { return _ . isEqual ( this . sortPropertyPaths , columnSetting . sortPropertyPaths ) ; } public reset ( ) { _ . forEach ( this . columnSettings , cs => cs . reset ( ) ) ; this . search = null ; this . sortPropertyPaths = this . defaultSortPropertyPaths ; this . sortReverse = false ; this . currentPage = 1 ; } public getPluckedObject ( item ) { if ( ! _ . isEmpty ( this . columnSettings ) ) { let newObj = { } ; _ . forEach ( _ . union ( this . columnSettings , this . secondRowColumnSettings ) , column => newObj [ column . propertyPath ] = column . getTextValue ( item ) ) ; return newObj ; } return item ; } public filterContextMenuToggled ( open ) { if ( open ) { HtmlUtils . repositionContextMenu ( ) ; } } } export class FilterValue { public isChecked : boolean = true ; public constructor ( public value ) { } } export class ListColumnSetting { public filterValues : FilterValue [ ] = [ ] ; public fixedWidthPx ? : number ; public get hasFilters ( ) : boolean { return this . enableFilter && this . filterValues . length > 0 ; } public get hasEffectiveFilters ( ) : boolean { return _ . some ( this . filterValues , filter => ! filter . isChecked ) ; } public get sortable ( ) : boolean { return ! _ . isEmpty ( this . sortPropertyPaths ) ; } param propertyPath param displayName param sortPropertyPaths param enableFilter param getDisplayHtml param colspan param clickEvent public constructor ( public propertyPath , public displayName , public sortPropertyPaths = [ propertyPath ] , public enableFilter ? , public getDisplayHtml ? , public colspan = 1 , public clickEvent = ( item ) => null ) { } public reset ( ) { _ . forEach ( this . filterValues , filter => filter . isChecked = true ) ; } public getProperty ( item ) { if ( this . propertyPath && _ . startsWith ( this . propertyPath , "#" ) ) { return this . propertyPath . substr ( 1 ) ; } return Utils . result ( item , this . propertyPath ) ; } public isBadge ( item ) { return Utils . isBadge ( item ) ; } public getTextValue ( item ) { let property = this . getProperty ( item ) ; if ( _ . isUndefined ( property ) || _ . isNull ( property ) ) { return "" ; } return property . toString ( ) ; } public getDisplayContentsInHtml ( item ) { let property = this . getProperty ( item ) ; if ( this . getDisplayHtml ) { return this . getDisplayHtml ( item , property ) ; } if ( _ . isUndefined ( property ) || _ . isNull ( property ) ) { return "" ; } return property . toString ( ) ; } } export class ListColumnSettingForBadge extends ListColumnSetting { public constructor ( propertyPath , displayName , sortPropertyPaths = [ propertyPath + ".text" ] ) { super ( propertyPath , displayName , sortPropertyPaths , true , ( item , property ) => HtmlUtils . getBadgeHtml ( property ) ) ; } public getTextValue ( item ) { let property = this . getProperty ( item ) ; if ( property ) { return property . text ; } return "" ; } } export class ListColumnSettingWithFilter extends ListColumnSetting { public constructor ( propertyPath , displayName , sortPropertyPaths = [ propertyPath ] ) { super ( propertyPath , displayName , sortPropertyPaths , true ) ; } } export class ListColumnSettingForLink extends ListColumnSetting { public constructor ( propertyPath , displayName , href ) { super ( propertyPath , displayName , [ propertyPath ] , false , ( item , property ) => HtmlUtils . getLinkHtml ( property , href ( item ) ) ) ; } } }