From bb36e40582a9506530d6cf3728049844f2d1caba Mon Sep 17 00:00:00 2001 From: Wesley Cho Date: Mon, 8 Feb 2016 06:49:06 -0800 Subject: [PATCH] feat(tab): add select expressions - Add support for event being available in select/deselect callbacks Closes #5438 --- src/tabs/docs/readme.md | 4 ++-- src/tabs/tabs.js | 14 +++++++++----- src/tabs/test/tabs.spec.js | 10 ++++++++-- template/tabs/tab.html | 2 +- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/tabs/docs/readme.md b/src/tabs/docs/readme.md index a8a9505116..0bdbfeb9ed 100644 --- a/src/tabs/docs/readme.md +++ b/src/tabs/docs/readme.md @@ -33,7 +33,7 @@ AngularJS version of the tabs directive. * `deselect()` $ - - An optional expression called when tab is deactivated. + An optional expression called when tab is deactivated. Supports $event in template for expression. * `disable` $ @@ -49,7 +49,7 @@ AngularJS version of the tabs directive. * `select()` $ - - An optional expression called when tab is activated. + An optional expression called when tab is activated. Supports $event in template for expression. * `template-url` _(Default: `uib/template/tabs/tab.html`)_ - diff --git a/src/tabs/tabs.js b/src/tabs/tabs.js index b5fdd15280..e79b9187d7 100644 --- a/src/tabs/tabs.js +++ b/src/tabs/tabs.js @@ -5,18 +5,22 @@ angular.module('ui.bootstrap.tabs', []) oldIndex; ctrl.tabs = []; - ctrl.select = function(index) { + ctrl.select = function(index, evt) { if (!destroyed) { var previousIndex = findTabIndex(oldIndex); var previousSelected = ctrl.tabs[previousIndex]; if (previousSelected) { - previousSelected.tab.onDeselect(); + previousSelected.tab.onDeselect({ + $event: evt + }); previousSelected.tab.active = false; } var selected = ctrl.tabs[index]; if (selected) { - selected.tab.onSelect(); + selected.tab.onSelect({ + $event: evt + }); selected.tab.active = true; ctrl.active = selected.index; oldIndex = selected.index; @@ -148,7 +152,7 @@ angular.module('ui.bootstrap.tabs', []) scope.classes = ''; } - scope.select = function() { + scope.select = function(evt) { if (!scope.disabled) { var index; for (var i = 0; i < tabsetCtrl.tabs.length; i++) { @@ -158,7 +162,7 @@ angular.module('ui.bootstrap.tabs', []) } } - tabsetCtrl.select(index); + tabsetCtrl.select(index, evt); } }; diff --git a/src/tabs/test/tabs.spec.js b/src/tabs/test/tabs.spec.js index 8b9c44dbaf..c18e9ea258 100644 --- a/src/tabs/test/tabs.spec.js +++ b/src/tabs/test/tabs.spec.js @@ -42,10 +42,10 @@ describe('tabs', function() { scope.deselectSecond = jasmine.createSpy(); elm = $compile([ '', - ' ', + ' ', ' first content is {{first}}', ' ', - ' ', + ' ', ' Second Tab {{second}}', ' second content is {{second}}', ' ', @@ -97,19 +97,25 @@ describe('tabs', function() { }); it('should call select callback on select', function() { + expect(scope.selectFirst.calls.count()).toBe(1); titles().eq(1).find('> a').click(); expect(scope.selectSecond).toHaveBeenCalled(); + expect(scope.selectSecond.calls.argsFor(0)[0].target).toBe(titles().eq(1).find('> a')[0]); titles().eq(0).find('> a').click(); expect(scope.selectFirst).toHaveBeenCalled(); + expect(scope.selectFirst.calls.argsFor(1)[0].target).toBe(titles().eq(0).find('> a')[0]); }); it('should call deselect callback on deselect', function() { titles().eq(1).find('> a').click(); expect(scope.deselectFirst).toHaveBeenCalled(); + expect(scope.deselectFirst.calls.argsFor(0)[0].target).toBe(titles().eq(1).find('> a')[0]); titles().eq(0).find('> a').click(); expect(scope.deselectSecond).toHaveBeenCalled(); + expect(scope.deselectSecond.calls.argsFor(0)[0].target).toBe(titles().eq(0).find('> a')[0]); titles().eq(1).find('> a').click(); expect(scope.deselectFirst.calls.count()).toBe(2); + expect(scope.deselectFirst.calls.argsFor(1)[0].target).toBe(titles().eq(1).find('> a')[0]); }); }); diff --git a/template/tabs/tab.html b/template/tabs/tab.html index 886417d42e..a368f86562 100644 --- a/template/tabs/tab.html +++ b/template/tabs/tab.html @@ -1,3 +1,3 @@