-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponent.js
More file actions
319 lines (287 loc) · 13.5 KB
/
Copy pathComponent.js
File metadata and controls
319 lines (287 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
sap.ui.define(
[
"sap/ui/core/UIComponent",
"z2ui5/model/models",
"z2ui5/core/Server",
"sap/ui/VersionInfo",
"z2ui5/devtools/DevTools",
"z2ui5/core/Lib",
"z2ui5/core/AppState",
"z2ui5/Util",
"z2ui5/model/formatter",
"z2ui5/core/Router",
"z2ui5/core/ScrollFocus",
],
(
UIComponent,
Models,
Server,
VersionInfo,
DevTools,
Lib,
AppState,
DateUtil,
Formatter,
Router,
ScrollFocus,
) => {
"use strict";
return UIComponent.extend("z2ui5.Component", {
metadata: {
manifest: "json",
interfaces: ["sap.ui.core.IAsyncContentCreation"],
},
init() {
// The global "z2ui5" object holds the shared state for the whole
// app; core/AppState owns it. initGlobal() creates the global if
// needed, resets the internal state to clean defaults and provides
// a fresh oConfig - so the base init() and all helpers can rely on
// a fully initialized global from here on.
AppState.initGlobal();
// Two sibling BSPs carry frontend artefacts the framework itself does
// not ship: z2ui5_cci (abap2UI5-addons/custom-controls) and z2ui5_ccc
// (abap2UI5/customer-frontend-extension, the customer's own library).
// The two roots differ in one letter and are NOT the same BSP - each
// matches the ABAP prefix of its repository (z2ui5_cl_cci for the
// community controls, z2ui5_cl_ccc for the customer extension).
// Both are normally found through their reserved resourceRoot in
// manifest.json ("z2ui5_cci": "../z2ui5_cci/", "z2ui5_ccc":
// "../z2ui5_ccc/"), a sibling of THIS BSP. In the standalone HTTP
// service there is no BSP for them to be a sibling of, so the backend
// hands the absolute paths over on the global instead
// (z2ui5_cl_http_handler=>_http_get).
//
// They have to be applied HERE and not in the page: the manifest
// registers its own value while the component is being created, which
// is after everything the shell can run, so a registration made there
// is overwritten again. init() runs after manifest processing.
// Absent in BSP and Launchpad mode, where the manifest entries are
// right. Neither BSP is loaded from here - nothing is requested until
// a view actually names the namespace - so a system that has only one
// of them installed (or neither) never pays for the other.
const ccResourceRoot = AppState.getGlobal("ccResourceRoot");
if (ccResourceRoot) {
sap.ui.loader.config({ paths: { z2ui5_cci: ccResourceRoot } });
}
const cccResourceRoot = AppState.getGlobal("cccResourceRoot");
if (cccResourceRoot) {
sap.ui.loader.config({ paths: { z2ui5_ccc: cccResourceRoot } });
}
UIComponent.prototype.init.call(this);
AppState.getGlobal("oConfig").ComponentData = this.getComponentData();
// The date helpers are a public contract: apps use them via the
// z2ui5.Util global (XML view formatter strings) or via
// core:require of the z2ui5/Util module. Publish the global here -
// since the custom controls were split out of App.controller.js,
// nothing else loads the module eagerly anymore.
AppState.setGlobal("Util", DateUtil);
// The curated formatter module in the standard app layout
// (model/formatter.js): views wire it via core:require of
// z2ui5/model/formatter; the global keeps binding strings working
// on releases without core:require (< 1.74). It owns the date
// helpers - Util above is the thin legacy alias re-exporting them.
AppState.setGlobal("Formatter", Formatter);
AppState.state.oDeviceModel = Models.createDeviceModel();
this.setModel(AppState.state.oDeviceModel, "device");
// Warm-load the messaging module so Lib.getMessaging's synchronous
// sap.ui.require resolves it before the first view is displayed.
// On UI5 2.x sap/ui/core/Messaging is the only messaging API (the
// sap.ui.getCore().getMessageManager() fallback is gone), and
// nothing else pulls the module into the graph - without this the
// message> model and validation collection would silently no-op.
// Only attempt it where the module exists (1.118+): on older releases
// (e.g. 1.71) the require would 404 and make the ui5loader retry
// loudly via synchronous XHR; there Lib.getMessaging falls back to
// sap.ui.getCore().getMessageManager() instead.
if (Lib.hasMessagingModule()) {
sap.ui.require(
["sap/ui/core/Messaging"],
() => {},
() => {},
);
}
this._initLaunchpad();
this._initVersionInfo();
this._installUnloadListener();
// The developer tools own everything of their own: the Ctrl+F12
// shortcut, the dialog instance, the roundtrip recorder and the
// "?z2ui5-devtools=" auto open. This call and the exit() below are
// the framework's ENTIRE coupling to devtools/ - keep it that
// way (see the module header there).
DevTools.install();
this._installScrollListener();
this._installRouterListener();
},
// ------------------------------------------------------------------
// Event listeners installed in init() and removed in exit()
// ------------------------------------------------------------------
_installUnloadListener() {
this._boundUnload = this._onUnload.bind(this);
// "pagehide", not "beforeunload": pagehide fires only after the
// navigation is committed (any "leave page?" prompt was answered),
// so tearing the app down here can neither swallow the cc/Dirty
// unsaved-changes prompt (destroying the app mid-beforeunload
// removed its window.onbeforeunload handler before the browser
// invoked it) nor kill the live session when the user chooses to
// stay. It is also the reliable event on iOS Safari, which never
// fired beforeunload dependably.
this._unloadEvent = "pagehide";
window.addEventListener(this._unloadEvent, this._boundUnload);
},
_installScrollListener() {
// Scroll events do not bubble, but they do trigger capture-phase
// listeners on ancestors - a single document-level listener observes
// every scrollable container. ScrollFocus.onScrollCapture records the
// last scrolled element per view slot for the S_SCROLL request info.
this._boundScroll = (event) => ScrollFocus.onScrollCapture(event);
document.addEventListener("scroll", this._boundScroll, {
capture: true,
passive: true,
});
},
_installRouterListener() {
// Hash-based app routing (UI5 Router style), owned by core/Router.js.
// It sits on the HashChanger - the same engine
// sap.ui.core.routing.Router uses, and inside the FLP the shell's own
// one - so the native browser Back/Forward buttons and the launchpad
// back button drive navigation. Only apps that opted in via
// follow_up_action( cs_event-set_nav_routing ) act on it, so apps that manage their own
// hash are unaffected. Server does the actual restore roundtrip; it is
// injected here so the router stays free of a Server dependency.
Router.init(() => Server.restoreFromRoute());
},
// ------------------------------------------------------------------
// SAP Fiori Launchpad integration (only when running inside FLP)
// ------------------------------------------------------------------
_initLaunchpad() {
const Container = sap.ui.require("sap/ushell/Container");
if (!Container) return; // not running inside the launchpad -> nothing to do
const launchpad = { Container };
this._launchpad = launchpad;
AppState.state.oLaunchpad = launchpad;
// The FLP services load asynchronously. By the time they resolve, the
// component may already have been destroyed (e.g. user navigated away
// before the services were ready). setIfAlive guards against writing
// to a stale launchpad object in that case.
const setIfAlive = (key, value) => {
if (Lib.isAlive(this) && this._launchpad === launchpad) {
launchpad[key] = value;
}
};
// ShellUIService is a UI5 service (factory
// sap.ushell.ui5service.ShellUIService, declared in manifest.json),
// not a Container service. Requesting it via Container.getServiceAsync
// resolves to sap/ushell/services/ShellUIService.js, which does not
// exist in the (ABAP) launchpad and fails with a 404. The component's
// getService() honors the manifest declaration and returns the
// correctly scoped instance.
this.getService("ShellUIService")
.then((s) => setIfAlive("ShellUIService", s))
.catch((e) =>
Lib.logError("Component: ShellUIService init failed", e),
);
Container.getServiceAsync("CrossApplicationNavigation")
.then((s) => setIfAlive("CrossAppNavigator", s))
.catch((e) =>
Lib.logError(
"Component: CrossApplicationNavigation init failed",
e,
),
);
sap.ui.require(
["sap/ushell/services/AppConfiguration"],
(ac) => setIfAlive("AppConfiguration", ac),
(e) => Lib.logError("Component: AppConfiguration init failed", e),
);
},
async _initVersionInfo() {
try {
const info = await VersionInfo.load();
if (Lib.isAlive(this)) {
AppState.getGlobal("oConfig").S_UI5 = {
VERSION: info.version,
BUILDTIMESTAMP: info.buildTimestamp,
GAV: info.gav,
THEME: this._getTheme(),
};
}
} catch (e) {
Lib.logError("Component: VersionInfo load failed", e);
}
},
_getTheme() {
// sap/ui/core/Theming only exists since UI5 1.118, so it must not be
// a hard dependency of this module - older bootstraps (e.g. 1.108)
// would fail to load the component. On 1.118+ the core itself loads
// Theming, so the probing require finds it; otherwise fall back to
// the legacy Configuration API.
try {
const Theming = sap.ui.require("sap/ui/core/Theming");
if (Theming?.getTheme) return Theming.getTheme();
/* ui5lint-disable no-globals, no-deprecated-api --
deliberate fallback for UI5 releases without sap/ui/core/Theming
(added in 1.118); the modern API is used in the branch above. */
if (sap.ui.getCore) {
return sap.ui.getCore().getConfiguration().getTheme();
}
/* ui5lint-enable no-globals, no-deprecated-api */
} catch (e) {
Lib.logError("Component: reading theme failed", e);
}
return "";
},
_onUnload(event) {
// pagehide with persisted = true means the page enters the browser's
// back/forward cache and may be shown again - keep the app alive.
if (event?.persisted) return;
// destroy() runs exit(), which removes the unload listener (and every
// other one) - no need to remove it here too.
this.destroy();
},
// ------------------------------------------------------------------
// Component teardown
// ------------------------------------------------------------------
exit() {
window.removeEventListener(this._unloadEvent, this._boundUnload);
document.removeEventListener("scroll", this._boundScroll, {
capture: true,
});
Router.exit();
// Drops the shortcut, the dialog instance and the recorded history -
// all of which are module-scoped and would otherwise outlive the
// component on an FLP re-launch.
DevTools.exit();
Server.endSession();
// Global state that would outlive the component (FLP keeps the page
// alive): cancel any pending backend timer, empty the shortcut
// registry so the module-scoped keydown listener becomes a no-op,
// and detach the device model's handlers from the Device singleton.
for (const key of Object.keys(AppState.state.timers)) {
clearTimeout(AppState.state.timers[key]);
delete AppState.state.timers[key];
}
AppState.state.shortcuts = {};
if (AppState.state.oDeviceModel) {
AppState.state.oDeviceModel.destroy();
AppState.state.oDeviceModel = null;
}
// Robust launchpad teardown:
// 1. Clear the FLP dirty flag so it does not carry over into the
// next app the user opens.
// 2. Detach the shared launchpad object so a subsequent re-launch
// starts from a clean state and any still-pending init Promises
// become no-ops via setIfAlive().
try {
this._launchpad?.Container?.setDirtyFlag?.(false);
} catch (e) {
Lib.logError("Component: clearing FLP dirty flag failed", e);
}
if (AppState.state.oLaunchpad === this._launchpad) {
AppState.state.oLaunchpad = null;
}
this._launchpad = null;
if (UIComponent.prototype.exit) UIComponent.prototype.exit.call(this);
},
});
},
);