Skip to content

Commit

Permalink
Add treenode.getNodesByRange
Browse files Browse the repository at this point in the history
  • Loading branch information
meirotstein committed Apr 1, 2018
1 parent 39e8d36 commit e57c292
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
17 changes: 17 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,23 @@ Set selection for a range of nodes, Only applicable for mode 'tree'.

Path for the end node


#### `JSONEditor.getNodesByRange(start, end)`

A utility function for getting a list of `Node` instances under certain range.

This function can be used as complementary to `getSelection` and `onSelectionChange` if a list of __all__ the selected nodes is required

*Parameters:*

- `{{path: Array.<String>}} start`

Path for the first node in range

- `{{path: Array.<String>}} end`

Path for the last node in range

### Examples

A tree editor:
Expand Down
22 changes: 18 additions & 4 deletions src/js/treemode.js
Original file line number Diff line number Diff line change
Expand Up @@ -1384,6 +1384,21 @@ treemode.setSelection = function (start, end) {
this.setDomSelection(startNode);
}

var nodes = this.getNodesByRange(start, end);

nodes.forEach(function(node) {
node.expandTo();
});
this.select(nodes);
};

/**
* Returns a set of Nodes according to a range of selection
* @param {{path: Array.<String>}} start object contains the path for selection start
* @param {{path: Array.<String>}=} end object contains the path for selection end
* @return {Array.<Node>} Node intances on the given range
*/
treemode.getNodesByRange = function (start, end) {
var startNode, endNode;

if (start && start.path) {
Expand Down Expand Up @@ -1418,10 +1433,9 @@ treemode.setSelection = function (start, end) {
nodes.push(startNode);
}
}
nodes.forEach(function(node) {
node.expandTo();
});
this.select(nodes);

return nodes;

};

// define modes
Expand Down

0 comments on commit e57c292

Please sign in to comment.