-
Notifications
You must be signed in to change notification settings - Fork 2
/
iframe.js
344 lines (306 loc) · 9.41 KB
/
iframe.js
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// @ts-check
/**
* This script is executed within the apps' iframe and allows to setup
* aspects that have to be controlled from within the iframe.
*/
///// iframe resizing /////
/**
* Fix scrollbar flickering on iframe resizing. Hidden overflow-y on html element.
*/
document.documentElement.style.overflowY = "hidden";
/**
* An array to track the absolute positioned elements
*
* @type {Array<Node>}
*/
let positionedNodes = [];
/**
* Observes the size of the HTML element and triggers a message with
* the new height if the size changes. The parent window will then
* dynamically adjust the height of the iframe.
*/
const resizeObserver = new ResizeObserver((entries) => {
for (const entry of entries) {
const height = entry.contentBoxSize
? entry.contentBoxSize[0].blockSize
: entry.contentRect.height;
postAppResize(height);
}
});
/**
* Observes absolute positioned elements that are added/removed
* from/to the DOM and triggers a message with the new height. The
* parent window will then dynamically adjust the height of the
* iframe.
*/
const mutationObserver = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
if (isAbsolutePositioned(node)) {
positionedNodes.push(node);
postAppResize();
}
});
mutation.removedNodes.forEach((node) => {
const index = positionedNodes.findIndex((n) => n === node);
if (index >= 0) {
positionedNodes.splice(index, 1);
postAppResize();
}
});
});
});
/**
* @param {Node} node
* @returns {boolean}
*/
function isAbsolutePositioned(node) {
if (!(node instanceof HTMLElement)) return false;
// For some nodes we have to consider the computed style to get the
// correct position value, but to avoid any performance issues we
// only do this for the nodes where it is a problem.
const position =
node.nodeName === "NG-DROPDOWN-PANEL"
? getComputedStyle(node).position
: node.style?.position;
return (
position === "absolute" || position === "fixed" || position === "sticky"
);
}
/**
* Notify the parent window that the iframe should be resized, either
* to the body height, or to the lowest bottom of any absolute
* positioned element.
*
* @param {number=} height
* @returns {void}
*/
function postAppResize(height) {
const viewportHeight =
height ?? document.documentElement.getBoundingClientRect().height;
const maxBottom = getMaxPositionedBottom();
const tolerance = 10; // Add some tolerance to show complete content of iframe and prevent scrollbar
parent.window.postMessage(
{
type: "bkdAppResize",
height: Math.max(maxBottom + tolerance, viewportHeight),
},
window.parent.origin,
);
}
/**
* @returns {number}
*/
function getMaxPositionedBottom() {
return positionedNodes.reduce((maxBottom, node) => {
const nodeBottom =
node instanceof HTMLElement
? node.getBoundingClientRect().top + node.clientHeight
: 0;
return Math.max(maxBottom, nodeBottom);
}, 0);
}
window.addEventListener("load", () => {
// For reasons we don't understand, the <body> in the
// kursauschreibung app always has full height in Chrome (not in
// Firefox), although there is no CSS causing this and the child
// element is smaller. As a workaround we observe the child in the
// kursausschreibung app. See also
// https://github.com/bkd-mba-fbi/evento-portal/issues/117
const mainElement =
(document.getElementById("kursausschreibung-root") &&
document.querySelector("body > div")) ||
document.documentElement;
resizeObserver.observe(mainElement);
const body = document.querySelector("body");
if (body) {
mutationObserver.observe(body, {
subtree: true,
childList: true,
});
}
});
///// HTML lang attribute updating /////
const url = new URL(parent.window.location.href);
const locale = url.searchParams.get("locale");
if (typeof locale === "string") {
document.documentElement.lang = locale;
}
///// Notify portal on state (URL) changes /////
// Post pushState calls to parent window
const pushState = history.pushState;
history.pushState = (...args) => {
pushState.call(history, ...args);
parent.window.postMessage(
{ type: "bkdAppPushState", args },
parent.window.origin,
);
};
// Post replaceState calls to parent window
const replaceState = history.replaceState;
history.replaceState = (...args) => {
replaceState.call(history, ...args);
parent.window.postMessage(
{ type: "bkdAppReplaceState", args },
parent.window.origin,
);
};
// Post hashchange events to parent window
window.addEventListener("hashchange", (event) => {
parent.window.postMessage(
{ type: "bkdAppHashChange", url: event.newURL },
parent.window.origin,
);
});
///// Scrolling /////
// Since the iframe is always resized to contain the full height of
// its contents (with the observers above), it never has
// scrollbars. So if the app wants to scroll vertically or read the
// vertical scroll position, we have to do this in the Evento Portal
// window, while also considering the iframe's top-offset within the
// portal. Therefore we monkey-patch the scroll-relevant browser APIs
// to make the app's scroll logic work.
/**
* @type {any}
*/
const iFrameScrollTo = window.scrollTo;
/**
* Monkey-patch `window.scrollTo` & `window.scroll` to scroll Evento
* Portal window instead.
*
* @param {...*} args
* @returns {void}
*/
function patchedScrollTo(...args) {
if (args.length === 2) {
const [x, y] = args;
// Scroll portal vertically
window.parent.scrollTo(0, y + getPortalOffset());
// Scroll iframe horizontally
iFrameScrollTo.call(window, x, 0);
} else if (args[0]) {
const { top, left, behavior } = args[0];
if (top != null) {
// Scroll portal vertically
window.parent.scrollTo({
top: top + getPortalOffset(),
behavior,
});
}
if (left != null) {
// Scroll iframe horizontally
iFrameScrollTo.call(window, { left, behavior });
}
}
}
window.scrollTo = patchedScrollTo;
window.scroll = patchedScrollTo;
/**
* @type {any}
*/
const iFrameScrollBy = window.scrollBy;
/**
* Monkey-patch `window.scrollBy` to scroll Evento Portal window
* instead.
*
* @param {...*} args
* @returns {void}
*/
function patchedScrollBy(...args) {
if (args.length === 2) {
const [x, y] = args;
// Scroll portal vertically
window.parent.scrollBy(0, y);
// Scroll iframe horizontally
iFrameScrollBy.call(window, x, 0);
} else if (args[0]) {
const { top, left, behavior } = args[0];
if (top != null) {
// Scroll portal vertically
window.parent.scrollBy({ top, behavior });
}
if (left != null) {
// Scroll iframe horizontally
iFrameScrollBy.call(window, { left, behavior });
}
}
}
window.scrollBy = patchedScrollBy;
/**
* Monkey-patch the `window.scrollY` & `window.pageYOffset`
* read-only properties to return the Evento Portal window's scroll
* position instead.
*
* @returns {number}
*/
function patchedScrollY() {
return Math.max(window.parent.scrollY - getPortalOffset(), 0);
}
Object.defineProperty(window, "scrollY", { get: patchedScrollY });
Object.defineProperty(window, "pageYOffset", { get: patchedScrollY });
const iFrameGetBoundingClientRect = Element.prototype.getBoundingClientRect;
/**
* Monkey-patch `Element.prototype.getBoundingClientRect` to
* incorporate Evento Portal window scroll position.
*
* @returns {DOMRect}
* @this {Element}
*/
function patchedGetBoundingClientRect() {
const rect = iFrameGetBoundingClientRect.call(this);
const y = rect.y - window.scrollY;
return new DOMRect(rect.x, y, rect.width, rect.height);
}
Element.prototype.getBoundingClientRect = patchedGetBoundingClientRect;
/**
* Monkey-patch `window.scrollIntoView` to scroll Evento Portal window
* instead.
*
* @param {Parameters<typeof Element.prototype.scrollIntoView>[0]} arg
* @returns {void}
* @this {Element}
*/
function patchedScrollIntoView(arg) {
let behavior;
if (typeof arg === "object") {
behavior = arg?.behavior;
if (arg?.block) {
console.warn(
"The block option is not supported by the simulated scrollIntoView of the Evento Portal",
);
}
if (arg?.inline) {
console.warn(
"The inline option is not supported by the simulated scrollIntoView of the Evento Portal",
);
}
} else if (typeof arg === "boolean") {
console.warn(
"The alignToTop option is not supported by the simulated scrollIntoView of the Evento Portal",
);
}
const { top, bottom } = this.getBoundingClientRect();
const viewportTop = window.parent.scrollY;
const viewportBottom = viewportTop + window.parent.innerHeight;
const offset = getPortalOffset();
if (top + offset < viewportTop || bottom + offset > viewportBottom) {
window.parent.scrollTo({ top: top + offset, behavior });
}
}
Element.prototype.scrollIntoView = patchedScrollIntoView;
/**
* Returns the offset of the iframe to the top of the Evento Portal
* document.
*
* @returns {number}
*/
function getPortalOffset() {
const parentDocument = window.parent.document;
const portalRoot = parentDocument?.querySelector("bkd-portal")?.shadowRoot;
const contentRoot = portalRoot?.querySelector("bkd-content")?.shadowRoot;
const iframe = contentRoot?.querySelector("iframe");
return (
(iframe?.getBoundingClientRect()?.top ?? 0) +
parentDocument.documentElement.scrollTop
);
}