Skip to content

Commit 7cbfcc1

Browse files
author
Adam Bradley
committed
view-state fixes
1 parent 7840c1b commit 7cbfcc1

File tree

4 files changed

+118
-50
lines changed

4 files changed

+118
-50
lines changed

dist/js/ionic-angular.js

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -620,19 +620,21 @@ angular.module('ionic.service.view', ['ui.router'])
620620
return null;
621621
};
622622
View.prototype.go = function(opts) {
623-
if(this.url && this.url !== $location.url() && (!opts || opts.enableUrlChange !== false)) {
623+
624+
if(this.stateName) {
625+
return $state.go(this.stateName, this.stateParams);
626+
}
627+
628+
if(this.url && this.url !== $location.url()) {
624629

625630
if($rootScope.$viewHistory.backView === this) {
626631
return $window.history.go(-1);
627632
} else if($rootScope.$viewHistory.forwardView === this) {
628633
return $window.history.go(1);
629634
}
630635

631-
return $location.url(this.url);
632-
}
633-
634-
if(this.stateName) {
635-
return $state.go(this.stateName, this.stateParams);
636+
$location.url(this.url);
637+
return;
636638
}
637639

638640
return null;
@@ -696,6 +698,13 @@ angular.module('ionic.service.view', ['ui.router'])
696698
rsp.historyId = forwardView.historyId;
697699
}
698700

701+
} else if(currentView && currentView.historyId !== hist.historyId &&
702+
hist.cursor > -1 && hist.stack.length > 0 && hist.cursor < hist.stack.length &&
703+
hist.stack[hist.cursor].stateId === currentStateId) {
704+
// they just changed to a different history and the history already has views in it
705+
rsp.viewId = hist.stack[hist.cursor].viewId;
706+
rsp.navAction = 'moveBack';
707+
699708
} else {
700709

701710
// set a new unique viewId
@@ -754,6 +763,10 @@ angular.module('ionic.service.view', ['ui.router'])
754763

755764
hist.cursor = viewHistory.currentView.index;
756765

766+
$rootScope.$broadcast('$viewHistory.historyChange', {
767+
showBack: (viewHistory.backView && viewHistory.backView.historyId === viewHistory.currentView.historyId)
768+
});
769+
757770
return rsp;
758771
},
759772

@@ -2881,7 +2894,7 @@ angular.module('ionic.ui.viewState', ['ionic.service.view', 'ionic.service.gestu
28812894
},
28822895
template: '<header class="bar bar-header nav-bar">'+//' ng-class="{invisible: !navController.navBar.isVisible}">' +
28832896
'<div class="buttons"> ' +
2884-
'<button view-back class="button" ng-show="enableBackButton && showBackButton" ng-class="backButtonClass" ng-bind-html="backButtonLabel"></button>' +
2897+
'<button view-back class="button" ng-if="enableBackButton" ng-class="backButtonClass" ng-bind-html="backButtonLabel"></button>' +
28852898
'<button ng-click="button.tap($event)" ng-repeat="button in leftButtons" class="button no-animation {{button.type}}" ng-bind-html="button.content"></button>' +
28862899
'</div>' +
28872900
'<h1 class="title" ng-bind="currentTitle"></h1>' +
@@ -2898,21 +2911,6 @@ angular.module('ionic.ui.viewState', ['ionic.service.view', 'ionic.service.gestu
28982911
$scope.backButtonClass += ' icon ' + $attr.backButtonIcon;
28992912
}
29002913

2901-
// Listen for changes in the stack cursor position to indicate whether a back
2902-
// button should be shown (this can still be disabled by the $scope.enableBackButton
2903-
$rootScope.$watch('$viewHistory.backView', function(backView) {
2904-
if(backView) {
2905-
var currentView = ViewService.getCurrentView();
2906-
if(currentView) {
2907-
if(backView.historyId === currentView.historyId) {
2908-
$scope.showBackButton = true;
2909-
return;
2910-
}
2911-
}
2912-
}
2913-
$scope.showBackButton = false;
2914-
});
2915-
29162914
// Store a reference to our nav controller
29172915
$scope.navController = navCtrl;
29182916

@@ -3025,7 +3023,7 @@ angular.module('ionic.ui.viewState', ['ionic.service.view', 'ionic.service.gestu
30253023
}])
30263024

30273025

3028-
.directive('viewBack', ['ViewService', function(ViewService) {
3026+
.directive('viewBack', ['ViewService', '$rootScope', function(ViewService, $rootScope) {
30293027
var goBack = function(e) {
30303028
var backView = ViewService.getBackView();
30313029
backView && backView.go();
@@ -3035,9 +3033,23 @@ angular.module('ionic.ui.viewState', ['ionic.service.view', 'ionic.service.gestu
30353033

30363034
return {
30373035
restrict: 'AC',
3038-
link: function($scope, $element) {
3039-
$element.bind('click', goBack);
3036+
compile: function(tElement) {
3037+
tElement.addClass('hide');
3038+
3039+
return function link($scope, $element) {
3040+
$element.bind('click', goBack);
3041+
3042+
$rootScope.$on('$viewHistory.historyChange', function(e, data) {
3043+
if(data.showBack) {
3044+
$element[0].classList.remove('hide');
3045+
} else {
3046+
$element[0].classList.add('hide');
3047+
}
3048+
});
3049+
3050+
}
30403051
}
3052+
30413053
};
30423054
}])
30433055

js/ext/angular/src/directive/ionicViewState.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ angular.module('ionic.ui.viewState', ['ionic.service.view', 'ionic.service.gestu
6969
},
7070
template: '<header class="bar bar-header nav-bar">'+//' ng-class="{invisible: !navController.navBar.isVisible}">' +
7171
'<div class="buttons"> ' +
72-
'<button view-back class="button" ng-show="enableBackButton && showBackButton" ng-class="backButtonClass" ng-bind-html="backButtonLabel"></button>' +
72+
'<button view-back class="button" ng-if="enableBackButton" ng-class="backButtonClass" ng-bind-html="backButtonLabel"></button>' +
7373
'<button ng-click="button.tap($event)" ng-repeat="button in leftButtons" class="button no-animation {{button.type}}" ng-bind-html="button.content"></button>' +
7474
'</div>' +
7575
'<h1 class="title" ng-bind="currentTitle"></h1>' +
@@ -86,21 +86,6 @@ angular.module('ionic.ui.viewState', ['ionic.service.view', 'ionic.service.gestu
8686
$scope.backButtonClass += ' icon ' + $attr.backButtonIcon;
8787
}
8888

89-
// Listen for changes in the stack cursor position to indicate whether a back
90-
// button should be shown (this can still be disabled by the $scope.enableBackButton
91-
$rootScope.$watch('$viewHistory.backView', function(backView) {
92-
if(backView) {
93-
var currentView = ViewService.getCurrentView();
94-
if(currentView) {
95-
if(backView.historyId === currentView.historyId) {
96-
$scope.showBackButton = true;
97-
return;
98-
}
99-
}
100-
}
101-
$scope.showBackButton = false;
102-
});
103-
10489
// Store a reference to our nav controller
10590
$scope.navController = navCtrl;
10691

@@ -213,7 +198,7 @@ angular.module('ionic.ui.viewState', ['ionic.service.view', 'ionic.service.gestu
213198
}])
214199

215200

216-
.directive('viewBack', ['ViewService', function(ViewService) {
201+
.directive('viewBack', ['ViewService', '$rootScope', function(ViewService, $rootScope) {
217202
var goBack = function(e) {
218203
var backView = ViewService.getBackView();
219204
backView && backView.go();
@@ -223,9 +208,23 @@ angular.module('ionic.ui.viewState', ['ionic.service.view', 'ionic.service.gestu
223208

224209
return {
225210
restrict: 'AC',
226-
link: function($scope, $element) {
227-
$element.bind('click', goBack);
211+
compile: function(tElement) {
212+
tElement.addClass('hide');
213+
214+
return function link($scope, $element) {
215+
$element.bind('click', goBack);
216+
217+
$rootScope.$on('$viewHistory.historyChange', function(e, data) {
218+
if(data.showBack) {
219+
$element[0].classList.remove('hide');
220+
} else {
221+
$element[0].classList.add('hide');
222+
}
223+
});
224+
225+
}
228226
}
227+
229228
};
230229
}])
231230

js/ext/angular/src/service/ionicView.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,21 @@ angular.module('ionic.service.view', ['ui.router'])
6363
return null;
6464
};
6565
View.prototype.go = function(opts) {
66-
if(this.url && this.url !== $location.url() && (!opts || opts.enableUrlChange !== false)) {
66+
67+
if(this.stateName) {
68+
return $state.go(this.stateName, this.stateParams);
69+
}
70+
71+
if(this.url && this.url !== $location.url()) {
6772

6873
if($rootScope.$viewHistory.backView === this) {
6974
return $window.history.go(-1);
7075
} else if($rootScope.$viewHistory.forwardView === this) {
7176
return $window.history.go(1);
7277
}
7378

74-
return $location.url(this.url);
75-
}
76-
77-
if(this.stateName) {
78-
return $state.go(this.stateName, this.stateParams);
79+
$location.url(this.url);
80+
return;
7981
}
8082

8183
return null;
@@ -139,6 +141,13 @@ angular.module('ionic.service.view', ['ui.router'])
139141
rsp.historyId = forwardView.historyId;
140142
}
141143

144+
} else if(currentView && currentView.historyId !== hist.historyId &&
145+
hist.cursor > -1 && hist.stack.length > 0 && hist.cursor < hist.stack.length &&
146+
hist.stack[hist.cursor].stateId === currentStateId) {
147+
// they just changed to a different history and the history already has views in it
148+
rsp.viewId = hist.stack[hist.cursor].viewId;
149+
rsp.navAction = 'moveBack';
150+
142151
} else {
143152

144153
// set a new unique viewId
@@ -197,6 +206,10 @@ angular.module('ionic.service.view', ['ui.router'])
197206

198207
hist.cursor = viewHistory.currentView.index;
199208

209+
$rootScope.$broadcast('$viewHistory.historyChange', {
210+
showBack: (viewHistory.backView && viewHistory.backView.historyId === viewHistory.currentView.historyId)
211+
});
212+
200213
return rsp;
201214
},
202215

js/ext/angular/test/service/ionicView.unit.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ describe('Ionic View Service', function() {
190190
$state.go('home');
191191
rootScope.$apply();
192192
var homeReg = viewService.register(homeViewScope);
193+
expect(homeReg.navAction).toEqual('initialView');
193194
expect(viewService.getCurrentStateName()).toEqual('home');
194195
expect(viewService.getCurrentView().stateName).toEqual('home');
195196
expect(viewService.getBackView()).toEqual(null);
@@ -204,6 +205,7 @@ describe('Ionic View Service', function() {
204205
var currentView = viewService.getCurrentView();
205206
var backView = viewService.getBackView();
206207
var forwardView = viewService.getForwardView();
208+
expect(aboutReg.navAction).toEqual('newView');
207209
expect(currentView.viewId).toEqual(aboutReg.viewId);
208210
expect(currentView.backViewId).toEqual(homeReg.viewId);
209211
expect(currentView.forwardViewId).toEqual(null);
@@ -219,6 +221,7 @@ describe('Ionic View Service', function() {
219221
$state.go('tabs.tab1view1');
220222
rootScope.$apply();
221223
var tab1view1Reg = viewService.register(tab1view1Scope);
224+
expect(tab1view1Reg.navAction).toEqual('newView');
222225

223226
expect(rootScope.$viewHistory.histories[tab1Scope.$historyId].historyId).toEqual(tab1Scope.$historyId);
224227
expect(rootScope.$viewHistory.histories[tab1Scope.$historyId].stack[0].viewId).toEqual(tab1view1Reg.viewId);
@@ -241,6 +244,7 @@ describe('Ionic View Service', function() {
241244
$state.go('home');
242245
rootScope.$apply();
243246
var home2reg = viewService.register({});
247+
expect(home2reg.navAction).toEqual('newView');
244248
currentView = viewService.getCurrentView();
245249
backView = viewService.getBackView();
246250
forwardView = viewService.getForwardView();
@@ -633,6 +637,46 @@ describe('Ionic View Service', function() {
633637
expect(rootScope.$viewHistory.histories[tab1Scope.$historyId].cursor).toEqual(1);
634638
}));
635639

640+
it('should go one level in tab1, vist tab2 and tab3, come back to tab1 and still be at spot', inject(function($location, $state) {
641+
var tab1Container = {};
642+
var tab2Container = {};
643+
var tab3Container = {};
644+
viewService.registerHistory(tab1Container);
645+
viewService.registerHistory(tab2Container);
646+
viewService.registerHistory(tab3Container);
647+
648+
// register tab1, view1
649+
$state.go('tabs.tab1view1');
650+
rootScope.$apply();
651+
var tab1view1Reg = viewService.register(tab1Container);
652+
expect(rootScope.$viewHistory.histories[tab1Container.$historyId].cursor).toEqual(0);
653+
654+
// register tab1, view2
655+
$state.go('tabs.tab1view2');
656+
rootScope.$apply();
657+
var tab1view2Reg = viewService.register(tab1Container);
658+
expect(rootScope.$viewHistory.histories[tab1Container.$historyId].cursor).toEqual(1);
659+
currentView = viewService.getCurrentView();
660+
expect(currentView.backViewId).toEqual(tab1view1Reg.viewId);
661+
662+
// register tab2, view1
663+
$state.go('tabs.tab2view1');
664+
rootScope.$apply();
665+
var tab2view1Reg = viewService.register(tab2Container);
666+
667+
// register tab3, view1
668+
$state.go('tabs.tab3view1');
669+
rootScope.$apply();
670+
var tab3view1Reg = viewService.register(tab3Container);
671+
672+
// register tab 1, view 2 again
673+
$state.go('tabs.tab1view2');
674+
rootScope.$apply();
675+
var tab1view2Reg2 = viewService.register(tab1Container);
676+
expect(tab1view2Reg2.navAction).toEqual('moveBack');
677+
expect(tab1view2Reg2.viewId).toEqual(tab1view2Reg.viewId);
678+
}));
679+
636680
it('should init root viewHistory data', inject(function() {
637681
expect(rootScope.$viewHistory.backView).toEqual(null);
638682
expect(rootScope.$viewHistory.currentView).toEqual(null);

0 commit comments

Comments
 (0)