This repository has been archived by the owner on Oct 18, 2024. It is now read-only.
forked from AutonomyLab/rosdash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrosdash.js
2017 lines (1934 loc) · 49.1 KB
/
rosdash.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var ROSDASH = new Object();
///////////////////////////////////// constant parameters
// the status of development, i.e. devel, stable
ROSDASH.develStatus = "devel";
// the parameters from url, executes immediately
ROSDASH.queryString = function () {
// This function is anonymous, is executed immediately and
// the return value is assigned to QueryString!
var query_string = {};
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
// If first entry with this name
if (typeof query_string[pair[0]] === "undefined") {
query_string[pair[0]] = pair[1];
// If second entry with this name
} else if (typeof query_string[pair[0]] === "string") {
var arr = [ query_string[pair[0]], pair[1] ];
query_string[pair[0]] = arr;
// If third or later entry with this name
} else {
query_string[pair[0]].push(pair[1]);
}
}
return query_string;
} ();
///////////////////////////////////// events
// event emitter
ROSDASH.ee = ("EventEmitter" in window) ? new EventEmitter() : undefined;
// event when the page is loaded
$(document).ready(function() {
// event when the document has been fully loaded
ROSDASH.ee.emitEvent('pageReady');
});
///////////////////////////////////// dashboard
// load the entire dashboard
ROSDASH.startDash = function ()
{
// be careful with the order
ROSDASH.loadDash();
ROSDASH.initJson();
ROSDASH.initToolbar("toolbar");
ROSDASH.initSidebar("sidebar");
}
// load an empty dashboard
ROSDASH.loadDash = function ()
{
$("#panel").empty();
// create empty dashboard editor
$("#panel").sDashboard({
dashboardData : [],
disableSelection : ROSDASH.dashConf.disable_selection
});
ROSDASH.dashBindEvent("panel");
// show it
$("#panel").css("visibility", "visible");
// fade in
$("#panel").fadeIn("slow");
$("#cy").empty();
// create an empty cytoscape diagram
$('#cy').cytoscape({
showOverlay: false,
style: ROSDASH.defaultStyle,
elements: {nodes: new Array(), edges: new Array()},
ready: function ()
{
window.cy = this;
}
});
}
// show view or not
ROSDASH.showView = function (from, to)
{
// if the same view
if (to == from)
{
return;
}
// only editor and diagram have sidebar
if ("panel" == to || "editor" == to || "diagram" == to)
{
$("#canvas").css("left", "160px");
$("#sidebar").show("slide", { direction: "left" }, 500);
} else
{
// remove sidebar
$("#sidebar").hide("slide", { direction: "left" }, 500);
$("#canvas").css("left", "0px");
}
var from_canvas;
// remove the original view
switch (from)
{
case "panel":
from_canvas = "panel";
break;
case "editor":
from_canvas = "editor";
break;
case "diagram":
from_canvas = "cy";
break;
case "json":
from_canvas = "json";
break;
case "docs":
from_canvas = "docs";
break;
default:
break;
}
if (undefined !== from_canvas)
{
// hide it
$("#" + from_canvas).css("visibility", "hidden");
// fade out
$("#" + from_canvas).fadeOut("slow");
}
var to_canvas;
// show the new view
switch (to)
{
case "panel":
to_canvas = "panel";
ROSDASH.resetPanelToolbar();
break;
case "editor":
to_canvas = "editor";
ROSDASH.resetEditorToolbar();
break;
case "diagram":
to_canvas = "cy";
ROSDASH.resetDiagramToolbar();
break;
case "json":
to_canvas = "json";
ROSDASH.resetJsonToolbar();
ROSDASH.loadJsonEditor(ROSDASH.getDashJson());
break;
case "docs":
to_canvas = "docs";
ROSDASH.resetDefaultToolbar();
break;
default:
to_canvas = undefined;
console.error("show wrong view", from, to);
break;
}
if (undefined !== to_canvas)
{
// show it
$("#" + to_canvas).css("visibility", "inherit");
// fade in
$("#" + to_canvas).fadeIn("slow");
}
// switch to new view type
ROSDASH.dashConf.view = to;
// init sidebar form
ROSDASH.initForm();
}
// if dashboard has been changed
ROSDASH.dashChanged = false;
// create a json representing a dashboard
ROSDASH.getDashJson = function ()
{
var json = ROSDASH.dashConf;
json.version = ROSDASH.version;
json.date = new Date().toString();
// diagram blocks
json.block = new Object();
// diagram edges
json.edge = new Array();
if ("cy" in window && typeof window.cy.fit == "function")
{
// don't save popups into file
ROSDASH.removeAllPopup();
// add all edges into json
window.cy.edges().each(function (i, ele)
{
var e = {
source: ele.source().id(),
target: ele.target().id()
};
json.edge.push(e);
});
}
// add all blocks into json
for (var i in ROSDASH.blocks)
{
json.block[i] = ROSDASH.blocks[i];
}
return json;
}
// dashboard configuration
ROSDASH.dashConf = {
// basic
name: "index",
discrip: "",
view: "panel",
// ros
host: "",
port: "",
// dependencies
require: [],
//@deprecated
js: [],
css: [],
json: [],
// panel
disable_selection: true,
run_msec: 200,
widget_width: 400,
widget_height: 230,
header_height: 16,
content_height: 180
};
// if started loading json files specified by dash file
ROSDASH.loadDashJson = false;
// set config, and load required json
ROSDASH.setDashConf = function (conf)
{
// set all config
for (var i in conf)
{
if (i in ROSDASH.dashConf)
{
if ("version" == i && ROSDASH.dashConf.version != conf.version)
{
console.error("configure version conflicts", conf.version, ROSDASH.dashConf.version);
continue;
}
if ("panel_name" == i && ROSDASH.dashConf.name != conf.name)
{
console.error("panel_name conflicts", conf.name, ROSDASH.dashConf.name);
continue;
}
ROSDASH.dashConf[i] = conf[i];
}
}
ROSDASH.checkDashConfValid(ROSDASH.dashConf);
// load json specified by dash config
for (var i in ROSDASH.dashConf.json)
{
if (undefined === ROSDASH.dashConf.json[i] || "" == ROSDASH.dashConf.json[i] || " " == ROSDASH.dashConf.json[i])
{
continue;
}
ROSDASH.loadJson(ROSDASH.dashConf.json[i], function (json)
{
ROSDASH.loadWidgetDef(json.widgets);
});
}
ROSDASH.loadDashJson = true;
}
// check if dashboard config is valid or not
ROSDASH.checkDashConfValid = function (conf)
{
// run speed too fast
if (conf.run_msec < 1)
{
console.warning("run_msec is too low: ", conf.run_msec);
conf.run_msec = 100;
}
// set default port
if (undefined === conf.port || "" == conf.port || " " == conf.port)
{
conf.port = "9090";
}
}
///////////////////////////////////// panel
// load widgets from json
ROSDASH.loadPanel = function (blocks)
{
if (undefined === blocks)
{
return;
}
// create an empty panel
$("#panel").empty();
$("#panel").sDashboard({
dashboardData : [],
disableSelection : ROSDASH.dashConf.disable_selection
});
ROSDASH.dashBindEvent("panel");
var widgets = new Array();
// create object for each widget config
for (var i in blocks)
{
if (("has_panel" in blocks[i]) && (true == blocks[i].has_panel || "true" == blocks[i].has_panel))
{
var order = parseInt(blocks[i].order);
widgets[order] = {
"widgetTitle": blocks[i].name,
"widgetId": blocks[i].id,
"number": blocks[i].number,
"widgetType": blocks[i].type,
"pos": order,
"width": blocks[i].width,
"height": blocks[i].height,
"header_height": blocks[i].header_height,
"content_height": blocks[i].content_height
};
}
}
// add widget in reverse order
for (var i = widgets.length - 1; i >= 0; -- i)
{
if (! (i in widgets))
{
continue;
}
ROSDASH.addWidget(widgets[i]);
}
ROSDASH.ee.emitEvent("panelReady");
}
// start to run widgets
ROSDASH.runPanel = function ()
{
ROSDASH.runStatus = "initialized";
ROSDASH.ee.emitEvent("runBegin");
ROSDASH.runWidgets();
}
// bind callback functions
ROSDASH.dashBindEvent = function (canvas)
{
$("#" + canvas).bind("sdashboardorderchanged", function(e, data)
{
ROSDASH.moveWidget(data.sortedDefinitions);
});
$("#" + canvas).bind("sdashboardheaderclicked", ROSDASH.selectWidgetCallback);
$("#" + canvas).bind("sdashboardwidgetmaximized", ROSDASH.widgetMaxCallback);
$("#" + canvas).bind("sdashboardwidgetminimized", ROSDASH.widgetMaxCallback);
$("#" + canvas).bind("sdashboardwidgetadded", ROSDASH.widgetAddCallback);
$("#" + canvas).bind("sdashboardwidgetremoved", function(e, data)
{
ROSDASH.removeWidget(data.widgetDefinition.widgetId);
});
$("#" + canvas).bind("sdashboardwidgetset", ROSDASH.widgetSetCallback);
$("#" + canvas).bind("sdashboardheaderset", ROSDASH.headerSetCallback);
}
ROSDASH.widgetMaxCallback = function (e, data)
{}
ROSDASH.widgetAddCallback = function (e, data)
{}
ROSDASH.widgetSetCallback = function (e, data)
{}
ROSDASH.headerSetCallback = function (e, data)
{}
///////////////////////////////////// jsonEditor
// the json in jsonEditor
ROSDASH.jsonEditorJson = {
"string": "test",
"number": 5,
"array": [1, 2, 3],
"object": {
"property": "test1",
"subobj": {
"arr": ["test2", "test3"],
"numero": 1
}
}
};
// load it
ROSDASH.loadJsonEditor = function (src)
{
ROSDASH.jsonEditorJson = src;
// callback for json text
$('#jsontext').change(function () {
var val = $('#jsontext').val();
if (val)
{
try {
ROSDASH.jsonEditorJson = JSON.parse(val);
}
catch (e) {
console.error('Error in parsing json', e);
return;
}
// update jsoneditor
$('#jsoneditor').jsonEditor(ROSDASH.jsonEditorJson, { change: function (data) {
ROSDASH.jsonEditorJson = data;
// update jsontext
$('#jsontext').val(JSON.stringify(json));
// reload everything
//ROSDASH.loadEditor(data.block);
ROSDASH.loadDiagram(data);
}, propertyclick: null });
// reload everything
//ROSDASH.loadEditor(ROSDASH.jsonEditorJson.block);
ROSDASH.loadDiagram(ROSDASH.jsonEditorJson);
} else
{
console.error("invalid json", val);
return;
}
});
// callback for expander button
$('#expander').click(function () {
var editor = $('#jsoneditor');
editor.toggleClass('expanded');
$(this).text(editor.hasClass('expanded') ? 'Collapse' : 'Expand all');
});
// set json to jsoneditor and text
$('#jsontext').val(JSON.stringify(ROSDASH.jsonEditorJson));
$('#jsoneditor').jsonEditor(ROSDASH.jsonEditorJson, { change: function (data) {
ROSDASH.jsonEditorJson = data;
// update jsontext
$('#jsontext').val(JSON.stringify(json));
// reload everything
//ROSDASH.loadEditor(data.block);
ROSDASH.loadDiagram(data);
}, propertyclick: null });
}
///////////////////////////////////// block definitions
// json file names for blocks
ROSDASH.blockFiles = ["blocks.json"];
// block definitions
ROSDASH.blockDef = new Object();
// block lists for sidebar
ROSDASH.blockList = new Object();
// widget lists for sidebar
ROSDASH.widgetList = new Object();
// load widget json from files
ROSDASH.loadBlockFiles = function (files)
{
// load from widget definition json
for (var i in files)
{
ROSDASH.loadJson(files[i], function (json)
{
ROSDASH.loadWidgetDef(json.widgets);
});
}
}
// load widgets from json
ROSDASH.loadWidgetDef = function (data)
{
// for each widget json
for (var k in data)
{
// wrong format
if (! ("type" in data[k]))
{
continue;
}
// save to ROSDASH.blockDef
ROSDASH.blockDef[data[k].type] = data[k];
// save to list for sidebar
ROSDASH.loadBlockList(data[k]);
}
}
// set to sidebar lists
ROSDASH.loadBlockList = function (json)
{
// alias for block list for sidebar
var list = ROSDASH.blockList;
// alias for widget list for panel sidebar
var list2 = ROSDASH.widgetList;
// add category name to list
for (var m in json.category)
{
// alias
var c = json.category[m];
// goto this category directory
if (c in list)
{
list = list[c];
} else
{
// add to block category directory
list[c] = new Object();
list = list[c];
}
// add to widget category directory
if (json.has_panel)
{
if (c in list2)
{
list2 = list2[c];
} else
{
list2[c] = new Object();
list2 = list2[c];
}
}
}
// add definition to block list
if (! ("_" in list))
{
list["_"] = new Array();
}
list["_"].push(json.type);
// add definition to widget list
if (json.has_panel)
{
if (! ("_" in list2))
{
list2["_"] = new Array();
}
list2["_"].push(json.type);
}
}
// if widget name valid in widget definition list
ROSDASH.checkBlockTypeValid = function (name)
{
return (name in ROSDASH.blockDef) && ("class_name" in ROSDASH.blockDef[name]);
}
///////////////////////////////////// msg type definitions
// file path list for msg jsons
ROSDASH.msgFiles = ["msgs.json"];
// msg list for sidebar
ROSDASH.msgList = new Object();
// msg definitions
ROSDASH.msgs = new Object();
// load message type definitions from json files
ROSDASH.loadMsgJson = function ()
{
for (var i in ROSDASH.msgFiles)
{
ROSDASH.loadJson(ROSDASH.msgFiles[i]);
}
}
// parse message for sidebar list
ROSDASH.loadMsgDef = function ()
{
if (undefined === ROSDASH.msgList)
{
ROSDASH.msgList = new Object();
}
if (undefined === ROSDASH.msgList["_"])
{
ROSDASH.msgList["_"] = new Array();
}
// add to msg list
var list = ROSDASH.msgList["_"];
for (var i in ROSDASH.msgFiles)
{
var data = ROSDASH.jsonLoadList[ROSDASH.msgFiles[i]].data.msgs;
for (var j in data)
{
if (undefined != data[j].name)
{
// add to definition list
ROSDASH.msgs[data[j].name] = data[j];
// add to msg list for sidebar
list.push(data[j].name);
}
}
}
ROSDASH.traverseMsgType();
}
//@todo msg type relations
ROSDASH.msgTypes = new Object();
ROSDASH.traverseMsgType = function ()
{
for (var i in ROSDASH.msgs)
{
if (! (i in ROSDASH.msgTypes))
{
ROSDASH.msgTypes[i] = "msgs";
}
if (typeof ROSDASH.msgs[i].definition != "array")
{
continue;
}
for (var j in ROSDASH.msgs[i].definition)
{
if (! ("type" in ROSDASH.msgs[i].definition[j]))
{
continue;
}
ROSDASH.msgTypes[ROSDASH.msgs[i].definition[j].type] = "def";
}
}
}
// the default value for a msg
ROSDASH.getMsgDefaultValue = function (name)
{
// not exist
if (! (name in ROSDASH.msgs) || ! ("definition" in ROSDASH.msgs[name]))
{
console.error("getMsgDefaultValue error", name);
return null;
}
var value;
// if it is a simple value without sub-msgs
if (1 == ROSDASH.msgs[name].definition.length)
{
switch (ROSDASH.msgs[name].definition[0].type)
{
case "int32":
case "int64":
value = 0;
break;
case "float32":
case "float64":
value = 0.0;
break;
case "string":
default:
value = "";
break;
}
} else
{
// an json value
value = new Object();
for (var i in ROSDASH.msgs[name].definition)
{
value[ROSDASH.msgs[name].definition[i].name] = "";
}
}
return value;
}
//@deprecated get message type definitions from ROSDASH.msg_json
ROSDASH.getMsgDef = function (name)
{
for (var i in ROSDASH.msgFiles)
{
var json = ROSDASH.jsonLoadList[ROSDASH.msgFiles[i]].data;
for (var j in json)
{
var json2 = json[j];
if (undefined === json2.name)
{
for (var k in json2)
{
if (json2[k].name == name)
{
return json2[k];
}
}
} else
{
if (json2.name == name)
{
return json2;
}
}
}
}
return undefined;
}
// check if it is an existing msg type
ROSDASH.checkMsgTypeValid = function (name)
{
return (undefined !== ROSDASH.getMsgDef(name));
}
///////////////////////////////////// load json
// the data list from json files
ROSDASH.jsonLoadList = new Object();
ROSDASH.frontpageJson = 'data/network.json';
// init loading msg type and widget definitions from json files
ROSDASH.initJson = function ()
{
ROSDASH.loadMsgJson();
ROSDASH.loadBlockFiles(ROSDASH.blockFiles);
// load the frontpage from json file
ROSDASH.loadJson(ROSDASH.frontpageJson, function (json)
{
//@todo
//ROSDASH.connectROS(ROSDASH.dashConf.host, ROSDASH.dashConf.port);
// load diagram
ROSDASH.loadDiagram(json);
// parse diagrma for loading panel
ROSDASH.parseDiagram(json);
ROSDASH.loadPanel(ROSDASH.blocks);
});
ROSDASH.waitJson();
}
// status if all json loading are ready
ROSDASH.jsonReady = false;
// wait when loading jsons
ROSDASH.waitJson = function ()
{
//@deprecated if dash conf is loaded, load specified jsons. must be executed before examine jsonLoadList
var conf_path = "data/index/conf.json";
if (! ROSDASH.loadDashJson && (conf_path in ROSDASH.jsonLoadList) && 2 == ROSDASH.jsonLoadList[conf_path].status)
{
ROSDASH.setDashConf(ROSDASH.jsonLoadList[conf_path].data);
}
// if loading finishes or not
var flag = true;
for (var i in ROSDASH.jsonLoadList)
{
// if loading fails
if (ROSDASH.jsonLoadList[i].status < 0)
{
// don't reload, just ignore it
}
// if loading is unsuccessful or not finishes
else if (ROSDASH.jsonLoadList[i].status < 2 && ROSDASH.jsonLoadList[i].status >= 0)
{
flag = false;
// if returned but not succeed, read again
if (1 == ROSDASH.jsonLoadList[i].status)
{
console.warn("load file again", i);
ROSDASH.loadJson(i);
}
break;
}
}
// if not ready
if (! flag)
{
// wait again
setTimeout(ROSDASH.waitJson, 300);
} else
{
// emit a event for json ready
ROSDASH.ee.emitEvent("jsonReady");
ROSDASH.jsonReady = true;
// parse msgs after loading json
ROSDASH.loadMsgDef();
}
}
///////////////////////////////////// widget requirements
// a list of requirements, i.e. js, css, json, etc.
ROSDASH.loadList = new Object();
// check all statically loaded scripts
ROSDASH.checkScripts = function ()
{
$("script").each(function (key, value) {
var src = $(this).attr("src");
ROSDASH.loadList[src] = new Object();
ROSDASH.loadList[src].data = value;
ROSDASH.loadList[src].type = undefined;
ROSDASH.loadList[src].status = 2;
});
}
ROSDASH.ee.addListener("pageReady", ROSDASH.checkScripts);
// load required files, i.e. js, css, etc.
ROSDASH.loadRequired = function (i)
{
if (undefined === ROSDASH.blocks[i])
{
setTimeout(ROSDASH.loadRequired[i], 100);
return;
}
if (undefined === ROSDASH.blocks[i].require)
{
ROSDASH.blocks[i].require = new Object();
}
// load required js
if (undefined !== ROSDASH.blockDef[ROSDASH.connection[i].block.type] && undefined !== ROSDASH.blockDef[ROSDASH.connection[i].block.type].js)
{
for (var j in ROSDASH.blockDef[ROSDASH.connection[i].block.type].js)
{
ROSDASH.blocks[i].require[ ROSDASH.blockDef[ROSDASH.connection[i].block.type].js[j] ] = false;
try {
ROSDASH.loadJs(ROSDASH.blockDef[ROSDASH.connection[i].block.type].js[j], function (data)
{
ROSDASH.blocks[i].require[ ROSDASH.blockDef[ROSDASH.connection[i].block.type].js[j] ] = true;
});
} catch (err)
{
ROSDASH.connection[i].error = true;
console.error("loading js required by widget error:", ROSDASH.connection[i].block.type, ROSDASH.blockDef[ROSDASH.connection[i].block.type].js[j], err.message, err.stack);
}
}
}
// load required css
if (ROSDASH.blockDef[ROSDASH.connection[i].block.type] && undefined !== ROSDASH.blockDef[ROSDASH.connection[i].block.type].css)
{
for (var j in ROSDASH.blockDef[ROSDASH.connection[i].block.type].css)
{
ROSDASH.blocks[i].require[ ROSDASH.blockDef[ROSDASH.connection[i].block.type].js[j] ] = false;
try {
ROSDASH.loadCss(ROSDASH.blockDef[ROSDASH.connection[i].block.type].js[j], function (data)
{
ROSDASH.blocks[i].require[ ROSDASH.blockDef[ROSDASH.connection[i].block.type].js[j] ] = true;
});
} catch (err)
{
ROSDASH.connection[i].error = true;
console.error("loading css required by widget error:", ROSDASH.connection[i].block.type, ROSDASH.blockDef[ROSDASH.connection[i].block.type].css[j], err.message, err.stack);
}
}
}
}
// wait for loading js
ROSDASH.waitLoadJs = function ()
{
// if loading finishes or not
var flag = true;
for (var i in ROSDASH.loadList)
{
// if not loaded
if (ROSDASH.loadList[i].status < 2 && ROSDASH.loadList[i].status != 0)
{
ROSDASH.loadJs(i);
flag = false;
}
}
if (! flag)
{
// wait for loading again
console.log("wait for loading js");
setTimeout(ROSDASH.waitLoadJs, 300);
return;
} else
{
//@todo successfully loaded
//ROSDASH.instantiateWidgets();
}
}
// load json and register them
ROSDASH.loadJson = function (file, func)
{
if (undefined === file || "" == file)
{
return;
}
// init status
if (! (file in ROSDASH.jsonLoadList))
{
ROSDASH.jsonLoadList[file] = new Object();
}
if (undefined === ROSDASH.jsonLoadList[file].status)
{
ROSDASH.jsonLoadList[file].status = 0;
ROSDASH.jsonLoadList[file].type = "json";
}
// do not load again
if (ROSDASH.jsonLoadList[file].status >= 2)
{
return;
}
$.getJSON(file, function (data, status, xhr)
{
ROSDASH.jsonLoadList[file].data = data;
// if successful, status = 1 + 1
ROSDASH.jsonLoadList[file].status = 1;
console.log("load", file);
if (typeof(func) == "function")
{
return func(data);
}
})
.fail(function (jqXHR, textStatus) {
console.error("fail to load", file, jqXHR, textStatus);
ROSDASH.jsonLoadList[file].status = -10;
})
.always(function () {
// if not successful, status = 1
++ ROSDASH.jsonLoadList[file].status;
});
}
// load js file required by widgets
ROSDASH.loadJs = function (file, func)
{
if (undefined === file || "" == file)
{
return;
}
if (undefined === ROSDASH.loadList[file])
{
ROSDASH.loadList[file] = new Object();
}
if (undefined === ROSDASH.loadList[file].status)
{
ROSDASH.loadList[file].status = 0;
ROSDASH.loadList[file].type = "js";
}
// do not load again
if (ROSDASH.loadList[file].status >= 2)
{
return;
}
$.getScript(file, function (data, status, jqxhr)
{
ROSDASH.loadList[file].data = data;
ROSDASH.loadList[file].status = 1;
console.log("load", file);
if (typeof(func) == "function")
{
return func(data);
}
}).fail(function (jqxhr, settings, exception)
{
ROSDASH.loadList[file].status = -10;
console.warn("fail to load", file, jqxhr, settings, exception);
}).always(function() {
++ ROSDASH.loadList[file].status;
});
}
// load css file
ROSDASH.loadCss = function (file, func)
{
if (undefined === file || "" == file)
{
return;
}
if (undefined === ROSDASH.loadList[file])
{
ROSDASH.loadList[file] = new Object();
}
if (undefined === ROSDASH.loadList[file].status)
{
ROSDASH.loadList[file].status = 0;
}
$('head').append('<link rel="stylesheet" href="' + file + '" type="text/css" />');
ROSDASH.loadList[file].data = file;
ROSDASH.loadList[file].type = "css";
ROSDASH.loadList[file].status = 2;
if (typeof(func) == "function")
{
return func(data);
}
console.log("load", file);
}
// transform from raw json into real json, i.e. "true" => true
ROSDASH.transformRawJson = function (json)
{
for (var i in json)
{
if ("true" == json[i])
{
json[i] = true;
} else if ("false" == json[i])
{
json[i] = false;
} else if ("null" == json[i])
{
json[i] = null;
} else if ("undefined" == json[i])
{
json[i] = undefined;
} else if (typeof json[i] == "object" || typeof json[i] == "array")
{
json[i] = ROSDASH.transformRawJson(json[i]);
}
}
return json;
}
// download json in a new window
ROSDASH.downloadJson = function (json)
{
window.open('data:text/json;charset=utf-8,' + encodeURIComponent(JSON.stringify(json)), 'Download');
}
// save data to json file in server
//@note PHP will ignore empty json part
ROSDASH.saveJson = function (data, filename)
{
$.ajax({