/********************************************************************************
 * Copyright (C) 2019 TypeFox and others.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * This Source Code may also be made available under the following Secondary
 * Licenses when the conditions for such availability set forth in the Eclipse
 * Public License v. 2.0 are satisfied: GNU General Public License, version 2
 * with the GNU Classpath Exception which is available at
 * https://www.gnu.org/software/classpath/license.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
 ********************************************************************************/ import { CommandHandler } from './command' ; import { SelectionService } from '../common/selection-service' ; export class SelectionCommandHandler < S > implements CommandHandler { constructor ( protected readonly selectionService , protected readonly toSelection , protected readonly options ) { } execute ( ... args ) { const selection = this . getSelection ( ... args ) ; return selection ? ( this . options . execute as any ) ( selection , ... args ) : undefined ; } isVisible ( ... args ) { const selection = this . getSelection ( ... args ) ; return ! ! selection && ( ! this . options . isVisible || ( this . options . isVisible as any ) ( selection as any , ... args ) ) ; } isEnabled ( ... args ) { const selection = this . getSelection ( ... args ) ; return ! ! selection && ( ! this . options . isEnabled || ( this . options . isEnabled as any ) ( selection as any , ... args ) ) ; } protected isMulti ( ) { return this . options && ! ! this . options . multi ; } protected getSelection ( ... args ) { const givenSelection = args . length && this . toSelection ( args [ 0 ] ) ; if ( givenSelection ) { return this . isMulti ( ) ? [ givenSelection ] : givenSelection ; } const globalSelection = this . getSingleSelection ( this . selectionService . selection ) ; if ( this . isMulti ( ) ) { return this . getMultiSelection ( globalSelection ) ; } return this . getSingleSelection ( globalSelection ) ; } protected getSingleSelection ( arg ) { let selection = this . toSelection ( arg ) ; if ( selection ) { return selection ; } if ( Array . isArray ( arg ) ) { for ( const element of arg ) { selection = this . toSelection ( element ) ; if ( selection ) { return selection ; } } } return undefined ; } protected getMultiSelection ( arg ) { let selection = this . toSelection ( arg ) ; if ( selection ) { return [ selection ] ; } const result = [ ] ; if ( Array . isArray ( arg ) ) { for ( const element of arg ) { selection = this . toSelection ( element ) ; if ( selection ) { result . push ( selection ) ; } } } return result . length ? result : undefined ; } } export namespace SelectionCommandHandler { export type Options < S > = SelectionOptions < false , S > | SelectionOptions < true , S [ ] > ; export interface SelectionOptions < Multi extends boolean , T > { multi : Multi ; execute ( selection , ... args ) : any ; isEnabled ? ( selection , ... args ) : boolean ; isVisible ? ( selection , ... args ) : boolean ; } }