From ee0300c38a0567c868b0998228687ce54d12e938 Mon Sep 17 00:00:00 2001 From: Maxime Douailin Date: Fri, 6 Nov 2015 16:24:36 +0100 Subject: [PATCH 1/3] added method setselection used by both selectNodes and selectEdges methods --- lib/network/modules/SelectionHandler.js | 76 +++++++++++++++---------- 1 file changed, 45 insertions(+), 31 deletions(-) diff --git a/lib/network/modules/SelectionHandler.js b/lib/network/modules/SelectionHandler.js index 59376c54..9fc83efd 100644 --- a/lib/network/modules/SelectionHandler.js +++ b/lib/network/modules/SelectionHandler.js @@ -300,7 +300,7 @@ class SelectionHandler { this.selectionObj.edges[edgeId].unselect(); } } - + this.selectionObj = {nodes:{},edges:{}}; } @@ -601,6 +601,44 @@ class SelectionHandler { return idArray; } + /** + * Updates the current selection + * @param {{nodes: Array., edges: Array.}} Selection + * @param {Object} options Options + */ + setSelection(selection, options = {}) { + let i, id; + + if (!selection || !selection.nodes || !selection.edges) + throw 'Selection must be an object with nodes and edges properties'; + // first unselect any selected node + if (options.unselectAll || options.unselectAll === undefined) { + this.unselectAll(); + } + + for (i = 0; i < selection.nodes.length; i++) { + id = selection.nodes[i]; + + let node = this.body.nodes[id]; + if (!node) { + throw new RangeError('Node with id "' + id + '" not found'); + } + // don't select edges with it + this.selectObject(node, options.highlightEdges); + } + + for (i = 0; i < selection.edges.length; i++) { + id = selection.edges[i]; + + let edge = this.body.edges[id]; + if (!edge) { + throw new RangeError('Edge with id "' + id + '" not found'); + } + this.selectObject(edge); + } + this.body.emitter.emit('_requestRedraw'); + } + /** * select zero or more nodes with the option to highlight edges @@ -610,23 +648,11 @@ class SelectionHandler { */ selectNodes(selection, highlightEdges = true) { let i, id; - + if (!selection || (selection.length === undefined)) throw 'Selection must be an array with ids'; - - // first unselect any selected node - this.unselectAll(); - - for (i = 0; i < selection.length; i++) { - id = selection[i]; - - let node = this.body.nodes[id]; - if (!node) { - throw new RangeError('Node with id "' + id + '" not found'); - } - this.selectObject(node,highlightEdges); - } - this.body.emitter.emit('_requestRedraw'); + + this.setSelection({nodes: selection, edges: []}, {highlightEdges: highlightEdges}); } @@ -637,23 +663,11 @@ class SelectionHandler { */ selectEdges(selection) { let i, id; - + if (!selection || (selection.length === undefined)) throw 'Selection must be an array with ids'; - - // first unselect any selected objects - this.unselectAll(); - - for (i = 0; i < selection.length; i++) { - id = selection[i]; - - let edge = this.body.edges[id]; - if (!edge) { - throw new RangeError('Edge with id "' + id + '" not found'); - } - this.selectObject(edge); - } - this.body.emitter.emit('_requestRedraw'); + + this.setSelection({nodes: [], edges: selection}); } /** From 572cafb1da08c1b459f6f9e30a38ae9cd3e6d6c5 Mon Sep 17 00:00:00 2001 From: Maxime Douailin Date: Fri, 6 Nov 2015 17:18:33 +0100 Subject: [PATCH 2/3] updated doc with new setSelection method --- docs/network/index.html | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/docs/network/index.html b/docs/network/index.html index df4f6c97..788b342f 100644 --- a/docs/network/index.html +++ b/docs/network/index.html @@ -940,6 +940,27 @@ function releaseFunction (clusterPosition, containedNodesPositions) { before selecting its own objects. Does not fire events. + + setSelection( + Object selection, + [Object options]) + + + Returns: none + Sets the selection, wich must be an object like this: +
+{
+  nodes: [Array of nodeIds],
+  edges: [Array of edgeIds]
+}
+ Available options are: +
+{
+  unselectAll: Boolean,
+  highlightEdges: Boolean
+}
+ + unselectAll() From 998bfac3f7d4b75485cf99b03ba08aabe4851c17 Mon Sep 17 00:00:00 2001 From: Maxime Douailin Date: Thu, 12 Nov 2015 15:55:11 +0100 Subject: [PATCH 3/3] make edges and nodes parameters optional. dead vars removal --- docs/network/index.html | 1 + lib/network/modules/SelectionHandler.js | 45 ++++++++++++------------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/docs/network/index.html b/docs/network/index.html index 788b342f..4878c6aa 100644 --- a/docs/network/index.html +++ b/docs/network/index.html @@ -953,6 +953,7 @@ function releaseFunction (clusterPosition, containedNodesPositions) { nodes: [Array of nodeIds], edges: [Array of edgeIds] } + You can also pass only nodes or edges in selection object. Available options are:
 {
diff --git a/lib/network/modules/SelectionHandler.js b/lib/network/modules/SelectionHandler.js
index 9fc83efd..15d43547 100644
--- a/lib/network/modules/SelectionHandler.js
+++ b/lib/network/modules/SelectionHandler.js
@@ -609,32 +609,35 @@ class SelectionHandler {
   setSelection(selection, options = {}) {
     let i, id;
 
-    if (!selection || !selection.nodes || !selection.edges)
-      throw 'Selection must be an object with nodes and edges properties';
-    // first unselect any selected node
+    if (!selection || (!selection.nodes && !selection.edges))
+      throw 'Selection must be an object with nodes and/or edges properties';
+    // first unselect any selected node, if option is true or undefined
     if (options.unselectAll || options.unselectAll === undefined) {
       this.unselectAll();
     }
+    if (selection.nodes) {
+      for (i = 0; i < selection.nodes.length; i++) {
+        id = selection.nodes[i];
 
-    for (i = 0; i < selection.nodes.length; i++) {
-      id = selection.nodes[i];
-
-      let node = this.body.nodes[id];
-      if (!node) {
-        throw new RangeError('Node with id "' + id + '" not found');
+        let node = this.body.nodes[id];
+        if (!node) {
+          throw new RangeError('Node with id "' + id + '" not found');
+        }
+        // don't select edges with it
+        this.selectObject(node, options.highlightEdges);
       }
-      // don't select edges with it
-      this.selectObject(node, options.highlightEdges);
     }
 
-    for (i = 0; i < selection.edges.length; i++) {
-      id = selection.edges[i];
+    if (selection.edges) {
+      for (i = 0; i < selection.edges.length; i++) {
+        id = selection.edges[i];
 
-      let edge = this.body.edges[id];
-      if (!edge) {
-        throw new RangeError('Edge with id "' + id + '" not found');
+        let edge = this.body.edges[id];
+        if (!edge) {
+          throw new RangeError('Edge with id "' + id + '" not found');
+        }
+        this.selectObject(edge);
       }
-      this.selectObject(edge);
     }
     this.body.emitter.emit('_requestRedraw');
   }
@@ -647,12 +650,10 @@ class SelectionHandler {
    * @param {boolean} [highlightEdges]
    */
   selectNodes(selection, highlightEdges = true) {
-    let i, id;
-
     if (!selection || (selection.length === undefined))
       throw 'Selection must be an array with ids';
 
-    this.setSelection({nodes: selection, edges: []}, {highlightEdges: highlightEdges});
+    this.setSelection({nodes: selection}, {highlightEdges: highlightEdges});
   }
 
 
@@ -662,12 +663,10 @@ class SelectionHandler {
    *                                            selected nodes.
    */
   selectEdges(selection) {
-    let i, id;
-
     if (!selection || (selection.length === undefined))
       throw 'Selection must be an array with ids';
 
-    this.setSelection({nodes: [], edges: selection});
+    this.setSelection({edges: selection});
   }
 
   /**