UNPKG

4.99 kBJavaScriptView Raw
1"use strict";
2var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5 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;
6 return c > 3 && r && Object.defineProperty(target, key, r), r;
7};
8var __metadata = (this && this.__metadata) || function (k, v) {
9 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10};
11var DefaultSecondaryWindowService_1;
12Object.defineProperty(exports, "__esModule", { value: true });
13exports.DefaultSecondaryWindowService = void 0;
14// *****************************************************************************
15// Copyright (C) 2022 STMicroelectronics, Ericsson, ARM, EclipseSource and others.
16//
17// This program and the accompanying materials are made available under the
18// terms of the Eclipse Public License v. 2.0 which is available at
19// http://www.eclipse.org/legal/epl-2.0.
20//
21// This Source Code may also be made available under the following Secondary
22// Licenses when the conditions for such availability set forth in the Eclipse
23// Public License v. 2.0 are satisfied: GNU General Public License, version 2
24// with the GNU Classpath Exception which is available at
25// https://www.gnu.org/software/classpath/license.html.
26//
27// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
28// *****************************************************************************
29const inversify_1 = require("inversify");
30const window_service_1 = require("./window-service");
31let DefaultSecondaryWindowService = DefaultSecondaryWindowService_1 = class DefaultSecondaryWindowService {
32 constructor() {
33 /**
34 * Randomized prefix to be included in opened windows' ids.
35 * This avoids conflicts when creating sub-windows from multiple theia instances (e.g. by opening Theia multiple times in the same browser)
36 */
37 this.prefix = crypto.getRandomValues(new Uint32Array(1))[0];
38 /** Unique id. Increase after every access. */
39 this.nextId = 0;
40 this.secondaryWindows = [];
41 }
42 init() {
43 // Close all open windows when the main window is closed.
44 this.windowService.onUnload(() => {
45 // Iterate backwards because calling window.close might remove the window from the array
46 for (let i = this.secondaryWindows.length - 1; i >= 0; i--) {
47 this.secondaryWindows[i].close();
48 }
49 });
50 }
51 createSecondaryWindow(onClose) {
52 const win = this.doCreateSecondaryWindow(onClose);
53 if (win) {
54 this.secondaryWindows.push(win);
55 }
56 return win;
57 }
58 doCreateSecondaryWindow(onClose) {
59 const win = window.open(DefaultSecondaryWindowService_1.SECONDARY_WINDOW_URL, this.nextWindowId(), 'popup');
60 if (win) {
61 // Add the unload listener after the dom content was loaded because otherwise the unload listener is called already on open in some browsers (e.g. Chrome).
62 win.addEventListener('DOMContentLoaded', () => {
63 win.addEventListener('unload', () => {
64 this.handleWindowClosed(win, onClose);
65 });
66 });
67 }
68 return win !== null && win !== void 0 ? win : undefined;
69 }
70 handleWindowClosed(win, onClose) {
71 const extIndex = this.secondaryWindows.indexOf(win);
72 if (extIndex > -1) {
73 this.secondaryWindows.splice(extIndex, 1);
74 }
75 ;
76 onClose === null || onClose === void 0 ? void 0 : onClose(win);
77 }
78 focus(win) {
79 win.focus();
80 }
81 nextWindowId() {
82 return `${this.prefix}-secondaryWindow-${this.nextId++}`;
83 }
84};
85// secondary-window.html is part of Theia's generated code. It is generated by dev-packages/application-manager/src/generator/frontend-generator.ts
86DefaultSecondaryWindowService.SECONDARY_WINDOW_URL = 'secondary-window.html';
87__decorate([
88 (0, inversify_1.inject)(window_service_1.WindowService),
89 __metadata("design:type", Object)
90], DefaultSecondaryWindowService.prototype, "windowService", void 0);
91__decorate([
92 (0, inversify_1.postConstruct)(),
93 __metadata("design:type", Function),
94 __metadata("design:paramtypes", []),
95 __metadata("design:returntype", void 0)
96], DefaultSecondaryWindowService.prototype, "init", null);
97DefaultSecondaryWindowService = DefaultSecondaryWindowService_1 = __decorate([
98 (0, inversify_1.injectable)()
99], DefaultSecondaryWindowService);
100exports.DefaultSecondaryWindowService = DefaultSecondaryWindowService;
101//# sourceMappingURL=default-secondary-window-service.js.map
\No newline at end of file