|
| 1 | +/* ************************************************************************ |
| 2 | +
|
| 3 | + osparc - the simcore frontend |
| 4 | +
|
| 5 | + https://osparc.io |
| 6 | +
|
| 7 | + Copyright: |
| 8 | + 2025 IT'IS Foundation, https://itis.swiss |
| 9 | +
|
| 10 | + License: |
| 11 | + MIT: https://opensource.org/licenses/MIT |
| 12 | +
|
| 13 | + Authors: |
| 14 | + * Odei Maiz (odeimaiz) |
| 15 | +
|
| 16 | +************************************************************************ */ |
| 17 | + |
| 18 | +/** |
| 19 | + * @asset(rocketPreview/build/index.html) |
| 20 | + * @asset(rocketPreview/build/**) |
| 21 | + * @asset(rocketPreview/osparc-bridge.js) // build needs to include it |
| 22 | + */ |
| 23 | + |
| 24 | +/** |
| 25 | + * A qooxdoo wrapper for The Rocket Preview |
| 26 | + * It loads the app in an iframe and communicates via postMessage. |
| 27 | + */ |
| 28 | + |
| 29 | +qx.Class.define("osparc.wrapper.RocketPreview", { |
| 30 | + extend: qx.ui.core.Widget, |
| 31 | + |
| 32 | + construct: function() { |
| 33 | + this.base(arguments); |
| 34 | + this._setLayout(new qx.ui.layout.Grow()); |
| 35 | + |
| 36 | + this.__queue = []; |
| 37 | + |
| 38 | + // force creation of the iframe child control |
| 39 | + this._createChildControl("iframe"); |
| 40 | + |
| 41 | + window.addEventListener("message", this.__onMessage.bind(this)); |
| 42 | + }, |
| 43 | + |
| 44 | + properties: { |
| 45 | + /** |
| 46 | + * True once the iframe signals it's ready (osparc:ready). |
| 47 | + */ |
| 48 | + ready: { |
| 49 | + check: "Boolean", |
| 50 | + init: false, |
| 51 | + event: "changeReady" |
| 52 | + }, |
| 53 | + }, |
| 54 | + |
| 55 | + members: { |
| 56 | + __queue: null, |
| 57 | + __iframeEl: null, |
| 58 | + |
| 59 | + _createChildControlImpl: function(id) { |
| 60 | + let control; |
| 61 | + switch (id) { |
| 62 | + case "iframe": |
| 63 | + const src = qx.util.ResourceManager.getInstance().toUri("rocketPreview/build/index.html"); |
| 64 | + control = new qx.ui.embed.Html("<iframe></iframe>"); |
| 65 | + control.set({ |
| 66 | + allowGrowX: true, |
| 67 | + allowGrowY: true |
| 68 | + }); |
| 69 | + |
| 70 | + // configure the real DOM iframe element |
| 71 | + control.addListenerOnce("appear", () => { |
| 72 | + const el = control.getContentElement().getDomElement().querySelector("iframe"); |
| 73 | + el.src = src; |
| 74 | + el.style.width = "100%"; |
| 75 | + el.style.height = "100%"; |
| 76 | + el.style.border = "0"; |
| 77 | + this.__iframeEl = el; |
| 78 | + }); |
| 79 | + |
| 80 | + this._add(control, { edge: 0 }); |
| 81 | + break; |
| 82 | + } |
| 83 | + return control || this.base(arguments, id); |
| 84 | + }, |
| 85 | + |
| 86 | + // ---- Public API ---- |
| 87 | + setTreeData: function(data) { |
| 88 | + this.__send({type: "setTreeData", payload: data}); |
| 89 | + }, |
| 90 | + |
| 91 | + setExtraData: function(data) { |
| 92 | + this.__send({type: "setExtraData", payload: data}); |
| 93 | + }, |
| 94 | + |
| 95 | + setImage: function(img) { |
| 96 | + this.__send({type: "setImage", payload: img}); |
| 97 | + }, |
| 98 | + // -------------------- |
| 99 | + |
| 100 | + __send: function(msg) { |
| 101 | + if (!this.isReady()) { |
| 102 | + this.__queue.push(msg); |
| 103 | + return; |
| 104 | + } |
| 105 | + this.__postMessage(msg); |
| 106 | + }, |
| 107 | + |
| 108 | + __onMessage: function(ev) { |
| 109 | + const data = ev.data; |
| 110 | + if (data && data.type === "osparc:ready") { |
| 111 | + this.setReady(true); |
| 112 | + while (this.__queue.length) { |
| 113 | + this.__postMessage(this.__queue.shift()); |
| 114 | + } |
| 115 | + } |
| 116 | + }, |
| 117 | + |
| 118 | + __postMessage: function(msg) { |
| 119 | + if (this.__iframeEl && this.__iframeEl.contentWindow) { |
| 120 | + this.__iframeEl.contentWindow.postMessage(msg, "*"); |
| 121 | + } |
| 122 | + }, |
| 123 | + } |
| 124 | +}); |
0 commit comments