import { types , getParent , SnapshotIn } from 'mobx-state-tree' ; import { AnyFilterModelType as AnyColumnFilter } from './ColumnDataTypes' ; const RowFullTextFilter = types . model ( 'RowFullTextFilter' , { type : types . literal ( 'RowFullText' ) , stringToFind : '' , } ) . views ( self => ( { get predicate ( ) { let s = self . stringToFind ; if ( ! s ) { return function alwaysTrue ( ) { return true ; } ; } s = s . toLowerCase ( ) ; return function stringPredicate ( _sheet , row ) { const { cellsWithDerived } = row ; for ( let columnNumber = 0 ; columnNumber < cellsWithDerived . length ; columnNumber += 1 ) { const cell = cellsWithDerived [ columnNumber ] ; if ( cell . text && cell . text . toLowerCase ( ) . includes ( s ) ) { return true ; } } return false ; } ; } , } ) ) . actions ( self => ( { setString ( s ) { self . stringToFind = s ; } , clear ( ) { self . stringToFind = '' ; } , } ) ) ; const model = types . model ( 'SpreadsheetFilterControls' , { rowFullText : types . optional ( RowFullTextFilter , ( ) => ( { type : 'RowFullText' , stringToFind : '' , } as SnapshotIn < typeof RowFullTextFilter > ) ) , columnFilters : types . array ( AnyColumnFilter ) , } ) . views ( self => ( { get filters ( ) { return [ self . rowFullText , ... self . columnFilters ] . filter ( f => ! ! f ) ; } , setRowFullTextFilter ( stringToFind ) { self . rowFullText = { type : 'RowFullText' , stringToFind , } ; } , rowPassesFilters ( sheet , row ) { for ( let i = 0 ; i < this . filters . length ; i += 1 ) { if ( ! this . filters [ i ] . predicate ( sheet , row ) ) { return false ; } } return true ; } , } ) ) . actions ( self => ( { addBlankColumnFilter ( columnNumber ) { const { dataType } = getParent ( self ) . spreadsheet . columns [ columnNumber ] ; self . columnFilters . push ( { type : dataType . type , columnNumber , } ) ; } , removeColumnFilter ( filter ) { return self . columnFilters . remove ( filter ) ; } , clearAllFilters ( ) { self . columnFilters . clear ( ) ; self . rowFullText . clear ( ) ; } , } ) ) ; export default model ;