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.

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