-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
69 lines (65 loc) · 2.53 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*global define, document, window */
/*jslint nomen: true */ // this causes JSLint to tolerate "_"
define(['codeMirror', 'inlet', 'd3', 'underscore', './dashboard'], function (CodeMirror, inlet, d3, _, Dashboard) {
"use strict";
function initEditor(config, dashboard, editor) {
var codeMirror = CodeMirror.fromTextArea(editor),
settingConfig = false,
invalidJSONConfig = {
"layout": {
"orientation": "vertical",
"children": [ { "name": "vis", "size": 1 } ]
},
"visualizations": {
"vis": {
"module": "vis",
"bkgColor": "red",
"lineColor": "red",
"labelText": "Invalid JSON",
"labelSize": "41pt"
}
},
"visDivCSS": {
"border-style": "solid",
"border-width": "2px"
}
};
inlet(codeMirror);
codeMirror.setOption('mode', 'javascript');
codeMirror.setSize('100%', '100%');
codeMirror.setOption('value', JSON.stringify(config, null, 2));
codeMirror.on('change', function () {
settingConfig = true;
try {
// This line will throw if parsing fails
dashboard.setConfig(JSON.parse(codeMirror.getValue()));
} catch (e) {
dashboard.setConfig(invalidJSONConfig);
}
settingConfig = false;
});
dashboard.on('configChanged', function (config) {
if (!settingConfig) {
codeMirror.setOption('value', JSON.stringify(config, null, 2));
}
});
}
function init(config, getModule, dashboardId, editorId) {
var dashboard = Dashboard.createDashboard(dashboardId, getModule),
editor = document.getElementById(editorId);
if (editor) { initEditor(config, dashboard, editor); }
dashboard.setConfig(config);
return dashboard;
}
return {
/**
* Initializes the dashboard configurator system.
* Arguments:
*
* * `config` The object parsed from the dashboard configuration JSON.
* * `editorId` The id of the textArea DOM element that should become the configuration editor.
* * `dashboardId` The id of the div DOM element that the dashboard will be injected into.
*/
init: init
};
});