Skip to content

Commit

Permalink
feature: Add click handlers for treevie toggles
Browse files Browse the repository at this point in the history
  • Loading branch information
jtrussell committed Nov 25, 2014
1 parent 90ad85b commit 3b36988
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
6 changes: 6 additions & 0 deletions dist/ivh-treeview.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ angular.module('ivh.treeview').directive('ivhTreeviewToggle', [function() {
}

element.bind('click', function() {
ctrl.onNodeClick(node);
if(ctrl.children(node).length) {
$li.toggleClass('ivh-treeview-node-collapsed');
} else {
Expand Down Expand Up @@ -217,6 +218,7 @@ angular.module('ivh.treeview').directive('ivhTreeview', ['ivhTreeviewMgr', funct

// Config options
childrenAttribute: '=ivhTreeviewChildrenAttribute',
clickHandler: '=ivhTreeviewClickHandler',
defaultSelectedState: '=ivhTreeviewDefaultSelectedState',
expandToDepth: '=ivhTreeviewExpandToDepth',
idAttribute: '=ivhTreeviewIdAttribute',
Expand Down Expand Up @@ -298,6 +300,10 @@ angular.module('ivh.treeview').directive('ivhTreeview', ['ivhTreeviewMgr', funct
Infinity : localOpts.expandToDepth;
return depth < expandTo;
};

ctrl.onNodeClick = function(node) {
$scope.clickHandler(node, $scope.root);
};
}],
link: function(scope, element, attrs) {
var opts = scope.ctrl.opts();
Expand Down
2 changes: 1 addition & 1 deletion dist/ivh-treeview.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/scripts/directives/ivh-treeview-toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ angular.module('ivh.treeview').directive('ivhTreeviewToggle', [function() {
}

element.bind('click', function() {
ctrl.onNodeClick(node);
if(ctrl.children(node).length) {
$li.toggleClass('ivh-treeview-node-collapsed');
} else {
Expand Down
5 changes: 5 additions & 0 deletions src/scripts/directives/ivh-treeview.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ angular.module('ivh.treeview').directive('ivhTreeview', ['ivhTreeviewMgr', funct

// Config options
childrenAttribute: '=ivhTreeviewChildrenAttribute',
clickHandler: '=ivhTreeviewClickHandler',
defaultSelectedState: '=ivhTreeviewDefaultSelectedState',
expandToDepth: '=ivhTreeviewExpandToDepth',
idAttribute: '=ivhTreeviewIdAttribute',
Expand Down Expand Up @@ -108,6 +109,10 @@ angular.module('ivh.treeview').directive('ivhTreeview', ['ivhTreeviewMgr', funct
Infinity : localOpts.expandToDepth;
return depth < expandTo;
};

ctrl.onNodeClick = function(node) {
$scope.clickHandler(node, $scope.root);
};
}],
link: function(scope, element, attrs) {
var opts = scope.ctrl.opts();
Expand Down
12 changes: 9 additions & 3 deletions test/spec/directives/ivh-treeview.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,20 @@ describe('Directive ivhTreeview', function() {
$el = compile(tplClickHandler, scope);
});

it('should call the click handler once per click', function() {
$el.find('li[title="top hat"] [ivh-treeview-toggle]').first().click();
scope.$apply();
expect(handlerSpy.calls.count()).toEqual(1);
});

it('should pass the clicked node to the handler', function() {
$el.find('li[title="top hat"]').click();
$el.find('li[title="top hat"] [ivh-treeview-toggle]').first().click();
scope.$apply();
expect(handlerSpy.calls.mostRecent().args[1]).toBe(scope.bag1[0]);
expect(handlerSpy.calls.mostRecent().args[0]).toBe(scope.bag1[0]);
});

it('should pass the tree itself to the click handler', function() {
$el.find('li[title="top hat"]').click();
$el.find('li[title="top hat"] [ivh-treeview-toggle]').click();
scope.$apply();
expect(handlerSpy.calls.mostRecent().args[1]).toBe(scope.bag1);
});
Expand Down

0 comments on commit 3b36988

Please sign in to comment.