Skip to content

Commit

Permalink
Merge branch 'features/63-get-selected-nodes' into develop
Browse files Browse the repository at this point in the history
* features/63-get-selected-nodes:
  Added tests and doc #63
  Implemented stated based get methods `getSelected`, `getUnselected`, `getExpanded` and `getCollapsed` #63
  • Loading branch information
jonmiles committed Apr 14, 2015
2 parents 3d7f9d1 + 2d081c7 commit 59835ed
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 8 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,21 @@ $('#tree').treeview('expandNode', [ nodeId, { levels: 2, silent: true } ]);

Triggers `nodeExpanded` event; pass silent to suppress events.

#### getCollapsed()

Returns an array of collapsed nodes e.g. state.expanded = false.

```javascript
$('#tree').treeview('getCollapsed', nodeId);
```
#### getExpanded()

Returns an array of expanded nodes e.g. state.expanded = true.

```javascript
$('#tree').treeview('getExpanded', nodeId);
```

#### getNode(nodeId)

Returns a single node object that matches the given node id.
Expand All @@ -429,6 +444,14 @@ Returns the parent node of a given node, if valid otherwise returns undefined.
$('#tree').treeview('getParent', node);
```

#### getSelected()

Returns an array of selected nodes e.g. state.selected = true.

```javascript
$('#tree').treeview('getSelected', nodeId);
```

#### getSiblings(node | nodeId)

Returns an array of sibling nodes for a given node, if valid otherwise returns undefined.
Expand All @@ -437,6 +460,14 @@ Returns an array of sibling nodes for a given node, if valid otherwise returns u
$('#tree').treeview('getSiblings', node);
```

#### getUnselected()

Returns an array of unselected nodes e.g. state.selected = false.

```javascript
$('#tree').treeview('getUnselected', nodeId);
```

#### remove()

Removes the tree view component. Removing attached events, internal attached objects, and added HTML elements.
Expand Down
2 changes: 1 addition & 1 deletion dist/bootstrap-treeview.min.js

Large diffs are not rendered by default.

40 changes: 38 additions & 2 deletions public/js/bootstrap-treeview.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@
getNode: $.proxy(this.getNode, this),
getParent: $.proxy(this.getParent, this),
getSiblings: $.proxy(this.getSiblings, this),
getSelected: $.proxy(this.getSelected, this),
getUnselected: $.proxy(this.getUnselected, this),
getExpanded: $.proxy(this.getExpanded, this),
getCollapsed: $.proxy(this.getCollapsed, this),

// Select methods
selectNode: $.proxy(this.selectNode, this),
Expand Down Expand Up @@ -572,7 +576,7 @@
/**
Returns the parent node of a given node, if valid otherwise returns undefined.
@param {Object|Number} identifier - A valid node or node id
@returns {Object} parent - The parent node
@returns {Object} node - The parent node
*/
Tree.prototype.getParent = function (identifier) {
var node = this.identifyNode(identifier);
Expand All @@ -582,7 +586,7 @@
/**
Returns an array of sibling nodes for a given node, if valid otherwise returns undefined.
@param {Object|Number} identifier - A valid node or node id
@returns {Array} siblings - Sibling nodes
@returns {Array} nodes - Sibling nodes
*/
Tree.prototype.getSiblings = function (identifier) {
var node = this.identifyNode(identifier);
Expand All @@ -593,6 +597,38 @@
});
};

/**
Returns an array of selected nodes.
@returns {Array} nodes - Selected nodes
*/
Tree.prototype.getSelected = function () {
return this.findNodes('true', 'g', 'state.selected');
};

/**
Returns an array of unselected nodes.
@returns {Array} nodes - Unselected nodes
*/
Tree.prototype.getUnselected = function () {
return this.findNodes('false', 'g', 'state.selected');
};

/**
Returns an array of expanded nodes.
@returns {Array} nodes - Expanded nodes
*/
Tree.prototype.getExpanded = function () {
return this.findNodes('true', 'g', 'state.expanded');
};

/**
Returns an array of collapsed nodes.
@returns {Array} nodes - Collapsed nodes
*/
Tree.prototype.getCollapsed = function () {
return this.findNodes('false', 'g', 'state.expanded');
};


/**
Set a node state to selected
Expand Down
40 changes: 38 additions & 2 deletions src/js/bootstrap-treeview.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@
getNode: $.proxy(this.getNode, this),
getParent: $.proxy(this.getParent, this),
getSiblings: $.proxy(this.getSiblings, this),
getSelected: $.proxy(this.getSelected, this),
getUnselected: $.proxy(this.getUnselected, this),
getExpanded: $.proxy(this.getExpanded, this),
getCollapsed: $.proxy(this.getCollapsed, this),

// Select methods
selectNode: $.proxy(this.selectNode, this),
Expand Down Expand Up @@ -572,7 +576,7 @@
/**
Returns the parent node of a given node, if valid otherwise returns undefined.
@param {Object|Number} identifier - A valid node or node id
@returns {Object} parent - The parent node
@returns {Object} node - The parent node
*/
Tree.prototype.getParent = function (identifier) {
var node = this.identifyNode(identifier);
Expand All @@ -582,7 +586,7 @@
/**
Returns an array of sibling nodes for a given node, if valid otherwise returns undefined.
@param {Object|Number} identifier - A valid node or node id
@returns {Array} siblings - Sibling nodes
@returns {Array} nodes - Sibling nodes
*/
Tree.prototype.getSiblings = function (identifier) {
var node = this.identifyNode(identifier);
Expand All @@ -593,6 +597,38 @@
});
};

/**
Returns an array of selected nodes.
@returns {Array} nodes - Selected nodes
*/
Tree.prototype.getSelected = function () {
return this.findNodes('true', 'g', 'state.selected');
};

/**
Returns an array of unselected nodes.
@returns {Array} nodes - Unselected nodes
*/
Tree.prototype.getUnselected = function () {
return this.findNodes('false', 'g', 'state.selected');
};

/**
Returns an array of expanded nodes.
@returns {Array} nodes - Expanded nodes
*/
Tree.prototype.getExpanded = function () {
return this.findNodes('true', 'g', 'state.expanded');
};

/**
Returns an array of collapsed nodes.
@returns {Array} nodes - Collapsed nodes
*/
Tree.prototype.getCollapsed = function () {
return this.findNodes('false', 'g', 'state.expanded');
};


/**
Set a node state to selected
Expand Down
40 changes: 38 additions & 2 deletions tests/lib/bootstrap-treeview.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@
getNode: $.proxy(this.getNode, this),
getParent: $.proxy(this.getParent, this),
getSiblings: $.proxy(this.getSiblings, this),
getSelected: $.proxy(this.getSelected, this),
getUnselected: $.proxy(this.getUnselected, this),
getExpanded: $.proxy(this.getExpanded, this),
getCollapsed: $.proxy(this.getCollapsed, this),

// Select methods
selectNode: $.proxy(this.selectNode, this),
Expand Down Expand Up @@ -572,7 +576,7 @@
/**
Returns the parent node of a given node, if valid otherwise returns undefined.
@param {Object|Number} identifier - A valid node or node id
@returns {Object} parent - The parent node
@returns {Object} node - The parent node
*/
Tree.prototype.getParent = function (identifier) {
var node = this.identifyNode(identifier);
Expand All @@ -582,7 +586,7 @@
/**
Returns an array of sibling nodes for a given node, if valid otherwise returns undefined.
@param {Object|Number} identifier - A valid node or node id
@returns {Array} siblings - Sibling nodes
@returns {Array} nodes - Sibling nodes
*/
Tree.prototype.getSiblings = function (identifier) {
var node = this.identifyNode(identifier);
Expand All @@ -593,6 +597,38 @@
});
};

/**
Returns an array of selected nodes.
@returns {Array} nodes - Selected nodes
*/
Tree.prototype.getSelected = function () {
return this.findNodes('true', 'g', 'state.selected');
};

/**
Returns an array of unselected nodes.
@returns {Array} nodes - Unselected nodes
*/
Tree.prototype.getUnselected = function () {
return this.findNodes('false', 'g', 'state.selected');
};

/**
Returns an array of expanded nodes.
@returns {Array} nodes - Expanded nodes
*/
Tree.prototype.getExpanded = function () {
return this.findNodes('true', 'g', 'state.expanded');
};

/**
Returns an array of collapsed nodes.
@returns {Array} nodes - Collapsed nodes
*/
Tree.prototype.getCollapsed = function () {
return this.findNodes('false', 'g', 'state.expanded');
};


/**
Set a node state to selected
Expand Down
43 changes: 42 additions & 1 deletion tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@
test('getNode', function () {
var $tree = init({ data: data });
var nodeParent1 = $tree.treeview('getNode', 0);
equal(nodeParent1.text, 'Parent 1', 'Correct node returned : requested "Parent 1", for "Parent 1"');
equal(nodeParent1.text, 'Parent 1', 'Correct node returned : requested "Parent 1", got "Parent 1"');
});

test('getParent', function () {
Expand Down Expand Up @@ -412,6 +412,47 @@
ok(results, 'Correct siblings for "Child 1" [non root] : results OK');
});

test('getSelected', function () {
var $tree = init({ data: data })
.treeview('selectNode', 0);

var selectedNodes = $tree.treeview('getSelected');
ok((selectedNodes instanceof Array), 'Result is an array');
equal(selectedNodes.length, 1, 'Correct number of nodes returned');
equal(selectedNodes[0].text, 'Parent 1', 'Correct node returned');
});

test('getUnselected', function () {
var $tree = init({ data: data })
.treeview('selectNode', 0);

var unselectedNodes = $tree.treeview('getUnselected');
ok((unselectedNodes instanceof Array), 'Result is an array');
equal(unselectedNodes.length, 8, 'Correct number of nodes returned');
});

// Assumptions:
// Default tree + expanded to 2 levels,
// means 1 node 'Parent 1' should be expanded and therefore returned
test('getExpanded', function () {
var $tree = init({ data: data });
var expandedNodes = $tree.treeview('getExpanded');
ok((expandedNodes instanceof Array), 'Result is an array');
equal(expandedNodes.length, 1, 'Correct number of nodes returned');
equal(expandedNodes[0].text, 'Parent 1', 'Correct node returned');
});

// Assumptions:
// Default tree + expanded to 2 levels, means only 'Parent 1' should be expanded
// as all other parent nodes have no children their state will be collapsed
// which means 8 of the 9 nodes should be returned
test('getCollapsed', function () {
var $tree = init({ data: data });
var collapsedNodes = $tree.treeview('getCollapsed');
ok((collapsedNodes instanceof Array), 'Result is an array');
equal(collapsedNodes.length, 8, 'Correct number of nodes returned');
});

test('selectNode / unselectNode', function () {
var $tree = init({ data: data });
var el;
Expand Down

0 comments on commit 59835ed

Please sign in to comment.