import type { IColumnDesc , IColumnConstructor } from '../model' ; import { DataProvider , LocalDataProvider , deriveColors , deriveColumnDescriptions , IDataProviderOptions , ILocalDataProviderOptions , IAggregationStrategy , } from '../provider' ; import { LineUp , Taggle } from '../ui' ; import ColumnBuilder from './column/ColumnBuilder' ; import LineUpBuilder from './LineUpBuilder' ; import { RankingBuilder } from './RankingBuilder' ; export * from './column' ; export * from './RankingBuilder' ; /**
 * builder for a LocalDataProvider along with LineUp configuration options
 */ export class DataBuilder extends LineUpBuilder { private readonly columns : ( IColumnDesc | ( ( data : any [ ] ) => IColumnDesc ) ) [ ] = [ ] ; private readonly providerOptions : Partial < ILocalDataProviderOptions & IDataProviderOptions > = { columnTypes : { } , } ; private readonly rankBuilders : ( ( data : DataProvider ) => void ) [ ] = [ ] ; private _deriveColors = false ; constructor ( private readonly data ) { super ( ) ; } /**
     * use the schedulded task executor to asynchronously compute aggregations
     */ scheduledTaskExecutor ( ) { this . providerOptions . taskExecutor = 'scheduled' ; return this ; } /**
     * when using a top-n strategy how many items should be shown
     */ showTopN ( n ) { this . providerOptions . showTopN = n ; return this ; } /**
     * change the aggregation strategy that should be used when grouping by a column
     */ aggregationStrategy ( strategy ) { this . providerOptions . aggregationStrategy = strategy ; return this ; } default propagateAggregationState ( value ) { this . providerOptions . propagateAggregationState = value ; return this ; } /**
     * allow just a single selection
     */ singleSelection ( ) { this . providerOptions . singleSelection = true ; return this ; } /**
     * allow multiple selections
     */ multiSelection ( ) { this . providerOptions . singleSelection = false ; return this ; } /**
     * filter all rankings by all filters in LineUp
     */ filterGlobally ( ) { this . providerOptions . filterGlobally = true ; return this ; } param { string } columns deriveColumns ( ... columns ) { const cols = [ ] . concat ( ... columns ) ; for ( const c of deriveColumnDescriptions ( this . data , { columns : cols } ) ) { this . columns . push ( c ) ; } return this ; } /**
     * tirggers to assign colors for the given descriptions
     */ deriveColors ( ) { this . _deriveColors = true ; return this ; } param { string } type param { IColumnConstructor } clazz registerColumnType ( type , clazz ) { this . providerOptions . columnTypes ! [ type ] = clazz ; return this ; } param { IColumnDesc | ColumnBuilder } column column ( column ) { this . columns . push ( column instanceof ColumnBuilder ? column . build . bind ( column ) : column ) ; return this ; } param dump restore ( dump ) { this . rankBuilders . push ( ( data ) => data . restore ( dump ) ) ; return this ; } param { boolean } addSupportTypes defaultRanking ( addSupportTypes = true ) { this . rankBuilders . push ( ( data ) => data . deriveDefault ( addSupportTypes ) ) ; return this ; } param { ( ( data : DataProvider ) => void ) | RankingBuilder } builder ranking ( builder ) { this . rankBuilders . push ( builder instanceof RankingBuilder ? builder . build . bind ( builder ) : builder ) ; return this ; } returns { LocalDataProvider } buildData ( ) { const columns = [ ] ; const contained = new Set < string > ( ) ; for ( const col of this . columns ) { const c = typeof col === 'function' ? col ( this . data ) : col ; const key = `${ c . type }@${ c . label }` ; if ( ! contained . has ( key ) ) { columns . push ( c ) ; contained . add ( key ) ; continue ; } const oldPos = columns . findIndex ( ( d ) => key === `${ d . type }@${ d . label }` ) ; columns . splice ( oldPos , 1 , c ) ; } if ( this . _deriveColors ) { deriveColors ( columns ) ; } const r = new LocalDataProvider ( this . data , columns , this . providerOptions ) ; if ( this . rankBuilders . length === 0 ) { this . defaultRanking ( ) ; } this . rankBuilders . forEach ( ( builder ) => builder ( r ) ) ; return r ; } param { HTMLElement } node returns { LineUp } build ( node ) { return new LineUp ( node , this . buildData ( ) , this . options ) ; } param { HTMLElement } node returns { Taggle } buildTaggle ( node ) { return new Taggle ( node , this . buildData ( ) , this . options ) ; } } param { Record < string , unknown > [ ] } arr returns { DataBuilder } export function builder ( arr ) { return new DataBuilder ( arr ) ; } param { HTMLElement } node param { any [ ] } data param { string [ ] } columns returns { Taggle } export function asTaggle ( node , data , ... columns ) { return builder ( data ) . deriveColumns ( columns ) . deriveColors ( ) . defaultRanking ( ) . buildTaggle ( node ) ; } param { HTMLElement } node param { any [ ] } data param { string [ ] } columns returns { LineUp } export function asLineUp ( node , data , ... columns ) { return builder ( data ) . deriveColumns ( columns ) . deriveColors ( ) . defaultRanking ( ) . build ( node ) ; }