-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraph-gen.js
1649 lines (1430 loc) · 59 KB
/
graph-gen.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
(function(global, factory){
if (typeof define === 'function' && define.amd) {
// AMD
define(['exports','d3'], factory);
} else if (typeof exports === 'object') {
// Node, CommonJS-like
factory(exports, require('d3'));
} else {
// Browser globals
(factory((global.ggen = global.ggen || {}), global.d3));
}
}(this, (function(exports, d3){ 'use strict';
// GGEN is a library in development, code is still to be cleaned and optimized.
var version = "1.0.2";
// Obtain basic information about document and window
var docEl = document.documentElement,
bodyEl = document.getElementsByTagName('body')[0];
var canvaswidth = window.innerWidth || docEl.clientWidth || bodyEl.clientWidth,
canvasheight = window.innerHeight || docEl.clientHeight|| bodyEl.clientHeight;
// list of nodes and list of connection between nodes
var nodes = [];
var edges = [];
//paths and blocks group to be appended to the "graphClass" group
var paths;
var blocks;
// Define some useful constants
var constants = {
graphClass: "graph",
nodeClass: "node-group",
arcClass: "arc-group",
clickableClass: "clickable",
wellsClass:"wells",
circleNodeClass:"circlenode",
binaryGraph:false,
draggableGraph: true,
zoomableGraph: true, //false
zoomScale: [0.5,5],
zoomStep:0.5,
start: { class:"strt-nd", width:50, height:50, deletable:false, draggable:false,
clickable:true, customFunction1Enabled: true },
end: { class:"strt-nd", width:50, height:50, deletable:true, draggable:false,
clickable:true, customFunction1Enabled: true,
nodeSingleParent:true},
block: { class:"block", width:180, height:120, marginl:10, marginr:10, //width:240, height:160
titleMaxChars:14, trimtext:false,
iconsize:25, icon1:"fas fa-cog", icon2:"fas fa-times-circle",
deletable:true, draggable:false, clickable:true, singleInput:true,
customFunction1Enabled:true, customFunction2Enabled:true,
customFunction3Enabled:true, nodeSingleChild:false },
offsety: 90, // 130, //180,
duration: 750 //of transactions
};
// state of the graph
var state = {
selectedNode: null,
//pointer to end node to avoid searching the nodes array
endNode: null,
currentID: 0,
graphVersion:0,
debug:false,
zoom:null
};
var settings = null;
//Define some settings to configure the library
function defineSettings(cwidth, cheight){
if(state.debug) console.log(" setting redefinition for w: ",cwidth," h: ",cheight);
canvaswidth = cwidth;
canvasheight = cheight;
settings = {
start: {
shape: "circle",
xLoc: cwidth/2-constants.start.width/2,
yLoc: constants.start.width,
x0Loc: cwidth/2-constants.start.width/2,
y0Loc: 0
},
end: {
shape: "circle",
xLoc: cwidth/2-constants.end.width/2,
yLoc: canvasheight-100,
x0Loc: cwidth/2-constants.end.width/2,
y0Loc: cheight-100
},
edge: {
shape:"curve" //"curve" or "line" //can be added the support to the arrows
},
block: { shape: "rect" }, //"rect" or "circle"
noStartNode: false,
noEndNode: false,
noIndexedDB: true,
useAlternativeAlgorithm:false,
algorithm:''
};
}
// ---- setter ----
function setNodeSize(size=null,type="block"){
if(type=="block" && size!=null){
if(size.height!=undefined && Number.isInteger(size.height))
constants.block.height = size.height;
if(size.width!=undefined && Number.isInteger(size.width))
constants.block.width = size.width;
if(size.offsetx!=undefined && Number.isInteger(size.offsetx)){
constants.block.marginl = size.offsetx/2;
constants.block.marginr = size.offsetx/2;
}
}else if(type=="start" && size!=null
&& size.width!=undefined && Number.isInteger(size.width)){
constants.start.height = size.width;
constants.start.width = size.width;
}else if(type=="end" && size!=null
&& size.width!=undefined && Number.isInteger(size.width)){
constants.end.height = size.width;
constants.end.width = size.width;
}
if(size!=null && size.offsety!=undefined && Number.isInteger(size.offsety)){
constants.offsety = size.offsety;
}
}
function setTrimText(trimtrue, chars){
if(trimtrue==true )
constants.block.trimtext=true;
else if(trimtrue==false)
constants.block.trimtext=false;
if(chars!=undefined && Number.isInteger(chars))
constants.block.titleMaxChars=chars;
}
function setMultipleNodesConnectedToEnd(dec=true){
constants.end.nodeSingleParent = dec==true ? false : dec==false ? true : constants.end.nodeSingleParent;
}
function setBinary(binary=true){
if(binary!=false &&binary!=true)
return;
if(binary)
constants.block.nodeSingleChild = false;
constants.binaryGraph=binary;
//if true: no more than 2 children per node
}
function setLinearEdges(linear=true){
if(linear!=false &&linear!=true)
return;
settings.edge.shape= linear==true ? "line" : "curve";
}
function setCircleBlocks(circle=true){
if(circle!=false &&circle!=true)
return;
constants.offsety = 110;
settings.block.shape= circle==true ? "circle" : "rect";
}
function setDrag(draggable=true){
constants.draggableGraph = draggable==true ? true : draggable==false ? false : constants.draggableGraph;
}
function setZoom(zoomable=true, step=[], extent=[]){
// if zoomable = true => also draggable MUST be true
constants.zoomableGraph = zoomable==true ? true : zoomable==false ? false : constants.zoomableGraph;
if(zoomable==true){
if(constants.draggableGraph==false){
constants.draggableGraph = true;
//console.log("- WARNING: if the graph is zoomable, it IS also draggable by default!");
}
if(step!=null && typeof step=='number' && step>0 && step<1){
constants.zoomStep = step;
}
if(extent.length==2 && extent[0]>0 && extent[1]>=extent[0]){
constants.zoomScale=extent;
}
}
}
function setNodeSpacing(offsetX, offsetY){
if(offsetY>0 && offsetY<500 && offsetX>0 && offsetX < 500)
constants.offsety = offsetY;
constants.block.marginl = offsetX/2;
constants.block.marginr = offsetX/2;
}
// functional ZOOM
function zoomIn(){
var zoom = state.zoom;
var svg = d3.select("svg");
svg.transition().call(zoom.scaleBy, (constants.zoomStep+1))
}
function zoomOut(){
var zoom = state.zoom;
var svg = d3.select("svg");
svg.transition().call(zoom.scaleBy, (1-constants.zoomStep))
}
function currentSelectedNode(node=null){
if(node!=null && nodes.includes(node))
state.selectedNode=node;
return state.selectedNode;
}
function initCanvas(container="default"){
if(state.debug) console.log("initCanvas: "+ canvaswidth)
if (!window.d3){ //check if d3.js is loaded or not
if(state.debug) console.log("\nIn order to use ggen library 'd3.js' must be included");
return;
}
var svg;
if(container=="default"){
svg = d3.select("body").append("svg")
.attr("width", canvaswidth)
.attr("height", canvasheight);
//.attr("id", constants.canvasID);
defineSettings(canvaswidth, canvasheight);
}else{
var svg = d3.select(container).append("svg")
.attr("width", "100%")
.attr("height", "100%");
var elwidth = d3.select(container).node().offsetWidth;
//document.getElementById(container).offsetWidth;
var elheight = d3.select(container).node().offsetHeight;
//document.getElementById(container).offsetHeight;
if(state.debug) console.log(" SVG appended at ", container, "with dimension w: ",elwidth," h: ",elheight);
defineSettings(elwidth, elheight);
}
//append a group g element to the svg giving to each members of the group a class
var svgG = svg.append("g").classed(constants.graphClass, true);
//panning and zoom
var scaleExt = [1,1];
if(constants.zoomableGraph) scaleExt = constants.zoomScale;
//svg call zoom
state.zoom = d3.zoom()
.scaleExtent(scaleExt) //[0.8, 20]
.on("zoom", d =>{svgG.attr("transform", d3.event.transform);});
if(constants.draggableGraph)
svg.call(state.zoom);
// svg nodes and edges groups
paths = svgG.append("g")
.attr('id','paths');
blocks = svgG.append("g")
.attr('id','blocks');
if(!window.indexedDB){
if(state.debug)
console.log("Your browser doesn't support a stable version of IndexedDB. Such feature will not be available.");
settings.noIndexedDB = true;
}
if(!settings.noIndexedDB){
// if in indexed DB there is already a graph
// load that graph
//if(state.debug) console.log("init from indexed db ");
}
updateGraph();
}
function moveNodesRecursive(node, offset){
if( (node.type=='operator'
&& node.parent.length>1)
|| (node.type=='block' && node.input.length==4 && node.parent.length>1)
){
if(node.parent.lenght==2
||!(node.type=='block' && node.input.length==4)){
if(node.parent[1].x>node.parent[0].x)
offset=offset-(node.parent[1].x-node.parent[0].x)/2;
else
offset=offset-(node.parent[0].x-node.parent[1].x)/2;
}
//search the lowest parent y and the leftmost and rightmost(for 4input case)
let posy=0;
let leftmostparentx=canvaswidth;
let rightmostparentx=0;
node.parent.forEach(function(d){
if(d.y+d.size.height>posy)
posy=d.y+d.size.height;
if(d.x<leftmostparentx)
leftmostparentx=d.x;
if(d.x>rightmostparentx)
rightmostparentx=d.x;
});
node.y = posy + constants.offsety;
if(node.type=='block' && node.input.length==4){
offset=offset-(rightmostparentx-leftmostparentx)/2;
console.log("leftmostparent ",leftmostparentx," position ",node.x," offset ",offset);
}
}
for(var i=0; i<node.children.length; i++){
if( (node.children[i].type=='operator'
&& node.children[i].parent.length>1
&& node.children[i].parent[1]==node)
||( node.children[i].type=='block'
&& node.children[i].input.length==4
&& node.children[i].parent.length>1
&& node.children[i].parent[0]!=node)
)
continue;
moveNodesRecursive(node.children[i],offset);
}
node.x += offset;
}
// ------ compute node positions and-----------
// -------use D3 JOIN UPDATE PATTERN-----------
function updateGraph(){
if(nodes!=null && nodes.length!=0){
if(!settings.useAlternativeAlgorithm)
VDPtreeLayout(nodes[0]);
//center the tree (in a time consuming way)
//it would be better to do this in secondWalk!
var rootX = nodes[0].x;
var rootWidth = nodes[0].size.width;
var a = canvaswidth/2 + rootWidth/2;
var b = rootX + rootWidth;
moveNodesRecursive(nodes[0],(a-b));
if(state.endNode!=null && state.endNode.parent.length>1){
//search the lowest parent y,
//the further-from-the-origin parent x
//and the closest-to-origin parent x
var posy=0;
var xnear=canvaswidth;
var xfar=0;
state.endNode.parent.forEach(function(d){
if(d.y+d.size.height>posy)
posy=d.y+d.size.height;
if(d.x<xnear)
xnear = d.x+d.size.width;
if(d.x>xfar)
xfar = d.x;
});
state.endNode.y = posy + constants.offsety;
state.endNode.x = xnear + (xfar-xnear)/2 - state.endNode.size.width/2;
}
}
// node update selection: existing nodes
var el_up = blocks.selectAll("g."+constants.nodeClass)
.data(nodes, function(d){ return d.id; });
// node enter selection: new nodes
var el_en = el_up.enter().append("g")
.classed(constants.nodeClass, true);
//give each new element a personalized class
el_en.each(function(d) {
this.classList.add("blk"+d.id);
if(d.class.nodeclass!=undefined)
this.classList.add(d.class.nodeclass);
});
// Transition update selection: old nodes to their new position.
var el_up_tr = el_up
.transition()
.duration(constants.duration)
.attr("transform", function(d) {
//translate to current position x, y
return "translate(" + d.x + "," + d.y + ")";
});
/* // ---- DEBUG ----
if(state.debug){
console.log("update: ");
el_up.each( function(i){
console.log(i);
});
}*/
//start and end enter selection
var strt_nd_en = el_en.filter( function(d){
return d.type == 'start' || d.type == 'end';
}).classed(constants.start.class, true);
var blks_en = el_en.filter( function(d){
return d.type == 'block' || d.type == 'operator';
}).classed(constants.block.class, true);
// if start-end insert a circle
strt_nd_en.append("circle")
.attr("r", 1e-6)
.attr("cx", function(d){return (d.size.width/2);})
.attr("cy", function(d){return (d.size.width/2);});
// append to the start-end enter group a new text
strt_nd_en.append('text')
.attr("class", "node-title")
.attr("x", function(d){return (d.size.width/2);})
.attr("y", function(d){return (d.size.width/2);})
.attr("font-family","sans-serif")
.attr("font-size","0px")
.attr("fill","steelblue")
.attr("text-anchor", "middle")
.attr("dominant-baseline","central")
.classed(constants.clickableClass, function(d){
if(d.type=="start" && constants.start.clickable==true ||
d.type=="end" && constants.end.clickable==true) return true;
else return false;
}).on('click', function(d){
if(d.type=="start")
return customStartFunction1(d);
else if(d.type=="end")
return customEndFunction1(d);
});
// move strt-end group to initial position
strt_nd_en.attr("transform", function(d){
return "translate(" + d.x0+ ", "+ d.y0 +" )";
});
//consider entering node blocks
if(settings.block.shape!="rect"){
blks_en.append('circle')
.attr("r", 1e-6)
.attr("cx", function(d){return (d.size.width/2);})
.attr("cy", function(d){return (d.size.width/2);})
.classed(constants.circleNodeClass,true);
blks_en.append('text')
.attr("class", "node-title")
.attr("x", function(d){return (d.size.width/2);})
.attr("y", function(d){return (d.size.width/2);})
.attr("font-family","sans-serif")
.attr("font-size","0px")
.attr("fill","steelblue")
.attr("text-anchor", "middle")
.attr("dominant-baseline","central")
.classed(constants.clickableClass, function(d){
if(d.type=="start" && constants.start.clickable==true ||
d.type=="end" && constants.end.clickable==true) return true;
else return false;
}).on('click', function(d){ return customBlockFunction3(d); });
blks_en.attr("transform", function(d){
return "translate(" + d.x0+ ", "+ d.y0 +" )";
});
}else{
// append main rect to entering node block
blks_en.append('rect')
.attr('width',0)
.attr('height',0)
.attr("rx", "5")
.attr("ry", "5");
//append title text and two circle to the node block
blks_en.append("circle")
.attr("r", 1e-6)
.classed(constants.wellsClass,true);
blks_en.append("circle")
.attr("r", 1e-6)
.classed(constants.wellsClass,true)
.classed(constants.clickableClass, function(){
if(constants.block.clickable==true) return true;
else return false;
}).on("click", function(d){ return customBlockFunction3(d);});
blks_en.append("text")
.attr("class", "node-title")
.attr("font-family","sans-serif")
.attr("font-size","0px")
.attr("fill","steelblue")
.attr("text-anchor", "middle")
.attr("dominant-baseline","central");
//append icon1 to node block and give it custom functions
blks_en.append("text") // Append a text element
.attr("class", function(){ return constants.block.icon1; }) // Give it the font-awesome class
.text("\uf013") // Specify your icon in unicode (https://fontawesome.com/cheatsheet)
.attr("font-size","0px")
.attr("fill","steelblue")
.attr("text-anchor", "middle")
.attr("dominant-baseline","central")
.classed(constants.clickableClass, function(){
if(constants.block.clickable==true) return true;
else return false;
}).on('click', function(d){ return customBlockFunction1(d);});
//append icon1 to node block
blks_en.append("text") // Append a text element
.attr("class", function(){ return constants.block.icon2; }) // Give it the font-awesome class
.text("\uf057") // Specify your icon in unicode (https://fontawesome.com/cheatsheet)
.attr("font-size","0px")
.attr("fill","steelblue")
.attr("text-anchor", "middle")
.attr("dominant-baseline","central")
.classed(constants.clickableClass, function(){
if(constants.block.clickable==true) return true;
else return false;
}).on('click', function(d){return customBlockFunction2(d);});
blks_en.attr("transform", function(d){
return "translate(" +d.x0+ ", "+d.y0+" )";
});
//just for operators
blks_en.filter( function(d){
return d.type == 'operator';
}).append("circle")
.attr("r", 1e-6)
.classed(constants.wellsClass,true);
//In the exceptional case in which input are 4
blks_en.filter( function(d){
return d.input.length==4;
}).append("circle").attr("r", 1e-6).classed(constants.wellsClass,true);
blks_en.filter( function(d){
return d.input.length==4;
}).append("circle").attr("r", 1e-6).classed(constants.wellsClass,true);
blks_en.filter( function(d){
return d.input.length==4;
}).append("circle").attr("r", 1e-6).classed(constants.wellsClass,true);
}
/* // ---- DEBUG ------
if(state.debug){
console.log("enter blks: ");
blks_en.each( function(i){
console.log(i);
});
console.log("enter strt: ");
strt_nd_en.each( function(i){
console.log(i);
});
} */
//transition enter selection: move new nodes to their new position
var strt_nd_tr = strt_nd_en.transition()
.duration(constants.duration);
var blks_tr = blks_en.transition()
.duration(constants.duration);
//start-end selection entering transition (applied to the group)
strt_nd_tr.attr("transform", function(d) {
//translate to current position x, y
return "translate("+d.x+"," + d.y + ")";
}).select("circle") // add to the transition the change in radius to 10
.attr("r", function(d){
return (d.size.width/2);
});
strt_nd_tr.select('text')
.attr("font-size","20px")
.text(function(d){ return d.title});
//considering block entering transition
if(settings.block.shape!="rect"){
blks_tr.attr("transform", function(d) {//translate to current position x, y
return "translate("+d.x+"," + d.y + ")";
}).select("circle") // add to the transition the change in radius to 10
.attr("r", function(d){
return (d.size.width/2);
});
blks_tr.select('text')
.attr("font-size","20px")
.text(function(d){
return d.title;
});
}else{
//blocks selection entering transition (applied to the group)
blks_tr.attr("transform", function(d) {
//translate to current position x, y
return "translate("+d.x+"," + d.y + ")";
}).selectAll('rect')
.attr('width', function(d){ return d.size.width;} )
.attr('height', function(d){ return d.size.height;} );
blks_tr.select('text')
.attr("font-size","20px")
.attr("x", function(d){ return d.size.width/2; })
.attr("y", function(d){ return d.size.height/2; })
.text(function(d){
if(constants.block.trimtext)
return trimText(d.title,constants.block.titleMaxChars);
else
return d.title;
});
blks_tr.select('text.fa-cog')
.attr("font-size", function(d){ var v=d.size.iconsize; return v.toString(); })
.attr("x", function(d){ return d.size.iconsize; })
.attr("y", function(d){ return d.size.iconsize; });
blks_tr.select('text.fa-times-circle')
.attr("font-size", function(d){ var v=d.size.iconsize; return v.toString(); })
.attr("x", function(d){ return d.size.width-(d.size.iconsize); })
.attr("y", function(d){ return d.size.iconsize; });
blks_tr.selectAll('circle')
.attr("r", 6)
.filter(function (d, i) { return i === 0 ;})
.attr("cx", function(d){ return d.input[0].x ; })
.attr("cy", function(d){ return d.input[0].y; })
blks_tr.selectAll('circle')
.filter(function (d, i) { return i === 1 ;})
.attr("cx", function(d){ return d.output[0].x; })
.attr("cy", function(d){ return d.output[0].y; });
blks_tr.selectAll('circle')
.filter(function (d, i) { return i === 2 ;}) //in case they are operators
.attr("cx", function(d){ return d.input[1].x; })
.attr("cy", function(d){ return d.input[1].y; });
blks_tr.selectAll('circle')
.filter(function (d, i) { return i === 3 ;}) //in case exception with 4 inputs
.attr("cx", function(d){ return d.input[2].x; })
.attr("cy", function(d){ return d.input[2].y; });
blks_tr.selectAll('circle')
.filter(function (d, i) { return i === 4 ;}) //in case exception with 4 inputs
.attr("cx", function(d){ return d.input[3].x; })
.attr("cy", function(d){ return d.input[3].y; });
}
// ----------- update PATHS -----------
var path_up = paths.selectAll("g.arc-group")
.data(edges, function(d){
return String(d.src.id) + "+" + String(d.dst.id);
});
// update existing paths: make the curve follow the node when moved
if(settings.edge.shape!="curve"){
path_up.selectAll("path").transition()
.duration(constants.duration)
.attr('d', function(d){
var output = "M " + (d.src.x+d.src.output[0].x) +" "+ (d.src.y+d.src.output[0].y);
if(d.dst.type=='operator'&& d.dst.parent.length>1
&& d.src.x>=d.dst.parent[0].x && d.src.x>=d.dst.parent[1].x)
output = output+"L" + (d.dst.x+d.dst.input[1].x)+" "+ (d.dst.y+d.dst.input[1].y);
else
output = output+"L" + (d.dst.x+d.dst.input[0].x)+" "+(d.dst.y+d.dst.input[0].y);
return output;
});
}else{
path_up.selectAll("path").transition()
.duration(constants.duration)
.attr('d', d3.linkVertical()
.source(function (d) {return [d.src.x+d.src.output[0].x, d.src.y+d.src.output[0].y]})
.target(function (d) {
if(d.dst.input.length==4 && d.dst.parent.length>1){
var i = d.dst.parent.indexOf(d.src);
return [d.dst.x+d.dst.input[i].x, d.dst.y+d.dst.input[i].y];
}else
if(d.dst.type=='operator'&& d.dst.parent.length>1 //right parent linked to the right well (input[1])
&& d.src.x>=d.dst.parent[0].x && d.src.x>=d.dst.parent[1].x) //left parent linked to the left well (input[0])
return [d.dst.x+d.dst.input[1].x, d.dst.y+d.dst.input[1].y];
else
return [d.dst.x+d.dst.input[0].x, d.dst.y+d.dst.input[0].y];
}));
}
// add new paths
// draw the new line/edges
var path_en = path_up.enter().append("g")
.attr("class", "arc-group");
//give each new path a personalized class
path_en.each(function(d) {
if(d.class!=undefined)
this.classList.add(d.class);
});
// add new paths
// draw the new line/edges
if(settings.edge.shape!="curve"){
path_en.append('path')
.attr("d", function(d){ return "M " + (d.src.x+d.src.size.width/2) +" "+ (d.src.y)+ //cursor to current position
"L" + (d.src.x+d.src.size.width/2)+" "+ (d.src.y)}); //draw a line to other position
}else{
path_en.append('path')
.attr("d", d3.linkVertical()
.source( function(d){ return [d.src.x+d.src.size.width/2, d.src.y]} )
.target( function(d){ return [d.src.x+d.src.size.width/2, d.src.y]} )
//.source( function(d){ return [d.src.output[0].x, d.src.output[0].y]} )
//.target( function(d){ return [d.dst.input[0].x, d.dst.input[0].y]} )
);
}
/* // ---- DEBUG ------
if(state.debug){
console.log("update paths: ");
path_up.each( function(i){
console.log(i);
});
console.log("enter path: ");
path_en.each( function(i){
console.log(i);
});
} */
//path transition
if(settings.edge.shape!="curve"){
path_en.selectAll('path').transition()
.duration(constants.duration)
.attr('d', function(d){
var output = "M " + (d.src.x+d.src.output[0].x) +" "+ (d.src.y+d.src.output[0].y);
if(d.dst.type=='operator'&& d.dst.parent.length>1
&& d.src.x>=d.dst.parent[0].x && d.src.x>=d.dst.parent[1].x)
output = output+ "L" + (d.dst.x+d.dst.input[1].x)+" "+ (d.dst.y+d.dst.input[1].y);
else
output = output+ "L" + (d.dst.x+d.dst.input[0].x)+" "+ (d.dst.y+d.dst.input[0].y);
return output;
});
}else{
path_en.selectAll('path').transition()
.duration(constants.duration)
.attr('d', d3.linkVertical()
.source(function (d) {return [d.src.x+d.src.output[0].x, d.src.y+d.src.output[0].y]})
.target(function (d) {
if(d.dst.input.length==4 && d.dst.parent.length>1){
var i = d.dst.parent.indexOf(d.src);
return [d.dst.x+d.dst.input[i].x, d.dst.y+d.dst.input[i].y];
}else
if(d.dst.type=='operator'&& d.dst.parent.length>1 //right parent linked to the right well (input[1])
&& d.src.x>=d.dst.parent[0].x && d.src.x>=d.dst.parent[1].x) //left parent linked to the left well (input[0])
return [d.dst.x+d.dst.input[1].x, d.dst.y+d.dst.input[1].y];
else
return [d.dst.x+d.dst.input[0].x, d.dst.y+d.dst.input[0].y];
}));
}
// remove eliminated/old paths
path_up.exit().remove()
// remove old/eliminated nodes
el_up.exit().remove();
state.graphVersion++;
}
// functions to add nodes
function addNode(
type= "block",
title= "node title",
parent = null,
nodeclass = null, //object containing both a class for the node and its connecting arc (.nodeclass e .arcclass)
numInputs = null,
x = null,
y = null,
x0= null,
y0= null
){
//integrity and constraints checks
if( (type == "start" && settings.noStartNode)
|| (type!="start" && ( parent==null || parent.id==null || parent.id<0))
){
if(state.debug) console.log( parent!=null && parent.id==null ? " -- ERR start" : " -- ERR parent node deleted");
}else if(type!='start' && parent.children.length!=0 &&
(parent.children[0].type=='end' || parent.children[0].type=='operator')
){
if(state.debug) console.log(" -- ERR cannot add a child to a node connected to end or an operator");
}else if(type!='start' && parent.type=="operator" && parent.children.length > 0){
if(state.debug) console.log(" -- ERR an operator can have just one child");
}else if(type == "end" && settings.noEndNode){
if(state.debug) console.log(" -- ERR end");
}else if( constants.binaryGraph==true && parent!=null && parent.children.length>1){
if(state.debug) console.log(" -- ERR binary graph!");
}else if( constants.block.nodeSingleChild == true
&& parent!=null && parent.type!="start"
&& parent.children.length>0 ){
if(state.debug) console.log(" -- ERR Node can only have one child!");
}else{
var newNode = {
id: type=="start" ? 0 : ++state.currentID,
type: type,
title: title,
parent: type=="start" ? null : [parent],
children: [],
class: nodeclass!=null ? nodeclass : {nodeclass: constants.nodeClass},
size: { width: type=="start" ? constants.start.width :
type=="end" ? constants.end.width :
settings.block.shape=="circle" ? constants.start.width :
constants.block.width,
height: type=="start" ? constants.start.width :
type=="end" ? constants.end.width :
settings.block.shape=="circle" ? constants.start.width :
constants.block.height,
marginl: type=="block"||type=="operator" ? constants.block.marginl : 0,
iconsize: type=="block"||type=="operator" ? constants.block.iconsize : 0
},
//position
x: x ? x :
type=="start" ? settings.start.xLoc :
type=="end" ? settings.end.xLoc :
parent!=null && type=='block' ? //&& settings.block.shape=="rect"
parent.x-constants.block.height-constants.block.width :
parent!=null ? parent.x : 250,
y: y ? y :
type=="start" ? settings.start.yLoc :
type=="end" ? parent.y+parent.size.height+constants.offsety :
//parent.type=="start" ? parent.y+constants.offsety :
parent!=null && type=='block' || type=='operator' ?
parent.y+parent.size.height+constants.offsety :
parent!=null ? (parent.y + constants.offsety) : 250,
x0: x0 ? x0 :
type=="start" ? settings.start.x0Loc :
type=="end" ? settings.end.x0Loc :
parent!=null ? (parent.x+parent.size.width/2) : x,
y0: y0 ? y0 :
type=="start" ? settings.start.y0Loc :
type=="end" ? settings.end.y0Loc :
parent!=null ? (parent.y +parent.size.height) : y,
input: type=="start" ? null :
type=="end" ? [{ x: constants.end.width/2, y: constants.end.width/2 }] :
type=="block"&&settings.block.shape=="circle" ? [{ x: constants.end.width/2, y: constants.end.width/2 }] :
type=="block"&&numInputs==4 ? [{x: constants.block.width/5, y: 0},
{x: (constants.block.width*2)/5, y: 0}, //workaround to handle this exception of 4 inputs node
{x: (constants.block.width*3)/5, y: 0},
{x: (constants.block.width*4)/5, y: 0}] :
type=="block" ? [ { x: constants.block.width/2, y: 0 }] :
type=="operator" ? [{x: constants.block.width/3, y: 0},
{x: (constants.block.width*2)/3, y: 0}]
: null,
output: type=="start" ? [{ x: (constants.start.width/2), y:(constants.start.width/2) }] :
type=="end" ? null :
type=="block"&&settings.block.shape=="circle" ? [{ x: (constants.start.width/2), y:(constants.start.width/2) }] :
type=="block"||type=="operator" ? [ { x: constants.block.width/2,
y: constants.block.height}]
: null,
//subtree is needed for the positioning algorithm to work
subtree: {
w: type=="start" ? constants.start.width :
type=="end" ? constants.end.width :
type=="block" && settings.block.shape=="circle" ? constants.start.width
+ constants.block.marginl+constants.block.marginr :
constants.block.width + constants.block.marginl+constants.block.marginr,
prelim: 0, //stores preliminar horizontal coordinate
mod:0, //modifier: how much each node of the subtree should be moved horizontally
shift:0, //to update the mod
change:0, //to update the mod
tl:null, //thread left: reference to next node in the left contour
tr:null, //thread right: reference to next node in right contour
el: null, // extreme left node: lowest node in the subtree that can be seen from left
er: null, //extreme right: lowest node visible from right
msel:0, //modifier sum for extreme left
mser:0 //modifier sum for extreme right
}, //contour: list of nodes that can be seen from left/right
data: {
height: type=="start" ? 0 : parent.data.height +1,
status: 0
},
config: {}
};
if(parent!=null){
//update the parent
newNode.parent[0].children.push(newNode);
}
nodes.push(newNode);
if(type!="start" && parent!=null){
addArc(parent,newNode, nodeclass!=null&&nodeclass.arcclass!=undefined ? nodeclass.arcclass : null);
}else{
updateGraph();
}
return newNode;
}
}
function addArc(
source = undefined,
destination = undefined,
arcclass = null){
if( !(source && destination)||
(destination.type=='block' && constants.block.singleInput &&
destination.input.length!=4 && //this case is an exception (node with 4 input)
destination.parent.length>0 && destination.parent[0]!=source)||
(destination.type=='operator' && destination.parent.length==2)
){
if(state.debug) console.log(" -- ERR ARC");
}else{
if( (destination.type=='operator'
&& destination.parent.length>0
&& destination.parent[0]!=source)
){
//check if operator is under the rightmost parent
//if it is swap the parents, so it will be under the leftmost
if(destination.parent[0].x>source.x){
var tmpconfig = destination.config;
var tmpdata = destination.data;
var tmpclass = destination.class;
removeNode(destination);
var op = addNode(type="operator", title=tmpdata.name, parent = source, tmpclass);
op.data=tmpdata;
op.config=tmpconfig;
addArc(destination.parent[0],op,tmpclass.arcclass!=undefined ? tmpclass.arcclass : null);
return;
}else
destination.parent.push(source);
source.children.push(destination);
// move all intermediate node in parent of source so that the
// two element attached to operator are close to each other.
//go up the tree until parent has more child (in las case this will be root)
var curr = source;
var parent = null;
var nodeB = null;
var nodeA = null;
var i=0;
while(!(curr.children.legth>1)){
if(curr.parent[0].children.length>1){
//curr is the node i need to insert before
parent = curr.parent[0];
nodeB = curr;
break;
}
if(curr.parent.length>1 && i>0)
curr = curr.parent[1]
else
curr = curr.parent[0];
i++;
}
curr = destination;
i=0;
while(!(curr.children.legth>1)){
if(curr.parent[0].children.length>1){
//curr is the node near which
//i wanna move nodeB
nodeA = curr;
break;
}
if(curr.parent.length>1 && i>0)
curr = curr.parent[1]
else
curr = curr.parent[0];
i++;
}
if(nodeA!=nodeB){
//i need to mode nodeA and nodeB close together to the left
//so the all the in-between subtrees will be move to the right
if(nodeA.x > nodeB.x ){
curr = nodeA;
nodeA = nodeB;
nodeB = curr;
}
//pop nodeB from parent children
var iB = parent.children.indexOf(nodeB);
if(iB>0)
parent.children.splice(iB,1)
//find index of nodeA
var iA = parent.children.indexOf(nodeA);
//push nodeB to the index next to nodeA
if(iA>-1)
parent.children.splice(iA+1,0,nodeB);
}
}else if(destination.input.length==4
&& destination.parent.length>0
&& destination.parent[0]!=source ){
//exception => do not care of edge overlapping
destination.parent.push(source);
source.children.push(destination);
}
var newArc = {
src: source,