import { IColumnDesc , Ranking } from '../model' ; import ADataProvider from './ADataProvider' ; import type { IDataProviderDump , IDataProviderOptions } from './interfaces' ; import { isComplexAccessor , rowGetter , rowComplexGetter , rowGuessGetter } from '../internal' ; function injectAccessor ( d ) { d . accessor = d . accessor || ( d . column ? ( isComplexAccessor ( d . column ) ? rowComplexGetter : rowGetter ) : rowGuessGetter ) ; d . label = d . label || d . column ; return d ; } /**
 * common base implementation of a DataProvider with a fixed list of column descriptions
 */ abstract class ACommonDataProvider extends ADataProvider { private rankingIndex = 0 ; constructor ( private columns = [ ] , options = { } ) { super ( options ) ; columns . forEach ( injectAccessor ) ; } cloneRanking ( existing ? ) { const id = this . nextRankingId ( ) ; const clone = new Ranking ( id ) ; if ( existing ) { existing . children . forEach ( ( child ) => { this . push ( clone , child . desc ) ; } ) ; } return clone ; } param column pushDesc ( column ) { injectAccessor ( column ) ; this . columns . push ( column ) ; this . fire ( ADataProvider . EVENT_ADD_DESC , column ) ; } clearColumns ( ) { this . clearRankings ( ) ; this . columns . splice ( 0 , this . columns . length ) ; this . fire ( ADataProvider . EVENT_CLEAR_DESC ) ; } param column param ignoreBeingUsed removeDesc ( column , ignoreBeingUsed = false ) { const i = this . columns . indexOf ( column ) ; if ( i < 0 ) { return false ; } const isUsed = ignoreBeingUsed ? false : this . getRankings ( ) . some ( ( d ) => d . flatColumns . some ( ( c ) => c . desc === column ) ) ; if ( isUsed ) { return false ; } this . columns . splice ( i , 1 ) ; this . fire ( ADataProvider . EVENT_REMOVE_DESC , column ) ; return true ; } getColumns ( ) { return this . columns . slice ( ) ; } findDesc ( ref ) { return this . columns . filter ( ( c ) => ( c as any ) . column === ref ) [ 0 ] ; } param desc returns { string } toDescRef ( desc ) { return typeof desc . column !== 'undefined' ? `${ desc . type }@${ desc . column }` : this . cleanDesc ( Object . assign ( { } , desc ) ) ; } fromDescRef ( descRef ) { if ( typeof descRef === 'string' ) { return this . columns . find ( ( d ) => `${ d . type }@${ d . column }` === descRef || d . type === descRef ) ; } const existing = this . columns . find ( ( d ) => descRef . column === ( d as any ) . column && descRef . label === d . label && descRef . type === d . type ) ; if ( existing ) { return existing ; } return descRef ; } restore ( dump ) { super . restore ( dump ) ; this . rankingIndex = 1 + Math . max ( ... this . getRankings ( ) . map ( ( r ) => + r . id . substring ( 4 ) ) ) ; } nextRankingId ( ) { return `rank${ this . rankingIndex ++ }` ; } } export default ACommonDataProvider ;