-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
145 lines (112 loc) · 3.27 KB
/
index.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
function evalJS(s) {
let ret = ["let __R = {}"];
let tpl = "if (typeof @ !== 'undefined') __R['@'] = @";
s.replace(/^\s*(?:var|let|const)\s+(\w+)/gm, function(_, v) {
ret.push(tpl.replace(/@/g, v))
});
ret.push("return __R");
ret = ret.join(';');
try {
let f = new Function(s + ';' + ret);
return [f(), 'js']
} catch (e) {
return [e, 'error'];
}
}
window.onload = function () {
function evalIt(s) {
s = s.trim();
if (!s)
return ['', 'null'];
try {
return [JSON.parse(s), 'json'];
} catch (e) {
}
;
return evalJS(s);
}
function showError(e) {
d3.select("#hint").classed("error", true).text(e ? e.message : '');
}
function plot() {
d3.select("#hint").text("");
d3.select("#frame").classed("has-frame", false);
d3.select("#main").classed("has-frame", false);
var txt = code.getValue().trim(),
res = evalIt(txt);
if (res[1] === 'null') {
return;
}
if (res[1] === 'error') {
showError(res[0]);
return;
}
if (res[1] === 'json') {
arrows.plot(res[0], {}, "JSON");
}
if (res[1] === 'js') {
for (var v in res[0]) {
arrows.plot(res[0][v], {withProto: true, withFunctions: true}, v, 0);
}
}
}
var timer = 0;
function updateCode() {
clearTimeout(timer);
timer = setTimeout(function () {
showError(null);
var res = evalIt(code.getValue().trim());
switch (res[1]) {
case "js":
case "json":
code.setOption("mode", "javascript");
break;
case "html":
code.setOption("mode", "htmlmixed");
break;
case "error":
showError(res[0]);
}
}, 400);
}
var code = CodeMirror.fromTextArea(document.getElementById("code"), {
lineNumbers: false,
mode: "htmlmixed",
theme: "blackboard",
scrollbarStyle: "simple",
tabSize: 4
});
code.on("change", updateCode);
updateCode();
d3.select("#toggle").on("click", function () {
var p = d3.select("body");
p.classed("panel-closed", !p.classed("panel-closed"));
});
d3.select("#btn_plot").on("click", plot);
d3.select("#btn_png").on("click", function () {
arrows.asPNG();
});
d3.select("#btn_clear").on("click", function () {
arrows.clear();
});
var pinned = false;
d3.select("#btn_pin").on("click", function () {
if (pinned) {
arrows.unpinAll();
} else {
arrows.pinAll();
}
pinned = !pinned;
d3.select("#btn_pin").text(pinned ? "unpin all" : "pin all");
});
d3.selectAll("input").on("input", function () {
var v = this.value;
if (this.type === "number")
v = parseFloat(v);
arrows.option(this.name, v);
});
d3.selectAll("input").each(function () {
if (arrows.option(this.name))
this.value = arrows.option(this.name);
});
}