UNPKG

3.25 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2019 TypeFox and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.SelectionCommandHandler = void 0;
19class SelectionCommandHandler {
20 constructor(selectionService, toSelection, options) {
21 this.selectionService = selectionService;
22 this.toSelection = toSelection;
23 this.options = options;
24 }
25 execute(...args) {
26 const selection = this.getSelection(...args);
27 return selection ? this.options.execute(selection, ...args) : undefined;
28 }
29 isVisible(...args) {
30 const selection = this.getSelection(...args);
31 return !!selection && (!this.options.isVisible || this.options.isVisible(selection, ...args));
32 }
33 isEnabled(...args) {
34 const selection = this.getSelection(...args);
35 return !!selection && (!this.options.isEnabled || this.options.isEnabled(selection, ...args));
36 }
37 isMulti() {
38 return this.options && !!this.options.multi;
39 }
40 getSelection(...args) {
41 const givenSelection = args.length && this.toSelection(args[0]);
42 if (givenSelection) {
43 return this.isMulti() ? [givenSelection] : givenSelection;
44 }
45 const globalSelection = this.getSingleSelection(this.selectionService.selection);
46 if (this.isMulti()) {
47 return this.getMultiSelection(globalSelection);
48 }
49 return this.getSingleSelection(globalSelection);
50 }
51 getSingleSelection(arg) {
52 let selection = this.toSelection(arg);
53 if (selection) {
54 return selection;
55 }
56 if (Array.isArray(arg)) {
57 for (const element of arg) {
58 selection = this.toSelection(element);
59 if (selection) {
60 return selection;
61 }
62 }
63 }
64 return undefined;
65 }
66 getMultiSelection(arg) {
67 let selection = this.toSelection(arg);
68 if (selection) {
69 return [selection];
70 }
71 const result = [];
72 if (Array.isArray(arg)) {
73 for (const element of arg) {
74 selection = this.toSelection(element);
75 if (selection) {
76 result.push(selection);
77 }
78 }
79 }
80 return result.length ? result : undefined;
81 }
82}
83exports.SelectionCommandHandler = SelectionCommandHandler;
84//# sourceMappingURL=selection-command-handler.js.map
\No newline at end of file