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