Skip to content

Commit

Permalink
Added event handling to expand / collapse methods, plus silent option…
Browse files Browse the repository at this point in the history
… to suppress events #28
  • Loading branch information
jonmiles committed Mar 21, 2015
1 parent b56196b commit 45a8395
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 132 deletions.
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,45 +287,45 @@ $('#tree').treeview('clearSearch');

Triggers `searchCleared` event

### collapseAll()
### collapseAll(options)

Collapse all tree nodes, collapsing the entire tree.

```javascript
$('#tree').treeview('collapseAll');
$('#tree').treeview('collapseAll', { silent: true });
```

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

### collapseNode(node | nodeId)
### collapseNode(node | nodeId, options)

Collapse a given tree node, accepts node or nodeId

```javascript
$('#tree').treeview('collapseNode', nodeId);
$('#tree').treeview('collapseNode', [ nodeId, { silent: true } ]);
```

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

### expandAll([levels])
### expandAll(options)

Expand all tree nodes. Optionally can be expanded to any given number of levels.

```javascript
$('#tree').treeview('expandAll', levels);
$('#tree').treeview('expandAll', { levels: 2, silent: true });
```

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

### expandNode(node | nodeId, [levels])
### expandNode(node | nodeId, options)

Expand a given tree node, accepts node or nodeId. Optionally can be expanded to any given number of levels.

```javascript
$('#tree').treeview('expandNode', [ nodeId, levels]);
$('#tree').treeview('expandNode', [ nodeId, { levels: 2, silent: true } ]);
```

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

### getNode(nodeId)

Expand Down Expand Up @@ -384,15 +384,15 @@ $('#tree').treeview('selectNode', nodeId);

Triggers `nodeSelected` event

### toggleNodeExpanded(node | nodeId)
### toggleNodeExpanded(node | nodeId, options)

Toggles a nodes expanded state; collapsing if expanded, expanding if collapsed.

```javascript
$('#tree').treeview('toggleNodeExpanded', nodeId);
$('#tree').treeview('toggleNodeExpanded', [ nodeId, { silent: true } ]);
```

Triggers either `nodeExpanded` or `nodeCollapsed` event
Triggers either `nodeExpanded` or `nodeCollapsed` event; pass silent to suppress events.

### unselectNode(nodeId)

Expand Down
2 changes: 1 addition & 1 deletion dist/bootstrap-treeview.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ <h2></h2>

$('#btn-expand-node.expand-node').on('click', function (e) {
var levels = $('#select-expand-node-levels').val();
$expandibleTree.treeview('expandNode', [ expandibleNode[0], levels ]);
$expandibleTree.treeview('expandNode', [ expandibleNode[0], { levels: levels } ]);
});

$('#btn-collapse-node.expand-node').on('click', function (e) {
Expand All @@ -486,7 +486,7 @@ <h2></h2>
// Expand/collapse all
$('#btn-expand-all').on('click', function (e) {
var levels = $('#select-expand-all-levels').val();
$expandibleTree.treeview('expandAll', levels);
$expandibleTree.treeview('expandAll', { levels: levels });
});

$('#btn-collapse-all').on('click', function (e) {
Expand Down
98 changes: 61 additions & 37 deletions public/js/bootstrap-treeview.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,20 +286,25 @@
return node;
};

Tree.prototype.toggleExpandedState = function (node) {

if (!node) { return; }
Tree.prototype.toggleExpandedState = function (node, silent) {
if (!node) return;
this.setExpandedState(node, !node.states.expanded, silent);
this.render();
};

if (node.states.expanded) {
node.states.expanded = false;
this.$element.trigger('nodeCollapsed', $.extend(true, {}, node));
Tree.prototype.setExpandedState = function (node, state, silent) {
if (state) {
node.states.expanded = true;
if (!silent) {
this.$element.trigger('nodeExpanded', $.extend(true, {}, node));
}
}
else {
node.states.expanded = true;
this.$element.trigger('nodeExpanded', $.extend(true, {}, node));
node.states.expanded = false;
if (!silent) {
this.$element.trigger('nodeCollapsed', $.extend(true, {}, node));
}
}

this.render();
};

Tree.prototype.toggleSelectedState = function (node) {
Expand Down Expand Up @@ -537,70 +542,83 @@

/**
Collapse all tree nodes
@param {optional Object} options
*/
Tree.prototype.collapseAll = function () {
$.each(this.nodes, function (index, node) {
node.states.expanded = false;
});
Tree.prototype.collapseAll = function (options) {
var silent = this.isSilent(options);

$.each(this.nodes, $.proxy(function (index, node) {
this.setExpandedState(node, false, silent);
}, this));

this.render();
};

/**
Collapse a given tree node
@param {Object|Number} identifier - node or node id
@param {optional Object} options
*/
Tree.prototype.collapseNode = function (identifier) {
this.identifyNode(identifier).states.expanded = false;
Tree.prototype.collapseNode = function (identifier, options) {
var silent = this.isSilent(options);
this.setExpandedState(this.identifyNode(identifier), false, silent);
this.render();
};

/**
Expand all tree nodes
@param {optional Object} options
*/
Tree.prototype.expandAll = function (levels) {
if (levels) {
this.expandLevels(this.tree, levels);
Tree.prototype.expandAll = function (options) {
var silent = this.isSilent(options);

if (options && options.levels) {
this.expandLevels(this.tree, options.levels, silent);
}
else {
$.each(this.nodes, function (index, node) {
node.states.expanded = true;
});
$.each(this.nodes, $.proxy(function (index, node) {
this.setExpandedState(node, true, silent);
}, this));
}

this.render();
};

/**
Expand a given tree node
@param {Object|Number} identifier - node or node id
@param {optional Number} levels - number of levels to expand to
@param {optional Object} options
*/
Tree.prototype.expandNode = function (identifier, levels) {
Tree.prototype.expandNode = function (identifier, options) {
var silent = this.isSilent(options);

var node = this.identifyNode(identifier);
node.states.expanded = true;
if (levels && node.nodes) {
this.expandLevels(node.nodes, levels-1);
this.setExpandedState(node, true, silent);

if (node.nodes && (options && options.levels)) {
this.expandLevels(node.nodes, options.levels-1, silent);
}

this.render();
};

Tree.prototype.expandLevels = function (nodes, level) {
var _this = this;
$.each(nodes, function (index, node) {
node.states.expanded = (level > 0) ? true : false;
Tree.prototype.expandLevels = function (nodes, level, silent) {
$.each(nodes, $.proxy(function (index, node) {
this.setExpandedState(node, (level > 0) ? true : false)
if (node.nodes) {
_this.expandLevels(node.nodes, level-1);
this.expandLevels(node.nodes, level-1, silent);
}
});
}, this));
}

/**
Toggles a nodes expanded state; collapsing if expanded, expanding if collapsed.
@param {Object|Number} identifier = node or node id
@param {optional Object} options
*/
Tree.prototype.toggleNodeExpanded = function (identifier) {
var node = this.identifyNode(identifier)
node.states.expanded = !node.states.expanded;
this.render();
Tree.prototype.toggleNodeExpanded = function (identifier, options) {
this.toggleExpandedState(this.identifyNode(identifier),
this.isSilent(options));
}

Tree.prototype.identifyNode = function (identifier) {
Expand All @@ -609,6 +627,12 @@
identifier;
}

Tree.prototype.isSilent = function (options) {
return (options && options.hasOwnProperty('silent')) ?
options.silent :
false;
}


/**
Set a node state to selected
Expand Down
Loading

0 comments on commit 45a8395

Please sign in to comment.