ngx-relationship-visualiser 1.0.6 → 1.0.7

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 (39) hide show
  1. package/dist/ngx-relationship-visualiser-1.0.7.tgz +0 -0
  2. package/package.json +1 -1
  3. package/.env +0 -2
  4. package/.vscode/settings.json +0 -3
  5. package/angular.json +0 -111
  6. package/dist/ngx-relationship-visualiser-1.0.6.tgz +0 -0
  7. package/docs/3rdpartylicenses.txt +0 -1574
  8. package/docs/favicon.ico +0 -1
  9. package/docs/index.html +0 -15
  10. package/docs/main.js +0 -1
  11. package/docs/polyfills.js +0 -1
  12. package/docs/runtime.js +0 -1
  13. package/docs/scripts.js +0 -11
  14. package/docs/styles.css +0 -0
  15. package/ng-package.json +0 -18
  16. package/projects/ngx-relationship-visualiser-lib/lib/db/graphDatabase.ts +0 -30
  17. package/projects/ngx-relationship-visualiser-lib/lib/models/data.interface.ts +0 -35
  18. package/projects/ngx-relationship-visualiser-lib/lib/models/mocked-data.ts +0 -150
  19. package/projects/ngx-relationship-visualiser-lib/lib/ngx-relationship-visualiser.module.ts +0 -15
  20. package/projects/ngx-relationship-visualiser-lib/lib/visualiser/services/dagre-layout.service.ts +0 -182
  21. package/projects/ngx-relationship-visualiser-lib/lib/visualiser/services/visualiser-graph.service.ts +0 -1031
  22. package/projects/ngx-relationship-visualiser-lib/lib/visualiser/visualiser-graph/visualiser-graph.component.html +0 -23
  23. package/projects/ngx-relationship-visualiser-lib/lib/visualiser/visualiser-graph/visualiser-graph.component.scss +0 -61
  24. package/projects/ngx-relationship-visualiser-lib/lib/visualiser/visualiser-graph/visualiser-graph.component.ts +0 -132
  25. package/public_api.ts +0 -2
  26. package/src/app/app.component.html +0 -6
  27. package/src/app/app.component.ts +0 -17
  28. package/src/app/app.module.ts +0 -16
  29. package/src/environments/environment.prod.ts +0 -3
  30. package/src/environments/environment.ts +0 -16
  31. package/src/favicon.ico +0 -1
  32. package/src/index.html +0 -15
  33. package/src/main.ts +0 -12
  34. package/src/polyfills.ts +0 -97
  35. package/src/styles.scss +0 -0
  36. package/src/tsconfig.app.json +0 -7
  37. package/src/tslint.json +0 -17
  38. package/tsconfig.json +0 -24
  39. package/tslint.json +0 -131
@@ -1,1031 +0,0 @@
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
- // Apply styling for the rect.selection
467
- d3.selectAll('rect.selection')
468
- .style('stroke', 'none')
469
- .style('fill', 'steelblue')
470
- .style('fill-opacity', 0.4);
471
- })
472
- .on('brush', () => {
473
- this.extent = d3.event.selection;
474
- if (!d3.event.sourceEvent || !this.extent || !this.brushMode) return;
475
- if (!currentZoom) return;
476
-
477
- nodeEnter
478
- .classed('selected', (d) => {
479
- return (d.selected =
480
- d.previouslySelected ^
481
- (<any>(
482
- (d3.event.selection[0][0] <=
483
- d.x * currentZoom.k + currentZoom.x &&
484
- d.x * currentZoom.k + currentZoom.x <
485
- d3.event.selection[1][0] &&
486
- d3.event.selection[0][1] <=
487
- d.y * currentZoom.k + currentZoom.y &&
488
- d.y * currentZoom.k + currentZoom.y <
489
- d3.event.selection[1][1])
490
- )));
491
- })
492
- .select('.nodeText')
493
- .classed('selected', (d) => d.selected)
494
- .style('fill', (d) => (d.selected ? 'blue' : '#999'))
495
- .style('font-weight', (d) => (d.selected ? 700 : 400));
496
-
497
- this.extent = d3.event.selection;
498
- })
499
- .on('end', () => {
500
- if (!d3.event.sourceEvent || !this.extent || !this.gBrush) return;
501
-
502
- this.gBrush.call(brush.move, null);
503
- if (!this.brushMode) {
504
- // the shift key has been release before we ended our brushing
505
- this.gBrush.remove();
506
- this.gBrush = null;
507
- }
508
- this.brushing = false;
509
-
510
- nodeEnter
511
- .select('.nodeText')
512
- .filter(function () {
513
- return !d3.select(this.parentNode).classed('selected');
514
- })
515
- .style('fill', '#212529')
516
- .style('font-weight', 400);
517
- });
518
-
519
- let keyup = () => {
520
- this.shiftKey = false;
521
- this.brushMode = false;
522
- if (this.gBrush && !this.brushing) {
523
- // only remove the brush if we're not actively brushing
524
- // otherwise it'll be removed when the brushing ends
525
- this.gBrush.remove();
526
- this.gBrush = null;
527
- }
528
- };
529
-
530
- let keydown = () => {
531
- // Allows us to turn off default listeners for keyModifiers(shift)
532
- brush.filter(() => d3.event.shiftKey);
533
- brush.keyModifiers(false);
534
- // holding shift key
535
- if (d3.event.keyCode === 16) {
536
- this.shiftKey = true;
537
-
538
- if (!this.gBrush) {
539
- this.brushMode = true;
540
- this.gBrush = gBrushHolder.append('g').attr('class', 'brush');
541
- this.gBrush.call(brush);
542
- }
543
- }
544
- };
545
-
546
- d3.select('body').on('keydown', keydown).on('keyup', keyup);
547
- // Brush End
548
-
549
- const filteredLine = this.links.filter(
550
- ({ source, target }, index, linksArray) => {
551
- // Filter out any objects that have matching source and target property values
552
- // To display only one line (parentLineStyle) - removes html bloat and a darkened line
553
- return (
554
- index ===
555
- linksArray.findIndex(
556
- (obj) => obj.source === source && obj.target === target
557
- )
558
- );
559
- }
560
- );
561
-
562
- const link = zoomContainer.selectAll().data(filteredLine, function (d) {
563
- return d.id;
564
- });
565
-
566
- zoomContainer.selectAll('line').data(link).exit().remove();
567
-
568
- const linkEnter = link
569
- .join('line')
570
- .style('stroke', function (d) {
571
- if (d.parentLineStyle === 'Solid') {
572
- return '#777';
573
- } else {
574
- return '#b4b4b4';
575
- }
576
- })
577
- .style('stroke-opacity', '.6')
578
- .style('stroke-dasharray', function (d) {
579
- if (d.parentLineStyle === 'Dotted') {
580
- return '8,5';
581
- }
582
- return null;
583
- })
584
- .style('stroke-width', '2px')
585
- .attr('class', 'link')
586
- .attr('id', function (d) {
587
- const suffix = '_line';
588
- const source = d.source ? d.source : '';
589
- const target = d.target ? d.target : '';
590
- return `${source}_${target}${suffix}`;
591
- })
592
- .attr('marker-end', function (d) {
593
- if (d.parentTargetArrow === true) {
594
- return 'url(#arrowheadTarget)';
595
- }
596
- return null;
597
- })
598
- .attr('marker-start', function (d) {
599
- if (d.parentSourceArrow === true) {
600
- return 'url(#arrowheadSource)';
601
- }
602
- return null;
603
- });
604
-
605
- link.append('title').text(function (d) {
606
- return d.label;
607
- });
608
-
609
- const edgepaths = zoomContainer.selectAll().data(this.links, function (d) {
610
- return d.id;
611
- });
612
-
613
- zoomContainer.selectAll('path').data(edgepaths).exit().remove();
614
-
615
- const edgepathsEnter = edgepaths
616
- .join('svg:path')
617
- .attr('class', 'edgepath')
618
- .attr('fill-opacity', 0)
619
- .attr('stroke-opacity', 0)
620
- .attr('id', function (d, i) {
621
- return 'edgepath' + i;
622
- });
623
-
624
- const edgelabels = zoomContainer.selectAll().data(this.links, function (d) {
625
- return d.id;
626
- });
627
-
628
- zoomContainer.selectAll('text').data(edgelabels).exit().remove();
629
-
630
- const edgelabelsEnter = edgelabels
631
- .enter()
632
- .append('text')
633
- .attr('class', 'edgelabel')
634
- .attr('id', function (d) {
635
- const suffix = '_text';
636
- const source = d.source ? d.source : '';
637
- const target = d.target ? d.target : '';
638
- return `${source}_${target}${suffix}`;
639
- })
640
- .style('text-anchor', 'middle')
641
- .attr('font-size', 14)
642
- .attr('dy', function (d) {
643
- return d.dy;
644
- });
645
-
646
-
647
- edgelabelsEnter
648
- .append('textPath')
649
- .attr('xlink:href', function (d, i) {
650
- return '#edgepath' + i;
651
- })
652
- .style('cursor', 'pointer')
653
- .attr('dominant-baseline', 'bottom')
654
- .attr('startOffset', '50%')
655
- .text(function (d) {
656
- return d.label;
657
- });
658
-
659
- edgelabelsEnter
660
- .selectAll('textPath')
661
- .filter(function (d) {
662
- return d.linkIcon;
663
- })
664
- .append('tspan')
665
- .style('fill', '#856404')
666
- .style('font-weight', '700')
667
- .attr('class', 'fa')
668
- .text(' \uf0c1');
669
- // on normal label link click - highlight labels
670
- svg.selectAll('.edgelabel').on('click', function (d) {
671
- _d3.event.stopPropagation();
672
- nodeEnter.each(function (d) {
673
- d.selected = false;
674
- d.previouslySelected = false;
675
- });
676
- node.classed('selected', false);
677
- _d3
678
- .selectAll('.nodeText')
679
- .style('fill', '#212529')
680
- .style('font-weight', 400);
681
- _d3
682
- .selectAll('.edgelabel')
683
- .style('font-weight', 400)
684
- .style('fill', '#212529');
685
- _d3.select(this).style('fill', 'blue').style('font-weight', 700);
686
- });
687
-
688
- const node = zoomContainer.selectAll().data(this.nodes, function (d) {
689
- return d.id;
690
- });
691
-
692
- zoomContainer.selectAll('g').data(node).exit().remove();
693
-
694
- const nodeEnter = node
695
- .join('g')
696
- .call(
697
- _d3
698
- .drag()
699
- .on('start', function dragstarted(d) {
700
- // Enable the save & reset btn
701
- document.getElementById('save_graph').removeAttribute('disabled');
702
-
703
- if (!_d3.event.active) simulation.alphaTarget(0.9).restart();
704
-
705
- if (!d.selected && !this.shiftKey) {
706
- // if this node isn't selected, then we have to unselect every other node
707
- nodeEnter.classed('selected', function (p) {
708
- return (p.selected = p.previouslySelected = false);
709
- });
710
- // remove the selected styling on other nodes and labels when we drag a non-selected node
711
- _d3
712
- .selectAll('.edgelabel')
713
- .style('fill', '#212529')
714
- .style('font-weight', 400);
715
- _d3
716
- .selectAll('.nodeText')
717
- .style('font-weight', 400)
718
- .style('fill', '#212529');
719
- }
720
-
721
- _d3.select(this).classed('selected', function (p) {
722
- d.previouslySelected = d.selected;
723
- return (d.selected = true);
724
- });
725
-
726
- nodeEnter
727
- .filter(function (d) {
728
- return d.selected;
729
- })
730
- .each(function (d) {
731
- d.fx = d.x;
732
- d.fy = d.y;
733
- });
734
- })
735
- .on('drag', function dragged(d) {
736
- nodeEnter
737
- .filter(function (d) {
738
- return d.selected;
739
- })
740
- .each(function (d) {
741
- d.fx += _d3.event.dx;
742
- d.fy += _d3.event.dy;
743
- });
744
- })
745
- .on('end', function dragended(d) {
746
- if (!_d3.event.active) {
747
- simulation.alphaTarget(0.3).restart();
748
- }
749
- d.fx = d.x;
750
- d.fy = d.y;
751
- // Subscribes to updated graph positions for save
752
- self.saveGraphData.next(data);
753
- })
754
- )
755
- .attr('class', 'node-wrapper')
756
- .attr('id', function (d) {
757
- return d.id;
758
- });
759
-
760
- // no collision - already using this in statement
761
- const self = this;
762
-
763
- // node click and ctrl + click
764
- svg.selectAll('.node-wrapper').on('click', function (d) {
765
- // so we don't activate the canvas .click event
766
- _d3.event.stopPropagation();
767
-
768
- // setting the select attribute to the object on single select so we can drag them
769
- d.selected = true;
770
-
771
- svg.selectAll('.node-wrapper').classed('selected', false);
772
- d3.select(this).classed('selected', true);
773
- nodeEnter.each(function (d) {
774
- d.selected = false;
775
- d.previouslySelected = false;
776
- });
777
-
778
- // remove style from selected node before the class is removed
779
- _d3
780
- .selectAll('.selected')
781
- .selectAll('.nodeText')
782
- .style('fill', '#212529');
783
- // Remove styles from all other nodes and labels on single left click
784
- _d3.selectAll('.edgelabel').style('fill', '#212529');
785
- _d3
786
- .selectAll('.nodeText')
787
- .style('fill', '#212529')
788
- .style('font-weight', 400);
789
- // Add style on single left click
790
- _d3
791
- .select(this)
792
- .select('.nodeText')
793
- .style('fill', 'blue')
794
- .style('font-weight', 700);
795
-
796
- return null;
797
- });
798
-
799
- //click on canvas to remove selected nodes
800
- _d3.select('svg').on('click', () => {
801
- nodeEnter.each(function (d) {
802
- d.selected = false;
803
- d.previouslySelected = false;
804
- });
805
- node.classed('selected', false);
806
- _d3
807
- .selectAll('.selected')
808
- .selectAll('.nodeText')
809
- .style('fill', '#212529')
810
- .style('font-weight', 400);
811
- _d3.selectAll('.selected').classed('selected', false);
812
- _d3
813
- .selectAll('.edgelabel')
814
- .style('fill', '#212529')
815
- .style('font-weight', 400);
816
- });
817
-
818
- nodeEnter
819
- .filter(function (d) {
820
- if (!d.imageUrl || d.imageUrl === '') {
821
- return null;
822
- }
823
- return true;
824
- })
825
- .append('image')
826
- .attr('xlink:href', function (d) {
827
- return d.imageUrl;
828
- })
829
- .attr('x', -15)
830
- .attr('y', -60)
831
- .attr('class', function (d) {
832
- const suffix = 'image';
833
- const id = d.id ? d.id : '';
834
- return `${id}_${suffix}`;
835
- })
836
- .attr('id', function (d) {
837
- const id = d.id ? d.id : '';
838
- return `${id}`;
839
- })
840
- .attr('width', 32)
841
- .attr('height', 32)
842
- .attr('class', 'image')
843
- .style('cursor', 'pointer');
844
-
845
- nodeEnter
846
- .filter(function (d) {
847
- if (!d.icon || d.icon === '') {
848
- return null;
849
- }
850
- return true;
851
- })
852
- .append('text')
853
- .text(function (d) {
854
- return d.icon;
855
- })
856
- .attr('x', -18)
857
- .attr('y', -30)
858
- .attr('class', function (d) {
859
- const suffix = 'icon';
860
- const id = d.id ? d.id : '';
861
- return `${id}_${suffix} fa`;
862
- })
863
- .attr('id', function (d) {
864
- const id = d.id ? d.id : '';
865
- return `${id}`;
866
- })
867
- .style('font-size', '35px')
868
- .style('cursor', 'pointer');
869
-
870
- const nodeText = nodeEnter
871
- .append('text')
872
- .attr('id', 'nodeText')
873
- .attr('class', 'nodeText')
874
- .style('text-anchor', 'middle')
875
- .style('cursor', 'pointer')
876
- .attr('dy', -3)
877
- .attr('y', -25)
878
- .attr('testhook', function (d) {
879
- const suffix = 'text';
880
- const id = d.id ? d.id : '';
881
- return `${id}_${suffix}`;
882
- });
883
-
884
- nodeText
885
- .selectAll('tspan.text')
886
- .data((d) => d.label)
887
- .enter()
888
- .append('tspan')
889
- .attr('class', 'nodeTextTspan')
890
- .text((d) => d)
891
- .style('font-size', '14px')
892
- .attr('x', -10)
893
- .attr('dx', 10)
894
- .attr('dy', 15);
895
-
896
- nodeEnter
897
- .filter(function (d) {
898
- if (!d.additionalIcon) {
899
- return null;
900
- }
901
- return true;
902
- })
903
- .append('text')
904
- .attr('id', function (d) {
905
- const suffix = 'additionalIcon';
906
- const id = d.id ? d.id : '';
907
- return `${id}_${suffix}`;
908
- })
909
- .attr('width', 100)
910
- .attr('height', 25)
911
- .attr('x', 30)
912
- .attr('y', -50)
913
- .attr('class', 'fa')
914
- .style('fill', '#856404')
915
- .text(function (d) {
916
- return d.additionalIcon;
917
- });
918
-
919
- // transition effects for new pulsating nodes
920
- nodeEnter
921
- .filter(function (d) {
922
- if (!d.newItem) {
923
- return null;
924
- }
925
- return true;
926
- })
927
- .select('text')
928
- .transition()
929
- .duration(1000)
930
- .attr('fill', 'blue')
931
- .attr('font-weight', 700)
932
- .attr('fill-opacity', 1)
933
- .transition()
934
- .duration(1000)
935
- .attr('fill', 'blue')
936
- .attr('font-weight', 700)
937
- .attr('fill-opacity', 0.1)
938
- .transition()
939
- .duration(1000)
940
- .attr('fill', 'blue')
941
- .attr('font-weight', 700)
942
- .attr('fill-opacity', 1)
943
- .transition()
944
- .duration(1000)
945
- .attr('fill', 'blue')
946
- .attr('font-weight', 700)
947
- .attr('fill-opacity', 0.1)
948
- .transition()
949
- .duration(1000)
950
- .attr('fill', 'blue')
951
- .attr('font-weight', 700)
952
- .attr('fill-opacity', 1)
953
- .transition()
954
- .duration(1000)
955
- .attr('fill', '#212529')
956
- .attr('font-weight', 400)
957
- .attr('fill-opacity', 1)
958
- .on('end', function () {
959
- return d3.select(this).call(_d3.transition);
960
- });
961
-
962
- nodeEnter
963
- .filter(function (d) {
964
- if (!d.newItem) {
965
- return null;
966
- }
967
- return true;
968
- })
969
- .select('image')
970
- .transition()
971
- .duration(1000)
972
- .attr('width', 45)
973
- .attr('height', 45)
974
- .transition()
975
- .duration(1000)
976
- .attr('width', 32)
977
- .attr('height', 32)
978
- .transition()
979
- .duration(1000)
980
- .attr('width', 45)
981
- .attr('height', 45)
982
- .transition()
983
- .duration(1000)
984
- .attr('width', 32)
985
- .attr('height', 32)
986
- .transition()
987
- .duration(1000)
988
- .attr('width', 45)
989
- .attr('height', 45)
990
- .transition()
991
- .duration(1000)
992
- .attr('width', 32)
993
- .attr('height', 32)
994
- .on('end', function () {
995
- return d3.select(this).call(d3.transition);
996
- });
997
-
998
- // Remove the newClass so they don't animate next time
999
- this.nodes = this.removeNewItem(this.nodes);
1000
-
1001
- nodeEnter.append('title').text(function (d) {
1002
- return d.label;
1003
- });
1004
-
1005
- const maxTicks = 30;
1006
- let tickCount = 0;
1007
- let zoomToFitCalled = false;
1008
-
1009
- simulation.nodes(this.nodes).on('tick', () => {
1010
- if (this.zoomToFit && tickCount >= maxTicks && !zoomToFitCalled) {
1011
- simulation.stop();
1012
- handleZoomToFit();
1013
- zoomToFitCalled = true;
1014
- } else {
1015
- this.ticked(linkEnter, nodeEnter, edgepathsEnter);
1016
- tickCount++;
1017
- }
1018
- });
1019
-
1020
- simulation.force('link').links(this.links);
1021
- self.saveGraphData.next(data);
1022
- }
1023
-
1024
- public resetGraph(initialData, element, zoom, zoomToFit) {
1025
- // Reset the data to its initial state
1026
- this.nodes = [];
1027
- this.links = [];
1028
- // Call the update method again to re-simulate the graph with the new data
1029
- this.update(initialData, element, zoom, zoomToFit);
1030
- }
1031
- }