@antv/g6 5.0.0-beta.20 → 5.0.0-beta.21

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 (184) hide show
  1. package/dist/g6.min.js +2 -2
  2. package/esm/index.d.ts +16 -9
  3. package/esm/index.js +1 -1
  4. package/esm/runtime/controller/data.d.ts +27 -10
  5. package/esm/runtime/controller/data.js +240 -216
  6. package/esm/runtime/controller/data.js.map +1 -1
  7. package/esm/stdlib/behavior/activate-relations.d.ts +1 -1
  8. package/esm/stdlib/behavior/brush-select.d.ts +2 -2
  9. package/esm/stdlib/behavior/click-select.d.ts +1 -1
  10. package/esm/stdlib/behavior/collapse-expand-combo.d.ts +1 -1
  11. package/esm/stdlib/behavior/collapse-expand-tree.d.ts +1 -1
  12. package/esm/stdlib/behavior/drag-canvas.js +1 -1
  13. package/esm/stdlib/behavior/drag-combo.d.ts +1 -1
  14. package/esm/stdlib/behavior/drag-node.d.ts +1 -1
  15. package/esm/stdlib/behavior/shortcuts-call.d.ts +1 -1
  16. package/esm/stdlib/behavior/zoom-canvas.js +1 -1
  17. package/esm/stdlib/data/index.d.ts +1 -0
  18. package/esm/stdlib/data/index.js +1 -0
  19. package/esm/stdlib/data/index.js.map +1 -1
  20. package/esm/stdlib/data/mapNodeSize.d.ts +3 -4
  21. package/esm/stdlib/data/mapNodeSize.js +10 -0
  22. package/esm/stdlib/data/mapNodeSize.js.map +1 -1
  23. package/esm/stdlib/data/processParallelEdges.d.ts +21 -0
  24. package/esm/stdlib/data/processParallelEdges.js +127 -0
  25. package/esm/stdlib/data/processParallelEdges.js.map +1 -0
  26. package/esm/stdlib/data/transformV4Data.d.ts +2 -3
  27. package/esm/stdlib/data/transformV4Data.js +8 -0
  28. package/esm/stdlib/data/transformV4Data.js.map +1 -1
  29. package/esm/stdlib/data/validateData.d.ts +3 -4
  30. package/esm/stdlib/data/validateData.js +9 -1
  31. package/esm/stdlib/data/validateData.js.map +1 -1
  32. package/esm/stdlib/index.d.ts +15 -8
  33. package/esm/stdlib/index.js +2 -1
  34. package/esm/stdlib/index.js.map +1 -1
  35. package/esm/stdlib/item/node/donut.d.ts +3 -3
  36. package/esm/stdlib/plugin/hull/hullComponent.d.ts +1 -1
  37. package/esm/stdlib/plugin/legend/index.d.ts +1 -1
  38. package/esm/stdlib/plugin/snapline/index.d.ts +2 -2
  39. package/esm/stdlib/plugin/snapline/index.js +239 -239
  40. package/esm/stdlib/plugin/snapline/index.js.map +1 -1
  41. package/esm/stdlib/plugin/timebar/index.d.ts +4 -4
  42. package/esm/stdlib/plugin/timebar/index.js +4 -4
  43. package/esm/stdlib/plugin/timebar/index.js.map +1 -1
  44. package/esm/stdlib/plugin/tooltip/index.d.ts +1 -1
  45. package/esm/stdlib/plugin/watermaker/index.d.ts +1 -1
  46. package/esm/types/animate.d.ts +2 -2
  47. package/esm/types/behavior.d.ts +1 -1
  48. package/esm/types/combo.d.ts +7 -7
  49. package/esm/types/common.d.ts +5 -5
  50. package/esm/types/data.d.ts +11 -6
  51. package/esm/types/edge.d.ts +9 -9
  52. package/esm/types/event.d.ts +1 -1
  53. package/esm/types/history.d.ts +2 -2
  54. package/esm/types/hook.d.ts +1 -1
  55. package/esm/types/item.d.ts +13 -13
  56. package/esm/types/item.js.map +1 -1
  57. package/esm/types/layout.d.ts +6 -6
  58. package/esm/types/loop.d.ts +2 -2
  59. package/esm/types/node.d.ts +7 -7
  60. package/esm/types/render.d.ts +1 -1
  61. package/esm/types/spec.d.ts +4 -3
  62. package/esm/types/stdlib.d.ts +1 -1
  63. package/esm/types/theme.d.ts +8 -8
  64. package/esm/types/view.d.ts +2 -2
  65. package/esm/util/array.d.ts +1 -0
  66. package/esm/util/array.js +3 -0
  67. package/esm/util/array.js.map +1 -1
  68. package/esm/util/data.d.ts +3 -0
  69. package/esm/util/data.js +15 -0
  70. package/esm/util/data.js.map +1 -1
  71. package/esm/util/event.d.ts +2 -2
  72. package/esm/util/layout.d.ts +1 -1
  73. package/esm/util/loop.d.ts +1 -0
  74. package/esm/util/loop.js +10 -0
  75. package/esm/util/loop.js.map +1 -1
  76. package/esm/util/shape.d.ts +2 -1
  77. package/esm/util/shape.js +2 -1
  78. package/esm/util/shape.js.map +1 -1
  79. package/esm/util/shape3d.d.ts +1 -1
  80. package/esm/util/warn.d.ts +1 -1
  81. package/lib/index.d.ts +16 -9
  82. package/lib/index.js +1 -1
  83. package/lib/runtime/controller/data.d.ts +27 -10
  84. package/lib/runtime/controller/data.js +238 -214
  85. package/lib/runtime/controller/data.js.map +1 -1
  86. package/lib/stdlib/behavior/activate-relations.d.ts +1 -1
  87. package/lib/stdlib/behavior/brush-select.d.ts +2 -2
  88. package/lib/stdlib/behavior/click-select.d.ts +1 -1
  89. package/lib/stdlib/behavior/collapse-expand-combo.d.ts +1 -1
  90. package/lib/stdlib/behavior/collapse-expand-tree.d.ts +1 -1
  91. package/lib/stdlib/behavior/drag-canvas.js +1 -1
  92. package/lib/stdlib/behavior/drag-combo.d.ts +1 -1
  93. package/lib/stdlib/behavior/drag-node.d.ts +1 -1
  94. package/lib/stdlib/behavior/shortcuts-call.d.ts +1 -1
  95. package/lib/stdlib/behavior/zoom-canvas.js +1 -1
  96. package/lib/stdlib/data/index.d.ts +1 -0
  97. package/lib/stdlib/data/index.js +1 -0
  98. package/lib/stdlib/data/index.js.map +1 -1
  99. package/lib/stdlib/data/mapNodeSize.d.ts +3 -4
  100. package/lib/stdlib/data/mapNodeSize.js +11 -1
  101. package/lib/stdlib/data/mapNodeSize.js.map +1 -1
  102. package/lib/stdlib/data/processParallelEdges.d.ts +21 -0
  103. package/lib/stdlib/data/processParallelEdges.js +131 -0
  104. package/lib/stdlib/data/processParallelEdges.js.map +1 -0
  105. package/lib/stdlib/data/transformV4Data.d.ts +2 -3
  106. package/lib/stdlib/data/transformV4Data.js +9 -1
  107. package/lib/stdlib/data/transformV4Data.js.map +1 -1
  108. package/lib/stdlib/data/validateData.d.ts +3 -4
  109. package/lib/stdlib/data/validateData.js +10 -2
  110. package/lib/stdlib/data/validateData.js.map +1 -1
  111. package/lib/stdlib/index.d.ts +15 -8
  112. package/lib/stdlib/index.js +2 -1
  113. package/lib/stdlib/index.js.map +1 -1
  114. package/lib/stdlib/item/node/donut.d.ts +3 -3
  115. package/lib/stdlib/plugin/hull/hullComponent.d.ts +1 -1
  116. package/lib/stdlib/plugin/legend/index.d.ts +1 -1
  117. package/lib/stdlib/plugin/snapline/index.d.ts +2 -2
  118. package/lib/stdlib/plugin/snapline/index.js +239 -239
  119. package/lib/stdlib/plugin/snapline/index.js.map +1 -1
  120. package/lib/stdlib/plugin/timebar/index.d.ts +4 -4
  121. package/lib/stdlib/plugin/timebar/index.js +4 -4
  122. package/lib/stdlib/plugin/timebar/index.js.map +1 -1
  123. package/lib/stdlib/plugin/tooltip/index.d.ts +1 -1
  124. package/lib/stdlib/plugin/watermaker/index.d.ts +1 -1
  125. package/lib/types/animate.d.ts +2 -2
  126. package/lib/types/behavior.d.ts +1 -1
  127. package/lib/types/combo.d.ts +7 -7
  128. package/lib/types/common.d.ts +5 -5
  129. package/lib/types/data.d.ts +11 -6
  130. package/lib/types/edge.d.ts +9 -9
  131. package/lib/types/event.d.ts +1 -1
  132. package/lib/types/event.js +2 -2
  133. package/lib/types/event.js.map +1 -1
  134. package/lib/types/history.d.ts +2 -2
  135. package/lib/types/history.js +1 -1
  136. package/lib/types/history.js.map +1 -1
  137. package/lib/types/hook.d.ts +1 -1
  138. package/lib/types/item.d.ts +13 -13
  139. package/lib/types/item.js +1 -1
  140. package/lib/types/item.js.map +1 -1
  141. package/lib/types/layout.d.ts +6 -6
  142. package/lib/types/loop.d.ts +2 -2
  143. package/lib/types/loop.js +1 -1
  144. package/lib/types/loop.js.map +1 -1
  145. package/lib/types/node.d.ts +7 -7
  146. package/lib/types/render.d.ts +1 -1
  147. package/lib/types/spec.d.ts +4 -3
  148. package/lib/types/stdlib.d.ts +1 -1
  149. package/lib/types/theme.d.ts +8 -8
  150. package/lib/types/view.d.ts +2 -2
  151. package/lib/util/array.d.ts +1 -0
  152. package/lib/util/array.js +5 -1
  153. package/lib/util/array.js.map +1 -1
  154. package/lib/util/data.d.ts +3 -0
  155. package/lib/util/data.js +16 -1
  156. package/lib/util/data.js.map +1 -1
  157. package/lib/util/event.d.ts +2 -2
  158. package/lib/util/layout.d.ts +1 -1
  159. package/lib/util/loop.d.ts +1 -0
  160. package/lib/util/loop.js +11 -1
  161. package/lib/util/loop.js.map +1 -1
  162. package/lib/util/shape.d.ts +2 -1
  163. package/lib/util/shape.js +1 -0
  164. package/lib/util/shape.js.map +1 -1
  165. package/lib/util/shape3d.d.ts +1 -1
  166. package/lib/util/warn.d.ts +1 -1
  167. package/package.json +3 -3
  168. package/src/index.ts +1 -1
  169. package/src/runtime/controller/data.ts +309 -229
  170. package/src/stdlib/behavior/drag-canvas.ts +1 -1
  171. package/src/stdlib/behavior/zoom-canvas.ts +1 -1
  172. package/src/stdlib/data/index.ts +1 -0
  173. package/src/stdlib/data/mapNodeSize.ts +20 -1
  174. package/src/stdlib/data/processParallelEdges.ts +203 -0
  175. package/src/stdlib/data/transformV4Data.ts +14 -1
  176. package/src/stdlib/data/validateData.ts +16 -2
  177. package/src/stdlib/index.ts +3 -1
  178. package/src/types/data.ts +6 -0
  179. package/src/types/item.ts +2 -1
  180. package/src/types/spec.ts +1 -0
  181. package/src/util/array.ts +12 -0
  182. package/src/util/data.ts +24 -1
  183. package/src/util/loop.ts +11 -0
  184. package/src/util/shape.ts +1 -0
@@ -1,13 +1,20 @@
1
1
  import { Graph as GraphLib, ID } from '@antv/graphlib';
2
- import { clone, isArray, isObject } from '@antv/util';
2
+ import { clone, isArray, isEmpty, isObject } from '@antv/util';
3
3
  import { registery as registry } from '../../stdlib';
4
- import { ComboModel, ComboUserModel, GraphData, IGraph } from '../../types';
4
+ import {
5
+ ComboModel,
6
+ ComboUserModel,
7
+ GraphData,
8
+ IGraph,
9
+ Specification,
10
+ } from '../../types';
5
11
  import { ComboUserModelData } from '../../types/combo';
6
12
  import {
7
13
  DataChangeType,
8
14
  DataConfig,
9
15
  FetchDataConfig,
10
16
  GraphCore,
17
+ GraphDataChanges,
11
18
  InlineGraphDataConfig,
12
19
  InlineTreeDataConfig,
13
20
  } from '../../types/data';
@@ -25,6 +32,9 @@ import {
25
32
  NodeUserModelData,
26
33
  } from '../../types/node';
27
34
  import {
35
+ AVAILABLE_DATA_LIFECYCLE,
36
+ DEFAULT_ACTIVE_DATA_LIFECYCLE,
37
+ dataLifecycleMap,
28
38
  deconstructData,
29
39
  graphComboTreeDfs,
30
40
  graphData2TreeData,
@@ -185,12 +195,6 @@ export class DataController {
185
195
  const { graphCore } = this;
186
196
  const change = () => {
187
197
  switch (changeType) {
188
- case 'remove':
189
- this.removeData(data as GraphData);
190
- break;
191
- case 'update':
192
- this.updateData(data);
193
- break;
194
198
  case 'moveCombo':
195
199
  this.moveCombo(data as GraphData);
196
200
  break;
@@ -198,7 +202,7 @@ export class DataController {
198
202
  this.addCombo(data as GraphData);
199
203
  break;
200
204
  default:
201
- // changeType is 'replace' | 'mergeReplace' | 'union'
205
+ // changeType is 'replace' | 'mergeReplace' | 'union' | 'remove' | 'update'
202
206
  this.changeData(data, changeType);
203
207
  break;
204
208
  }
@@ -275,21 +279,26 @@ export class DataController {
275
279
  }
276
280
 
277
281
  /**
278
- * Change data by replace, merge repalce, or union.
282
+ * Change data by replace, merge repalce, union, remove or update.
279
283
  * @param data new data
280
- * @param changeType type of data change, 'replace' means discard the old data. 'mergeReplace' means merge the common part. 'union' means merge whole sets of old and new one
284
+ * @param changeType type of data change, 'replace' means discard the old data. 'mergeReplace' means merge the common part. 'union' means merge whole sets of old and new one. 'remove' means remove the common part. 'update' means update the comme part.
281
285
  */
282
286
  private changeData(
283
287
  dataConfig: DataConfig,
284
- changeType: 'replace' | 'mergeReplace' | 'union',
288
+ changeType: 'replace' | 'mergeReplace' | 'union' | 'remove' | 'update',
285
289
  ) {
286
290
  const { type: dataType, data } = this.formatData(dataConfig) || {};
287
291
  if (!dataType) return;
288
292
  this.dataType = dataType;
289
293
 
290
- const { nodes = [], edges = [], combos = [] } = this.transformData(data);
294
+ const preprocessedData = this.preprocessData(data, changeType);
295
+ const { dataAdded, dataRemoved, dataUpdated } = this.transformData(
296
+ preprocessedData,
297
+ changeType,
298
+ );
291
299
 
292
300
  if (changeType === 'replace') {
301
+ const { nodes, edges, combos } = dataAdded;
293
302
  this.graphCore = new GraphLib<NodeModelData, EdgeModelData>(
294
303
  clone({
295
304
  nodes: nodes.concat(
@@ -331,77 +340,160 @@ export class DataController {
331
340
  });
332
341
  }
333
342
  } else {
334
- const { graphCore } = this;
335
- const prevData = deconstructData({
336
- nodes: graphCore.getAllNodes(),
337
- edges: [],
343
+ this.doRemove(dataRemoved);
344
+ this.doAdd(dataAdded);
345
+ this.doUpdate(dataUpdated);
346
+ }
347
+
348
+ if (
349
+ data.edges?.filter(
350
+ (edge) =>
351
+ edge.hasOwnProperty('source') || edge.hasOwnProperty('target'),
352
+ ).length
353
+ ) {
354
+ // convert and store tree structure to graphCore
355
+ this.updateTreeGraph(dataType, {
356
+ nodes: this.graphCore.getAllNodes(),
357
+ edges: this.graphCore.getAllEdges(),
338
358
  });
339
- const nodesAndCombos = nodes.concat(
340
- combos.map((combo) => ({
341
- id: combo.id,
342
- data: { ...combo.data, _isCombo: true },
343
- })),
344
- );
359
+ }
360
+ }
345
361
 
346
- // =========== node & combos ============
347
- if (!prevData.nodes.length) {
348
- graphCore.addNodes(nodesAndCombos);
349
- } else {
350
- if (changeType === 'mergeReplace') {
351
- // remove the nodes which are not in incoming data but in graphCore
352
- const nodeAndComboIds = nodesAndCombos.map((node) => node.id);
353
- prevData.nodes.forEach((prevNode) => {
354
- if (!nodeAndComboIds.includes(prevNode.id)) {
355
- this.removeNode(prevNode);
356
- }
357
- });
358
- }
359
- // add or update node
360
- nodesAndCombos.forEach((item) => {
361
- if (graphCore.hasNode(item.id)) {
362
- // update node which is in the graphCore
363
- graphCore.mergeNodeData(item.id, item.data);
364
- } else {
365
- // add node which is in data but not in graphCore
366
- graphCore.addNode(item);
367
- }
368
- });
362
+ /**
363
+ * Add new data.
364
+ * @param data data to be added which is not in graphCore
365
+ */
366
+ private doAdd(data: GraphData) {
367
+ if (isEmpty(data)) return;
368
+
369
+ const { graphCore } = this;
370
+ const { nodes = [], edges = [], combos = [] } = data;
371
+
372
+ if (nodes.length || combos.length) {
373
+ const nodesAndCombos = getNodesAndCombos(data);
374
+ graphCore.addNodes(nodesAndCombos);
375
+ }
376
+
377
+ if (edges.length) {
378
+ graphCore.addEdges(edges);
379
+ }
380
+ }
381
+
382
+ /**
383
+ * Update part of old data.
384
+ * @param data data to be updated which is part of old one in the graphCore
385
+ */
386
+ private doUpdate(data: GraphData) {
387
+ if (isEmpty(data)) return;
388
+
389
+ const { graphCore } = this;
390
+ const { nodes = [], edges = [], combos = [] } = data;
391
+
392
+ // update node
393
+ nodes.forEach((newModel) => {
394
+ const { id, data } = newModel;
395
+ if (data) {
396
+ const mergedData = mergeOneLevelData(graphCore.getNode(id), newModel);
397
+ graphCore.mergeNodeData(id, mergedData);
369
398
  }
399
+ if (data.hasOwnProperty('parentId')) {
400
+ graphCore.setParent(id, data.parentId, 'combo');
401
+ }
402
+ });
370
403
 
371
- // =========== edge ============
372
- prevData.edges = graphCore.getAllEdges();
373
- if (!prevData.edges.length) {
374
- graphCore.addEdges(edges);
375
- } else {
376
- if (changeType === 'mergeReplace') {
377
- // remove the edges which are not in incoming data but in graphCore
378
- const edgeIds = edges.map((edge) => edge.id);
379
- prevData.edges.forEach((prevEdge) => {
380
- if (!edgeIds.includes(prevEdge.id))
381
- graphCore.removeEdge(prevEdge.id);
404
+ // update edge
405
+ edges.forEach((newModel) => {
406
+ const oldModel = graphCore.getEdge(newModel.id);
407
+ if (!oldModel) return;
408
+ const { id, source, target, data } = newModel;
409
+ if (source && oldModel.source !== source)
410
+ graphCore.updateEdgeSource(id, source);
411
+ if (target && oldModel.target !== target)
412
+ graphCore.updateEdgeTarget(id, target);
413
+ if (data) {
414
+ const mergedData = mergeOneLevelData(graphCore.getEdge(id), newModel);
415
+ graphCore.mergeEdgeData(id, mergedData);
416
+ }
417
+ });
418
+
419
+ // update combos
420
+ const modelsToMove = [];
421
+ combos.forEach((newModel) => {
422
+ const { id, data } = newModel;
423
+ const { x: comboNewX, y: comboNewY, ...others } = data;
424
+ if (comboNewX !== undefined || comboNewY !== undefined) {
425
+ if (this.graphCore.getChildren(id, 'combo').length) {
426
+ // combo's position is updated, update the succeed nodes' positions
427
+ const oldBounds = this.graph.getRenderBBox(id, true);
428
+ if (!oldBounds) return;
429
+ const [comboOldX, comboOldY] = oldBounds.center;
430
+ const dx = (comboNewX as number) - comboOldX;
431
+ const dy = (comboNewY as number) - comboOldY;
432
+ graphComboTreeDfs(
433
+ this.graph,
434
+ [newModel],
435
+ (succeed) => {
436
+ const { x, y, _isCombo } = succeed.data;
437
+ if (!_isCombo) {
438
+ modelsToMove.push({
439
+ id: succeed.id,
440
+ data: {
441
+ x: x + dx,
442
+ y: y + dy,
443
+ },
444
+ });
445
+ }
446
+ },
447
+ 'BT',
448
+ );
449
+ } else {
450
+ // empty combo, modify the position directly
451
+ modelsToMove.push({
452
+ id,
453
+ data: {
454
+ x: comboNewX,
455
+ y: comboNewY,
456
+ },
382
457
  });
383
458
  }
384
- // add or update edge
385
- edges.forEach((edge) => {
386
- if (graphCore.hasEdge(edge.id)) {
387
- // update edge which is in the graphCore
388
- graphCore.mergeEdgeData(edge.id, edge.data);
389
- } else {
390
- // add edge which is in data but not in graphCore
391
- graphCore.addEdge(edge);
392
- }
459
+ }
460
+ // update other properties
461
+ if (Object.keys(others).length) {
462
+ const mergedData = mergeOneLevelData(graphCore.getNode(id), {
463
+ id,
464
+ data: others,
393
465
  });
466
+ graphCore.mergeNodeData(id, mergedData);
467
+ if (others.hasOwnProperty('parentId')) {
468
+ graphCore.setParent(id, others.parentId, 'combo');
469
+ }
394
470
  }
395
- }
471
+ });
472
+ // update succeed nodes
473
+ modelsToMove.forEach((newModel) => {
474
+ const { id, data } = newModel;
475
+ graphCore.mergeNodeData(id, data);
476
+ });
477
+ }
396
478
 
397
- if (data.edges?.length) {
398
- const { graphCore } = this;
399
- // convert and store tree structure to graphCore
400
- this.updateTreeGraph(dataType, {
401
- nodes: graphCore.getAllNodes(),
402
- edges: graphCore.getAllEdges(),
403
- });
479
+ /**
480
+ * Remove part of old data.
481
+ * @param data data to be removed which is part of old one in the graphCore
482
+ */
483
+ private doRemove(data: GraphData) {
484
+ if (isEmpty(data)) return;
485
+
486
+ const { graphCore } = this;
487
+
488
+ if (data.edges.length) {
489
+ // add or update edge
490
+ const ids = data.edges.map((edge) => edge.id);
491
+ graphCore.removeEdges(ids);
404
492
  }
493
+
494
+ const nodesAndCombos = getNodesAndCombos(data);
495
+ // update combo tree view and tree graph view
496
+ nodesAndCombos?.forEach((item) => this.removeNode(item));
405
497
  }
406
498
 
407
499
  private removeNode(nodeModel: NodeModel) {
@@ -449,155 +541,6 @@ export class DataController {
449
541
  graphCore.removeNode(id);
450
542
  }
451
543
 
452
- /**
453
- * Remove part of old data.
454
- * @param data data to be removed which is part of old one
455
- */
456
- private removeData(data: GraphData) {
457
- const { graphCore } = this;
458
- const { nodes = [], edges = [], combos = [] } = data;
459
- const nodesAndCombos = nodes.concat(combos);
460
- const prevNodesAndCombos = graphCore.getAllNodes();
461
- const prevEdges = graphCore.getAllEdges();
462
- if (prevNodesAndCombos.length && nodesAndCombos.length) {
463
- // update combo tree view and tree graph view
464
- nodesAndCombos.forEach((item) => {
465
- this.removeNode(item);
466
- });
467
- // remove the node
468
- // graphCore.removeNodes(nodesAndCombos.map((node) => node.id));
469
- }
470
- if (prevEdges.length && edges.length) {
471
- // add or update edge
472
- const ids = edges
473
- .map((edge) => edge.id)
474
- .filter((id) => graphCore.hasEdge(id));
475
- graphCore.removeEdges(ids);
476
- }
477
- }
478
-
479
- /**
480
- * Update part of old data.
481
- * @param data data to be updated which is part of old one
482
- */
483
- private updateData(dataConfig: DataConfig) {
484
- const { graphCore } = this;
485
- const { type: dataType, data } = this.formatData(dataConfig);
486
- if (!dataType) return;
487
- const { nodes = [], edges = [], combos = [] } = data; //this.transformData(data as GraphData);
488
- const {
489
- nodes: prevNodes,
490
- edges: prevEdges,
491
- combos: prevCombos,
492
- } = deconstructData({
493
- nodes: graphCore.getAllNodes(),
494
- edges: graphCore.getAllEdges(),
495
- });
496
- if (prevNodes.length) {
497
- // update node
498
- nodes.forEach((newModel) => {
499
- const { id, data } = newModel;
500
- if (data) {
501
- const mergedData = mergeOneLevelData(graphCore.getNode(id), newModel);
502
- graphCore.mergeNodeData(id, mergedData);
503
- }
504
- if (data.hasOwnProperty('parentId')) {
505
- graphCore.setParent(id, data.parentId, 'combo');
506
- }
507
- });
508
- }
509
- if (prevEdges.length) {
510
- // update edge
511
- edges.forEach((newModel) => {
512
- const oldModel = graphCore.getEdge(newModel.id);
513
- if (!oldModel) return;
514
- const { id, source, target, data } = newModel;
515
- if (source && oldModel.source !== source)
516
- graphCore.updateEdgeSource(id, source);
517
- if (target && oldModel.target !== target)
518
- graphCore.updateEdgeTarget(id, target);
519
- if (data) {
520
- const mergedData = mergeOneLevelData(graphCore.getEdge(id), newModel);
521
- graphCore.mergeEdgeData(id, mergedData);
522
- }
523
- });
524
- }
525
- if (prevCombos.length) {
526
- // update combos
527
- const modelsToMove = [];
528
- combos.forEach((newModel) => {
529
- const { id, data } = newModel;
530
- if (!data) return;
531
- const { x: comboNewX, y: comboNewY, ...others } = data;
532
- if (comboNewX !== undefined || comboNewY !== undefined) {
533
- if (this.graphCore.getChildren(id, 'combo').length) {
534
- // combo's position is updated, update the succeed nodes' positions
535
- const oldBounds = this.graph.getRenderBBox(id, true);
536
- if (!oldBounds) return;
537
- const [comboOldX, comboOldY] = oldBounds.center;
538
- const dx = (comboNewX as number) - comboOldX;
539
- const dy = (comboNewY as number) - comboOldY;
540
- graphComboTreeDfs(
541
- this.graph,
542
- [newModel],
543
- (succeed) => {
544
- const { x, y, _isCombo } = succeed.data;
545
- if (!_isCombo) {
546
- modelsToMove.push({
547
- id: succeed.id,
548
- data: {
549
- x: x + dx,
550
- y: y + dy,
551
- },
552
- });
553
- }
554
- },
555
- 'BT',
556
- );
557
- } else {
558
- // empty combo, modify the position directly
559
- modelsToMove.push({
560
- id,
561
- data: {
562
- x: comboNewX,
563
- y: comboNewY,
564
- },
565
- });
566
- }
567
- }
568
- // update other properties
569
- if (Object.keys(others).length) {
570
- const mergedData = mergeOneLevelData(graphCore.getNode(id), {
571
- id,
572
- data: others,
573
- });
574
- graphCore.mergeNodeData(id, mergedData);
575
- if (others.hasOwnProperty('parentId')) {
576
- graphCore.setParent(id, others.parentId, 'combo');
577
- }
578
- }
579
- });
580
- // update succeed nodes
581
- modelsToMove.forEach((newModel) => {
582
- const { id, data } = newModel;
583
- graphCore.mergeNodeData(id, data);
584
- });
585
- }
586
-
587
- if (
588
- edges?.filter(
589
- (edge) =>
590
- edge.hasOwnProperty('source') || edge.hasOwnProperty('target'),
591
- ).length
592
- ) {
593
- // convert and store tree structure to graphCore
594
- this.updateTreeGraph(dataType, {
595
- nodes: this.graphCore.getAllNodes(),
596
- edges: this.graphCore.getAllEdges(),
597
- });
598
- }
599
- }
600
-
601
544
  private formatData(dataConfig: DataConfig): {
602
545
  data: GraphData;
603
546
  type: 'graphData' | 'treeData' | 'fetch';
@@ -627,7 +570,7 @@ export class DataController {
627
570
  }
628
571
 
629
572
  /**
630
- * Relatively move the position of combo a dinstance (dx and dy in combo data).
573
+ * Relatively move the position of combo a distance (dx and dy in combo data).
631
574
  * @param data graph data which contains the combo dx dy info
632
575
  */
633
576
  private moveCombo(data: GraphData) {
@@ -716,16 +659,86 @@ export class DataController {
716
659
  }
717
660
 
718
661
  /**
719
- * Clone data from graphCore, and run transforms
662
+ * Preprocess the data and divide the data into three categories: Add, Update, Delete according to the operation type.
663
+ * @param data new graph data
664
+ * @param type type of data change
665
+ */
666
+ private preprocessData(
667
+ data: DataConfig,
668
+ type: DataChangeType,
669
+ ): GraphDataChanges {
670
+ const { graphCore } = this;
671
+ const dataCloned: GraphData = clone(data);
672
+ const prevData = graphCore
673
+ ? deconstructData({
674
+ nodes: graphCore.getAllNodes(),
675
+ edges: graphCore.getAllEdges(),
676
+ })
677
+ : {};
678
+ const { prevMinusNew, newMinusPrev, intersectionOfPrevAndNew } =
679
+ diffGraphData(prevData, dataCloned);
680
+
681
+ const dataChangeMap: Record<string, GraphDataChanges> = {
682
+ replace: { dataAdded: dataCloned, dataUpdated: {}, dataRemoved: {} },
683
+ mergeReplace: {
684
+ dataAdded: newMinusPrev,
685
+ dataUpdated: intersectionOfPrevAndNew,
686
+ dataRemoved: prevMinusNew,
687
+ },
688
+ union: {
689
+ dataAdded: newMinusPrev,
690
+ dataUpdated: intersectionOfPrevAndNew,
691
+ dataRemoved: {},
692
+ },
693
+ remove: {
694
+ dataAdded: {},
695
+ dataUpdated: {},
696
+ dataRemoved: intersectionOfPrevAndNew,
697
+ },
698
+ update: {
699
+ dataAdded: {},
700
+ dataUpdated: intersectionOfPrevAndNew,
701
+ dataRemoved: {},
702
+ },
703
+ };
704
+
705
+ return dataChangeMap[type];
706
+ }
707
+
708
+ /**
709
+ * Determine whether to execute transform in the current data life cycle
710
+ * @param config transform plugin's configuration
711
+ * @param changeType type of data change
712
+ */
713
+ private isTransformActive = (config: any, changeType: DataChangeType) => {
714
+ let activeLifecycle =
715
+ isObject(config) && 'activeLifecycle' in config
716
+ ? (config as any).activeLifecycle
717
+ : DEFAULT_ACTIVE_DATA_LIFECYCLE;
718
+ activeLifecycle = Array.isArray(activeLifecycle)
719
+ ? activeLifecycle
720
+ : activeLifecycle === 'all'
721
+ ? AVAILABLE_DATA_LIFECYCLE
722
+ : [activeLifecycle];
723
+
724
+ return (activeLifecycle as string[]).includes(dataLifecycleMap[changeType]);
725
+ };
726
+
727
+ /**
728
+ * run transforms on preprocessed data
720
729
  * @returns transformed data and the id map list
721
730
  */
722
- private transformData(data): GraphData {
723
- let dataCloned: GraphData = clone(data);
731
+ private transformData(
732
+ data: GraphDataChanges,
733
+ changeType: DataChangeType,
734
+ ): GraphDataChanges {
724
735
  // transform the data with transform extensions, output innerData and idMaps ===
725
736
  this.extensions.forEach(({ func, config }) => {
726
- dataCloned = func(dataCloned, config, this.graphCore);
737
+ if (this.isTransformActive(config, changeType)) {
738
+ data = func(data, config, this.graphCore);
739
+ }
727
740
  });
728
- return dataCloned;
741
+ return data;
729
742
  }
730
743
 
731
744
  /**
@@ -848,3 +861,70 @@ const mergeOneLevelData = (
848
861
  });
849
862
  return mergedData;
850
863
  };
864
+
865
+ type UserModels = (
866
+ | Partial<NodeUserModel>
867
+ | Partial<EdgeUserModel>
868
+ | Partial<ComboUserModel>
869
+ )[];
870
+
871
+ /**
872
+ * Generate the difference between two model list
873
+ * @param prevModels
874
+ * @param newModels
875
+ * @returns
876
+ */
877
+ const diffData = (
878
+ prevModels: UserModels = [],
879
+ newModels: UserModels = [],
880
+ ): {
881
+ prevMinusNew: UserModels;
882
+ newMinusPrev: UserModels;
883
+ intersectionOfPrevAndNew: UserModels;
884
+ } => {
885
+ const prevIds = new Set(prevModels?.map((item) => item.id));
886
+ const newIds = new Set(newModels?.map((item) => item.id));
887
+
888
+ return {
889
+ prevMinusNew: prevModels?.filter((item) => !newIds.has(item.id)),
890
+ newMinusPrev: newModels?.filter((item) => !prevIds.has(item.id)),
891
+ intersectionOfPrevAndNew: newModels?.filter((item) => prevIds.has(item.id)),
892
+ };
893
+ };
894
+
895
+ /**
896
+ * Compute the difference between two graph data structure.
897
+ * @param prevData
898
+ * @param newData
899
+ * @returns
900
+ */
901
+ const diffGraphData = (prevData: GraphData, newData: GraphData) => {
902
+ const result: {
903
+ prevMinusNew: GraphData;
904
+ newMinusPrev: GraphData;
905
+ intersectionOfPrevAndNew: GraphData;
906
+ } = {
907
+ newMinusPrev: { nodes: [], edges: [], combos: [] },
908
+ prevMinusNew: { nodes: [], edges: [], combos: [] },
909
+ intersectionOfPrevAndNew: { nodes: [], edges: [], combos: [] },
910
+ };
911
+
912
+ ['nodes', 'edges', 'combos'].forEach((key) => {
913
+ const diff = diffData(prevData[key], newData[key]);
914
+ result.newMinusPrev[key] = diff.newMinusPrev;
915
+ result.prevMinusNew[key] = diff.prevMinusNew;
916
+ result.intersectionOfPrevAndNew[key] = diff.intersectionOfPrevAndNew;
917
+ });
918
+
919
+ return result;
920
+ };
921
+
922
+ const getNodesAndCombos = (data: GraphData) => {
923
+ const { nodes = [], combos = [] } = data;
924
+ return nodes.concat(
925
+ combos.map((combo) => ({
926
+ id: combo.id,
927
+ data: { ...combo.data, _isCombo: true },
928
+ })),
929
+ );
930
+ };
@@ -252,7 +252,7 @@ export class DragCanvas extends Behavior {
252
252
  const shouldDebounce =
253
253
  typeof graphBehaviorOptimize === 'boolean'
254
254
  ? graphBehaviorOptimize
255
- : this.graph.getAllNodesData().length < graphBehaviorOptimize;
255
+ : this.graph.getAllNodesData().length > graphBehaviorOptimize;
256
256
 
257
257
  const now = Date.now();
258
258
  if (
@@ -242,7 +242,7 @@ export class ZoomCanvas extends Behavior {
242
242
  const shouldDebounce =
243
243
  typeof graphBehaviorOptimize === 'boolean'
244
244
  ? graphBehaviorOptimize
245
- : graph.getAllNodesData().length < graphBehaviorOptimize;
245
+ : graph.getAllNodesData().length > graphBehaviorOptimize;
246
246
 
247
247
  const now = Date.now();
248
248
  if (
@@ -1,3 +1,4 @@
1
1
  export * from './mapNodeSize';
2
2
  export * from './transformV4Data';
3
3
  export * from './validateData';
4
+ export * from './processParallelEdges';
@@ -1,4 +1,4 @@
1
- import { GraphCore } from '../../types/data';
1
+ import { GraphCore, GraphDataChanges } from '../../types/data';
2
2
  import {
3
3
  GraphData,
4
4
  ComboUserModel,
@@ -12,7 +12,24 @@ import {
12
12
  * @param graphCore the graph core stores the previous data.
13
13
  * @returns formatted data.
14
14
  */
15
+
15
16
  export const MapNodeSize = (
17
+ data: GraphDataChanges,
18
+ options: {
19
+ field?: string;
20
+ range?: [number, number];
21
+ } = {},
22
+ graphCore?: GraphCore,
23
+ ): GraphDataChanges => {
24
+ const { dataAdded, dataUpdated, dataRemoved } = data;
25
+ return {
26
+ dataAdded: handler(dataAdded, options, graphCore),
27
+ dataUpdated: handler(dataUpdated, options, graphCore),
28
+ dataRemoved: handler(dataRemoved, options, graphCore),
29
+ };
30
+ };
31
+
32
+ const handler = (
16
33
  data: GraphData,
17
34
  options: {
18
35
  field?: string;
@@ -24,6 +41,8 @@ export const MapNodeSize = (
24
41
  if (!field) return data;
25
42
  const { nodes } = data;
26
43
 
44
+ if (!nodes) return data;
45
+
27
46
  const nodeMap = new Map();
28
47
  // map the value to node size
29
48
  let maxValue = -Infinity,