From 082259dcfbe126e9945d017c93b8794af3feb06c Mon Sep 17 00:00:00 2001 From: Max Lynch Date: Tue, 29 Oct 2013 14:27:54 -0500 Subject: [PATCH] Fixed #40 with test --- dist/js/ionic-angular.js | 11 +++++++++- dist/js/ionic.js | 6 ++++- js/controllers/tabBarController.js | 6 ++++- js/ext/angular/src/directive/ionicTabBar.js | 11 +++++++++- .../test/directive/ionicTabBar.unit.js | 22 +++++++++++++++++++ js/ext/angular/test/tabs.html | 12 ++++++++-- test/controllers/tabs.html | 5 +++-- 7 files changed, 65 insertions(+), 8 deletions(-) diff --git a/dist/js/ionic-angular.js b/dist/js/ionic-angular.js index 0791263645..3e5598b05b 100644 --- a/dist/js/ionic-angular.js +++ b/dist/js/ionic-angular.js @@ -858,6 +858,14 @@ angular.module('ionic.ui.tabs', ['ngAnimate']) angular.extend(this, ionic.controllers.TabBarController.prototype); ionic.controllers.TabBarController.call(this, { + controllerChanged: function(oldC, oldI, newC, newI) { + $scope.controllerChanged && $scope.controllerChanged({ + oldController: oldC, + oldIndex: oldI, + newController: newC, + newIndex: newI + }); + }, tabBar: { tryTabSelect: function() {}, setSelectedItem: function(index) {}, @@ -891,7 +899,8 @@ angular.module('ionic.ui.tabs', ['ngAnimate']) restrict: 'E', replace: true, scope: { - animation: '@' + animation: '@', + controllerChanged: '&' }, transclude: true, controller: 'TabsCtrl', diff --git a/dist/js/ionic.js b/dist/js/ionic.js index 8679651f55..f4f5da6c71 100644 --- a/dist/js/ionic.js +++ b/dist/js/ionic.js @@ -4923,7 +4923,6 @@ ionic.controllers.TabBarController = ionic.controllers.ViewController.inherit({ if(shouldChange) { this.setSelectedController(index); - this.controllerChanged && this.controllerChanged(this.selectedController, this.selectedIndex); } }, @@ -4932,11 +4931,16 @@ ionic.controllers.TabBarController = ionic.controllers.ViewController.inherit({ if(index >= this.controllers.length) { return; } + var lastController = this.selectedController; + var lastIndex = this.selectedIndex; + this.selectedController = this.controllers[index]; this.selectedIndex = index; this._showController(index); this.tabBar.setSelectedItem(index); + + this.controllerChanged && this.controllerChanged(lastController, lastIndex, this.selectedController, this.selectedIndex); }, _showController: function(index) { diff --git a/js/controllers/tabBarController.js b/js/controllers/tabBarController.js index 34f9383846..45bcab26d9 100644 --- a/js/controllers/tabBarController.js +++ b/js/controllers/tabBarController.js @@ -55,7 +55,6 @@ ionic.controllers.TabBarController = ionic.controllers.ViewController.inherit({ if(shouldChange) { this.setSelectedController(index); - this.controllerChanged && this.controllerChanged(this.selectedController, this.selectedIndex); } }, @@ -64,11 +63,16 @@ ionic.controllers.TabBarController = ionic.controllers.ViewController.inherit({ if(index >= this.controllers.length) { return; } + var lastController = this.selectedController; + var lastIndex = this.selectedIndex; + this.selectedController = this.controllers[index]; this.selectedIndex = index; this._showController(index); this.tabBar.setSelectedItem(index); + + this.controllerChanged && this.controllerChanged(lastController, lastIndex, this.selectedController, this.selectedIndex); }, _showController: function(index) { diff --git a/js/ext/angular/src/directive/ionicTabBar.js b/js/ext/angular/src/directive/ionicTabBar.js index 6c1b285842..1b8dfbd59c 100644 --- a/js/ext/angular/src/directive/ionicTabBar.js +++ b/js/ext/angular/src/directive/ionicTabBar.js @@ -6,6 +6,14 @@ angular.module('ionic.ui.tabs', ['ngAnimate']) angular.extend(this, ionic.controllers.TabBarController.prototype); ionic.controllers.TabBarController.call(this, { + controllerChanged: function(oldC, oldI, newC, newI) { + $scope.controllerChanged && $scope.controllerChanged({ + oldController: oldC, + oldIndex: oldI, + newController: newC, + newIndex: newI + }); + }, tabBar: { tryTabSelect: function() {}, setSelectedItem: function(index) {}, @@ -39,7 +47,8 @@ angular.module('ionic.ui.tabs', ['ngAnimate']) restrict: 'E', replace: true, scope: { - animation: '@' + animation: '@', + controllerChanged: '&' }, transclude: true, controller: 'TabsCtrl', diff --git a/js/ext/angular/test/directive/ionicTabBar.unit.js b/js/ext/angular/test/directive/ionicTabBar.unit.js index 02dcb91ce8..9834db4136 100644 --- a/js/ext/angular/test/directive/ionicTabBar.unit.js +++ b/js/ext/angular/test/directive/ionicTabBar.unit.js @@ -36,6 +36,28 @@ describe('Tab Bar Controller', function() { expect(ctrl.getSelectedControllerIndex()).toEqual(1); }); + + it('Calls change callback', function() { + scope.onControllerChanged = function(oldC, oldI, newC, newI) { + }; + + // Add a controller + ctrl.add({ + title: 'Cats', + icon: 'icon-kitty-kat' + }); + ctrl.add({ + title: 'Dogs', + icon: 'icon-rufus' + }); + + spyOn(ctrl, 'controllerChanged'); + + expect(ctrl.getSelectedControllerIndex()).toEqual(0); + ctrl.select(1); + + expect(ctrl.controllerChanged).toHaveBeenCalled(); + }); }); describe('Tab Bar directive', function() { diff --git a/js/ext/angular/test/tabs.html b/js/ext/angular/test/tabs.html index 2e6b3901f2..b25239cf96 100644 --- a/js/ext/angular/test/tabs.html +++ b/js/ext/angular/test/tabs.html @@ -47,8 +47,8 @@ } - - + +
@@ -111,6 +111,14 @@ @@ -88,7 +89,7 @@ el: tab2 }); c2.title = 'Dogs'; - c2.icon = 'icon-gear'; + c2.icon = 'icon-gear-a'; var c3 = new controller({ el: tab3 });