-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.js
54 lines (47 loc) · 1.4 KB
/
main.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
var container = document.querySelector('#view'),
loader = vega.loader(),
renderType = 'svg',
runtime = null,
spec = null,
view = null,
current = null;
var select = document.querySelector('#specs');
select.addEventListener('change', function() {
load(select.options[select.selectedIndex].value);
});
var render = document.querySelector('#render');
render.addEventListener('change', function() {
renderType = render.options[render.selectedIndex].value;
if (current) load(current);
});
loader.load( 'specs.json')
.then(function(data) {
// load manifest of test specifications
JSON.parse(data).forEach(function(name) {
var opt = document.createElement('option');
opt.setAttribute('value', name);
opt.textContent = name;
select.appendChild(opt);
});
})
.then(function() {
// if query string is present, try to load spec
if (location.search) load( location.search.slice(1));
});
function load(name) {
if (!name) {
if (view) current = null, container.innerHTML = '';
return;
}
// update select widget state
select.selectedIndex = 0;
for (var i=0, n=select.options.length; i<n; ++i) {
if (select.options[i].value === name) {
select.selectedIndex = i; break;
}
}
vegaEmbed('#view', './spec/' + name, {renderer: renderType}).then(function(res) {
view = res.view;
current = name;
}).catch( console.error );
}