Commit 6a2e4922 authored by Roque's avatar Roque

erp5_notebook: JSMD viewer refactoring

See merge request nexedi/erp5-bin!8
parents c510cfd6 ff372f17
......@@ -11,8 +11,10 @@
<link rel="stylesheet" type="text/css" href="gadget_jsmd_eval.css" />
<script src="rsvp.js" type="text/javascript"></script>
<script src="renderjs.js" type="text/javascript"></script>
<script src="marked.js" type="text/javascript"></script>
<script src="iodide_utils.js" type="text/javascript"></script>
<script src="gadget_global.js" type="text/javascript"></script>
<script src="gadget_jsmd_eval.js" type="text/javascript"></script>
</head>
......
/*global window, console, RSVP, document, URL, eval, XMLHttpRequest, marked, WebAssembly, loadSharedlib */
/*global window, document, rJS, RSVP, XMLHttpRequest, WebAssembly, loadSharedlib, marked, console */
/*jslint nomen: true, indent: 2, maxerr: 3 */
(function (window) {
(function (window, document, rJS, RSVP, XMLHttpRequest, WebAssembly, loadSharedlib, marked, console) {
"use strict";
var IODide = function createIODide() {
......@@ -532,15 +532,19 @@
};
}
rJS(window)
document.addEventListener('DOMContentLoaded', function () {
.declareMethod("render", function (options) {
return this.changeState(options);
})
var jsmd = document.querySelector('[type="text/x-jsmd"]').textContent,
.onStateChange(function (options) {
var gadget = this,
jsmd = options.jsmd_code,
cell_list = parseJSMDCellList(jsmd),
len = cell_list.length,
i,
queue = new RSVP.Queue();
for (i = 0; i < len; i += 1) {
queue.push(deferCellExecution(cell_list[i]));
}
......@@ -552,11 +556,7 @@
var pre = document.createElement('pre');
pre.textContent = error;
document.body.appendChild(pre);
})
.push(function () {
window.parent.postMessage("jsmd_eval_done", window.origin);
});
});
}, false);
}(window));
\ No newline at end of file
}(window, document, rJS, RSVP, XMLHttpRequest, WebAssembly, loadSharedlib, marked, console));
\ No newline at end of file
......@@ -15,6 +15,9 @@
</head>
<body>
<div></div>
<div data-gadget-url="gadget_jsmd_eval.html"
data-gadget-scope="jsmd_eval"
data-gadget-sandbox="iframe">
</div>
</body>
</html>
/*global window, rJS, console, RSVP, jIO, DOMParser, Blob, document,
URL, loopEventListener */
/*jslint nomen: true, indent: 2, maxerr: 3 */
(function (window, rJS, RSVP, jIO, DOMParser, document, URL,
loopEventListener) {
/*global window, rJS, RSVP, document, loopEventListener, console */
/*jslint nomen: true, indent: 2, maxerr: 30 */
(function (window, rJS, RSVP, document, loopEventListener, console) {
"use strict";
function fetchHTML(url, base_url) {
url = new URL(url, base_url).href;
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax({url: url});
})
.push(function (evt) {
// Insert a "base" element, in order to resolve all relative links
// which could get broken with a data url
var doc = (new DOMParser()).parseFromString(evt.target.responseText,
'text/html'),
base = doc.createElement('base');
base.href = url;
doc.head.insertBefore(base, doc.head.firstChild);
return doc;
});
}
rJS(window)
.ready(function (gadget) {
gadget.property_dict = {};
})
/////////////////////////////////////////////////////////////////
// declared methods
/////////////////////////////////////////////////////////////////
.declareMethod("render", function (options) {
return this.changeState(options);
})
.onStateChange(function () {
// Reset everything if something change
var gadget = this,
base_url = document.location.toString(),
doc;
var gadget = this;
gadget.property_dict.deferred = RSVP.defer();
return fetchHTML("gadget_jsmd_eval.html", base_url)
.push(function (result) {
doc = result;
// Insert text
doc.body.textContent = '';
// Insert the JSMD value inside the HTML
var script = document.createElement('script'),
iframe = document.createElement("iframe"),
loading_spinner = document.createElement("div");
script.setAttribute('type', 'text/x-jsmd');
script.setAttribute('id', 'jsmd-source');
script.textContent = gadget.state.value;
doc.head.appendChild(script);
return gadget.getDeclaredGadget('jsmd_eval')
.push(function (eval_gadget) {
var loading_spinner = document.createElement("div");
loading_spinner.setAttribute('id', 'js-spinner');
loading_spinner.classList.add("nb-ui-icon-spinner");
loading_spinner.classList.add("nb-ui-btn-icon-notext");
loading_spinner.classList.add("nb-loader");
loading_spinner.innerHTML = "Loading notebook...";
/*
blob = new Blob([doc.documentElement.outerHTML],
{type: "text/html;charset=UTF-8"});
return jIO.util.readBlobAsDataURL(blob);
})
.push(function (evt) {
*/
// XXX Insecure
iframe.setAttribute("sandbox", "allow-scripts allow-same-origin");
// iframe.setAttribute("csp", "default-src *; script-src * 'unsafe-inline';");
// iframe.setAttribute("src", evt.target.result);
iframe.setAttribute('id', 'jsmd_eval_notebook');
iframe.setAttribute('name', 'jsmd_eval_notebook');
iframe.setAttribute("srcdoc", doc.documentElement.outerHTML);
gadget.element.innerHTML = iframe.outerHTML;
gadget.element.prepend(loading_spinner);
return eval_gadget.render({'jsmd_code' : gadget.state.value});
})
.push(function () {
gadget.listenResize();
gadget.listenMessage();
console.log("JSMD Eval Done.");
document.querySelector('[id="js-spinner"]').remove();
return gadget.property_dict.deferred.resolve();
});
})
......@@ -102,20 +51,6 @@
});
})
.declareJob('listenMessage', function () {
var gadget = this,
dict = gadget.property_dict;
return loopEventListener(window, 'message', false, function (event) {
//only handle jsmd eval child messages
if ((event.origin !== window.origin) || (event.data !== "jsmd_eval_done"))
return;
console.log("JSMD Eval Done.");
document.querySelector('[id="js-spinner"]').remove();
return dict.deferred.resolve();
}
);
})
.declareJob('listenResize', function () {
var gadget = this;
function resize() {
......@@ -127,4 +62,4 @@
return loopEventListener(window, 'resize', false, resize);
});
}(window, rJS, RSVP, jIO, DOMParser, document, URL, loopEventListener));
\ No newline at end of file
}(window, rJS, RSVP, document, loopEventListener, console));
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment