-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
1082 lines (943 loc) · 37.8 KB
/
index.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
const d3 = require('d3');
class OrgTree {
constructor() {
this.defaults = {
svgWidth: 800,
svgHeight: 600,
};
// exported variables
const attrs = {
id: `ID${Math.floor(Math.random() * 1000000)}`, // Id for event handlings
svgWidth: 0,
svgHeight: 0,
marginTop: 0,
marginBottom: 0,
marginRight: 0,
marginLeft: 0,
container: 'body',
defaultTextFill: '#2C3E50',
nodeTextFill: 'white',
defaultFont: 'Helvetica',
backgroundColor: '#fafafa',
data: null,
highlight: {
"borderWidth": 1,
"borderRadius": 15,
"borderColor": {
"red": 50,
"green": 255,
"blue": 30,
"alpha": 1
},
"backgroundColor": {
"red": 20,
"green": 100,
"blue": 40,
"alpha": 1
}
},
linkColor: {
"red": 11,
"green": 123,
"blue": 108,
"alpha": 1
},
linkWidth: 5,
displayArrow: false,
straightLink: false,
collapsible: false,
current: null,
depth: 180,
duration: 600,
strokeWidth: 3,
initialZoom: 0.4,
orientation: 'right-to-left',
onNodeClick: d => d,
onNodeAdd: d => d,
onNodeRemove: d => d,
};
this.getChartState = () => attrs;
//(x,y) for the node position
//(x1,y1) for the add btn position
//(x2,y2) for the remove btn position
//(x3,y3) for the collapse/expanded btn position
//(from,to) for the link position
//diagonal for curve link style
//diagonal2 for straight link style
this.orientations = {
"top-to-bottom": {
size: [attrs.svgWidth, attrs.svgHeight],
x: function (d) {
return d.x;
},
y: function (d) {
return d.y;
},
x1: function (d) {
return -d.width / 2
},
y1: function (d) {
return 0
},
x2: function (d) {
return d.width / 2
},
y2: function (d) {
return 0
},
x3: function (d) {
return 0
},
y3: function (d) {
return -d.height / 2
},
from: function (d) {
return {x: d.x, y: d.y - d.height / 2}
},
to: function (d) {
return {x: d.parent.x, y: d.parent.y + d.parent.height / 2}
},
diagonal: function (s, t) {
return `M ${s.x} ${s.y}
C ${s.x} ${(s.y + t.y) / 2} ,
${t.x} ${(s.y + t.y) / 2} ,
${t.x} ${t.y}`
},
diagonal2: function (s, t) {
return `M ${s.x} ${s.y}
C ${s.x} ${s.y} ,
${t.x} ${t.y} ,
${t.x} ${t.y}`
}
},
"bottom-to-top": {
size: [attrs.svgWidth, attrs.svgHeight],
x: function (d) {
return d.x;
},
y: function (d) {
return attrs.svgHeight - d.y;
},
x1: function (d) {
return -d.width / 2
},
y1: function (d) {
return 0
},
x2: function (d) {
return d.width / 2
},
y2: function (d) {
return 0
},
x3: function (d) {
return 0
},
y3: function (d) {
return -d.width / 4
},
from: function (d) {
return {x: d.x, y: d.y - d.height / 2}
},
to: function (d) {
return {x: d.parent.x, y: d.parent.y + d.parent.height / 2}
},
diagonal: function (s, t) {
return `M ${s.x} ${attrs.svgHeight - s.y}
C ${s.x} ${attrs.svgHeight - (s.y + t.y) / 2} ,
${t.x} ${attrs.svgHeight - (s.y + t.y) / 2} ,
${t.x} ${attrs.svgHeight - t.y}`
},
diagonal2: function (s, t) {
return `M ${s.x} ${attrs.svgHeight - s.y}
C ${s.x} ${attrs.svgHeight - s.y} ,
${t.x} ${attrs.svgHeight - t.y} ,
${t.x} ${attrs.svgHeight - t.y}`
}
},
"right-to-left": {
size: [attrs.svgHeight, attrs.svgWidth],
x: function (d) {
return attrs.svgWidth - d.y;
},
y: function (d) {
return d.x;
},
x1: function (d) {
return 0
},
y1: function (d) {
return -d.height / 2
},
x2: function (d) {
return 0
},
y2: function (d) {
return d.height / 2
},
x3: function (d) {
return -d.width / 2
},
y3: function (d) {
return 0
},
from: function (d) {
return {x: d.x, y: d.y - d.width / 2}
},
to: function (d) {
return {x: d.parent.x, y: d.parent.y + d.parent.width / 2}
},
diagonal: function (s, t) {
return `M ${attrs.svgWidth - s.y} ${s.x}
C ${attrs.svgWidth - (s.y + t.y) / 2} ${s.x},
${attrs.svgWidth - (s.y + t.y) / 2} ${t.x},
${attrs.svgWidth - t.y} ${t.x}`
},
diagonal2: function (s, t) {
return `M ${attrs.svgWidth - s.y} ${s.x}
C ${attrs.svgWidth - s.y} ${s.x},
${attrs.svgWidth - s.y} ${s.x},
${attrs.svgWidth - t.y} ${t.x}`
}
},
"left-to-right": {
size: [attrs.svgHeight, attrs.svgWidth],
x: function (d) {
return d.y;
},
y: function (d) {
return d.x;
},
x1: function (d) {
return 0
},
y1: function (d) {
return -d.height / 2
},
x2: function (d) {
return 0
},
y2: function (d) {
return d.height / 2
},
x3: function (d) {
return d.width / 2
},
y3: function (d) {
return 0
},
from: function (d) {
return {x: d.x, y: d.y - d.width / 2}
},
to: function (d) {
return {x: d.parent.x, y: d.parent.y + d.parent.width / 2}
},
diagonal: function (s, t) {
return `M ${s.y} ${s.x}
C ${(s.y + t.y) / 2} ${s.x},
${(s.y + t.y) / 2} ${t.x},
${t.y} ${t.x}`
},
diagonal2: function (s, t) {
return `M ${s.y} ${s.x}
C ${s.y} ${s.x},
${t.y} ${t.x},
${t.y} ${t.x}`
}
}
}
//make properties functional and chaining call
Object.keys(attrs).forEach((key) => {
this[key] = (args) => {
((_) => {
return attrs[key] = _
})(args)
return this
};
});
this.initializeEnterExitUpdatePattern();
}
initializeEnterExitUpdatePattern() {
d3.selection.prototype.patternify = function (params) {
var container = this;
var selector = params.selector;
var elementTag = params.tag;
var data = params.data || [selector];
// Pattern in action
var selection = container.selectAll('.' + selector).data(data, (d, i) => {
if (typeof d === 'object') {
if (d.id) {
return d.id;
}
}
return i;
});
selection.exit().remove();
selection = selection.enter().append(elementTag).merge(selection);
selection.attr('class', selector);
return selection;
};
}
getNodeChildrenIds({data, children, _children}, nodeIdsStore) {
// Store current node ID
nodeIdsStore.push(data.nodeId);
// Loop over children and recursively store descendants id (expanded nodes)
if (children) {
children.forEach(d => {
this.getNodeChildrenIds(d, nodeIdsStore)
})
}
// Loop over _children and recursively store descendants id (collapsed nodes)
if (_children) {
_children.forEach(d => {
this.getNodeChildrenIds(d, nodeIdsStore)
})
}
// Return result
return nodeIdsStore;
}
setZoomFactor(zoomLevel) {
const attrs = this.getChartState();
const calc = attrs.calc;
// Store passed zoom level
attrs.initialZoom = zoomLevel;
const group = attrs.centerG.node().getBoundingClientRect()
// Rescale container element accordingly
attrs.centerG.attr('transform', `translate(${attrs.calc.centerX - group.width / 2},${attrs.calc.centerY - group.height / 2}) scale(${attrs.initialZoom})`)
}
render() {
const attrs = this.getChartState();
const thisObjRef = this;
// Define the arrowhead marker variables
const markerBoxWidth = 4;
const markerBoxHeight = 4;
const refX = markerBoxWidth / 2;
const refY = markerBoxHeight / 2;
const markerWidth = markerBoxWidth / 2;
const markerHeight = markerBoxHeight / 2;
const arrowPoints = [[0, 0], [4, 2], [0, 4],];
//Drawing containers
const container = d3.select(attrs.container);
const containerRect = container.node().getBoundingClientRect();
if (!attrs.svgWidth) {
attrs.svgWidth = containerRect.width || this.defaults.svgWidth
}
if (!attrs.svgHeight) {
attrs.svgHeight = containerRect.height || this.defaults.svgHeight
}
//Calculated properties
const calc = {
id: null,
chartWidth: null,
chartHeight: null
};
calc.id = `ID${Math.floor(Math.random() * 1000000)}`; // id for event handlings
calc.chartWidth = attrs.svgWidth - attrs.marginLeft - attrs.marginRight;
calc.chartHeight = attrs.svgHeight - attrs.marginTop - attrs.marginBottom;
attrs.calc = calc;
// Get maximum node width and height
calc.nodeMaxWidth = d3.max(attrs.data, ({width}) => width);
calc.nodeMaxHeight = d3.max(attrs.data, ({height}) => height);
// Calculate max node depth (it's needed for layout heights calculation)
attrs.depth = ['top-to-bottom', 'bottom-to-top'].includes(attrs.orientation) > 0 ? (calc.nodeMaxHeight + 100) : (calc.nodeMaxWidth + 100);
calc.centerX = calc.chartWidth / 2;
calc.centerY = calc.chartHeight / 2;
//******************** LAYOUTS ***********************
const layouts = {
treemap: null
}
attrs.layouts = layouts;
// Generate tree layout function
layouts.treemap = d3.tree().size([calc.chartWidth, calc.chartHeight])
.nodeSize([calc.nodeMaxWidth + 100, calc.nodeMaxHeight + 100])
// ******************* BEHAVIORS . **********************
const behaviors = {zoom: null}
// Get zooming function
behaviors.zoom = d3.zoom()
.scaleExtent([0.5, 2])
.on('zoom', (event) => {
const attrs = this.getChartState();
attrs.chart.attr('transform', event.transform);
// Apply new styles to the foreign object element
if (this.isEdge()) {
this.restyleForeignObjectElements();
}
});
//****************** ROOT node work ************************
// Convert flat data to hierarchical
attrs.root = d3.stratify()
.id(({nodeId}) => nodeId)
.parentId(({parentNodeId}) => parentNodeId)(attrs.data)
// Set child nodes enter appearance positions
attrs.root.x0 = 0;
attrs.root.y0 = 0;
/** Get all nodes as array (with extended parent & children properties set)
This way we can access any node's parent directly using node.parent - pretty cool, huh?
*/
attrs.allNodes = attrs.layouts.treemap(attrs.root).descendants()
// Assign direct children and total subordinate children's cound
attrs.allNodes.forEach(d => {
Object.assign(d.data, {
directSubordinates: d.children ? d.children.length : 0,
totalSubordinates: d.descendants().length - 1
})
})
if (attrs.collapsible) {
// Collapse all children at first
attrs.root.children.forEach(d => this.collapse(d));
// Then expand some nodes, which have `expanded` property set
attrs.root.children.forEach(d => this.expandSomeNodes(d));
} else {
// Expand all children at first
attrs.root.children.forEach(d => this.expand(d));
}
// ************************* DRAWING **************************
//Add svg
const svg = container
.patternify({
tag: 'svg',
selector: 'svg-chart-container'
})
.attr('width', attrs.svgWidth)
.attr('height', attrs.svgHeight)
.attr('font-family', attrs.defaultFont)
.call(behaviors.zoom)
.attr('cursor', 'move')
.style('background-color', attrs.backgroundColor);
attrs.svg = svg;
// Add the arrowhead marker definition to the svg element
svg
.append('defs')
.append('marker')
.attr('id', 'arrow')
.attr('viewBox', [-markerBoxWidth / 2, -markerBoxHeight / 2, markerBoxWidth, markerBoxHeight])
.attr('refX', 0)
.attr('refY', 0)
.attr('markerWidth', markerBoxWidth)
.attr('markerHeight', markerBoxHeight)
.attr('orient', 'auto-start-reverse')
.append('path')
.attr('d', 'M 0,0 m -2,-2 L 2,0 L -2,2 Z')
.attr('fill', () => this.rgbaObjToColor(attrs.linkColor) || 'green')
//Add container g element
const chart = svg
.patternify({
tag: 'g',
selector: 'chart'
})
// Add one more container g element, for better positioning controls
attrs.centerG = chart.patternify({
tag: 'g',
selector: 'center-group'
})
attrs.chart = chart;
// Display tree contenrs
this.update(attrs.root)
attrs.centerG.attr('transform', `translate(${attrs.calc.centerX},${attrs.calc.centerY}) scale(${attrs.initialZoom})`);
//######################################### UTIL FUNCS ##################################
// This function restyles foreign object elements ()
d3.select(window).on(`resize.${attrs.id}`, () => {
const containerRect = container.node().getBoundingClientRect();
// if (containerRect.width > 0) attrs.svgWidth = containerRect.width;
// main();
});
return this;
}
update({x0, y0, x, y}) {
const attrs = this.getChartState();
const calc = attrs.calc;
// Assigns the x and y position for the nodes
const treeData = attrs.layouts.treemap(attrs.root);
// Get tree nodes and links and attach some properties
const nodes = treeData.descendants()
.map(d => {
// If at least one property is already set, then we don't want to reset other properties
if (d.width) return d;
// Declare properties with default values
let imageWidth = 100;
let imageHeight = 100;
let imageBorderColor = 'steelblue';
let imageBorderWidth = 0;
let imageRx = 0;
let imageCenterTopDistance = 0;
let imageCenterLeftDistance = 0;
let borderColor = 'steelblue';
let backgroundColor = 'steelblue';
let width = d.data.width;
let height = d.data.height;
// Override default values based on data
if (d.data.borderColor) {
borderColor = this.rgbaObjToColor(d.data.borderColor);
}
if (d.data.backgroundColor) {
backgroundColor = this.rgbaObjToColor(d.data.backgroundColor);
}
// Extend node object with calculated properties
return Object.assign(d, {
imageWidth,
imageHeight,
imageBorderColor,
imageBorderWidth,
borderColor,
backgroundColor,
imageRx,
width,
height,
imageCenterTopDistance,
imageCenterLeftDistance,
});
});
// Get all links
const links = treeData.descendants().slice(1);
//re-calculate depth for each nodes according to orientation
attrs.depth = ['top-to-bottom', 'bottom-to-top'].includes(attrs.orientation) > 0 ? (calc.nodeMaxHeight + 100) : (calc.nodeMaxWidth + 100);
// Set constant depth for each nodes
nodes.forEach(d => {
d.y = d.depth * attrs.depth
});
// -------------------------- LINKS ----------------------
// Get links selection
const linkSelection = attrs.centerG.selectAll('path.link')
.data(links, ({id}) => id);
// Enter any new links at the parent's previous position.
const linkEnter = linkSelection.enter()
.insert('path', "g")
.attr("class", "link")
.attr('d', d => {
const o = {
x: x0,
y: y0
};
return this.diagonal(o, o)
});
// Get links update selection
const linkUpdate = linkEnter.merge(linkSelection);
// Styling links
linkUpdate
.attr('marker-start', () => {
return attrs.displayArrow ? 'url(#arrow)' : ''
})
.attr("fill", "none")
.attr("stroke-width", () => attrs.linkWidth || 2)
.attr('stroke', () => this.rgbaObjToColor(attrs.linkColor) || 'green')
// Transition back to the parent element position
linkUpdate.transition()
.duration(attrs.duration)
.attr('d', d => {
return this.diagonal(this.orientations[attrs.orientation].from(d), this.orientations[attrs.orientation].to(d))
});
// Remove any links which is exiting after animation
const linkExit = linkSelection.exit().transition()
.duration(attrs.duration)
.attr('d', d => {
const o = {
x: x,
y: y
};
return this.diagonal(o, o)
})
.remove();
// -------------------------- NODES ----------------------
// Get nodes selection
const nodesSelection = attrs.centerG.selectAll('g.node')
.data(nodes, ({id}) => id)
// Enter any new nodes at the parent's previous position.
const nodeEnter = nodesSelection.enter().append('g')
.attr('class', d => {
return attrs.current === d.id ? 'node current' : 'node'
})
.attr('id', d => d.id)
.attr("transform", d => `translate(${x0},${y0})`)
.attr('cursor', 'pointer')
.on('click', (event, {data}) => {
if ([...event.srcElement.classList].includes('node-expand-button-circle') || [...event.srcElement.classList].includes('node-add-button-circle') || [...event.srcElement.classList].includes('node-remove-button-circle')) {
return;
}
//remove the previous current node style
d3.selectAll('g.node.current > .node-rect')
.attr('stroke-width', ({data}) => data.borderWidth || attrs.strokeWidth)
.attr('stroke', ({borderColor}) => borderColor)
.style("fill", ({backgroundColor}) => backgroundColor)
if (attrs.current && d3.selectAll('g.node.current').length) {
d3.selectAll('g.node.current').node().classList.remove("current")
}
attrs.current = data.nodeId
//add the target node current style
d3.select('#' + data.nodeId).node().classList.add("current")
d3.select('#' + data.nodeId + ' > .node-rect')
.attr('stroke-width', attrs['highlight']['borderWidth'] || attrs['highlight']['strokeWidth'])
.attr('stroke', this.rgbaObjToColor(attrs['highlight']['borderColor']))
.style("fill", this.rgbaObjToColor(attrs['highlight']['backgroundColor']))
attrs.onNodeClick(data.nodeId);
});
// Add background rectangle for the nodes
nodeEnter
.patternify({tag: 'rect', selector: 'node-rect', data: d => [d]})
// Node update styles
const nodeUpdate = nodeEnter.merge(nodesSelection)
.style('font', '12px sans-serif');
// Add foreignObject element inside rectangle
const fo = nodeUpdate
.patternify({
tag: 'foreignObject', selector: 'node-foreign-object', data: d => [d]
})
// Add foreign object
fo.patternify({
tag: 'xhtml:div', selector: 'node-foreign-object-div', data: d => [d]
})
this.restyleForeignObjectElements();
//add button for expand/collapse children nodes
const nodeExpandButtonGroups = nodeEnter
.patternify({
tag: 'g', selector: 'node-expand-button-g', data: d => [d]
})
.on('click', (event,d) => this.onButtonClick(d))
nodeExpandButtonGroups
.patternify({
tag: 'circle', selector: 'node-expand-button-circle', data: d => [d]
})
nodeExpandButtonGroups
.patternify({
tag: 'text', selector: 'node-expand-button-text', data: d => [d]
})
.attr('pointer-events', 'none')
//add button for add children node
const nodeAddButtonGroups = nodeEnter
.patternify({
tag: 'g', selector: 'node-add-button-g', data: d => [d]
})
.on('click', (event,d) => {
attrs.onNodeAdd(d.id);
})
nodeAddButtonGroups
.patternify({
tag: 'circle', selector: 'node-add-button-circle', data: d => [d]
})
nodeAddButtonGroups
.patternify({
tag: 'text', selector: 'node-add-button-text', data: d => [d]
})
.attr('pointer-events', 'none')
//add button for remove one node
const nodeRemoveButtonGroups = nodeEnter
.patternify({
tag: 'g', selector: 'node-remove-button-g', data: d => [d]
})
.on('click', (event, d) => {
attrs.onNodeRemove(d.id);
})
nodeRemoveButtonGroups
.patternify({
tag: 'circle', selector: 'node-remove-button-circle', data: d => [d]
})
nodeRemoveButtonGroups
.patternify({
tag: 'text', selector: 'node-remove-button-text', data: d => [d]
})
.attr('pointer-events', 'none')
// Transition to the proper position for the node
nodeUpdate.transition()
.attr('opacity', 0)
.duration(attrs.duration)
.attr("transform", (d) => `translate(${this.orientations[attrs.orientation].x(d)},${this.orientations[attrs.orientation].y(d)})`)
.attr('opacity', 1)
// Style node rectangles
nodeUpdate.select('.node-rect')
.attr('width', ({data}) => data.width)
.attr('height', ({data}) => data.height)
.attr('x', ({data}) => -data.width / 2)
.attr('y', ({data}) => -data.height / 2)
.attr('rx', ({data}) => data.borderRadius || 0)
.attr('cursor', 'pointer')
//can define highlight style
.attr('stroke-width', ({data}) =>
data.nodeId === attrs.current ? (attrs['highlight']['borderWidth'] || attrs['highlight']['strokeWidth']) : (data.borderWidth || attrs.strokeWidth)
)
.attr('stroke', ({data, borderColor}) => data.nodeId === attrs.current ? this.rgbaObjToColor(attrs['highlight']['borderColor']) : borderColor)
.style("fill", ({data, backgroundColor}) => data.nodeId === attrs.current ? this.rgbaObjToColor(attrs['highlight']['backgroundColor']) : backgroundColor)
//Move node button group to the desired position
nodeUpdate.select('.node-expand-button-g')
.attr('transform', ({data}) => 'translate(' + this.orientations[attrs.orientation].x3(data) + ',' + this.orientations[attrs.orientation].y3(data) + ')')
.attr('display', ({children, _children}) => {
if (attrs.collapsible && (children || _children)) {
return "block";
}
return "none";
})
nodeUpdate.select('.node-expand-button-circle')
.attr('r', 18)
.attr('stroke-width', ({data}) => data.borderWidth || attrs.strokeWidth)
.attr('fill', attrs.backgroundColor)
.attr('stroke', ({borderColor}) => borderColor)
nodeUpdate.select('.node-expand-button-text')
.attr('text-anchor', 'middle')
.attr('alignment-baseline', ({children}) => {
if (children) return 'middle';
return 'top';
})
.attr('fill', attrs.defaultTextFill)
.attr('font-size', ({children}) => {
return 26;
})
.text(({children}) => {
if (children) return '≡';
return '…';
})
.attr('y', this.isEdge() ? 10 : 0)
// Move node button group to the desired position
nodeUpdate.select('.node-add-button-g')
.attr('transform', ({data}) => 'translate(' + this.orientations[attrs.orientation].x1(data) + ',' + this.orientations[attrs.orientation].y1(data) + ')')
.attr('display', ({data}) => {
if (data.added) {
return "block";
}
return "none";
})
nodeUpdate.select('.node-add-button-circle')
.attr('r', 16)
.attr('stroke-width', ({data}) => data.borderWidth || attrs.strokeWidth)
.attr('fill', attrs.backgroundColor)
.attr('stroke', ({borderColor}) => borderColor)
nodeUpdate.select('.node-add-button-text')
.attr('text-anchor', 'middle')
.attr('alignment-baseline', 'middle')
.attr('fill', attrs.defaultTextFill)
.attr('font-size', ({children}) => {
return 26;
})
.text(({children}) => {
return '+';
})
.attr('y', this.isEdge() ? 10 : 0)
// Move node button group to the desired position
nodeUpdate.select('.node-remove-button-g')
.attr('transform', ({data}) => 'translate(' + this.orientations[attrs.orientation].x2(data) + ',' + this.orientations[attrs.orientation].y2(data) + ')')
.attr('display', ({data}) => {
if (data.removed) {
return "block";
}
return "none";
})
nodeUpdate.select('.node-remove-button-circle')
.attr('r', 16)
.attr('stroke-width', ({data}) => data.borderWidth || attrs.strokeWidth)
.attr('fill', attrs.backgroundColor)
.attr('stroke', ({borderColor}) => borderColor)
nodeUpdate.select('.node-remove-button-text')
.attr('text-anchor', 'middle')
.attr('alignment-baseline', 'middle')
.attr('fill', attrs.defaultTextFill)
.attr('font-size', ({children}) => {
return 26;
})
.text(({children}) => {
return '-';
})
.attr('y', this.isEdge() ? 10 : 0)
// Remove any exiting nodes after transition
const nodeExitTransition = nodesSelection.exit()
.attr('opacity', 1)
.transition()
.duration(attrs.duration)
.attr("transform", d => `translate(${x},${y})`)
.on('end', function () {
d3.select(this).remove();
})
.attr('opacity', 0);
// On exit reduce the node rects size to 0
nodeExitTransition.selectAll('.node-rect')
.attr('width', 10)
.attr('height', 10)
.attr('x', 0)
.attr('y', 0);
// Store the old positions for transition.
nodes.forEach(d => {
d.x0 = d.x;
d.y0 = d.y;
});
}
addNode(obj) {
const attrs = this.getChartState();
attrs.data.push(obj);
// Update state of nodes and redraw graph
this.updateNodesState();
return this;
}
removeNode(nodeId) {
const attrs = this.getChartState();
const node = attrs.allNodes.filter(({data}) => data.nodeId == nodeId)[0];
// Remove all node childs
if (node) {
// Retrieve all children nodes ids (including current node itself)
const nodeChildrenIds = this.getNodeChildrenIds(node, []);
// Filter out retrieved nodes and reassign data
attrs.data = attrs.data.filter(d => !nodeChildrenIds.includes(d.nodeId))
const updateNodesState = this.updateNodesState.bind(this);
// Update state of nodes and redraw graph
updateNodesState();
}
}
transformLayout(orientation) {
const attrs = this.getChartState();
attrs.orientation = orientation
this.update(attrs.root)
}
transformStraightLink(straightLink) {
const attrs = this.getChartState();
attrs.straightLink = straightLink
this.update(attrs.root)
}
toggleArrow(displayArrow) {
const attrs = this.getChartState();
attrs.displayArrow = displayArrow
this.update(attrs.root)
}
isEdge() {
return window.navigator.userAgent.includes("Edge");
}
rgbaObjToColor({red, green, blue, alpha}) {
return `rgba(${red},${green},${blue},${alpha})`;
}
diagonal(s, t) {
const attrs = this.getChartState()
if (attrs.straightLink) {
return this.orientations[attrs.orientation].diagonal2(s, t)
} else {
return this.orientations[attrs.orientation].diagonal(s, t)
}
}
restyleForeignObjectElements() {
const attrs = this.getChartState();
attrs.svg.selectAll('.node-foreign-object')
.attr('width', ({width}) => width)
.attr('height', ({height}) => height)
.attr('x', ({width}) => -width / 2)
.attr('y', ({height}) => -height / 2)
attrs.svg.selectAll('.node-foreign-object-div')
.style('width', ({width}) => `${width}px`)
.style('height', ({height}) => `${height}px`)
.style('color', 'white')
.html(({data}) => data.template)
}
onButtonClick(d) {
// If childrens are expanded
if (d.children) {
//Collapse them
d._children = d.children;
d.children = null;
// Set descendants expanded property to false
this.setExpansionFlagToChildren(d, false);
} else {
// Expand children
d.children = d._children;
d._children = null;
// Set each children as expanded
d.children && d.children.forEach(({data}) => data.expanded = true)
}
// Redraw Graph
this.update(d);
}
setExpansionFlagToChildren({data, children, _children}, flag) {
// Set flag to the current property
data.expanded = flag;
// Loop over and recursively update expanded children's descendants
if (children) {
children.forEach(d => {
this.setExpansionFlagToChildren(d, flag)
})
}
// Loop over and recursively update collapsed children's descendants
if (_children) {
_children.forEach(d => {
this.setExpansionFlagToChildren(d, flag)
})
}
}
setExpanded(id, expandedFlag) {
const attrs = this.getChartState();
// Retrieve node by node Id
const node = attrs.allNodes.filter(({data}) => data.nodeId == id)[0]
// If node exists, set expansion flag
if (node) node.data.expanded = expandedFlag;
if (attrs.collapsible) {
// Then collapse them all
attrs.root.children && attrs.root.children.forEach(d => this.collapse(d));
// Then only expand nodes, which have expanded proprty set to true
attrs.root.children && attrs.root.children.forEach(ch => this.expandSomeNodes(ch));
} else {
// Expand all nodes first
attrs.root.children && attrs.root.children.forEach(this.expand);
}
// Redraw graph
this.update(attrs.root);
}
expandSomeNodes(d) {
// If node has expanded property set
if (d.data.expanded) {
// Retrieve node's parent
let parent = d.parent;