Browse Source

Protect Network from zero and negative mass values (#3135)

* Protect Network from zero and negative mass values

Fix for #3133

Option-field 'node.mass` must be >= 0.
Checks have been added at the nodes level, for both nodes-global and nodes specific options.
In addition, an internal check has been added for `NodeHandler.defaultOptions`.

The documentation has been adjusted for this change.

* Fix whitespace
gemini
wimrijnders 7 years ago
committed by yotamberk
parent
commit
f8ba6f037c
3 changed files with 32 additions and 1 deletions
  1. +4
    -1
      docs/network/nodes.html
  2. +6
    -0
      lib/network/modules/NodesHandler.js
  3. +22
    -0
      lib/network/modules/components/Node.js

+ 4
- 1
docs/network/nodes.html View File

@ -809,7 +809,10 @@ network.setOptions(options);
<td>Number</td> <td>Number</td>
<td><code>1</code></td> <td><code>1</code></td>
<td>The barnesHut physics model (which is enabled by default) is based on an inverted gravity model. By <td>The barnesHut physics model (which is enabled by default) is based on an inverted gravity model. By
increasing the mass of a node, you increase it's repulsion. Values lower than 1 are not recommended.
increasing the mass of a node, you increase it's repulsion.
<br><br>
Values between 0 and 1 are not recommended.<br>
Negative or zero values are not allowed. These will generate a console error and will be set to 1.
</td> </td>
</tr> </tr>
<tr> <tr>

+ 6
- 0
lib/network/modules/NodesHandler.js View File

@ -129,6 +129,12 @@ class NodesHandler {
x: undefined, x: undefined,
y: undefined y: undefined
}; };
// Protect from idiocy
if (this.defaultOptions.mass <= 0) {
throw 'Internal error: mass in defaultOptions of NodesHandler may not be zero or negative';
}
util.extend(this.options, this.defaultOptions); util.extend(this.options, this.defaultOptions);
this.bindEventListeners(); this.bindEventListeners();

+ 22
- 0
lib/network/modules/components/Node.js View File

@ -115,6 +115,8 @@ class Node {
throw "Node must have an id"; throw "Node must have an id";
} }
Node.checkMass(options, this.id);
// set these options locally // set these options locally
// clear x and y positions // clear x and y positions
if (options.x !== undefined) { if (options.x !== undefined) {
@ -210,6 +212,8 @@ class Node {
]; ];
util.selectiveNotDeepExtend(fields, parentOptions, newOptions, allowDeletion); util.selectiveNotDeepExtend(fields, parentOptions, newOptions, allowDeletion);
Node.checkMass(newOptions);
// merge the shadow options into the parent. // merge the shadow options into the parent.
util.mergeOptions(parentOptions, newOptions, 'shadow', allowDeletion, globalOptions); util.mergeOptions(parentOptions, newOptions, 'shadow', allowDeletion, globalOptions);
@ -538,6 +542,24 @@ class Node {
this.shape.boundingBox.bottom > obj.top this.shape.boundingBox.bottom > obj.top
); );
} }
/**
* Check valid values for mass
*
* The mass may not be negative or zero. If it is, reset to 1
*/
static checkMass(options, id) {
if (options.mass !== undefined && options.mass <= 0) {
let strId = '';
if (id !== undefined) {
strId = ' in node id: ' + id;
}
console.log('%cNegative or zero mass disallowed' + strId +
', setting mass to 1.' , printStyle);
options.mass = 1;
}
}
} }
export default Node; export default Node;

Loading…
Cancel
Save