From 251025e512cf05298b5bd92fbfbc2c5ad78f3220 Mon Sep 17 00:00:00 2001 From: Adam Bradley Date: Sat, 11 Jan 2014 00:43:41 -0600 Subject: [PATCH] clear view history, closes #393 --- dist/js/ionic-angular.js | 41 ++++++++++++++++--- js/ext/angular/src/service/ionicView.js | 41 ++++++++++++++++--- js/ext/angular/test/service/ionicView.unit.js | 12 ++++++ js/ext/angular/test/viewState.html | 10 ++++- 4 files changed, 92 insertions(+), 12 deletions(-) diff --git a/dist/js/ionic-angular.js b/dist/js/ionic-angular.js index 9c9b195603..3c109feac1 100644 --- a/dist/js/ionic-angular.js +++ b/dist/js/ionic-angular.js @@ -756,17 +756,23 @@ angular.module('ionic.service.view', ['ui.router']) hist.stack.push(viewHistory.histories[rsp.viewId]); } - viewHistory.currentView = this._getView(rsp.viewId); - viewHistory.backView = this._getBackView(viewHistory.currentView); - viewHistory.forwardView = this._getForwardView(viewHistory.currentView); + this.setNavViews(rsp.viewId); hist.cursor = viewHistory.currentView.index; + return rsp; + }, + + setNavViews: function(viewId) { + var viewHistory = $rootScope.$viewHistory; + + viewHistory.currentView = this._getView(viewId); + viewHistory.backView = this._getBackView(viewHistory.currentView); + viewHistory.forwardView = this._getForwardView(viewHistory.currentView); + $rootScope.$broadcast('$viewHistory.historyChange', { showBack: (viewHistory.backView && viewHistory.backView.historyId === viewHistory.currentView.historyId) }); - - return rsp; }, registerHistory: function(scope) { @@ -937,6 +943,31 @@ angular.module('ionic.service.view', ['ui.router']) // make sure the reverse class isn't already added element[0].classList.remove('reverse'); } + }, + + clearHistory: function() { + var historyId, x, view, + histories = $rootScope.$viewHistory.histories, + currentView = $rootScope.$viewHistory.currentView; + + for(historyId in histories) { + + if(histories[historyId].stack) { + histories[historyId].stack = []; + histories[historyId].cursor = -1; + } + + if(currentView.historyId === historyId) { + currentView.backViewId = null; + currentView.forwardViewId = null; + histories[historyId].stack.push(currentView); + } else if(histories[historyId].destroy) { + histories[historyId].destroy(); + } + + } + + this.setNavViews(currentView.viewId); } }; diff --git a/js/ext/angular/src/service/ionicView.js b/js/ext/angular/src/service/ionicView.js index 7368bf8425..6872c38cc8 100644 --- a/js/ext/angular/src/service/ionicView.js +++ b/js/ext/angular/src/service/ionicView.js @@ -200,17 +200,23 @@ angular.module('ionic.service.view', ['ui.router']) hist.stack.push(viewHistory.histories[rsp.viewId]); } - viewHistory.currentView = this._getView(rsp.viewId); - viewHistory.backView = this._getBackView(viewHistory.currentView); - viewHistory.forwardView = this._getForwardView(viewHistory.currentView); + this.setNavViews(rsp.viewId); hist.cursor = viewHistory.currentView.index; + return rsp; + }, + + setNavViews: function(viewId) { + var viewHistory = $rootScope.$viewHistory; + + viewHistory.currentView = this._getView(viewId); + viewHistory.backView = this._getBackView(viewHistory.currentView); + viewHistory.forwardView = this._getForwardView(viewHistory.currentView); + $rootScope.$broadcast('$viewHistory.historyChange', { showBack: (viewHistory.backView && viewHistory.backView.historyId === viewHistory.currentView.historyId) }); - - return rsp; }, registerHistory: function(scope) { @@ -381,6 +387,31 @@ angular.module('ionic.service.view', ['ui.router']) // make sure the reverse class isn't already added element[0].classList.remove('reverse'); } + }, + + clearHistory: function() { + var historyId, x, view, + histories = $rootScope.$viewHistory.histories, + currentView = $rootScope.$viewHistory.currentView; + + for(historyId in histories) { + + if(histories[historyId].stack) { + histories[historyId].stack = []; + histories[historyId].cursor = -1; + } + + if(currentView.historyId === historyId) { + currentView.backViewId = null; + currentView.forwardViewId = null; + histories[historyId].stack.push(currentView); + } else if(histories[historyId].destroy) { + histories[historyId].destroy(); + } + + } + + this.setNavViews(currentView.viewId); } }; diff --git a/js/ext/angular/test/service/ionicView.unit.js b/js/ext/angular/test/service/ionicView.unit.js index 0242092757..6807ebc1d2 100644 --- a/js/ext/angular/test/service/ionicView.unit.js +++ b/js/ext/angular/test/service/ionicView.unit.js @@ -675,6 +675,18 @@ describe('Ionic View Service', function() { var tab1view2Reg2 = viewService.register(tab1Container); expect(tab1view2Reg2.navAction).toEqual('moveBack'); expect(tab1view2Reg2.viewId).toEqual(tab1view2Reg.viewId); + + + var currentViewId = tab1view2Reg.viewId; + expect(rootScope.$viewHistory.histories[tab1view2Reg.historyId].stack.length).toEqual(2); + backView = viewService.getBackView(); + expect(backView).toBeDefined(); + viewService.clearHistory(); + expect(rootScope.$viewHistory.histories[tab1view2Reg.historyId].stack.length).toEqual(1); + backView = viewService.getBackView(); + expect(backView).toEqual(null); + currentView = viewService.getCurrentView(); + expect(currentView.viewId).toEqual(currentViewId); })); it('should init root viewHistory data', inject(function() { diff --git a/js/ext/angular/test/viewState.html b/js/ext/angular/test/viewState.html index d5a85994dc..a1c2b06a0e 100644 --- a/js/ext/angular/test/viewState.html +++ b/js/ext/angular/test/viewState.html @@ -62,6 +62,9 @@

Seriously!? Again?!

How about I just give you a password, hows this:
{{password}}

+

+ +

Sign-In - Forgot password - @@ -303,9 +306,12 @@ }; }) - .controller('ForgotPasswordCtrl', function($scope, $state) { - //console.log('ForgotPasswordCtrl'); + .controller('ForgotPasswordCtrl', function(ViewService, $scope, $state) { $scope.password = Math.round(Math.random() * 10000); + + $scope.clearViewHistory = function() { + ViewService.clearHistory(); + }; }) .controller('AutoListCtrl', function($scope, $state) {