-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdepgraph.js
executable file
·3720 lines (3330 loc) · 120 KB
/
depgraph.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
/************************************************************/
/** Utils **/
/************************************************************/
define('app/utils',[],function(){
var depgraphlib = {};
depgraphlib.windowOpenPost = function(data,url){
depgraphlib.windowOpenPostForm = '<form id="depgraphlibWindowOpenPostForm" method="post" action="'+url+'" target="_blank"></form>';
var existingForm = jQuery('#depgraphlibWindowOpenPostForm');
if(!existingForm.length){
existingForm = jQuery(depgraphlib.windowOpenPostForm);
jQuery('body').append(existingForm);
}
existingForm.html('');
for(param in data){
var stringdata = null;
if(typeof data[param] == 'string'){
stringdata = data[param];
}else{
stringdata = JSON.stringify(data[param]);
}
existingForm.append('<input type="hidden" name="'+param+'" value="">');
existingForm[0][param].value = stringdata;
}
document.getElementById('depgraphlibWindowOpenPostForm').submit();
};
/**
* clone an object
*/
depgraphlib.clone = function(obj) {
if (null == obj || "object" != typeof obj)
return obj;
if (obj instanceof Date) {
var copy = new Date();
copy.setTime(obj.getTime());
return copy;
}
if (obj instanceof Array) {
var copy = [];
for ( var i = 0, len = obj.length; i < len; i++) {
copy[i] = depgraphlib.clone(obj[i]);
}
return copy;
}
if (obj instanceof Object) {
var copy = {};
for ( var attr in obj) {
if (obj.hasOwnProperty(attr))
copy[attr] = depgraphlib.clone(obj[attr]);
}
return copy;
}
throw new Error("Unable to copy obj! Its type isn't supported.");
};
/**
* Set the position for a d3 selection of an SVGElement
* @param x
* @param y
*/
depgraphlib.setGroupPosition = function(node,x,y){
node.attr("transform","translate("+x+","+y+")");
};
/**
* center a node in x in reference of an other
* @param node
* @param refNode
*/
depgraphlib.center = function(node,refNode){
var refbbox = refNode.node().getBBox();
var bbox = node.node().getBBox();
node.attr('x',-bbox.width/2+refbbox.width/2);
};
/**
* get the transform value of a g svg element
* @param elt
* @returns {Object}
*/
depgraphlib.getTransformValues = function (elt){
var value = elt.attr("transform");
var pairRegex = /(\w+)\((.*?)\)/g;
var result = new Object();
var tmp;
while((tmp = pairRegex.exec(value)) != null){
var valuesRegex = /(-*\w+\.*\w*)/g;
result[tmp[1]] = [];
var values;
while((values= valuesRegex.exec(tmp[2]))!=null){
result[tmp[1]].push(parseFloat(values[1]));
}
}
return result;
};
/**
* resolve the references in a json object
* the references are su objects id starting with a refPrefix
* (eg. '@15' for a object of id 15 and using '@' as a refPrefix
* @param obj
* @param refPrefix
*/
depgraphlib.JSONresolveReferences = function (obj,refPrefix){
var refids = [];
var queue = [];
subResolveRef(obj,refPrefix,refids,queue);
for(elt in queue){
if(refids[elt] == null){
throw "This object has missing references!";
}else{
elt = refids[elt];
}
}
function subResolveRef(obj,refPrefix,refids,queue){
for(var property in obj){
if(!obj.hasOwnProperty(property)){
continue;
}
if(property == 'id'){
refids[obj[property]]=obj;
continue;
}
var type = typeof obj[property];
if(type == 'object'){
subResolveRef(obj[property],refPrefix,refids,queue);
}else if(type == 'string'){
if(obj[property].indexOf(refPrefix) == 0){
var refid = obj[property].substring(1);
if(refids[refid] != null){
//TODO resolve for uri rdf type
obj[property] = refids[refid];
subResolveRef(obj[property],refPrefix,refids,queue);
}else{
queue.push[obj[property]];
}
}
}
}
}
};
/**
* remove the unit 'px' from a string an returns the number as a float number
* @param value
* @returns
*/
depgraphlib.removeUnit = function (value){
var regex_px = /(-*)(\d+\.*\d*)px/;
var match = regex_px.exec(value);
if(match != null){
var sign = (match[1].length%2==0)?'':'-';
value = sign+match[2];
}
return parseFloat(value);
};
/**
* add multiple arguments (number or string with units) and returns their sum in a string appended by 'px'
* @returns {String}
*/
depgraphlib.addPxs = function(){
var sum = 0;
for(var i=0; i<arguments.length;i++){
var arg = depgraphlib.removeUnit(arguments[i]);
sum += parseInt(arg);
}
return sum+'px';
};
/************************************************************/
/** Colors **/
/************************************************************/
depgraphlib.rgbToHex = function(r, g, b) {
return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
};
depgraphlib.hexToRgb = function(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
};
depgraphlib.rgbToHsl = function(r, g, b){
r /= 255, g /= 255, b /= 255;
var max = Math.max(r, g, b), min = Math.min(r, g, b);
var h, s, l = (max + min) / 2;
if(max == min){
h = s = 0; // achromatic
}else{
var d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch(max){
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
return {h:h ,s: s ,l: l };
};
depgraphlib.hslToRgb = function(h, s, l){
var r, g, b;
if(s == 0){
r = g = b = l; // achromatic
}else{
function hue2rgb(p, q, t){
if(t < 0) t += 1;
if(t > 1) t -= 1;
if(t < 1/6) return p + (q - p) * 6 * t;
if(t < 1/2) return q;
if(t < 2/3) return p + (q - p) * (2/3 - t) * 6;
return p;
}
var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
var p = 2 * l - q;
r = hue2rgb(p, q, h + 1/3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1/3);
}
return {r:Math.floor(r * 255),g: Math.floor(g * 255),b: Math.floor(b * 255)};
};
return depgraphlib;
});
define('app/graphviewer',['app/utils'],function(depgraphlib){
/**
* GraphViewer.js
* This part of the library contains functions utilities to create
* a viewer frame for a the graph display integration in html dom page
*
* Author : Paul Bui-Quang
* INRIA
*/
/**
* Create a new instance of a viewer
* @param container
* @param uid
* @returns
*/
var GraphViewer = function (container,uid){
if(uid == null){
uid = Math.floor(Math.random() * 10000000000000001).toString();
}
this.allowFullScreen = true;
this.imagemode = false;
this.debugPanelActive = false;
this.fixedToolbar = false;
this._height = '600px';
this._width = "100%";
this.basemargin = 10;
this.margin = {top:100,left:0,right:0,bottom:10};
this.borders = true;
if(GraphViewer.instances[uid]){
uid += "_";
}
var uid = this.uid = uid; // uid
if(typeof container == 'string'){ // check if jquery selection or id. id must be prepend a '#'!
container = jQuery(container);
}
var container = this.container = container;
container.css('display','inline-block');
var mainpanel = this.mainpanel = this.createDiv("gv-main-panel","gv-main-panel");
container.append(mainpanel);
var chart = this.chart = this.createDiv("gv-chart","gv-chart");
mainpanel.append(chart);
var toolbar = this.toolbar = this.initToolBar();
var debugpanel = this.debugpanel = this.createDebugPanel();
this.tooltip = this.createToolTipPanel();
this.tooltip.hide();
this.altpanel = this.createDiv("gv-alt-panel","gv-alt-panel");
this.altpanelcontent = this.createDiv("alt-panel-content","alt-panel-content");
this.ajaxLoader = this.createDiv('ajax-loader','ajax-loader');
this.ajaxLoader.hide();
this.mainpanel.append(this.ajaxLoader);
this.extraDiv = this.createDiv("gv-extra","gv-extra");
this.extraDiv.hide();
this.mainpanel.append(this.extraDiv);
mainpanel.append(this.tooltip);
mainpanel.append(this.altpanel);
this.altpanel.append(this.altpanelcontent);
GraphViewer.instances[uid]=this;
this.callbacks = new Object();
this.callbacks.fullscreen = new Object();
this.callbacks.fullscreen.oncomplete = [];
this.callbacks.fullscreen.onclose = [];
this.callbacks.showaltpanel = [];
this.callbacks.hidealtpanel = [];
this.toolbar.hide();
this.toolbar_landing_area.hover(
function(){
var me = GraphViewer.getInstance(this);
if(!me.fixedToolbar){
if(me.toolbar.queue().length == 0){
me.toolbar.slideDown(100,function(){
if(me.toolbar.queue('depgraph_toolbar_hiding_bufferqueue').length %2 == 1){
me.toolbar.dequeue('depgraph_toolbar_hiding_bufferqueue');
}
});
me.mainpanel.css("border","1px solid grey");
}else{
me.toolbar.slideDown({duration:100,queue:'depgraph_toolbar_hiding_bufferqueue'});
}
}
},
function(){
var me = GraphViewer.getInstance(this);
if(!me.fixedToolbar){
if(me.toolbar.queue().length == 0){
me.toolbar.slideUp(100,function(){
if(me.toolbar.queue('depgraph_toolbar_hiding_bufferqueue').length %2 == 1){
me.toolbar.dequeue('depgraph_toolbar_hiding_bufferqueue');
}
});
if(!me.borders){
me.mainpanel.css("border","none");
}
}else{
me.toolbar.slideUp({duration:100,queue:'depgraph_toolbar_hiding_bufferqueue'});
}
}
}
);
};
GraphViewer.instances = GraphViewer.instances || [];
/**
* Retrieve the viewer instance from :
* - id of a div
* - svg element
* - jquery div selection
* @param fromdiv
* @returns
*/
GraphViewer.getInstance = function(fromdiv){
var id = "";
if(GraphViewer.prototype.isPrototypeOf(fromdiv)){
return fromdiv;
}else if(fromdiv.ownerSVGElement != null){
id = fromdiv.ownerSVGElement.parentNode.id;
}else if(typeof fromdiv == 'object' && fromdiv.id != null){
id = fromdiv.id;
}else{
id = fromdiv;
}
regex = /.*-(\w+)/;
var match = regex.exec(id);
var viewer = null;
if(match != null){
viewer = GraphViewer.instances[match[1]];
}
if(viewer == null){
viewer = GraphViewer.getInstance(fromdiv.parentNode);
}
return viewer;
};
/***********************************************************/
/** Builders */
/***********************************************************/
/**
* init the toolbar
*/
GraphViewer.prototype.initToolBar = function(){
var landing_area = this.toolbar_landing_area = this.createDiv("gv-toolbar-landing-area","gv-toolbar-landing-area");
var toolbar = this.createDiv("gv-toolbar", "gv-toolbar");
landing_area.append(toolbar);
this.mainpanel.append(this.toolbar_landing_area);
var toolbarbuttons = this.toolbarbuttons = this.createDiv("gv-toolbarbuttons","gv-toolbarbuttons");
toolbar.append(toolbarbuttons);
// use colorbox for fullscreen mode
this.addFullScreenButton();
return toolbar;
};
/**
* create the debug panel
* @returns
*/
GraphViewer.prototype.createDebugPanel = function(){
var debugpanel = this.debugpanel = this.createDiv("gv-debug-panel","gv-debug-panel");
this.debugpanelinfo = this.createDiv("debug-panel-info","debug-panel-info");
debugpanel.append(this.debugpanelinfo);
var slider = this.createDiv("debug-panel-slider","debug-panel-slider slider-button slide-up");
slider.click(toggleSlider);
debugpanel.append(slider);
var maximize = this.createDiv("debug-panel-fullscreen","debug-panel-fullscreen slider-button maximize");
maximize.click(
function(e){
var graph = GraphViewer.getInstance(this);
if(graph!=null){
graph.viewAltContent('debug-panel-content'+graph.appendOwnID(''));
var slider = jQuery('#debug-panel-slider'+graph.appendOwnID(''));
if(slider.hasClass('slide-down')){
debugSlideDown.call(slider);
}
}
}
);
debugpanel.append(maximize);
var debugcontentcontainer = this.debugcontentcontainer = this.createDiv("debug-panel-container","debug-panel-container");
debugpanel.append(debugcontentcontainer);
this.debugpanelcontent = this.createDiv("debug-panel-content","debug-panel-content");
debugcontentcontainer.append(this.debugpanelcontent);
this.mainpanel.append(debugpanel);
this.adjustDebugHeight();
debugpanel.hide();
return debugpanel;
};
/**
* append msg to the debug log panel
* @param msg
*/
GraphViewer.prototype.debugLog = function(msg){
this.debugpanelcontent.append(msg + '<br/>');
};
/**
* create tooltip panel
* @returns
*/
GraphViewer.prototype.createToolTipPanel = function(){
var me = this;
this.tooltip = this.createDiv("gv-tooltip","gv-tooltip");
var maincontainer = jQuery('<div style="display:inline-block;"></div>');
this.tooltip.append(maincontainer);
this.tooltipExitButton = this.createDiv('tooltip-exit-button','tooltip-exit-button');
this.tooltipExitButton.css('float','right');
this.tooltipExitButton.css('display','block');
this.tooltipExitButton.click(function(){me.hideToolTip();});
maincontainer.append(this.tooltipExitButton);
this.tooltipExitButton.hide();
this.tooltip.draggable();
this.tooltipContainer = this.createDiv('tooltip-container');
maincontainer.append(this.tooltipContainer);
return this.tooltip;
};
/**
* append the viewer uid to the id of an element
* @param id
* @returns
*/
GraphViewer.prototype.appendOwnID = function(id){
if(id.endsWith(this.uid)){
return id;
}else{
return id + "-" + this.uid;
}
};
/**
* create a wrapper that will allow (un/)folding of the viewer
* @param title
* @returns
*/
GraphViewer.prototype.addWrapper = function(title){
var me = this;
var elt = this.wrapper = me.createElement("a", 'gv-wrapper','gv-wrapper');
elt.html(title);
me.container.prepend(elt);
me.mainpanel.hide();
elt.click(function(e){
me.mainpanel.toggle();
});
return elt;
};
/**
* create a div with the viewer id appended
* @param id
* @param classes
* @returns
*/
GraphViewer.prototype.createDiv = function(id,classes){
return this.createElement('div',id,classes);
};
GraphViewer.prototype.createElement = function(eltname,id,classes){
classes = (classes!=null)?('class="'+classes+'"'):'';
var div = '<'+eltname+' id="'+this.appendOwnID(id)
+'" '+classes+'></'+eltname+'>';
return jQuery(div);
};
/***********************************************************/
/** Modes / Modifiers */
/***********************************************************/
/**
* set up the debug panel proper height according to the viewer height
*/
GraphViewer.prototype.adjustDebugHeight = function(){
var height = this.mainpanel.height() - this.toolbar.height();
this.debugpanel.css('height',height);
this.debugcontentcontainer.css('height',height-20);
this.debugpanel.css('bottom',-height+20);
};
/**
* adjust height to the content of the viewer
* @param marginBottom
*/
GraphViewer.prototype.shrinkHeightToContent = function(marginBottom){
if(marginBottom == null){
marginBottom=0;
}
var vis = d3.select(this.chart.children()[0]).select('g');
var bbox = vis.node().getBBox();
var transform = depgraphlib.getTransformValues(vis);
this.setHeight(transform.translate[1]+bbox.y*transform.scale[0]+bbox.height*transform.scale[0]+marginBottom*transform.scale[0]);
};
/**
* adjust width to the content of the viewer
* @param marginRight
*/
GraphViewer.prototype.shrinkWidthToContent = function(marginRight){
if(marginRight == null){
marginRight=0;
}
var vis = d3.select(this.chart.children()[0]).select('g');
var bbox = vis.node().getBBox();
var transform = depgraphlib.getTransformValues(vis);
this.setWidth(transform.translate[0]+bbox.x+bbox.width+marginLeft);
};
/**
* adjust width and height to the content of the viewer
* @param marginRight
* @param marginBottom
*/
GraphViewer.prototype.shrinkToContent = function(marginRight,marginBottom){
if(marginBottom == null){
marginBottom=0;
}
if(marginRight == null){
marginRight=0;
}
var vis = d3.select(this.chart.children()[0]).select('g');
var bbox = vis.node().getBBox();
var transform = depgraphlib.getTransformValues(vis);
var maxWidth = this.mainpanel.width();
var newWidth = transform.translate[0]+bbox.x+bbox.width+marginRight;
//if(newWidth>maxWidth)
//newWidth = maxWidth-10;
this.setSize(newWidth,transform.translate[1]+bbox.y+bbox.height+marginBottom);
};
/**
* set width and height of the viewer
* @param width
* @param height
*/
GraphViewer.prototype.setSize = function(width,height){
this.setWidth(width);
this.setHeight(height);
};
/**
* remove toolbar, debugpanel and tooltip
* @param value
*/
GraphViewer.prototype.setImageMode = function(value){
if(value == null){
value = true;
}
this.imagemode = value;
if(value){
this.toolbar.detach();
this.debugpanel.detach();
this.tooltip.detach();
}else{
this.mainpanel.append(this.toolbar);
this.mainpanel.append(this.debugpanel);
this.mainpanel.append(this.tooltip);
}
this.margin.top = this.chart.height()/10 + this.basemargin + ((this.imagemode)?0:20);
this.margin.bottom = this.basemargin + ((this.imagemode)?0:20);
};
/**
* remove border of the viewer
*/
GraphViewer.prototype.noBorders = function(){
this.borders = false;
this.mainpanel.css("border","none");
};
/**
* switch on/off the debug mode (display or not the debug panel)
* @param value
*/
GraphViewer.prototype.debugMode = function(value){
if(value == null){
value = true;
}
this.debugPanelActive = value;
if(value){
this.debugpanel.show();
}else{
this.debugpanel.hide();
}
};
/**
* set the width of the viewer
* @param width
*/
GraphViewer.prototype.setWidth = function(width){
this._width = width;
this.mainpanel.css('width',width);
this.ajaxLoader.width(width);
};
/**
* set the height of the viewer
* @param height
*/
GraphViewer.prototype.setHeight = function(height){
height += 30;
this._height = height;
this.mainpanel.css('height',height);
this.margin.top = this.chart.height()/10 + this.basemargin + ((this.imagemode)?0:20);
this.margin.bottom = this.basemargin + (this.imagemode)?0:20;
this.ajaxLoader.height(height-this.toolbar.height());
this.ajaxLoader.css('top',this.toolbar.height());
if(this.debugPanelActive){
this.adjustDebugHeight();
}
};
/**
* enable or disable the toolbar auto hiding
* @param value
*/
GraphViewer.prototype.setFixedToolbar = function(value){
if(value == null){
value = true;
}
this.fixedToolbar = value;
if(value){
this.toolbar.show();
}else{
this.toolbar.hide();
}
};
/***********************************************************/
/** ToolBar */
/***********************************************************/
/**
* add the fullscreen mode button to the toolbar
*/
GraphViewer.prototype.addFullScreenButton = function(){
if(!this.allowFullScreen){
return;
}
if(typeof this.container.colorbox != 'undefined'){
this.addToolbarButton('fullscreen',null,'right','fullscreen','View in fullscreen');
this.initFullscreenMode();
}
};
/**
* returns true if found a toolbar button with the name 'name'
* @param name
* @returns {Boolean}
*/
GraphViewer.prototype.existToolbarButton = function(name){
return jQuery('#button-'+this.appendOwnID(name)).length > 0;
};
/**
* return the div element corresponding to the tool bar button with name 'name'
* @param name
* @returns
*/
GraphViewer.prototype.getToolbarButton = function(name){
return jQuery('#button-'+this.appendOwnID(name));
};
/**
* set toolbar buttons from a array definition of buttons
* 0 : name, 1 : callback on click, 2: position (left or right), 3 : style (css classes)
* @param definition
*/
GraphViewer.prototype.setToolbarButtons = function(definition){
this.tmpLeft = [];
definition.forEach(function(item,index){
if(item[2] == 'left'){
this.tmpLeft.push(item);
}else{
this.addToolbarButton(item[0], item[1], item[2], item[3], item[4]);
}
},this);
this.tmpLeft.forEach(function(item,index){
this.addToolbarButton(item[0], item[1], item[2], item[3], item[4]);
},this);
this.tmpLeft = null;
};
/**
* remove all toolbar buttons
*/
GraphViewer.prototype.resetToolbarButtons = function(){
var children = this.toolbarbuttons.children();
children.remove();
this.addFullScreenButton();
};
/**
* add a button to the toolbar
* the button is a div that will be put either on left, or right side of the toolbar
* @param elt
* @param position
*/
GraphViewer.prototype.addToolbarElement = function(elt,position){
elt.css('float',position);
this.toolbarbuttons.append(elt);
};
/**
* create and add a button to the toolbar from minimal information
* @param name
* @param callback
* @param position
* @param style
*/
GraphViewer.prototype.addToolbarButton = function(name,callback,position,style,tooltip){
var text = '';
if(style == null){
style = 'tab white';
text = name;
}else{
style += " icon";
}
if(position == null){
position = 'left';
}
var button='<div id="button-'+this.appendOwnID(name)+'" title="'+(tooltip || name)+'" class="'+style+' tab '+position+'">'+text+'</div>';
button = jQuery(button);
button.click(callback);
this.toolbarbuttons.append(button);
};
/**
* remove a toolbar button given its name
* @param name
*/
GraphViewer.prototype.removeToolbarButton = function(name){
jQuery('#button-'+this.appendOwnID(name)).remove();
};
/**
* create a dropdown menu given items that compose the menu
* items is an object of objects with at least the property 'cb' defining the callback when the item
* is clicked.
*/
GraphViewer.createDropDownMenu = function(name,items,autoslidedown,tooltip){
var div = jQuery('<div class="gv-menu"><div class="gv-menu-header"></div><div class="gv-menu-body"></div></div>');
var header = jQuery('.gv-menu-header',div);
var body = jQuery('.gv-menu-body',div);
body.hide();
header.html(name);
header.attr('title',tooltip||name);
header.addClass('white');
for(i in items){
var item = jQuery('<div>'+i+'</div>');
item.attr('title',items[i].tt || i);
body.append(item);
jQuery(item).click(items[i].cb);
}
if(autoslidedown === false){
jQuery('.gv-menu-header',div).click(function(event){jQuery('.gv-menu-body',event.currentTarget.parentNode).slideDown();});
jQuery(div).mouseleave(function(event){
jQuery('.gv-menu-body',event.currentTarget).slideUp();
});
}else{
div.hover(
function(event){jQuery('.gv-menu-body',event.currentTarget).slideDown();},
function(event){jQuery('.gv-menu-body',event.currentTarget).slideUp();}
);
}
return div;
};
/***********************************************************/
/** Alt Content */
/***********************************************************/
GraphViewer.prototype.viewAltContent = function(divid){
this.resetAltPanel();
this.addContentToAltPanel(divid);
this.showAltPanel();
};
GraphViewer.prototype.hideAltPanel = function(){
this.chart.show();
this.altpanel.hide();
this.removeToolbarButton('back');
executeCallbacks(this.callbacks.hidealtpanel);
};
GraphViewer.prototype.showAltPanel = function(callbacks){
this.chart.hide();
this.altpanel.show();
this.addToolbarButton('back',function(){GraphViewer.getInstance(this.id).hideAltPanel();},'left','back');
executeCallbacks(this.callbacks.showaltpanel);
};
GraphViewer.prototype.addContentToAltPanel = function(divid,title){
this.altpanelcontent.html(jQuery('#'+this.appendOwnID(divid)).html());
if(title!=null){
this.addToolbarButton(title,function(){GraphViewer.getInstance(this.id).addContentOnAltPanel(divid);},'left');
}
};
GraphViewer.prototype.resetAltPanel = function(){
this.altpanelcontent.html('');
};
/***********************************************************/
/** Quick Windows */
/***********************************************************/
/**
* Create a new box and bind it with the viewer
* @see Box for parameters description
* @return the new box created
*/
GraphViewer.prototype.createBox = function(options){
options.viewer = this;
return new depgraphlib.Box(options);
};
/**
* Box class
* @param id the id of the box window
* @param options the options for the box are :
* {draggable : bool,
* position : {float,float},
* title : string, # displat a title
* closeButton : bool # true display a close button,
* autodestroy : bool # destroy the box when click away
* }
* @param viewer [optional] a viewer to bind the box with
* @returns {depgraphlib.Box}
*/
depgraphlib.Box = function(options){
var me = this;
if(options.viewer != null){
depgraphlib.Box.instances.push(this);
this.viewer = options.viewer;
}
this.object = jQuery('<div class="depgraphlib-box"><div class="depgraphlib-box-header"></div><div class="depgraphlib-box-content"></div><div class="depgraphlib-box-footer"></div></div>');
if(options.id){
this.object.attr('id',options.id);
}
if(options.closeButton){
var tooltipExitButton = jQuery('<div class="tooltip-exit-button"/>');
tooltipExitButton.css('float','right');
tooltipExitButton.css('display','block');
tooltipExitButton.click(function(){me.close(true);});
jQuery('.depgraphlib-box-header',this.object).append(tooltipExitButton);
}
if(options.position){
this.object.css('top',options.position.y);
this.object.css('left',options.position.x);
}
if(options.draggable){
this.object.draggable();
}
if(options.autodestroy){
this.tooltipCreationBug = true;
d3.select(document).on('click.box_'+depgraphlib.Box.instances.length,function(e){
if(!me.tooltipCreationBug && !jQuery.contains( me.object[0], d3.event.originalTarget )){
me.destroy();
}
delete me.tooltipCreationBug;
});
}
jQuery('body').append(this.object);
return this;
};
depgraphlib.Box.prototype.setContent = function(content){
var boxcontent = jQuery('.depgraphlib-box-content',this.object);
boxcontent.html(content.html());
return this;
};
depgraphlib.Box.prototype.setHeader = function(content){
return this;
};
depgraphlib.Box.prototype.setFooter = function(content){
return this;
};
/**
* Instances of boxes
*/
depgraphlib.Box.instances = depgraphlib.Box.instances || [];
depgraphlib.Box.prototype.open = function(position){
if(position){
this.object.css('top',position.y);
this.object.css('left',position.x);
}
this.object.show();
return this;
};
/**
*
* @param raw if true, returns the dom element, else the jquery selection
* @return the dom object corresponding to this box (jquery object or dom element depending of parameter)
*/
depgraphlib.Box.prototype.getDOM = function(raw){
if(raw){
return this.dom;
}
else{
return this.object;
}
};
depgraphlib.Box.getBox = function(elt){
for(var i=0; i< depgraphlib.Box.instances.length; ++i){
if(jQuery.contains( depgraphlib.Box.instances[i], elt)){
return depgraphlib.Box.instances[i];
}
}