Skip to content

Commit 4427e87

Browse files
committed
feat(ionTab): allow custom ngClick expression that doesnt select tab
Closes #784
1 parent 0ffb748 commit 4427e87

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

js/ext/angular/src/directive/ionicTabBar.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ function($scope, $ionicViewService, $rootScope, $element) {
245245
* @param {expression=} badge-style The style of badge to put on this tab (eg tabs-positive).
246246
* @param {expression=} on-select Called when this tab is selected.
247247
* @param {expression=} on-deselect Called when this tab is deselected.
248+
* @param {expression=} ng-click By default, the tab will be selected on click. If ngClick is set, it will not. You can explicitly switch tabs using {@link ionic.controller:ionicTabs#select ionicTabBar controller's select method}.
248249
*/
249250
.directive('ionTab', ['$rootScope', '$animate', '$ionicBind', '$compile', '$ionicViewService',
250251
function($rootScope, $animate, $ionicBind, $compile, $ionicViewService) {
@@ -297,6 +298,7 @@ function($rootScope, $animate, $ionicBind, $compile, $ionicViewService) {
297298

298299
tabNavElement = angular.element(
299300
'<ion-tab-nav' +
301+
attrStr('ng-click', attr.ngClick) +
300302
attrStr('title', attr.title) +
301303
attrStr('icon', attr.icon) +
302304
attrStr('icon-on', attr.iconOn) +
@@ -337,14 +339,14 @@ function($rootScope, $animate, $ionicBind, $compile, $ionicViewService) {
337339
};
338340
}])
339341

340-
.directive('ionTabNav', function() {
342+
.directive('ionTabNav', ['$ionicNgClick', function($ionicNgClick) {
341343
return {
342344
restrict: 'E',
343345
replace: true,
344346
require: ['^ionTabs', '^ionTab'],
345347
template:
346348
'<a ng-class="{active: isTabActive(), \'has-badge\':badge}" ' +
347-
'ng-click="selectTab($event)" class="tab-item">' +
349+
' class="tab-item">' +
348350
'<span class="badge {{badgeStyle}}" ng-if="badge">{{badge}}</span>' +
349351
'<i class="icon {{getIconOn()}}" ng-if="getIconOn() && isTabActive()"></i>' +
350352
'<i class="icon {{getIconOff()}}" ng-if="getIconOff() && !isTabActive()"></i>' +
@@ -363,6 +365,14 @@ function($rootScope, $animate, $ionicBind, $compile, $ionicViewService) {
363365
var tabsCtrl = ctrls[0],
364366
tabCtrl = ctrls[1];
365367

368+
$scope.selectTab = function(e) {
369+
e.preventDefault();
370+
tabsCtrl.select(tabCtrl.$scope, true);
371+
};
372+
if (!$attrs.ngClick) {
373+
$ionicNgClick($scope, $element, 'selectTab($event)');
374+
}
375+
366376
$scope.getIconOn = function() {
367377
return $scope.iconOn || $scope.icon;
368378
};
@@ -373,11 +383,7 @@ function($rootScope, $animate, $ionicBind, $compile, $ionicViewService) {
373383
$scope.isTabActive = function() {
374384
return tabsCtrl.selectedTab() === tabCtrl.$scope;
375385
};
376-
$scope.selectTab = function(e) {
377-
e.preventDefault();
378-
tabsCtrl.select(tabCtrl.$scope, true);
379-
};
380386
};
381387
}
382388
};
383-
});
389+
}]);

js/ext/angular/test/directive/ionicTabBar.unit.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,13 +292,14 @@ describe('tabs', function() {
292292
});
293293

294294
it('should compile a <ion-tab-nav> with all of the relevant attrs', function() {
295-
setup('title=1 icon-on=2 icon-off=3 badge=4 badge-style=5');
295+
setup('title=1 icon-on=2 icon-off=3 badge=4 badge-style=5 ng-click=6');
296296
var navItem = angular.element(tabsEl[0].querySelector('.tab-item'));
297297
expect(navItem.attr('title')).toEqual('1');
298298
expect(navItem.attr('icon-on')).toEqual('2');
299299
expect(navItem.attr('icon-off')).toEqual('3');
300300
expect(navItem.attr('badge')).toEqual('4');
301301
expect(navItem.attr('badge-style')).toEqual('5');
302+
expect(navItem.attr('ng-click')).toEqual('6');
302303

303304
expect(navItem.parent()[0]).toBe(tabsCtrl.$tabsElement[0]);
304305
});
@@ -437,12 +438,20 @@ describe('tabs', function() {
437438

438439
});
439440

440-
it('should select tab on click', function() {
441+
it('should select tab on click by default', function() {
441442
var el = setup();
442443
el.triggerHandler('click');
443444
expect(tabsCtrl.select).toHaveBeenCalledWith(tabCtrl.$scope, true);
444445
});
445446

447+
it('should use ngClick if defined', function() {
448+
var el = setup('ng-click="doSomething()"');
449+
el.scope().doSomething = jasmine.createSpy('doSomething');
450+
el.triggerHandler('click');
451+
expect(tabsCtrl.select).not.toHaveBeenCalled();
452+
expect(el.scope().doSomething).toHaveBeenCalled();
453+
});
454+
446455
it('should have title and only title', function() {
447456
var el = setup('title="<b>hi, {{name}}!</b>"');
448457
expect(el.find('.tab-title').html()).toBe('<b>hi, !</b>');

0 commit comments

Comments
 (0)