-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwidget.js
109 lines (97 loc) · 2.65 KB
/
widget.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
var widgets = require("@jupyter-widgets/base");
var idomClientReact = require("idom-client-react");
var _ = require("lodash");
var IdomModel = widgets.DOMWidgetModel.extend({
defaults: _.extend(widgets.DOMWidgetModel.prototype.defaults(), {
_model_name: "IdomModel",
_view_name: "IdomView",
_model_module: "idom-client-jupyter",
_view_module: "idom-client-jupyter",
_model_module_version: "0.4.0",
_view_module_version: "0.4.0",
}),
});
var _nextViewID = { id: 0 };
const jupyterServerBaseUrl = (() => {
const jupyterConfig = document.getElementById("jupyter-config-data");
if (jupyterConfig) {
return JSON.parse(jupyterConfig.text)["baseUrl"];
}
return document.getElementsByTagName("body")[0].getAttribute("data-base-url");
})();
class IdomView extends widgets.DOMWidgetView {
constructor(options) {
super(options);
this.render = this.render.bind(this);
this.remove = this.remove.bind(this);
}
render() {
this.viewID = _nextViewID.id;
_nextViewID.id++;
var saveUpdateHook = (updateHook) => {
this.model.on("msg:custom", (msg, buffers) => {
if (msg.viewID == this.viewID) {
updateHook(...msg.data);
}
});
this.send({
type: "client-ready",
viewID: this.viewID,
data: null,
});
};
var sendEvent = (event) => {
this.send({
type: "dom-event",
viewID: this.viewID,
data: event,
});
};
const importSourceBaseUrl = concatAndResolveUrl(
this.model.attributes._jupyter_server_base_url || jupyterServerBaseUrl,
"_idom_web_modules"
);
var loadImportSource = (source, sourceType) => {
return import( /* webpackIgnore: true */
sourceType == "NAME" ? `${importSourceBaseUrl}/${source}` : source
);
};
idomClientReact.mountLayout(this.el, {
saveUpdateHook,
sendEvent,
loadImportSource,
});
}
remove() {
this.send({ type: "client-removed", viewID: this.viewID });
return super.remove();
}
}
function concatAndResolveUrl(url, concat) {
var url1 = (url.endsWith("/") ? url.slice(0, -1) : url).split("/");
var url2 = concat.split("/");
var url3 = [];
for (var i = 0, l = url1.length; i < l; i++) {
if (url1[i] == "..") {
url3.pop();
} else if (url1[i] == ".") {
continue;
} else {
url3.push(url1[i]);
}
}
for (var i = 0, l = url2.length; i < l; i++) {
if (url2[i] == "..") {
url3.pop();
} else if (url2[i] == ".") {
continue;
} else {
url3.push(url2[i]);
}
}
return url3.join("/");
}
module.exports = {
IdomModel: IdomModel,
IdomView: IdomView,
};