UNPKG

4.15 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2018 Ericsson 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 WITH Classpath-exception-2.0
16// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.UriAwareCommandHandler = void 0;
19const selection_1 = require("../common/selection");
20const uri_1 = require("./uri");
21class UriAwareCommandHandler {
22 /**
23 * @deprecated since 1.6.0. Please use `UriAwareCommandHandler.MonoSelect` or `UriAwareCommandHandler.MultiSelect`.
24 */
25 constructor(selectionService, handler, options) {
26 this.selectionService = selectionService;
27 this.handler = handler;
28 this.options = options;
29 }
30 getUri(...args) {
31 const [maybeUriArray] = args;
32 const firstArgIsOK = this.isMulti()
33 ? Array.isArray(maybeUriArray) && maybeUriArray.every(uri => uri instanceof uri_1.default)
34 : maybeUriArray instanceof uri_1.default;
35 if (firstArgIsOK) {
36 return maybeUriArray;
37 }
38 const { selection } = this.selectionService;
39 const uriOrUris = this.isMulti()
40 ? selection_1.UriSelection.getUris(selection)
41 : selection_1.UriSelection.getUri(selection);
42 return uriOrUris;
43 }
44 getArgsWithUri(...args) {
45 const uri = this.getUri(...args);
46 const [maybeUri, ...others] = args;
47 if (uri === maybeUri) {
48 return [maybeUri, ...others];
49 }
50 return [uri, ...args];
51 }
52 execute(...args) {
53 const [uri, ...others] = this.getArgsWithUri(...args);
54 return uri ? this.handler.execute(uri, ...others) : undefined;
55 }
56 isVisible(...args) {
57 const [uri, ...others] = this.getArgsWithUri(...args);
58 if (uri) {
59 if (this.handler.isVisible) {
60 return this.handler.isVisible(uri, ...others);
61 }
62 return true;
63 }
64 return false;
65 }
66 isEnabled(...args) {
67 const [uri, ...others] = this.getArgsWithUri(...args);
68 if (uri) {
69 if (this.handler.isEnabled) {
70 return this.handler.isEnabled(uri, ...others);
71 }
72 return true;
73 }
74 return false;
75 }
76 isMulti() {
77 return this.options && !!this.options.multi;
78 }
79}
80exports.UriAwareCommandHandler = UriAwareCommandHandler;
81(function (UriAwareCommandHandler) {
82 /**
83 * @returns a command handler for mono-select contexts that expects a `URI` as the first parameter of its methods.
84 */
85 function MonoSelect(selectionService, handler) {
86 /* eslint-disable-next-line deprecation/deprecation*/ // Safe to use when the generic and the options agree.
87 return new UriAwareCommandHandler(selectionService, handler, { multi: false });
88 }
89 UriAwareCommandHandler.MonoSelect = MonoSelect;
90 /**
91 * @returns a command handler for multi-select contexts that expects a `URI[]` as the first parameter of its methods.
92 */
93 function MultiSelect(selectionService, handler) {
94 /* eslint-disable-next-line deprecation/deprecation*/ // Safe to use when the generic and the options agree.
95 return new UriAwareCommandHandler(selectionService, handler, { multi: true });
96 }
97 UriAwareCommandHandler.MultiSelect = MultiSelect;
98})(UriAwareCommandHandler = exports.UriAwareCommandHandler || (exports.UriAwareCommandHandler = {}));
99//# sourceMappingURL=uri-command-handler.js.map
\No newline at end of file