ngx-relationship-visualiser 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/.env +2 -0
  2. package/.vscode/settings.json +3 -0
  3. package/angular.json +111 -0
  4. package/dist/README.md +356 -0
  5. package/dist/ngx-relationship-visualiser-1.0.2.tgz +0 -0
  6. package/dist/src/styles.scss +7 -0
  7. package/docs/3rdpartylicenses.txt +1600 -0
  8. package/docs/favicon.ico +1 -0
  9. package/docs/index.html +15 -0
  10. package/docs/main.js +1 -0
  11. package/docs/polyfills.js +1 -0
  12. package/docs/runtime.js +1 -0
  13. package/docs/scripts.js +11 -0
  14. package/docs/styles.css +6 -0
  15. package/ng-package.json +19 -0
  16. package/package.json +30 -16
  17. package/projects/ngx-relationship-visualiser-lib/lib/db/graphDatabase.ts +30 -0
  18. package/projects/ngx-relationship-visualiser-lib/lib/models/data.interface.ts +35 -0
  19. package/projects/ngx-relationship-visualiser-lib/lib/models/mocked-data.ts +150 -0
  20. package/projects/ngx-relationship-visualiser-lib/lib/ngx-relationship-visualiser.module.ts +15 -0
  21. package/projects/ngx-relationship-visualiser-lib/lib/visualiser/services/dagre-layout.service.ts +182 -0
  22. package/projects/ngx-relationship-visualiser-lib/lib/visualiser/services/visualiser-graph.service.ts +1025 -0
  23. package/projects/ngx-relationship-visualiser-lib/lib/visualiser/visualiser-graph/visualiser-graph.component.html +23 -0
  24. package/projects/ngx-relationship-visualiser-lib/lib/visualiser/visualiser-graph/visualiser-graph.component.scss +58 -0
  25. package/projects/ngx-relationship-visualiser-lib/lib/visualiser/visualiser-graph/visualiser-graph.component.ts +132 -0
  26. package/public_api.ts +2 -0
  27. package/src/app/app.component.html +6 -0
  28. package/src/app/app.component.ts +17 -0
  29. package/src/app/app.module.ts +16 -0
  30. package/src/environments/environment.prod.ts +3 -0
  31. package/src/environments/environment.ts +16 -0
  32. package/src/favicon.ico +1 -0
  33. package/src/index.html +15 -0
  34. package/src/main.ts +12 -0
  35. package/src/polyfills.ts +97 -0
  36. package/src/tsconfig.app.json +7 -0
  37. package/src/tslint.json +17 -0
  38. package/tsconfig.json +24 -0
  39. package/tslint.json +131 -0
  40. package/ngx-relationship-visualiser-1.0.1.tgz +0 -0
  41. /package/{esm2022 → dist/esm2022}/ngx-relationship-visualiser.mjs +0 -0
  42. /package/{esm2022 → dist/esm2022}/projects/ngx-relationship-visualiser-lib/lib/db/graphDatabase.mjs +0 -0
  43. /package/{esm2022 → dist/esm2022}/projects/ngx-relationship-visualiser-lib/lib/models/data.interface.mjs +0 -0
  44. /package/{esm2022 → dist/esm2022}/projects/ngx-relationship-visualiser-lib/lib/ngx-relationship-visualiser.module.mjs +0 -0
  45. /package/{esm2022 → dist/esm2022}/projects/ngx-relationship-visualiser-lib/lib/visualiser/services/dagre-layout.service.mjs +0 -0
  46. /package/{esm2022 → dist/esm2022}/projects/ngx-relationship-visualiser-lib/lib/visualiser/services/visualiser-graph.service.mjs +0 -0
  47. /package/{esm2022 → dist/esm2022}/projects/ngx-relationship-visualiser-lib/lib/visualiser/visualiser-graph/visualiser-graph.component.mjs +0 -0
  48. /package/{esm2022 → dist/esm2022}/public_api.mjs +0 -0
  49. /package/{fesm2022 → dist/fesm2022}/ngx-relationship-visualiser.mjs +0 -0
  50. /package/{fesm2022 → dist/fesm2022}/ngx-relationship-visualiser.mjs.map +0 -0
  51. /package/{index.d.ts → dist/index.d.ts} +0 -0
  52. /package/{projects → dist/projects}/ngx-relationship-visualiser-lib/lib/db/graphDatabase.d.ts +0 -0
  53. /package/{projects → dist/projects}/ngx-relationship-visualiser-lib/lib/models/data.interface.d.ts +0 -0
  54. /package/{projects → dist/projects}/ngx-relationship-visualiser-lib/lib/ngx-relationship-visualiser.module.d.ts +0 -0
  55. /package/{projects → dist/projects}/ngx-relationship-visualiser-lib/lib/visualiser/services/dagre-layout.service.d.ts +0 -0
  56. /package/{projects → dist/projects}/ngx-relationship-visualiser-lib/lib/visualiser/services/visualiser-graph.service.d.ts +0 -0
  57. /package/{projects → dist/projects}/ngx-relationship-visualiser-lib/lib/visualiser/visualiser-graph/visualiser-graph.component.d.ts +0 -0
  58. /package/{public_api.d.ts → dist/public_api.d.ts} +0 -0
@@ -0,0 +1,1025 @@
1
+ import { Injectable } from '@angular/core';
2
+ import * as d3 from 'd3';
3
+ import { ReplaySubject } from 'rxjs';
4
+ import { DexieService } from '../../db/graphDatabase';
5
+
6
+ @Injectable({
7
+ providedIn: 'root',
8
+ })
9
+ export class VisualiserGraphService {
10
+ constructor(private dexieService: DexieService) { }
11
+ public links = [];
12
+ public nodes = [];
13
+ public gBrush = null;
14
+ public brushMode = false;
15
+ public brushing = false;
16
+ public shiftKey;
17
+ public extent = null;
18
+ public zoom = false;
19
+ public zoomToFit = false;
20
+ public saveGraphData = new ReplaySubject();
21
+
22
+ public update(data, element, zoom, zoomToFit) {
23
+ const svg = d3.select(element);
24
+ this.zoom = zoom;
25
+ this.zoomToFit = zoomToFit;
26
+ return this._update(d3, svg, data);
27
+ }
28
+
29
+ private ticked(link, node, edgepaths) {
30
+ link.each(function (d, i, n) {
31
+ // Total difference in x and y from source to target
32
+ let diffX = d.target.x - d.source.x;
33
+ let diffY = d.target.y - d.source.y;
34
+
35
+ // Length of path from center of source node to center of target node
36
+ let pathLength = Math.sqrt(diffX * diffX + diffY * diffY);
37
+
38
+ // x and y distances from center to outside edge of target node
39
+ let offsetX = (diffX * 40) / pathLength;
40
+ let offsetY = (diffY * 40) / pathLength;
41
+
42
+ d3.select(n[i])
43
+ .attr('x1', d.source.x + offsetX)
44
+ .attr('y1', d.source.y + offsetY)
45
+ .attr('x2', d.target.x - offsetX)
46
+ .attr('y2', d.target.y - offsetY);
47
+ });
48
+
49
+ node.attr('transform', function (d) {
50
+ return `translate(${d.x}, ${d.y + 50})`;
51
+ });
52
+
53
+ // Sets a boundry for the nodes
54
+ // node.attr('cx', function (d) {
55
+ // return (d.x = Math.max(40, Math.min(900 - 15, d.x)));
56
+ // });
57
+ // node.attr('cy', function (d) {
58
+ // return (d.y = Math.max(50, Math.min(600 - 40, d.y)));
59
+ // });
60
+
61
+ edgepaths.attr('d', function (d) {
62
+ return `M ${d.source.x} ${d.source.y} L ${d.target.x} ${d.target.y}`;
63
+ });
64
+
65
+ edgepaths.attr('transform', function (d) {
66
+ if (d.target.x < d.source.x) {
67
+ const bbox = this.getBBox();
68
+ const rx = bbox.x + bbox.width / 2;
69
+ const ry = bbox.y + bbox.height / 2;
70
+ return `rotate(180 ${rx} ${ry})`;
71
+ } else {
72
+ return 'rotate(0)';
73
+ }
74
+ });
75
+ }
76
+
77
+ private initDefinitions(svg) {
78
+ const defs = svg.append('defs');
79
+
80
+ function createMarker(id, refX, path) {
81
+ defs
82
+ .append('marker')
83
+ .attr('id', id)
84
+ .attr('viewBox', '-0 -5 10 10')
85
+ .attr('refX', refX)
86
+ .attr('refY', 0)
87
+ .attr('orient', 'auto')
88
+ .attr('markerWidth', 8)
89
+ .attr('markerHeight', 8)
90
+ .attr('xoverflow', 'visible')
91
+ .append('svg:path')
92
+ .attr('d', path)
93
+ .attr('fill', '#b4b4b4')
94
+ .style('stroke', 'none');
95
+ }
96
+
97
+ createMarker('arrowheadTarget', 0, 'M 0,-5 L 10 ,0 L 0,5');
98
+ createMarker('arrowheadSource', 2, 'M 10 -5 L 0 0 L 10 5');
99
+
100
+ return svg;
101
+ }
102
+
103
+ private forceSimulation(_d3, { width, height }) {
104
+ return _d3
105
+ .forceSimulation()
106
+ .velocityDecay(0.1)
107
+ .force(
108
+ 'link',
109
+ _d3
110
+ .forceLink()
111
+ .id(function (d) {
112
+ return d.id;
113
+ })
114
+ .distance(500)
115
+ .strength(1)
116
+ )
117
+ .force('charge', _d3.forceManyBody().strength(0.1))
118
+ .force('center', _d3.forceCenter(width / 2, height / 2))
119
+ .force('collision', _d3.forceCollide().radius(15));
120
+ }
121
+
122
+ private compareAndMarkNodesNew(nodes, old_nodes) {
123
+ // Create a map of ids to node objects for the old_nodes array
124
+ const oldMap = old_nodes.reduce((map, node) => {
125
+ map[node.id] = node;
126
+ return map;
127
+ }, {});
128
+
129
+ // Check each node in the nodes array to see if it's new or not
130
+ nodes.forEach((node) => {
131
+ if (!oldMap[node.id]) {
132
+ // Node is new, mark it with the newItem property
133
+ node.newItem = true;
134
+ // Remove the dagre coordinates from new nodes so we can set a random one in view
135
+ node.fx = null;
136
+ node.fy = null;
137
+ }
138
+ });
139
+
140
+ return nodes;
141
+ }
142
+
143
+ private removeNewItem(nodes) {
144
+ for (const node of nodes) {
145
+ if (node.hasOwnProperty('newItem')) {
146
+ delete node.newItem;
147
+ }
148
+ }
149
+ return nodes;
150
+ }
151
+
152
+ private randomiseNodePositions(nodeData, width, height) {
153
+ let minDistance = 100;
154
+ const availableSpace = width * height;
155
+ let adjustedRequiredSpace = nodeData.length * minDistance * minDistance;
156
+
157
+ if (adjustedRequiredSpace > availableSpace) {
158
+ while (adjustedRequiredSpace > availableSpace && minDistance > 0) {
159
+ minDistance -= 10;
160
+ adjustedRequiredSpace = nodeData.length * minDistance * minDistance;
161
+ }
162
+
163
+ if (adjustedRequiredSpace > availableSpace) {
164
+ throw new Error(
165
+ 'Not enough space to accommodate all nodes without a fixed position.'
166
+ );
167
+ }
168
+ }
169
+
170
+ nodeData.forEach((node) => {
171
+ if (node.fx === null && node.fy === null) {
172
+ let currentMinDistance = minDistance;
173
+ let canPlaceNode = false;
174
+
175
+ while (!canPlaceNode && currentMinDistance > 0) {
176
+ node.fx = crypto.getRandomValues(new Uint32Array(1))[0] % width;
177
+ node.fy = crypto.getRandomValues(new Uint32Array(1))[0] % height;
178
+
179
+ canPlaceNode = !nodeData.some((otherNode) => {
180
+ if (
181
+ otherNode.fx === null ||
182
+ otherNode.fy === null ||
183
+ otherNode === node
184
+ ) {
185
+ return false;
186
+ }
187
+
188
+ const dx = otherNode.fx - node.fx;
189
+ const dy = otherNode.fy - node.fy;
190
+ return Math.sqrt(dx * dx + dy * dy) < currentMinDistance;
191
+ });
192
+
193
+ if (!canPlaceNode) {
194
+ currentMinDistance--;
195
+ }
196
+ }
197
+
198
+ if (!canPlaceNode) {
199
+ throw new Error(
200
+ 'Not enough space to accommodate all nodes without a fixed position.'
201
+ );
202
+ }
203
+ }
204
+ });
205
+
206
+ return nodeData;
207
+ }
208
+
209
+ public async _update(_d3, svg, data) {
210
+ const { nodes, links } = data;
211
+ this.nodes = nodes || [];
212
+ this.links = links || [];
213
+
214
+ // Disable the reset btn
215
+
216
+ let saveBtn = document.getElementById('save_graph');
217
+ saveBtn.setAttribute('disabled', 'true');
218
+ // Width/Height of canvas
219
+ const parentWidth = _d3.select('svg').node().parentNode.clientWidth;
220
+ const parentHeight = _d3.select('svg').node().parentNode.clientHeight;
221
+
222
+ // Check to see if nodes are in Dexie
223
+ const oldData = await this.dexieService.getGraphData('nodes');
224
+ const oldNodes = oldData ? oldData.nodes : [];
225
+ if (Array.isArray(oldNodes)) {
226
+ // Compare and set property for new nodes
227
+ this.nodes = this.compareAndMarkNodesNew(nodes, oldNodes);
228
+ // Remove old nodes from Dexie
229
+ await this.dexieService.deleteGraphData('nodes');
230
+ // Add new nodes to Dexie
231
+ await this.dexieService.saveGraphData({ dataId: 'nodes', nodes: data.nodes, links: data.links });
232
+ } else {
233
+ // Add first set of nodes to Dexie
234
+ await this.dexieService.saveGraphData({ dataId: 'nodes', nodes: data.nodes, links: data.links });
235
+ }
236
+
237
+ // If nodes don't have a fx/fy coordinate we generate a random one - dagre nodes without links and new nodes added to canvas have null coordinates by design
238
+ this.nodes = this.randomiseNodePositions(
239
+ this.nodes,
240
+ parentWidth,
241
+ parentHeight
242
+ );
243
+
244
+ // Getting parents lineStyle and adding it to child objects
245
+ const relationshipsArray = this.links.map(
246
+ ({ lineStyle, targetArrow, sourceArrow, relationships }) =>
247
+ relationships.map((r) => ({
248
+ parentLineStyle: lineStyle,
249
+ parentSourceArrow: sourceArrow,
250
+ parentTargetArrow: targetArrow,
251
+ ...r,
252
+ }))
253
+ );
254
+
255
+ // Adding dy value based on link number and position in parent
256
+ relationshipsArray.map((linkRelationship) => {
257
+ linkRelationship.map((linkObject, i) => {
258
+ // dy increments of 15px
259
+ linkObject['dy'] = 20 + i * 15;
260
+ });
261
+ });
262
+
263
+ // IE11 does not like .flat
264
+ this.links = relationshipsArray.reduce((acc, val) => acc.concat(val), []);
265
+
266
+ d3.select('svg').append('g');
267
+
268
+ // Zoom Start
269
+ const zoomContainer = _d3.select('svg g');
270
+ let currentZoom = d3.zoomTransform(d3.select('svg').node());
271
+ const updateZoomLevel = () => {
272
+ const currentScale = currentZoom.k;
273
+ const maxScale = zoom.scaleExtent()[1];
274
+ const zoomPercentage = ((currentScale - 0.5) / (maxScale - 0.5)) * 200;
275
+ const zoomLevelDisplay = document.getElementById('zoom_level');
276
+ const zoomLevelText = `Zoom: ${zoomPercentage.toFixed(0)}%`;
277
+ const zoomInBtn = document.getElementById('zoom_in');
278
+ const zoomOutBtn = document.getElementById('zoom_out');
279
+ const zoomResetBtn = document.getElementById('zoom_reset');
280
+
281
+ // It might not exist depending on the this.zoom boolean
282
+ if (zoomResetBtn) {
283
+ zoomResetBtn.setAttribute('disabled', 'true');
284
+ }
285
+ // Check if the zoom level has changed before updating the display / allows for panning without showing the zoom percentage
286
+ if (zoomLevelDisplay && zoomLevelDisplay.innerHTML !== zoomLevelText) {
287
+ zoomLevelDisplay.innerHTML = zoomLevelText;
288
+ zoomLevelDisplay.style.opacity = '1';
289
+ setTimeout(() => {
290
+ if (zoomLevelDisplay) {
291
+ zoomLevelDisplay.style.opacity = '0';
292
+ }
293
+ }, 2000);
294
+ }
295
+ // Disable the zoomInBtn if the zoom level is at 200%
296
+ if (zoomInBtn) {
297
+ if (zoomPercentage === 200) {
298
+ zoomInBtn.setAttribute('disabled', 'true');
299
+ } else {
300
+ zoomInBtn.removeAttribute('disabled');
301
+ }
302
+ }
303
+ // Disable the zoomOutBtn if the zoom level is at 0%
304
+ if (zoomOutBtn) {
305
+ if (zoomPercentage === 0) {
306
+ zoomOutBtn.setAttribute('disabled', 'true');
307
+ } else {
308
+ zoomOutBtn.removeAttribute('disabled');
309
+ }
310
+ }
311
+ // Disable the zoomResetBtn if the zoom level is at 100%
312
+ if (zoomResetBtn) {
313
+ if (zoomPercentage === 100) {
314
+ zoomResetBtn.setAttribute('disabled', 'true');
315
+ } else {
316
+ zoomResetBtn.removeAttribute('disabled');
317
+ }
318
+ }
319
+ };
320
+ let zoomedInit;
321
+ const zoomed = () => {
322
+ const transform = d3.event.transform;
323
+ zoomContainer.attr(
324
+ 'transform',
325
+ `translate(${transform.x}, ${transform.y}) scale(${transform.k})`
326
+ );
327
+ currentZoom = transform;
328
+ zoomedInit = true;
329
+ updateZoomLevel();
330
+ };
331
+
332
+ const zoom = d3
333
+ .zoom()
334
+ .scaleExtent([0.5, 1.5])
335
+ .on('start', function () {
336
+ d3.select(this).style('cursor', this.zoom ? null : 'grabbing');
337
+ })
338
+ .on('zoom', this.zoom ? zoomed : null)
339
+ .on('end', function () {
340
+ d3.select(this).style('cursor', 'grab');
341
+ });
342
+ svg
343
+ .call(zoom)
344
+ .style('cursor', 'grab')
345
+ .on(this.zoom ? null : 'wheel.zoom', null)
346
+ .on('dblclick.zoom', null);
347
+ zoom.filter(() => !d3.event.shiftKey);
348
+
349
+ // Zoom button controls
350
+ d3.select('#zoom_in').on('click', function () {
351
+ zoom.scaleBy(svg.transition().duration(750), 1.2);
352
+ updateZoomLevel();
353
+ });
354
+ d3.select('#zoom_out').on('click', function () {
355
+ zoom.scaleBy(svg.transition().duration(750), 0.8);
356
+ updateZoomLevel();
357
+ });
358
+ d3.select('#zoom_reset').on('click', function () {
359
+ zoom.scaleTo(svg.transition().duration(750), 1);
360
+ updateZoomLevel();
361
+ });
362
+ // Zoom to fit function and Button
363
+ const handleZoomToFit = () => {
364
+ const nodeBBox = zoomContainer.node().getBBox();
365
+
366
+ // Calculate scale and translate values to fit all nodes
367
+ const padding = 30;
368
+ const scaleX = (parentWidth - padding * 2) / nodeBBox.width;
369
+ const scaleY = (parentHeight - padding * 2) / nodeBBox.height;
370
+ const scale = Math.min(scaleX, scaleY, 1.0); // Restrict scale to a maximum of 1.0
371
+
372
+ const translateX =
373
+ -nodeBBox.x * scale + (parentWidth - nodeBBox.width * scale) / 2;
374
+ const translateY =
375
+ -nodeBBox.y * scale + (parentHeight - nodeBBox.height * scale) / 2;
376
+
377
+ // Get the bounding box of all nodes
378
+ const allNodes = zoomContainer.selectAll('.node-wrapper');
379
+ const allNodesBBox = allNodes.nodes().reduce(
380
+ (acc, node) => {
381
+ const nodeBBox = node.getBBox();
382
+ acc.x = Math.min(acc.x, nodeBBox.x);
383
+ acc.y = Math.min(acc.y, nodeBBox.y);
384
+ acc.width = Math.max(acc.width, nodeBBox.x + nodeBBox.width);
385
+ acc.height = Math.max(acc.height, nodeBBox.y + nodeBBox.height);
386
+ return acc;
387
+ },
388
+ { x: Infinity, y: Infinity, width: -Infinity, height: -Infinity }
389
+ );
390
+
391
+ // Check if all nodes are within the viewable container
392
+ if (
393
+ allNodesBBox.x * scale >= 0 &&
394
+ allNodesBBox.y * scale >= 0 &&
395
+ allNodesBBox.width * scale <= parentWidth &&
396
+ allNodesBBox.height * scale <= parentHeight
397
+ ) {
398
+ // All nodes are within the viewable container, no need to apply zoom transform
399
+ return;
400
+ }
401
+
402
+ // Manually reset the zoom transform
403
+ zoomContainer
404
+ .transition()
405
+ .duration(750)
406
+ .attr('transform', 'translate(0, 0) scale(1)');
407
+
408
+ // Apply zoom transform to zoomContainer
409
+ zoomContainer
410
+ .transition()
411
+ .duration(750)
412
+ .attr(
413
+ 'transform',
414
+ `translate(${translateX}, ${translateY}) scale(${scale})`
415
+ );
416
+
417
+ // Update the currentZoom variable with the new transform
418
+ // zoomedInit - created because if zoomToFit is called before anything else it screws up the base transform - e.g. showCurrentMatch
419
+ if (zoomedInit) {
420
+ currentZoom.x = translateX;
421
+ currentZoom.y = translateY;
422
+ currentZoom.k = scale;
423
+ }
424
+ updateZoomLevel();
425
+ };
426
+
427
+ d3.select('#zoom_to_fit').on('click', handleZoomToFit);
428
+
429
+ // Check if zoom level is at 0% or 100% before allowing mousewheel zoom - this stabilises the canvas when the limit is reached
430
+ svg.on('wheel', () => {
431
+ const currentScale = currentZoom.k;
432
+ const maxScale = zoom.scaleExtent()[1];
433
+ const minScale = zoom.scaleExtent()[0];
434
+ if (currentScale === maxScale || currentScale === minScale) {
435
+ d3.event.preventDefault();
436
+ }
437
+ });
438
+
439
+ // Zoom End
440
+
441
+ // For arrows
442
+ this.initDefinitions(svg);
443
+
444
+ const simulation = this.forceSimulation(_d3, {
445
+ width: +svg.attr('width'),
446
+ height: +svg.attr('height'),
447
+ });
448
+
449
+ // Brush Start
450
+ let gBrushHolder = svg.append('g');
451
+
452
+ let brush = d3
453
+ .brush()
454
+ .on('start', () => {
455
+ this.brushing = true;
456
+
457
+ nodeEnter.each((d) => {
458
+ d.previouslySelected = this.shiftKey && d.selected;
459
+ });
460
+
461
+ _d3
462
+ .selectAll('.edgelabel')
463
+ .style('font-weight', 400)
464
+ .style('fill', '#212529');
465
+ })
466
+ .on('brush', () => {
467
+ this.extent = d3.event.selection;
468
+ if (!d3.event.sourceEvent || !this.extent || !this.brushMode) return;
469
+ if (!currentZoom) return;
470
+
471
+ nodeEnter
472
+ .classed('selected', (d) => {
473
+ return (d.selected =
474
+ d.previouslySelected ^
475
+ (<any>(
476
+ (d3.event.selection[0][0] <=
477
+ d.x * currentZoom.k + currentZoom.x &&
478
+ d.x * currentZoom.k + currentZoom.x <
479
+ d3.event.selection[1][0] &&
480
+ d3.event.selection[0][1] <=
481
+ d.y * currentZoom.k + currentZoom.y &&
482
+ d.y * currentZoom.k + currentZoom.y <
483
+ d3.event.selection[1][1])
484
+ )));
485
+ })
486
+ .select('.nodeText')
487
+ .classed('selected', (d) => d.selected)
488
+ .style('fill', (d) => (d.selected ? 'blue' : '#999'))
489
+ .style('font-weight', (d) => (d.selected ? 700 : 400));
490
+
491
+ this.extent = d3.event.selection;
492
+ })
493
+ .on('end', () => {
494
+ if (!d3.event.sourceEvent || !this.extent || !this.gBrush) return;
495
+
496
+ this.gBrush.call(brush.move, null);
497
+ if (!this.brushMode) {
498
+ // the shift key has been release before we ended our brushing
499
+ this.gBrush.remove();
500
+ this.gBrush = null;
501
+ }
502
+ this.brushing = false;
503
+
504
+ nodeEnter
505
+ .select('.nodeText')
506
+ .filter(function () {
507
+ return !d3.select(this.parentNode).classed('selected');
508
+ })
509
+ .style('fill', '#212529')
510
+ .style('font-weight', 400);
511
+ });
512
+
513
+ let keyup = () => {
514
+ this.shiftKey = false;
515
+ this.brushMode = false;
516
+ if (this.gBrush && !this.brushing) {
517
+ // only remove the brush if we're not actively brushing
518
+ // otherwise it'll be removed when the brushing ends
519
+ this.gBrush.remove();
520
+ this.gBrush = null;
521
+ }
522
+ };
523
+
524
+ let keydown = () => {
525
+ // Allows us to turn off default listeners for keyModifiers(shift)
526
+ brush.filter(() => d3.event.shiftKey);
527
+ brush.keyModifiers(false);
528
+ // holding shift key
529
+ if (d3.event.keyCode === 16) {
530
+ this.shiftKey = true;
531
+
532
+ if (!this.gBrush) {
533
+ this.brushMode = true;
534
+ this.gBrush = gBrushHolder.append('g').attr('class', 'brush');
535
+ this.gBrush.call(brush);
536
+ }
537
+ }
538
+ };
539
+
540
+ d3.select('body').on('keydown', keydown).on('keyup', keyup);
541
+ // Brush End
542
+
543
+ const filteredLine = this.links.filter(
544
+ ({ source, target }, index, linksArray) => {
545
+ // Filter out any objects that have matching source and target property values
546
+ // To display only one line (parentLineStyle) - removes html bloat and a darkened line
547
+ return (
548
+ index ===
549
+ linksArray.findIndex(
550
+ (obj) => obj.source === source && obj.target === target
551
+ )
552
+ );
553
+ }
554
+ );
555
+
556
+ const link = zoomContainer.selectAll().data(filteredLine, function (d) {
557
+ return d.id;
558
+ });
559
+
560
+ zoomContainer.selectAll('line').data(link).exit().remove();
561
+
562
+ const linkEnter = link
563
+ .join('line')
564
+ .style('stroke', function (d) {
565
+ if (d.parentLineStyle === 'Solid') {
566
+ return '#777';
567
+ } else {
568
+ return '#b4b4b4';
569
+ }
570
+ })
571
+ .style('stroke-opacity', '.6')
572
+ .style('stroke-dasharray', function (d) {
573
+ if (d.parentLineStyle === 'Dotted') {
574
+ return '8,5';
575
+ }
576
+ return null;
577
+ })
578
+ .style('stroke-width', '2px')
579
+ .attr('class', 'link')
580
+ .attr('id', function (d) {
581
+ const suffix = '_line';
582
+ const source = d.source ? d.source : '';
583
+ const target = d.target ? d.target : '';
584
+ return `${source}_${target}${suffix}`;
585
+ })
586
+ .attr('marker-end', function (d) {
587
+ if (d.parentTargetArrow === true) {
588
+ return 'url(#arrowheadTarget)';
589
+ }
590
+ return null;
591
+ })
592
+ .attr('marker-start', function (d) {
593
+ if (d.parentSourceArrow === true) {
594
+ return 'url(#arrowheadSource)';
595
+ }
596
+ return null;
597
+ });
598
+
599
+ link.append('title').text(function (d) {
600
+ return d.label;
601
+ });
602
+
603
+ const edgepaths = zoomContainer.selectAll().data(this.links, function (d) {
604
+ return d.id;
605
+ });
606
+
607
+ zoomContainer.selectAll('path').data(edgepaths).exit().remove();
608
+
609
+ const edgepathsEnter = edgepaths
610
+ .join('svg:path')
611
+ .attr('class', 'edgepath')
612
+ .attr('fill-opacity', 0)
613
+ .attr('stroke-opacity', 0)
614
+ .attr('id', function (d, i) {
615
+ return 'edgepath' + i;
616
+ });
617
+
618
+ const edgelabels = zoomContainer.selectAll().data(this.links, function (d) {
619
+ return d.id;
620
+ });
621
+
622
+ zoomContainer.selectAll('text').data(edgelabels).exit().remove();
623
+
624
+ const edgelabelsEnter = edgelabels
625
+ .enter()
626
+ .append('text')
627
+ .attr('class', 'edgelabel')
628
+ .attr('id', function (d) {
629
+ const suffix = '_text';
630
+ const source = d.source ? d.source : '';
631
+ const target = d.target ? d.target : '';
632
+ return `${source}_${target}${suffix}`;
633
+ })
634
+ .style('text-anchor', 'middle')
635
+ .attr('font-size', 14)
636
+ .attr('dy', function (d) {
637
+ return d.dy;
638
+ });
639
+
640
+
641
+ edgelabelsEnter
642
+ .append('textPath')
643
+ .attr('xlink:href', function (d, i) {
644
+ return '#edgepath' + i;
645
+ })
646
+ .style('cursor', 'pointer')
647
+ .attr('dominant-baseline', 'bottom')
648
+ .attr('startOffset', '50%')
649
+ .text(function (d) {
650
+ return d.label;
651
+ });
652
+
653
+ edgelabelsEnter
654
+ .selectAll('textPath')
655
+ .filter(function (d) {
656
+ return d.linkIcon;
657
+ })
658
+ .append('tspan')
659
+ .style('fill', '#856404')
660
+ .style('font-weight', '700')
661
+ .attr('class', 'fa')
662
+ .text(' \uf0c1');
663
+ // on normal label link click - highlight labels
664
+ svg.selectAll('.edgelabel').on('click', function (d) {
665
+ _d3.event.stopPropagation();
666
+ nodeEnter.each(function (d) {
667
+ d.selected = false;
668
+ d.previouslySelected = false;
669
+ });
670
+ node.classed('selected', false);
671
+ _d3
672
+ .selectAll('.nodeText')
673
+ .style('fill', '#212529')
674
+ .style('font-weight', 400);
675
+ _d3
676
+ .selectAll('.edgelabel')
677
+ .style('font-weight', 400)
678
+ .style('fill', '#212529');
679
+ _d3.select(this).style('fill', 'blue').style('font-weight', 700);
680
+ });
681
+
682
+ const node = zoomContainer.selectAll().data(this.nodes, function (d) {
683
+ return d.id;
684
+ });
685
+
686
+ zoomContainer.selectAll('g').data(node).exit().remove();
687
+
688
+ const nodeEnter = node
689
+ .join('g')
690
+ .call(
691
+ _d3
692
+ .drag()
693
+ .on('start', function dragstarted(d) {
694
+ // Enable the save & reset btn
695
+ document.getElementById('save_graph').removeAttribute('disabled');
696
+
697
+ if (!_d3.event.active) simulation.alphaTarget(0.9).restart();
698
+
699
+ if (!d.selected && !this.shiftKey) {
700
+ // if this node isn't selected, then we have to unselect every other node
701
+ nodeEnter.classed('selected', function (p) {
702
+ return (p.selected = p.previouslySelected = false);
703
+ });
704
+ // remove the selected styling on other nodes and labels when we drag a non-selected node
705
+ _d3
706
+ .selectAll('.edgelabel')
707
+ .style('fill', '#212529')
708
+ .style('font-weight', 400);
709
+ _d3
710
+ .selectAll('.nodeText')
711
+ .style('font-weight', 400)
712
+ .style('fill', '#212529');
713
+ }
714
+
715
+ _d3.select(this).classed('selected', function (p) {
716
+ d.previouslySelected = d.selected;
717
+ return (d.selected = true);
718
+ });
719
+
720
+ nodeEnter
721
+ .filter(function (d) {
722
+ return d.selected;
723
+ })
724
+ .each(function (d) {
725
+ d.fx = d.x;
726
+ d.fy = d.y;
727
+ });
728
+ })
729
+ .on('drag', function dragged(d) {
730
+ nodeEnter
731
+ .filter(function (d) {
732
+ return d.selected;
733
+ })
734
+ .each(function (d) {
735
+ d.fx += _d3.event.dx;
736
+ d.fy += _d3.event.dy;
737
+ });
738
+ })
739
+ .on('end', function dragended(d) {
740
+ if (!_d3.event.active) {
741
+ simulation.alphaTarget(0.3).restart();
742
+ }
743
+ d.fx = d.x;
744
+ d.fy = d.y;
745
+ // Subscribes to updated graph positions for save
746
+ self.saveGraphData.next(data);
747
+ })
748
+ )
749
+ .attr('class', 'node-wrapper')
750
+ .attr('id', function (d) {
751
+ return d.id;
752
+ });
753
+
754
+ // no collision - already using this in statement
755
+ const self = this;
756
+
757
+ // node click and ctrl + click
758
+ svg.selectAll('.node-wrapper').on('click', function (d) {
759
+ // so we don't activate the canvas .click event
760
+ _d3.event.stopPropagation();
761
+
762
+ // setting the select attribute to the object on single select so we can drag them
763
+ d.selected = true;
764
+
765
+ svg.selectAll('.node-wrapper').classed('selected', false);
766
+ d3.select(this).classed('selected', true);
767
+ nodeEnter.each(function (d) {
768
+ d.selected = false;
769
+ d.previouslySelected = false;
770
+ });
771
+
772
+ // remove style from selected node before the class is removed
773
+ _d3
774
+ .selectAll('.selected')
775
+ .selectAll('.nodeText')
776
+ .style('fill', '#212529');
777
+ // Remove styles from all other nodes and labels on single left click
778
+ _d3.selectAll('.edgelabel').style('fill', '#212529');
779
+ _d3
780
+ .selectAll('.nodeText')
781
+ .style('fill', '#212529')
782
+ .style('font-weight', 400);
783
+ // Add style on single left click
784
+ _d3
785
+ .select(this)
786
+ .select('.nodeText')
787
+ .style('fill', 'blue')
788
+ .style('font-weight', 700);
789
+
790
+ return null;
791
+ });
792
+
793
+ //click on canvas to remove selected nodes
794
+ _d3.select('svg').on('click', () => {
795
+ nodeEnter.each(function (d) {
796
+ d.selected = false;
797
+ d.previouslySelected = false;
798
+ });
799
+ node.classed('selected', false);
800
+ _d3
801
+ .selectAll('.selected')
802
+ .selectAll('.nodeText')
803
+ .style('fill', '#212529')
804
+ .style('font-weight', 400);
805
+ _d3.selectAll('.selected').classed('selected', false);
806
+ _d3
807
+ .selectAll('.edgelabel')
808
+ .style('fill', '#212529')
809
+ .style('font-weight', 400);
810
+ });
811
+
812
+ nodeEnter
813
+ .filter(function (d) {
814
+ if (!d.imageUrl || d.imageUrl === '') {
815
+ return null;
816
+ }
817
+ return true;
818
+ })
819
+ .append('image')
820
+ .attr('xlink:href', function (d) {
821
+ return d.imageUrl;
822
+ })
823
+ .attr('x', -15)
824
+ .attr('y', -60)
825
+ .attr('class', function (d) {
826
+ const suffix = 'image';
827
+ const id = d.id ? d.id : '';
828
+ return `${id}_${suffix}`;
829
+ })
830
+ .attr('id', function (d) {
831
+ const id = d.id ? d.id : '';
832
+ return `${id}`;
833
+ })
834
+ .attr('width', 32)
835
+ .attr('height', 32)
836
+ .attr('class', 'image')
837
+ .style('cursor', 'pointer');
838
+
839
+ nodeEnter
840
+ .filter(function (d) {
841
+ if (!d.icon || d.icon === '') {
842
+ return null;
843
+ }
844
+ return true;
845
+ })
846
+ .append('text')
847
+ .text(function (d) {
848
+ return d.icon;
849
+ })
850
+ .attr('x', -18)
851
+ .attr('y', -30)
852
+ .attr('class', function (d) {
853
+ const suffix = 'icon';
854
+ const id = d.id ? d.id : '';
855
+ return `${id}_${suffix} fa`;
856
+ })
857
+ .attr('id', function (d) {
858
+ const id = d.id ? d.id : '';
859
+ return `${id}`;
860
+ })
861
+ .style('font-size', '35px')
862
+ .style('cursor', 'pointer');
863
+
864
+ const nodeText = nodeEnter
865
+ .append('text')
866
+ .attr('id', 'nodeText')
867
+ .attr('class', 'nodeText')
868
+ .style('text-anchor', 'middle')
869
+ .style('cursor', 'pointer')
870
+ .attr('dy', -3)
871
+ .attr('y', -25)
872
+ .attr('testhook', function (d) {
873
+ const suffix = 'text';
874
+ const id = d.id ? d.id : '';
875
+ return `${id}_${suffix}`;
876
+ });
877
+
878
+ nodeText
879
+ .selectAll('tspan.text')
880
+ .data((d) => d.label)
881
+ .enter()
882
+ .append('tspan')
883
+ .attr('class', 'nodeTextTspan')
884
+ .text((d) => d)
885
+ .style('font-size', '14px')
886
+ .attr('x', -10)
887
+ .attr('dx', 10)
888
+ .attr('dy', 15);
889
+
890
+ nodeEnter
891
+ .filter(function (d) {
892
+ if (!d.additionalIcon) {
893
+ return null;
894
+ }
895
+ return true;
896
+ })
897
+ .append('text')
898
+ .attr('id', function (d) {
899
+ const suffix = 'additionalIcon';
900
+ const id = d.id ? d.id : '';
901
+ return `${id}_${suffix}`;
902
+ })
903
+ .attr('width', 100)
904
+ .attr('height', 25)
905
+ .attr('x', 30)
906
+ .attr('y', -50)
907
+ .attr('class', 'fa')
908
+ .style('fill', '#856404')
909
+ .text(function (d) {
910
+ return d.additionalIcon;
911
+ });
912
+
913
+ // transition effects for new pulsating nodes
914
+ nodeEnter
915
+ .filter(function (d) {
916
+ if (!d.newItem) {
917
+ return null;
918
+ }
919
+ return true;
920
+ })
921
+ .select('text')
922
+ .transition()
923
+ .duration(1000)
924
+ .attr('fill', 'blue')
925
+ .attr('font-weight', 700)
926
+ .attr('fill-opacity', 1)
927
+ .transition()
928
+ .duration(1000)
929
+ .attr('fill', 'blue')
930
+ .attr('font-weight', 700)
931
+ .attr('fill-opacity', 0.1)
932
+ .transition()
933
+ .duration(1000)
934
+ .attr('fill', 'blue')
935
+ .attr('font-weight', 700)
936
+ .attr('fill-opacity', 1)
937
+ .transition()
938
+ .duration(1000)
939
+ .attr('fill', 'blue')
940
+ .attr('font-weight', 700)
941
+ .attr('fill-opacity', 0.1)
942
+ .transition()
943
+ .duration(1000)
944
+ .attr('fill', 'blue')
945
+ .attr('font-weight', 700)
946
+ .attr('fill-opacity', 1)
947
+ .transition()
948
+ .duration(1000)
949
+ .attr('fill', '#212529')
950
+ .attr('font-weight', 400)
951
+ .attr('fill-opacity', 1)
952
+ .on('end', function () {
953
+ return d3.select(this).call(_d3.transition);
954
+ });
955
+
956
+ nodeEnter
957
+ .filter(function (d) {
958
+ if (!d.newItem) {
959
+ return null;
960
+ }
961
+ return true;
962
+ })
963
+ .select('image')
964
+ .transition()
965
+ .duration(1000)
966
+ .attr('width', 45)
967
+ .attr('height', 45)
968
+ .transition()
969
+ .duration(1000)
970
+ .attr('width', 32)
971
+ .attr('height', 32)
972
+ .transition()
973
+ .duration(1000)
974
+ .attr('width', 45)
975
+ .attr('height', 45)
976
+ .transition()
977
+ .duration(1000)
978
+ .attr('width', 32)
979
+ .attr('height', 32)
980
+ .transition()
981
+ .duration(1000)
982
+ .attr('width', 45)
983
+ .attr('height', 45)
984
+ .transition()
985
+ .duration(1000)
986
+ .attr('width', 32)
987
+ .attr('height', 32)
988
+ .on('end', function () {
989
+ return d3.select(this).call(d3.transition);
990
+ });
991
+
992
+ // Remove the newClass so they don't animate next time
993
+ this.nodes = this.removeNewItem(this.nodes);
994
+
995
+ nodeEnter.append('title').text(function (d) {
996
+ return d.label;
997
+ });
998
+
999
+ const maxTicks = 30;
1000
+ let tickCount = 0;
1001
+ let zoomToFitCalled = false;
1002
+
1003
+ simulation.nodes(this.nodes).on('tick', () => {
1004
+ if (this.zoomToFit && tickCount >= maxTicks && !zoomToFitCalled) {
1005
+ simulation.stop();
1006
+ handleZoomToFit();
1007
+ zoomToFitCalled = true;
1008
+ } else {
1009
+ this.ticked(linkEnter, nodeEnter, edgepathsEnter);
1010
+ tickCount++;
1011
+ }
1012
+ });
1013
+
1014
+ simulation.force('link').links(this.links);
1015
+ self.saveGraphData.next(data);
1016
+ }
1017
+
1018
+ public resetGraph(initialData, element, zoom, zoomToFit) {
1019
+ // Reset the data to its initial state
1020
+ this.nodes = [];
1021
+ this.links = [];
1022
+ // Call the update method again to re-simulate the graph with the new data
1023
+ this.update(initialData, element, zoom, zoomToFit);
1024
+ }
1025
+ }