jupyterlab_rootjs
Version:
jupyter lab extension to support CERN ROOT JS
109 lines (108 loc) • 3.92 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const widgets_1 = require("@phosphor/widgets");
const HTML_MIME_TYPE = 'text/html';
const JS_MIME_TYPE = 'application/javascript';
exports.ROOT_LOAD_MIME_TYPE = 'application/vnd.rootjs_load.v0+json';
exports.ROOT_EXEC_MIME_TYPE = 'application/vnd.rootjs_exec.v0+json';
/**
* Load ROOTJS into the DOM
*/
class ROOTJSLoad extends widgets_1.Widget {
constructor(options) {
super();
this._load_mimetype = exports.ROOT_LOAD_MIME_TYPE;
this._script_element = document.createElement("script");
}
renderModel(model) {
console.log("LOAD!");
let data = model.data[this._load_mimetype];
console.log(data);
// load require js on first pass
// after it loads then configure for ROOTJS
let that = this;
this._script_element.onload = function () {
that._script_element = document.createElement("script");
that._script_element.textContent = data;
that.node.appendChild(that._script_element);
console.log("requireJS configured");
};
this._script_element.src = "https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.5/require.js";
this.node.appendChild(this._script_element);
return Promise.resolve();
} // renderModel
} //ROOTJSLoad
exports.ROOTJSLoad = ROOTJSLoad;
/**
* Exec HVJS in window
*/
class ROOTJSExec extends widgets_1.Widget {
constructor(options) {
super();
// for classic nb compat reasons, the payload in contained in these mime messages
// private _html_mimetype: string = HTML_MIME_TYPE
this._js_mimetype = JS_MIME_TYPE;
this._exec_mimetype = exports.ROOT_EXEC_MIME_TYPE;
this._createNodes();
this._displayed = false;
this._disposedPlot = false;
}
_createNodes() {
this._div_element = document.createElement("div");
this._script_element = document.createElement("script");
this._script_element.setAttribute('type', 'text/javascript');
}
get isDisposed() {
return this._disposedPlot === true;
}
renderModel(model) {
console.log("ROOTJS EXEC");
let metadata = model.metadata[this._exec_mimetype];
const id = metadata.id;
const cw = metadata.width;
const ch = metadata.height;
if (this._displayed) {
this._disposePlot();
this.node.removeChild(this._div_element);
this.node.removeChild(this._script_element);
this._createNodes();
}
if (id !== undefined) {
console.log("ID is : ", id);
// do we need to make the DIV?
if (HTML_MIME_TYPE in model.data) {
console.log("ROOTJS: creating DIV for canvas, id=", id);
this._div_element.id = id;
this._div_element.style.width = cw + 'px';
this._div_element.style.height = ch + 'px';
this.node.appendChild(this._div_element);
}
let data = model.data[this._js_mimetype];
if ("" != data) {
this._script_element.textContent = data;
this.node.appendChild(this._script_element);
this._displayed = true;
this._document_id = id;
}
else {
// just adding the figure
}
}
return Promise.resolve();
} // renderModel
_disposePlot() {
const id = this._document_id;
if (id !== null) {
this._document_id = null;
}
this._disposedPlot = false;
} // _disposePlot
dispose() {
if (this.isDisposed) {
return;
}
this._disposePlot();
this._disposedPlot = false;
} // dispose
} // ROOTJSExec
exports.ROOTJSExec = ROOTJSExec;