From e61f01217b7e56d9de401665fc1303f7b6666d12 Mon Sep 17 00:00:00 2001 From: tangkikodo Date: Tue, 11 Apr 2017 18:07:06 +0800 Subject: [PATCH 1/3] enable scripts in iframe head --- src/canvas/config/config.js | 3 +++ src/canvas/view/CanvasView.js | 31 ++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/canvas/config/config.js b/src/canvas/config/config.js index 9e211d85e..95aeb7a80 100644 --- a/src/canvas/config/config.js +++ b/src/canvas/config/config.js @@ -6,5 +6,8 @@ define(function () { // Coming soon rulers: false, + // append scripts in head + scripts: [] + }; }); \ No newline at end of file diff --git a/src/canvas/view/CanvasView.js b/src/canvas/view/CanvasView.js index d3e59801f..c749b7b19 100644 --- a/src/canvas/view/CanvasView.js +++ b/src/canvas/view/CanvasView.js @@ -30,6 +30,31 @@ function(Backbone, FrameView) { this.em.trigger('canvasScroll'); }, + /** + * Insert scripts into head, it will call renderBody after all scripts loaded or failed + * @private + */ + renderScripts: function () { + var frame = this.frame; + var that = this; + + frame.el.onload = function () { + var scripts = that.config.scripts, + counter = 0; + + scripts.map(function (scriptUrl) { + var script = document.createElement('script'); + script.type = 'text/javascript'; + script.src = scriptUrl; + script.onerror = script.onload = function () { + counter++; + if (counter == scripts.length) that.renderBody(); + }; + frame.el.contentDocument.head.appendChild(script); + }); + }; + }, + /** * Render inside frame's body * @private @@ -198,7 +223,11 @@ function(Backbone, FrameView) { this.model.get('frame').set('wrapper', this.wrapper); this.$el.append(this.frame.render().el); var frame = this.frame; - frame.el.onload = this.renderBody; + if (this.config.scripts.length === 0) { + frame.el.onload = this.renderBody; + } else { + this.renderScripts(); // will call renderBody later + } } var ppfx = this.ppfx; toolsEl = $('
', { id: ppfx + 'tools' }).get(0); From b8c64ac1652f92ec93fa72c5f8c91eb5f80bf716 Mon Sep 17 00:00:00 2001 From: tangkikodo Date: Thu, 13 Apr 2017 16:35:20 +0800 Subject: [PATCH 2/3] change async loading into one-by-one loading --- src/canvas/view/CanvasView.js | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/canvas/view/CanvasView.js b/src/canvas/view/CanvasView.js index c749b7b19..01814934d 100644 --- a/src/canvas/view/CanvasView.js +++ b/src/canvas/view/CanvasView.js @@ -39,19 +39,21 @@ function(Backbone, FrameView) { var that = this; frame.el.onload = function () { - var scripts = that.config.scripts, + var scripts = that.config.scripts.slice(0), // clone counter = 0; - - scripts.map(function (scriptUrl) { - var script = document.createElement('script'); - script.type = 'text/javascript'; - script.src = scriptUrl; - script.onerror = script.onload = function () { - counter++; - if (counter == scripts.length) that.renderBody(); - }; - frame.el.contentDocument.head.appendChild(script); - }); + + function appendScript(scripts) { + if (scripts.length > 0) { + var script = document.createElement('script'); + script.type = 'text/javascript'; + script.src = scripts.shift(); + script.onerror = script.onload = appendScript.bind(null, scripts); + frame.el.contentDocument.head.appendChild(script); + } else { + that.renderBody(); + } + } + appendScript(scripts); }; }, From cff7984fefc83d45cd043b760a18a7d98cc84cf7 Mon Sep 17 00:00:00 2001 From: tangkikodo Date: Thu, 13 Apr 2017 17:08:28 +0800 Subject: [PATCH 3/3] update comment of scripts --- src/canvas/config/config.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/canvas/config/config.js b/src/canvas/config/config.js index 95aeb7a80..ffd5c5c4c 100644 --- a/src/canvas/config/config.js +++ b/src/canvas/config/config.js @@ -6,7 +6,10 @@ define(function () { // Coming soon rulers: false, - // append scripts in head + /* + * append scripts in head of iframe before renderBody content + * need to manually maintain the same scripts in cms's render template + */ scripts: [] };