UNPKG

6.02 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2018 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// *****************************************************************************
17var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
18 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
19 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
20 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
21 return c > 3 && r && Object.defineProperty(target, key, r), r;
22};
23var __metadata = (this && this.__metadata) || function (k, v) {
24 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
25};
26Object.defineProperty(exports, "__esModule", { value: true });
27exports.WidgetOpenHandler = void 0;
28const inversify_1 = require("inversify");
29const common_1 = require("../common");
30const shell_1 = require("./shell");
31const widget_manager_1 = require("./widget-manager");
32/**
33 * Generic base class for {@link OpenHandler}s that are opening a widget for a given {@link URI}.
34 */
35let WidgetOpenHandler = class WidgetOpenHandler {
36 constructor() {
37 this.onCreatedEmitter = new common_1.Emitter();
38 /**
39 * Emit when a new widget is created.
40 */
41 this.onCreated = this.onCreatedEmitter.event;
42 }
43 init() {
44 this.widgetManager.onDidCreateWidget(({ factoryId, widget }) => {
45 if (factoryId === this.id) {
46 this.onCreatedEmitter.fire(widget);
47 }
48 });
49 }
50 /**
51 * Open a widget for the given uri and options.
52 * Reject if the given options are not widget options or a widget cannot be opened.
53 * @param uri the uri of the resource that should be opened.
54 * @param options the widget opener options.
55 *
56 * @returns promise of the widget that resolves when the widget has been opened.
57 */
58 async open(uri, options) {
59 const widget = await this.getOrCreateWidget(uri, options);
60 await this.doOpen(widget, options);
61 return widget;
62 }
63 async doOpen(widget, options) {
64 const op = {
65 mode: 'activate',
66 ...options
67 };
68 if (!widget.isAttached) {
69 this.shell.addWidget(widget, op.widgetOptions || { area: 'main' });
70 }
71 if (op.mode === 'activate') {
72 await this.shell.activateWidget(widget.id);
73 }
74 else if (op.mode === 'reveal') {
75 await this.shell.revealWidget(widget.id);
76 }
77 }
78 /**
79 * Tries to get an existing widget for the given uri.
80 * @param uri the uri of the widget.
81 *
82 * @returns a promise that resolves to the existing widget or `undefined` if no widget for the given uri exists.
83 */
84 getByUri(uri) {
85 return this.getWidget(uri);
86 }
87 /**
88 * Return an existing widget for the given uri or creates a new one.
89 *
90 * It does not open a widget, use {@link WidgetOpenHandler#open} instead.
91 * @param uri uri of the widget.
92 *
93 * @returns a promise of the existing or newly created widget.
94 */
95 getOrCreateByUri(uri) {
96 return this.getOrCreateWidget(uri);
97 }
98 /**
99 * Retrieves all open widgets that have been opened by this handler.
100 *
101 * @returns all open widgets for this open handler.
102 */
103 get all() {
104 return this.widgetManager.getWidgets(this.id);
105 }
106 tryGetPendingWidget(uri, options) {
107 const factoryOptions = this.createWidgetOptions(uri, options);
108 return this.widgetManager.tryGetPendingWidget(this.id, factoryOptions);
109 }
110 getWidget(uri, options) {
111 const widgetOptions = this.createWidgetOptions(uri, options);
112 return this.widgetManager.getWidget(this.id, widgetOptions);
113 }
114 getOrCreateWidget(uri, options) {
115 const widgetOptions = this.createWidgetOptions(uri, options);
116 return this.widgetManager.getOrCreateWidget(this.id, widgetOptions);
117 }
118 /**
119 * Closes all widgets that have been opened by this open handler.
120 * @param options the close options that should be applied to all widgets.
121 *
122 * @returns a promise of all closed widgets that resolves after they have been closed.
123 */
124 async closeAll(options) {
125 return this.shell.closeMany(this.all, options);
126 }
127};
128__decorate([
129 (0, inversify_1.inject)(shell_1.ApplicationShell),
130 __metadata("design:type", shell_1.ApplicationShell)
131], WidgetOpenHandler.prototype, "shell", void 0);
132__decorate([
133 (0, inversify_1.inject)(widget_manager_1.WidgetManager),
134 __metadata("design:type", widget_manager_1.WidgetManager)
135], WidgetOpenHandler.prototype, "widgetManager", void 0);
136__decorate([
137 (0, inversify_1.postConstruct)(),
138 __metadata("design:type", Function),
139 __metadata("design:paramtypes", []),
140 __metadata("design:returntype", void 0)
141], WidgetOpenHandler.prototype, "init", null);
142WidgetOpenHandler = __decorate([
143 (0, inversify_1.injectable)()
144], WidgetOpenHandler);
145exports.WidgetOpenHandler = WidgetOpenHandler;
146//# sourceMappingURL=widget-open-handler.js.map
\No newline at end of file