import { Category , SupportType , toolbar } from './annotations' ; import { IndicesArray , IDataRow , IGroup , ECompareValueType , IValueColumnDesc , ITypeFactory } from './interfaces' ; import type { widthChanged , labelChanged , metaDataChanged , dirty , dirtyHeader , dirtyValues , rendererTypeChanged , groupRendererChanged , summaryRendererChanged , visibilityChanged , dirtyCaches , } from './Column' ; import Column from './Column' ; import type { dataLoaded } from './ValueColumn' ; import ValueColumn from './ValueColumn' ; import type { IEventListener } from '../internal' ; import { integrateDefaults } from './internal' ; param label returns { { type : string , label : string } } export function createSelectionDesc ( label = 'Selections' ) { return { type : 'selection' , label , fixed : true } ; } export interface ISelectionColumnDesc extends IValueColumnDesc < boolean > { /**
     * setter for selecting/deselecting the given row
     */ setter ( index , value ) : void ; /**
     * setter for selecting/deselecting the given row
     */ setterAll ( indices , value ) : void ; } asMemberOf param dataIndex param value param dataIndices event export declare function select_SEC ( dataIndex , value , dataIndices ? ) ; asMemberOf event export declare function filterChanged_SEC ( previous , current ) ; /**
 * a checkbox column for selections
 */ @ SupportType ( ) @ toolbar ( 'sort' , 'sortBy' , 'group' , 'groupBy' , 'invertSelection' , 'filterSelection' ) @ Category ( 'support' ) export default class SelectionColumn extends ValueColumn < boolean > { static readonly EVENT_FILTER_CHANGED = 'filterChanged' ; static readonly EVENT_SELECT = 'select' ; private static SELECTED_GROUP : IGroup = { name : 'Selected' , color : 'orange' , } ; private static NOT_SELECTED_GROUP : IGroup = { name : 'Unselected' , color : 'gray' , } ; private currentFilter : Set < number > | null = null ; constructor ( id , desc ) { super ( id , integrateDefaults ( desc , { width : 50 , } ) ) ; } get frozen ( ) { return this . desc . frozen !== false ; } protected createEventList ( ) { return super . createEventList ( ) . concat ( [ SelectionColumn . EVENT_SELECT , SelectionColumn . EVENT_FILTER_CHANGED ] ) ; } on ( type , listener ) ; on ( type , listener ) ; on ( type , listener ) ; on ( type , listener ) ; on ( type , listener ) ; on ( type , listener ) ; on ( type , listener ) ; on ( type , listener ) ; on ( type , listener ) ; on ( type , listener ) ; on ( type , listener ) ; on ( type , listener ) ; on ( type , listener ) ; on ( type , listener ) ; on ( type , listener ) ; on ( type , listener ) { return super . on ( type as any , listener ) ; } setValue ( row , value ) { const old = this . getValue ( row ) ; if ( old === value ) { return true ; } return this . setImpl ( row , value ) ; } setValues ( rows , value ) { if ( rows . length === 0 ) { return false ; } if ( ( this . desc as ISelectionColumnDesc ) . setterAll ) { ( this . desc as ISelectionColumnDesc ) . setterAll ( rows , value ) ; } this . fire ( SelectionColumn . EVENT_SELECT , rows [ 0 ] , value , rows ) ; return true ; } private setImpl ( row , value ) { if ( ( this . desc as ISelectionColumnDesc ) . setter ) { ( this . desc as ISelectionColumnDesc ) . setter ( row . i , value ) ; } this . fire ( SelectionColumn . EVENT_SELECT , row . i , value ) ; return true ; } toggleValue ( row ) { const old = this . getValue ( row ) ; this . setImpl ( row , ! old ) ; return ! old ; } toCompareValue ( row ) { const v = this . getValue ( row ) === true ; return v ? 1 : 0 ; } toCompareValueType ( ) { return ECompareValueType . BINARY ; } group ( row ) { const isSelected = this . getValue ( row ) ; return Object . assign ( { } , isSelected ? SelectionColumn . SELECTED_GROUP : SelectionColumn . NOT_SELECTED_GROUP ) ; } dump ( toDescRef ) { const r = super . dump ( toDescRef ) ; r . filter = this . currentFilter ? Array . from ( this . currentFilter ) . sort ( ( a , b ) => a - b ) : null ; return r ; } restore ( dump , factory ) { super . restore ( dump , factory ) ; if ( dump . filter ) { const filter = dump . filter ; this . currentFilter = new Set ( filter ) ; } else { this . currentFilter = null ; } } isFiltered ( ) { return this . currentFilter != null ; } filter ( row ) { if ( ! this . isFiltered ( ) ) { return true ; } const filter = this . currentFilter ! ; return filter . has ( row . i ) ; } getFilter ( ) { return this . currentFilter == null ? null : Array . from ( this . currentFilter ) ; } setFilter ( filter ) { const newValue = filter ? new Set ( filter ) : null ; if ( areSameSets ( newValue , this . currentFilter ) ) { return ; } this . fire ( [ SelectionColumn . EVENT_FILTER_CHANGED , Column . EVENT_DIRTY_VALUES , Column . EVENT_DIRTY ] , this . currentFilter , ( this . currentFilter = newValue ) ) ; } clearFilter ( ) { const was = this . isFiltered ( ) ; this . setFilter ( null ) ; return was ; } } function areSameSets ( a , b ) { const aL = a != null ? a . size : 0 ; const bL = b != null ? b . size : 0 ; if ( aL !== bL ) { return false ; } if ( aL === 0 || bL === 0 ) { return true ; } return Array . from ( a ) . every ( ( d ) => b . has ( d ) ) ; }