From 0a7636850c312f27201cd56afb7b34f5bdf4458d Mon Sep 17 00:00:00 2001 From: odeimaiz Date: Mon, 2 Dec 2024 17:14:36 +0100 Subject: [PATCH 1/6] refactor --- .../class/osparc/widget/PersistentIframe.js | 46 ++++++++++--------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/services/static-webserver/client/source/class/osparc/widget/PersistentIframe.js b/services/static-webserver/client/source/class/osparc/widget/PersistentIframe.js index 975d529aeb2..ff2e1ada4f0 100644 --- a/services/static-webserver/client/source/class/osparc/widget/PersistentIframe.js +++ b/services/static-webserver/client/source/class/osparc/widget/PersistentIframe.js @@ -27,27 +27,7 @@ qx.Class.define("osparc.widget.PersistentIframe", { construct: function(source, el) { this.base(arguments, source); - this.themeSwitchHandler = msg => { - this.postThemeSwitch(msg.getData()); - }; - - this.postThemeSwitch = theme => { - const iframe = this._getIframeElement(); - if (this._getIframeElement()) { - const iframeDomEl = iframe.getDomElement(); - const iframeSource = iframe.getSource(); - if (iframeDomEl && iframeSource) { - const msg = "osparc;theme=" + theme; - try { - iframeDomEl.contentWindow.postMessage(msg, iframeSource); - } catch (err) { - console.log(`Failed posting message ${msg} to iframe ${iframeSource}\n${err.message}`); - } - } - } - }; - - qx.event.message.Bus.getInstance().subscribe("themeSwitch", this.themeSwitchHandler); + this.__attacheIframeMessageHanders(); }, statics: { @@ -265,6 +245,30 @@ qx.Class.define("osparc.widget.PersistentIframe", { this.__iframe.setSource(newValue); }, + __attacheIframeMessageHanders: function() { + // post messages + this.postThemeSwitch = theme => { + const iframe = this._getIframeElement(); + if (iframe) { + const iframeDomEl = iframe.getDomElement(); + const iframeSource = iframe.getSource(); + if (iframeDomEl && iframeSource) { + const msg = "osparc;theme=" + theme; + try { + iframeDomEl.contentWindow.postMessage(msg, iframeSource); + } catch (err) { + console.log(`Failed posting message ${msg} to iframe ${iframeSource}\n${err.message}`); + } + } + } + }; + + this.themeSwitchHandler = msg => { + this.postThemeSwitch(msg.getData()); + }; + qx.event.message.Bus.getInstance().subscribe("themeSwitch", this.themeSwitchHandler); + }, + // override _getIframeElement: function() { return this.__iframe._getIframeElement(); // eslint-disable-line no-underscore-dangle From 48606027584e8e31eda191cd260d19edab8025fe Mon Sep 17 00:00:00 2001 From: odeimaiz Date: Mon, 2 Dec 2024 17:15:10 +0100 Subject: [PATCH 2/6] minor --- .../client/source/class/osparc/widget/PersistentIframe.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/static-webserver/client/source/class/osparc/widget/PersistentIframe.js b/services/static-webserver/client/source/class/osparc/widget/PersistentIframe.js index ff2e1ada4f0..5dafcac9b16 100644 --- a/services/static-webserver/client/source/class/osparc/widget/PersistentIframe.js +++ b/services/static-webserver/client/source/class/osparc/widget/PersistentIframe.js @@ -27,7 +27,7 @@ qx.Class.define("osparc.widget.PersistentIframe", { construct: function(source, el) { this.base(arguments, source); - this.__attacheIframeMessageHanders(); + this.__attachIframeMessageHandlers(); }, statics: { @@ -245,7 +245,7 @@ qx.Class.define("osparc.widget.PersistentIframe", { this.__iframe.setSource(newValue); }, - __attacheIframeMessageHanders: function() { + __attachIframeMessageHandlers: function() { // post messages this.postThemeSwitch = theme => { const iframe = this._getIframeElement(); From 449853e9a76e43c168dfaa7a3490a7013e8cc5dc Mon Sep 17 00:00:00 2001 From: odeimaiz Date: Mon, 2 Dec 2024 17:23:19 +0100 Subject: [PATCH 3/6] switch theme driven by the iframe --- .../class/osparc/widget/PersistentIframe.js | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/services/static-webserver/client/source/class/osparc/widget/PersistentIframe.js b/services/static-webserver/client/source/class/osparc/widget/PersistentIframe.js index 5dafcac9b16..e6bd9a59433 100644 --- a/services/static-webserver/client/source/class/osparc/widget/PersistentIframe.js +++ b/services/static-webserver/client/source/class/osparc/widget/PersistentIframe.js @@ -267,6 +267,41 @@ qx.Class.define("osparc.widget.PersistentIframe", { this.postThemeSwitch(msg.getData()); }; qx.event.message.Bus.getInstance().subscribe("themeSwitch", this.themeSwitchHandler); + + // listen to messages + this.__iframe.addListener("load", () => { + const iframe = this._getIframeElement(); + if (iframe) { + const iframeDomEl = iframe.getDomElement(); + if (iframeDomEl) { + const iframeWindow = iframe.getDomElement().contentWindow; + window.addEventListener('message', message => { + if (message.source === iframeWindow) { + const data = message.data; + if (data) { + this.__handleIframeMessage(data); + } + } + }); + } + } + }, this); + }, + + __handleIframeMessage: function(data) { + // switch theme driven by the iframe + if (data["type"] && data["type"] === "theme") { + const message = data["message"]; + console.log(message); + if (message.includes("osparc;theme=")) { + const themeName = message.replace("osparc;theme=", ""); + const validThemes = osparc.ui.switch.ThemeSwitcher.getValidThemes(); + const themeFound = validThemes.find(theme => theme.basename === themeName); + if (themeFound) { + qx.theme.manager.Meta.getInstance().setTheme(themeFound); + } + } + } }, // override From 2f9811bc5a846754e234fd68fe181b268ea6a093 Mon Sep 17 00:00:00 2001 From: odeimaiz Date: Mon, 2 Dec 2024 18:17:10 +0100 Subject: [PATCH 4/6] only if it changed --- .../client/source/class/osparc/widget/PersistentIframe.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/services/static-webserver/client/source/class/osparc/widget/PersistentIframe.js b/services/static-webserver/client/source/class/osparc/widget/PersistentIframe.js index e6bd9a59433..75c8eb14792 100644 --- a/services/static-webserver/client/source/class/osparc/widget/PersistentIframe.js +++ b/services/static-webserver/client/source/class/osparc/widget/PersistentIframe.js @@ -297,8 +297,9 @@ qx.Class.define("osparc.widget.PersistentIframe", { const themeName = message.replace("osparc;theme=", ""); const validThemes = osparc.ui.switch.ThemeSwitcher.getValidThemes(); const themeFound = validThemes.find(theme => theme.basename === themeName); - if (themeFound) { - qx.theme.manager.Meta.getInstance().setTheme(themeFound); + const themeManager = qx.theme.manager.Meta.getInstance(); + if (themeFound !== themeManager.getTheme()) { + themeManager.setTheme(themeFound); } } } From c3ca7ba25fe2790fa9999e82c7a841ca4c0a2980 Mon Sep 17 00:00:00 2001 From: odeimaiz Date: Wed, 4 Dec 2024 10:47:40 +0100 Subject: [PATCH 5/6] refactor --- .../class/osparc/widget/PersistentIframe.js | 49 ++++++++++--------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/services/static-webserver/client/source/class/osparc/widget/PersistentIframe.js b/services/static-webserver/client/source/class/osparc/widget/PersistentIframe.js index 75c8eb14792..3ce4541d1d3 100644 --- a/services/static-webserver/client/source/class/osparc/widget/PersistentIframe.js +++ b/services/static-webserver/client/source/class/osparc/widget/PersistentIframe.js @@ -27,7 +27,7 @@ qx.Class.define("osparc.widget.PersistentIframe", { construct: function(source, el) { this.base(arguments, source); - this.__attachIframeMessageHandlers(); + this.__attachInterframeMessageHandlers(); }, statics: { @@ -245,8 +245,12 @@ qx.Class.define("osparc.widget.PersistentIframe", { this.__iframe.setSource(newValue); }, - __attachIframeMessageHandlers: function() { - // post messages + __attachInterframeMessageHandlers: function() { + this.__attachTriggerers(); + this.__attachListeners(); + }, + + __attachTriggerers: function() { this.postThemeSwitch = theme => { const iframe = this._getIframeElement(); if (iframe) { @@ -262,25 +266,23 @@ qx.Class.define("osparc.widget.PersistentIframe", { } } }; - + this.themeSwitchHandler = msg => { this.postThemeSwitch(msg.getData()); }; qx.event.message.Bus.getInstance().subscribe("themeSwitch", this.themeSwitchHandler); - - // listen to messages + }, + + __attachListeners: function() { this.__iframe.addListener("load", () => { const iframe = this._getIframeElement(); if (iframe) { const iframeDomEl = iframe.getDomElement(); if (iframeDomEl) { - const iframeWindow = iframe.getDomElement().contentWindow; window.addEventListener('message', message => { - if (message.source === iframeWindow) { - const data = message.data; - if (data) { - this.__handleIframeMessage(data); - } + const data = message.data; + if (data) { + this.__handleIframeMessage(data); } }); } @@ -289,17 +291,18 @@ qx.Class.define("osparc.widget.PersistentIframe", { }, __handleIframeMessage: function(data) { - // switch theme driven by the iframe - if (data["type"] && data["type"] === "theme") { - const message = data["message"]; - console.log(message); - if (message.includes("osparc;theme=")) { - const themeName = message.replace("osparc;theme=", ""); - const validThemes = osparc.ui.switch.ThemeSwitcher.getValidThemes(); - const themeFound = validThemes.find(theme => theme.basename === themeName); - const themeManager = qx.theme.manager.Meta.getInstance(); - if (themeFound !== themeManager.getTheme()) { - themeManager.setTheme(themeFound); + if (data["type"] && data["message"]) { + if (data["type"] === "theme") { + // switch theme driven by the iframe + const message = data["message"]; + if (message.includes("osparc;theme=")) { + const themeName = message.replace("osparc;theme=", ""); + const validThemes = osparc.ui.switch.ThemeSwitcher.getValidThemes(); + const themeFound = validThemes.find(theme => theme.basename === themeName); + const themeManager = qx.theme.manager.Meta.getInstance(); + if (themeFound !== themeManager.getTheme()) { + themeManager.setTheme(themeFound); + } } } } From dc40be4fe4902d33ee7b434da42a83cbd5c85ed9 Mon Sep 17 00:00:00 2001 From: odeimaiz Date: Wed, 4 Dec 2024 10:54:25 +0100 Subject: [PATCH 6/6] minor --- .../client/source/class/osparc/widget/PersistentIframe.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/static-webserver/client/source/class/osparc/widget/PersistentIframe.js b/services/static-webserver/client/source/class/osparc/widget/PersistentIframe.js index 3ce4541d1d3..83b128e82c9 100644 --- a/services/static-webserver/client/source/class/osparc/widget/PersistentIframe.js +++ b/services/static-webserver/client/source/class/osparc/widget/PersistentIframe.js @@ -279,7 +279,7 @@ qx.Class.define("osparc.widget.PersistentIframe", { if (iframe) { const iframeDomEl = iframe.getDomElement(); if (iframeDomEl) { - window.addEventListener('message', message => { + window.addEventListener("message", message => { const data = message.data; if (data) { this.__handleIframeMessage(data);