Browse Source

Some more fixes regarding custom id fields

codeClimate
jos 8 years ago
parent
commit
a95aa84b18
2 changed files with 15 additions and 11 deletions
  1. +13
    -9
      lib/network/modules/Clustering.js
  2. +2
    -2
      lib/network/modules/components/nodes/Cluster.js

+ 13
- 9
lib/network/modules/Clustering.js View File

@ -258,6 +258,8 @@ class ClusterEngine {
*/ */
_createClusterEdges (childNodesObj, childEdgesObj, clusterNodeProperties, clusterEdgeProperties) { _createClusterEdges (childNodesObj, childEdgesObj, clusterNodeProperties, clusterEdgeProperties) {
let edge, childNodeId, childNode, toId, fromId, otherNodeId; let edge, childNodeId, childNode, toId, fromId, otherNodeId;
let nodeIdField = this.body.data.nodes._fieldId;
let edgeIdField = this.body.data.edges._fieldId;
// loop over all child nodes and their edges to find edges going out of the cluster // loop over all child nodes and their edges to find edges going out of the cluster
// these edges will be replaced by clusterEdges. // these edges will be replaced by clusterEdges.
@ -279,13 +281,13 @@ class ClusterEngine {
else { else {
// set up the from and to. // set up the from and to.
if (edge.toId == childNodeId) { // this is a double equals because ints and strings can be interchanged here. if (edge.toId == childNodeId) { // this is a double equals because ints and strings can be interchanged here.
toId = clusterNodeProperties.id;
toId = clusterNodeProperties[nodeIdField];
fromId = edge.fromId; fromId = edge.fromId;
otherNodeId = fromId; otherNodeId = fromId;
} }
else { else {
toId = edge.toId; toId = edge.toId;
fromId = clusterNodeProperties.id;
fromId = clusterNodeProperties[nodeIdField];
otherNodeId = toId; otherNodeId = toId;
} }
} }
@ -310,7 +312,7 @@ class ClusterEngine {
// set up the edge // set up the edge
clonedOptions.from = createEdges[j].fromId; clonedOptions.from = createEdges[j].fromId;
clonedOptions.to = createEdges[j].toId; clonedOptions.to = createEdges[j].toId;
clonedOptions.id = 'clusterEdge:' + util.randomUUID();
clonedOptions[edgeIdField] = 'clusterEdge:' + util.randomUUID();
//clonedOptions.id = '(cf: ' + createEdges[j].fromId + " to: " + createEdges[j].toId + ")" + Math.random(); //clonedOptions.id = '(cf: ' + createEdges[j].fromId + " to: " + createEdges[j].toId + ")" + Math.random();
// create the edge and give a reference to the one it replaced. // create the edge and give a reference to the one it replaced.
@ -351,6 +353,8 @@ class ClusterEngine {
* @private * @private
*/ */
_cluster(childNodesObj, childEdgesObj, options, refreshData = true) { _cluster(childNodesObj, childEdgesObj, options, refreshData = true) {
let nodeIdField = this.body.data.nodes._fieldId;
// kill condition: no children so can't cluster or only one node in the cluster, don't bother // kill condition: no children so can't cluster or only one node in the cluster, don't bother
if (Object.keys(childNodesObj).length < 2) {return;} if (Object.keys(childNodesObj).length < 2) {return;}
@ -395,8 +399,8 @@ class ClusterEngine {
} }
// check if we have an unique id; // check if we have an unique id;
if (clusterNodeProperties.id === undefined) {clusterNodeProperties.id = 'cluster:' + util.randomUUID();}
let clusterId = clusterNodeProperties.id;
if (clusterNodeProperties[nodeIdField] === undefined) {clusterNodeProperties[nodeIdField] = 'cluster:' + util.randomUUID();}
let clusterId = clusterNodeProperties[nodeIdField];
if (clusterNodeProperties.label === undefined) { if (clusterNodeProperties.label === undefined) {
clusterNodeProperties.label = 'cluster'; clusterNodeProperties.label = 'cluster';
@ -415,7 +419,7 @@ class ClusterEngine {
} }
// force the ID to remain the same // force the ID to remain the same
clusterNodeProperties.id = clusterId;
clusterNodeProperties[nodeIdField] = clusterId;
// create the clusterNode // create the clusterNode
let clusterNode = this.body.functions.createNode(clusterNodeProperties, Cluster); let clusterNode = this.body.functions.createNode(clusterNodeProperties, Cluster);
@ -426,7 +430,7 @@ class ClusterEngine {
clusterNode.clusterEdgeProperties = options.clusterEdgeProperties; clusterNode.clusterEdgeProperties = options.clusterEdgeProperties;
// finally put the cluster node into global // finally put the cluster node into global
this.body.nodes[clusterNodeProperties.id] = clusterNode;
this.body.nodes[clusterNodeProperties[nodeIdField]] = clusterNode;
// create the new edges that will connect to the cluster, all self-referencing edges will be added to childEdgesObject here. // create the new edges that will connect to the cluster, all self-referencing edges will be added to childEdgesObject here.
this._createClusterEdges(childNodesObj, childEdgesObj, clusterNodeProperties, options.clusterEdgeProperties); this._createClusterEdges(childNodesObj, childEdgesObj, clusterNodeProperties, options.clusterEdgeProperties);
@ -447,13 +451,13 @@ class ClusterEngine {
// disable the childNodes // disable the childNodes
for (let nodeId in childNodesObj) { for (let nodeId in childNodesObj) {
if (childNodesObj.hasOwnProperty(nodeId)) { if (childNodesObj.hasOwnProperty(nodeId)) {
this.clusteredNodes[nodeId] = {clusterId:clusterNodeProperties.id, node: this.body.nodes[nodeId]};
this.clusteredNodes[nodeId] = {clusterId:clusterNodeProperties[nodeIdField], node: this.body.nodes[nodeId]};
this.body.nodes[nodeId].setOptions({hidden:true, physics:false}); this.body.nodes[nodeId].setOptions({hidden:true, physics:false});
} }
} }
// set ID to undefined so no duplicates arise // set ID to undefined so no duplicates arise
clusterNodeProperties.id = undefined;
clusterNodeProperties[nodeIdField] = undefined;
// wrap up // wrap up
if (refreshData === true) { if (refreshData === true) {

+ 2
- 2
lib/network/modules/components/nodes/Cluster.js View File

@ -4,8 +4,8 @@ import Node from '../Node'
* *
*/ */
class Cluster extends Node { class Cluster extends Node {
constructor(options, body, imagelist, grouplist, globalOptions) {
super(options, body, imagelist, grouplist, globalOptions);
constructor(id, options, body, imagelist, grouplist, globalOptions) {
super(id, options, body, imagelist, grouplist, globalOptions);
this.isCluster = true; this.isCluster = true;
this.containedNodes = {}; this.containedNodes = {};

Loading…
Cancel
Save