Skip to content

Commit c3ca7ba

Browse files
committed
refactor
1 parent ae98122 commit c3ca7ba

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

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

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

30-
this.__attachIframeMessageHandlers();
30+
this.__attachInterframeMessageHandlers();
3131
},
3232

3333
statics: {
@@ -245,8 +245,12 @@ qx.Class.define("osparc.widget.PersistentIframe", {
245245
this.__iframe.setSource(newValue);
246246
},
247247

248-
__attachIframeMessageHandlers: function() {
249-
// post messages
248+
__attachInterframeMessageHandlers: function() {
249+
this.__attachTriggerers();
250+
this.__attachListeners();
251+
},
252+
253+
__attachTriggerers: function() {
250254
this.postThemeSwitch = theme => {
251255
const iframe = this._getIframeElement();
252256
if (iframe) {
@@ -262,25 +266,23 @@ qx.Class.define("osparc.widget.PersistentIframe", {
262266
}
263267
}
264268
};
265-
269+
266270
this.themeSwitchHandler = msg => {
267271
this.postThemeSwitch(msg.getData());
268272
};
269273
qx.event.message.Bus.getInstance().subscribe("themeSwitch", this.themeSwitchHandler);
270-
271-
// listen to messages
274+
},
275+
276+
__attachListeners: function() {
272277
this.__iframe.addListener("load", () => {
273278
const iframe = this._getIframeElement();
274279
if (iframe) {
275280
const iframeDomEl = iframe.getDomElement();
276281
if (iframeDomEl) {
277-
const iframeWindow = iframe.getDomElement().contentWindow;
278282
window.addEventListener('message', message => {
279-
if (message.source === iframeWindow) {
280-
const data = message.data;
281-
if (data) {
282-
this.__handleIframeMessage(data);
283-
}
283+
const data = message.data;
284+
if (data) {
285+
this.__handleIframeMessage(data);
284286
}
285287
});
286288
}
@@ -289,17 +291,18 @@ qx.Class.define("osparc.widget.PersistentIframe", {
289291
},
290292

291293
__handleIframeMessage: function(data) {
292-
// switch theme driven by the iframe
293-
if (data["type"] && data["type"] === "theme") {
294-
const message = data["message"];
295-
console.log(message);
296-
if (message.includes("osparc;theme=")) {
297-
const themeName = message.replace("osparc;theme=", "");
298-
const validThemes = osparc.ui.switch.ThemeSwitcher.getValidThemes();
299-
const themeFound = validThemes.find(theme => theme.basename === themeName);
300-
const themeManager = qx.theme.manager.Meta.getInstance();
301-
if (themeFound !== themeManager.getTheme()) {
302-
themeManager.setTheme(themeFound);
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+
}
303306
}
304307
}
305308
}

0 commit comments

Comments
 (0)