Skip to content

Commit f56acf6

Browse files
authored
✨ [Frontend] listen to iframe messages PoC (#6884)
1 parent 8817832 commit f56acf6

File tree

1 file changed

+64
-21
lines changed

1 file changed

+64
-21
lines changed

services/static-webserver/client/source/class/osparc/widget/PersistentIframe.js

Lines changed: 64 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,7 @@ qx.Class.define("osparc.widget.PersistentIframe", {
2727
construct: function(source, el) {
2828
this.base(arguments, source);
2929

30-
this.themeSwitchHandler = msg => {
31-
this.postThemeSwitch(msg.getData());
32-
};
33-
34-
this.postThemeSwitch = theme => {
35-
const iframe = this._getIframeElement();
36-
if (this._getIframeElement()) {
37-
const iframeDomEl = iframe.getDomElement();
38-
const iframeSource = iframe.getSource();
39-
if (iframeDomEl && iframeSource) {
40-
const msg = "osparc;theme=" + theme;
41-
try {
42-
iframeDomEl.contentWindow.postMessage(msg, iframeSource);
43-
} catch (err) {
44-
console.log(`Failed posting message ${msg} to iframe ${iframeSource}\n${err.message}`);
45-
}
46-
}
47-
}
48-
};
49-
50-
qx.event.message.Bus.getInstance().subscribe("themeSwitch", this.themeSwitchHandler);
30+
this.__attachInterframeMessageHandlers();
5131
},
5232

5333
statics: {
@@ -265,6 +245,69 @@ qx.Class.define("osparc.widget.PersistentIframe", {
265245
this.__iframe.setSource(newValue);
266246
},
267247

248+
__attachInterframeMessageHandlers: function() {
249+
this.__attachTriggerers();
250+
this.__attachListeners();
251+
},
252+
253+
__attachTriggerers: function() {
254+
this.postThemeSwitch = theme => {
255+
const iframe = this._getIframeElement();
256+
if (iframe) {
257+
const iframeDomEl = iframe.getDomElement();
258+
const iframeSource = iframe.getSource();
259+
if (iframeDomEl && iframeSource) {
260+
const msg = "osparc;theme=" + theme;
261+
try {
262+
iframeDomEl.contentWindow.postMessage(msg, iframeSource);
263+
} catch (err) {
264+
console.log(`Failed posting message ${msg} to iframe ${iframeSource}\n${err.message}`);
265+
}
266+
}
267+
}
268+
};
269+
270+
this.themeSwitchHandler = msg => {
271+
this.postThemeSwitch(msg.getData());
272+
};
273+
qx.event.message.Bus.getInstance().subscribe("themeSwitch", this.themeSwitchHandler);
274+
},
275+
276+
__attachListeners: function() {
277+
this.__iframe.addListener("load", () => {
278+
const iframe = this._getIframeElement();
279+
if (iframe) {
280+
const iframeDomEl = iframe.getDomElement();
281+
if (iframeDomEl) {
282+
window.addEventListener("message", message => {
283+
const data = message.data;
284+
if (data) {
285+
this.__handleIframeMessage(data);
286+
}
287+
});
288+
}
289+
}
290+
}, this);
291+
},
292+
293+
__handleIframeMessage: function(data) {
294+
if (data["type"] && data["message"]) {
295+
if (data["type"] === "theme") {
296+
// switch theme driven by the iframe
297+
const message = data["message"];
298+
if (message.includes("osparc;theme=")) {
299+
const themeName = message.replace("osparc;theme=", "");
300+
const validThemes = osparc.ui.switch.ThemeSwitcher.getValidThemes();
301+
const themeFound = validThemes.find(theme => theme.basename === themeName);
302+
const themeManager = qx.theme.manager.Meta.getInstance();
303+
if (themeFound !== themeManager.getTheme()) {
304+
themeManager.setTheme(themeFound);
305+
}
306+
}
307+
}
308+
}
309+
},
310+
268311
// override
269312
_getIframeElement: function() {
270313
return this.__iframe._getIframeElement(); // eslint-disable-line no-underscore-dangle

0 commit comments

Comments
 (0)