vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.8 KiB

  1. import Node from '../Node'
  2. /**
  3. * A Cluster is a special Node that allows a group of Nodes positioned closely together
  4. * to be represented by a single Cluster Node.
  5. *
  6. * @extends Node
  7. */
  8. class Cluster extends Node {
  9. /**
  10. * @param {Object} options
  11. * @param {Object} body
  12. * @param {Array.<HTMLImageElement>}imagelist
  13. * @param {Array} grouplist
  14. * @param {Object} globalOptions
  15. */
  16. constructor(options, body, imagelist, grouplist, globalOptions) {
  17. super(options, body, imagelist, grouplist, globalOptions);
  18. this.isCluster = true;
  19. this.containedNodes = {};
  20. this.containedEdges = {};
  21. }
  22. /**
  23. * Transfer child cluster data to current and disconnect the child cluster.
  24. *
  25. * Please consult the header comment in 'Clustering.js' for the fields set here.
  26. *
  27. * @param {string|number} childClusterId id of child cluster to open
  28. */
  29. _openChildCluster(childClusterId) {
  30. let childCluster = this.body.nodes[childClusterId];
  31. if (this.containedNodes[childClusterId] === undefined) {
  32. throw new Error('node with id: ' + childClusterId + ' not in current cluster');
  33. }
  34. if (!childCluster.isCluster) {
  35. throw new Error('node with id: ' + childClusterId + ' is not a cluster');
  36. }
  37. // Disconnect child cluster from current cluster
  38. delete this.containedNodes[childClusterId];
  39. for(let n in childCluster.edges) {
  40. let edgeId = childCluster.edges[n].id;
  41. delete this.containedEdges[edgeId];
  42. }
  43. // Transfer nodes and edges
  44. for (let nodeId in childCluster.containedNodes) {
  45. this.containedNodes[nodeId] = childCluster.containedNodes[nodeId];
  46. }
  47. childCluster.containedNodes = {};
  48. for (let edgeId in childCluster.containedEdges) {
  49. this.containedEdges[edgeId] = childCluster.containedEdges[edgeId];
  50. }
  51. childCluster.containedEdges = {};
  52. // Transfer edges within cluster edges which are clustered
  53. for (let n in childCluster.edges) {
  54. let clusterEdge = childCluster.edges[n];
  55. for (let m in this.edges) {
  56. let parentClusterEdge = this.edges[m];
  57. let index = parentClusterEdge.clusteringEdgeReplacingIds.indexOf(clusterEdge.id);
  58. if (index === -1) continue;
  59. for (let n in clusterEdge.clusteringEdgeReplacingIds) {
  60. let srcId = clusterEdge.clusteringEdgeReplacingIds[n];
  61. parentClusterEdge.clusteringEdgeReplacingIds.push(srcId);
  62. // Maintain correct bookkeeping for transferred edge
  63. this.body.edges[srcId].edgeReplacedById = parentClusterEdge.id;
  64. }
  65. // Remove cluster edge from parent cluster edge
  66. parentClusterEdge.clusteringEdgeReplacingIds.splice(index, 1);
  67. break; // Assumption: a clustered edge can only be present in a single clustering edge
  68. }
  69. }
  70. childCluster.edges = [];
  71. }
  72. }
  73. export default Cluster;