1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | var __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 | };
|
23 | var __metadata = (this && this.__metadata) || function (k, v) {
|
24 | if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
25 | };
|
26 | Object.defineProperty(exports, "__esModule", { value: true });
|
27 | exports.DefaultWindowService = void 0;
|
28 | const inversify_1 = require("inversify");
|
29 | const common_1 = require("../../common");
|
30 | const core_preferences_1 = require("../core-preferences");
|
31 | const contribution_provider_1 = require("../../common/contribution-provider");
|
32 | const frontend_application_1 = require("../frontend-application");
|
33 | const window_1 = require("../../common/window");
|
34 | const dialogs_1 = require("../dialogs");
|
35 | const frontend_application_state_1 = require("../../common/frontend-application-state");
|
36 | let DefaultWindowService = class DefaultWindowService {
|
37 | constructor() {
|
38 | this.allowVetoes = true;
|
39 | this.onUnloadEmitter = new common_1.Emitter();
|
40 | }
|
41 | get onUnload() {
|
42 | return this.onUnloadEmitter.event;
|
43 | }
|
44 | onStart(app) {
|
45 | this.frontendApplication = app;
|
46 | this.registerUnloadListeners();
|
47 | }
|
48 | openNewWindow(url) {
|
49 | window.open(url, undefined, 'noopener');
|
50 | return undefined;
|
51 | }
|
52 | openNewDefaultWindow() {
|
53 | this.openNewWindow(`#${window_1.DEFAULT_WINDOW_HASH}`);
|
54 | }
|
55 | |
56 |
|
57 |
|
58 |
|
59 |
|
60 | collectContributionUnloadVetoes() {
|
61 | var _a;
|
62 | const vetoes = [];
|
63 | if (this.allowVetoes) {
|
64 | const shouldConfirmExit = this.corePreferences['application.confirmExit'];
|
65 | for (const contribution of this.contributions.getContributions()) {
|
66 | const veto = (_a = contribution.onWillStop) === null || _a === void 0 ? void 0 : _a.call(contribution, this.frontendApplication);
|
67 | if (veto && shouldConfirmExit !== 'never') {
|
68 | if (frontend_application_1.OnWillStopAction.is(veto)) {
|
69 | vetoes.push(veto);
|
70 | }
|
71 | else {
|
72 | vetoes.push({ reason: 'No reason given', action: () => false });
|
73 | }
|
74 | }
|
75 | }
|
76 | vetoes.sort((a, b) => { var _a, _b; return ((_a = a.priority) !== null && _a !== void 0 ? _a : -Infinity) - ((_b = b.priority) !== null && _b !== void 0 ? _b : -Infinity); });
|
77 | if (vetoes.length === 0 && shouldConfirmExit === 'always') {
|
78 | vetoes.push({ reason: 'application.confirmExit preference', action: () => (0, dialogs_1.confirmExit)() });
|
79 | }
|
80 | if (vetoes.length === 0) {
|
81 | this.allowVetoes = false;
|
82 | }
|
83 | }
|
84 | return vetoes;
|
85 | }
|
86 | |
87 |
|
88 |
|
89 | registerUnloadListeners() {
|
90 | window.addEventListener('beforeunload', event => this.handleBeforeUnloadEvent(event));
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | window.addEventListener('unload', () => this.onUnloadEmitter.fire());
|
96 | }
|
97 | async isSafeToShutDown(stopReason) {
|
98 | const vetoes = this.collectContributionUnloadVetoes();
|
99 | if (vetoes.length === 0) {
|
100 | return true;
|
101 | }
|
102 | const preparedValues = await Promise.all(vetoes.map(e => { var _a; return (_a = e.prepare) === null || _a === void 0 ? void 0 : _a.call(e, stopReason); }));
|
103 | console.debug('Shutdown prevented by', vetoes.map(({ reason }) => reason).join(', '));
|
104 | for (let i = 0; i < vetoes.length; i++) {
|
105 | try {
|
106 | const result = await vetoes[i].action(preparedValues[i], stopReason);
|
107 | if (!result) {
|
108 | return false;
|
109 | }
|
110 | }
|
111 | catch (e) {
|
112 | console.error(e);
|
113 | }
|
114 | }
|
115 | console.debug('OnWillStop actions resolved; allowing shutdown');
|
116 | this.allowVetoes = false;
|
117 | return true;
|
118 | }
|
119 | setSafeToShutDown() {
|
120 | this.allowVetoes = false;
|
121 | }
|
122 | |
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | handleBeforeUnloadEvent(event) {
|
130 | const vetoes = this.collectContributionUnloadVetoes();
|
131 | if (vetoes.length) {
|
132 |
|
133 | console.debug('Shutdown prevented by', vetoes.map(({ reason }) => reason).join(', '));
|
134 | return this.preventUnload(event);
|
135 | }
|
136 | console.debug('Shutdown will proceed.');
|
137 | }
|
138 | |
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 | preventUnload(event) {
|
148 | event.returnValue = '';
|
149 | event.preventDefault();
|
150 | return '';
|
151 | }
|
152 | reload() {
|
153 | this.isSafeToShutDown(frontend_application_state_1.StopReason.Reload).then(isSafe => {
|
154 | if (isSafe) {
|
155 | window.location.reload();
|
156 | }
|
157 | });
|
158 | }
|
159 | };
|
160 | __decorate([
|
161 | (0, inversify_1.inject)(core_preferences_1.CorePreferences),
|
162 | __metadata("design:type", Object)
|
163 | ], DefaultWindowService.prototype, "corePreferences", void 0);
|
164 | __decorate([
|
165 | (0, inversify_1.inject)(contribution_provider_1.ContributionProvider),
|
166 | (0, inversify_1.named)(frontend_application_1.FrontendApplicationContribution),
|
167 | __metadata("design:type", Object)
|
168 | ], DefaultWindowService.prototype, "contributions", void 0);
|
169 | DefaultWindowService = __decorate([
|
170 | (0, inversify_1.injectable)()
|
171 | ], DefaultWindowService);
|
172 | exports.DefaultWindowService = DefaultWindowService;
|
173 |
|
\ | No newline at end of file |