import { DOWN_ARROW , ENTER , SPACE , UP_ARROW } from '@angular/cdk/keycodes' ; import { SelectionStrategy } from '../../../directives/selection/index' ; export class MultipleSelectListStrategy < T > extends SelectionStrategy < T > { private _lastSelection : T ; /** Prevent the browser from highlighting text on shift click */ mousedown ( event ) { event . preventDefault ( ) ; } click ( event , data ) { this . selectionService . activate ( data ) ; if ( event . shiftKey ) { return this . multipleSelect ( data ) ; } if ( this . selectionService . isSelected ( data ) ) { this . deselect ( data ) ; this . _lastSelection = null ; } else { this . select ( data ) ; this . _lastSelection = data ; } } keydown ( event , data ) { switch ( event . which ) { case UP_ARROW : { event . preventDefault ( ) ; const sibling = this . selectionService . activateSibling ( true ) ; if ( event . shiftKey ) { this . select ( data , sibling ) ; this . _lastSelection = sibling ; } break ; } case DOWN_ARROW : { event . preventDefault ( ) ; const sibling = this . selectionService . activateSibling ( false ) ; if ( event . shiftKey ) { this . select ( data , sibling ) ; this . _lastSelection = sibling ; } break ; } case SPACE : case ENTER : event . preventDefault ( ) ; this . toggle ( data ) ; this . _lastSelection = this . selectionService . isSelected ( data ) ? data : null ; break ; } } multipleSelect ( data ) { if ( ! this . _lastSelection ) { this . select ( data ) ; this . _lastSelection = data ; return ; } this . select ( ... this . getSelectedItems ( this . _lastSelection , data ) ) ; this . _lastSelection = data ; } private getSelectedItems ( start , end ) { const { dataset } = this . selectionService ; const startIdx = dataset . indexOf ( start ) ; const endIdx = dataset . indexOf ( end ) ; return dataset . slice ( Math . min ( startIdx , endIdx ) , Math . max ( startIdx , endIdx ) + 1 ) ; } }