UNPKG

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