import React from 'react' ; import { IndexPath } from '../../devsupport' ; import { SelectItemElement } from './selectItem.component' ; export interface SelectItemDescriptor { multiSelect : boolean ; index : IndexPath ; groupIndices ? : IndexPath [ ] ; } const SEPARATOR = ', ' ; export class SelectService { public selectItem = ( multiSelect , descriptor , selected ) : IndexPath | IndexPath [ ] => { if ( multiSelect ) { return this . createMultiSelectIndices ( descriptor , selected ) ; } return descriptor . index ; } ; public toStringSelected = ( selected ) : string => { if ( ! Array . isArray ( selected ) ) { return '' ; } const options = selected . map ( ( index ) : string => { return `Option ${ index . toString ( ) }` ; } ) ; return options . join ( SEPARATOR ) ; } ; public isSelected = ( descriptor , selected ) : boolean => { if ( descriptor . multiSelect && this . isGroup ( descriptor ) ) { return this . containsSomeFromGroup ( descriptor . index , selected ) ; } return this . contains ( descriptor . index , selected ) ; } ; public isDisabled = ( descriptor ) : boolean => { return ! descriptor . multiSelect && this . isGroup ( descriptor ) ; } ; public createDescriptorForElement = ( element , multiSelect , index ) : SelectItemDescriptor => { const groupIndices = React . Children . map ( element . props . children , ( ( child , row ) => { return new IndexPath ( row , index ) ; } ) ) ; return { multiSelect , groupIndices , index : new IndexPath ( index ) } ; } ; public createDescriptorForNestedElement = ( element , descriptor , index ) : SelectItemDescriptor => { return { ... descriptor , index : new IndexPath ( index , descriptor . index . row ) , groupIndices : [ ] , } ; } ; private createMultiSelectIndices = ( descriptor , selected ) : IndexPath [ ] => { const isIndexSelected = this . isSelected ( descriptor , selected ) ; return ! isIndexSelected ? this . addIndex ( descriptor , selected ) : this . removeIndex ( descriptor , selected ) ; } ; private isGroup = ( descriptor ) : boolean => { return descriptor . groupIndices && descriptor . groupIndices . length > 0 ; } ; private createGroupIndices = ( descriptor ) : IndexPath [ ] => { return this . isGroup ( descriptor ) ? descriptor . groupIndices : [ descriptor . index ] ; } ; private addIndex = ( descriptor , selected ) : IndexPath [ ] => { return [ ... selected , ... this . createGroupIndices ( descriptor ) ] ; } ; private removeIndex = ( descriptor , selected ) : IndexPath [ ] => { const groupIndices = this . createGroupIndices ( descriptor ) ; return selected . filter ( ( selectedIndex ) : boolean => { return ! this . contains ( selectedIndex , groupIndices ) ; } ) ; } ; private contains = ( index , selected ) : boolean => { return selected . some ( ( selectedIndex ) : boolean => { return selectedIndex . equals ( index ) ; } ) ; } ; private containsSomeFromGroup = ( index , selected ) : boolean => { return selected . some ( ( selectedIndex ) : boolean => { return selectedIndex . section === index . row ; } ) ; } ; }