From 08353f6703ce1f0b9e3fe5276e9244895b2bac40 Mon Sep 17 00:00:00 2001 From: Adam Bradley Date: Thu, 13 Nov 2014 21:14:40 -0600 Subject: [PATCH] refactor($ionicHistory): add nextViewOptions --- js/angular/directive/menuClose.js | 10 ++-- js/angular/service/history.js | 38 ++++++++----- test/unit/angular/service/history.unit.js | 68 ++++++++++++++++++++++- 3 files changed, 95 insertions(+), 21 deletions(-) diff --git a/js/angular/directive/menuClose.js b/js/angular/directive/menuClose.js index 81cfc6ae4a..e9b30bdbb8 100644 --- a/js/angular/directive/menuClose.js +++ b/js/angular/directive/menuClose.js @@ -24,17 +24,17 @@ * ``` */ IonicModule -.directive('menuClose', ['$ionicViewSwitcher', '$ionicHistory', function($ionicViewSwitcher, $ionicHistory) { +.directive('menuClose', ['$ionicHistory', function($ionicHistory) { return { restrict: 'AC', link: function($scope, $element, $attr) { $element.bind('click', function() { var sideMenuCtrl = $element.inheritedData('$ionSideMenusController'); if (sideMenuCtrl) { - // lower priority than navTransition which allows navTransition - // to override this directive's nextTransition() call - $ionicViewSwitcher.nextTransition('none'); - $ionicHistory.resetHistory(); + $ionicHistory.nextViewOptions({ + historyRoot: true, + disableAnimate: true + }); sideMenuCtrl.close(); } }); diff --git a/js/angular/service/history.js b/js/angular/service/history.js index 4a1023508c..e3036d7730 100644 --- a/js/angular/service/history.js +++ b/js/angular/service/history.js @@ -29,7 +29,7 @@ function($rootScope, $state, $location, $window) { var DIRECTION_NONE = 'none'; var stateChangeCounter = 0; - var lastStateId, setNextAsHistoryRoot; + var lastStateId, nextViewOptions; var viewHistory = { histories: { root: { historyId: 'root', parentHistoryId: null, stack: [], cursor: -1 } }, @@ -347,17 +347,21 @@ function($rootScope, $state, $location, $window) { hist.stack.push(viewHistory.views[viewId]); } - if (setNextAsHistoryRoot) { - for (x = 0; x < hist.stack.length; x++) { - if (hist.stack[x].viewId === viewId) { - hist.stack[x].index = 0; - hist.stack[x].backViewId = hist.stack[x].forwardViewId = null; - } else { - delete viewHistory.views[hist.stack[x].viewId]; + if (nextViewOptions) { + if (nextViewOptions.disableAnimate) direction = DIRECTION_NONE; + if (nextViewOptions.disableBack) viewHistory.views[viewId].backViewId = null; + if (nextViewOptions.historyRoot) { + for (x = 0; x < hist.stack.length; x++) { + if (hist.stack[x].viewId === viewId) { + hist.stack[x].index = 0; + hist.stack[x].backViewId = hist.stack[x].forwardViewId = null; + } else { + delete viewHistory.views[hist.stack[x].viewId]; + } } + hist.stack = [viewHistory.views[viewId]]; } - hist.stack = [viewHistory.views[viewId]]; - setNextAsHistoryRoot = false; + nextViewOptions = null; } setNavViews(viewId); @@ -481,8 +485,16 @@ function($rootScope, $state, $location, $window) { } }, - resetHistory: function() { - setNextAsHistoryRoot = true; + nextViewOptions: function(opts) { + if (arguments.length) { + if (opts === null) { + nextViewOptions = opts; + } else { + nextViewOptions = nextViewOptions || {}; + extend(nextViewOptions, opts); + } + } + return nextViewOptions; } }; @@ -539,7 +551,7 @@ function($rootScope, $state, $location, $document, $ionicPlatform, $ionicHistory }; // Set the document title when a new view is shown - $rootScope.$on('viewState.viewEnter', function(e, data) { + $rootScope.$on('$ionicView.afterEnter', function(ev, data) { if (data && data.title) { $document[0].title = data.title; } diff --git a/test/unit/angular/service/history.unit.js b/test/unit/angular/service/history.unit.js index 0f14833db0..7ff3ab7386 100644 --- a/test/unit/angular/service/history.unit.js +++ b/test/unit/angular/service/history.unit.js @@ -971,6 +971,68 @@ describe('Ionic History', function() { expect(homeReg.direction).toEqual('exit'); })); + it('should set nextViewOptions disableAnimate', inject(function($state) { + $state.go('home'); + rootScope.$apply(); + var homeReg = ionicHistory.register({}, false); + expect(homeReg.direction).toEqual('none'); + + ionicHistory.nextViewOptions({ disableAnimate: true }); + + $state.go('info'); + rootScope.$apply(); + var infoReg = ionicHistory.register({}, false); + expect(infoReg.direction).toEqual('none'); + expect(infoReg.showBack).toEqual(true); + expect(ionicHistory.viewHistory().histories[infoReg.historyId].stack.length).toEqual(2); + expect(ionicHistory.viewHistory().backView.viewId).toBe(homeReg.viewId); + })); + + it('should set nextViewOptions disableBack', inject(function($state) { + $state.go('home'); + rootScope.$apply(); + var homeReg = ionicHistory.register({}, false); + expect(homeReg.direction).toEqual('none'); + + ionicHistory.nextViewOptions({ disableBack: true }); + + $state.go('info'); + rootScope.$apply(); + var infoReg = ionicHistory.register({}, false); + expect(infoReg.showBack).toEqual(false); + expect(infoReg.direction).toEqual('forward'); + expect(ionicHistory.viewHistory().histories[infoReg.historyId].stack.length).toEqual(2); + expect(ionicHistory.viewHistory().backView).toBe(null); + })); + + it('should set nextViewOptions historyRoot', inject(function($state) { + $state.go('home'); + rootScope.$apply(); + var homeReg = ionicHistory.register({}, false); + expect(homeReg.direction).toEqual('none'); + + ionicHistory.nextViewOptions({ historyRoot: true }); + + $state.go('info'); + rootScope.$apply(); + var infoReg = ionicHistory.register({}, false); + expect(infoReg.showBack).toEqual(false); + expect(infoReg.direction).toEqual('forward'); + expect(ionicHistory.viewHistory().histories[infoReg.historyId].stack.length).toEqual(1); + expect(ionicHistory.viewHistory().backView).toBe(null); + })); + + it('should set and overwrite nextViewOptions', inject(function($state) { + expect( ionicHistory.nextViewOptions() ).toBeUndefined(); + expect( ionicHistory.nextViewOptions({}) ).toEqual({}); + ionicHistory.nextViewOptions(null); + expect( ionicHistory.nextViewOptions({ historyRoot: true }) ).toEqual({ historyRoot: true }); + expect( ionicHistory.nextViewOptions({ disableBack: true }) ).toEqual({ historyRoot: true, disableBack: true }); + expect( ionicHistory.nextViewOptions({ historyRoot: false }) ).toEqual({ historyRoot: false, disableBack: true }); + ionicHistory.nextViewOptions(null); + expect( ionicHistory.nextViewOptions({}) ).toEqual({}); + })); + it('should be an abstract view', inject(function($document) { var reg = ionicHistory.register({}, false); expect(reg.action).not.toEqual('abstractView'); @@ -1058,13 +1120,13 @@ describe('Ionic History', function() { it('should update document title', inject(function($document) { $document[0].title = 'Original Title'; - rootScope.$broadcast("viewState.viewEnter"); + rootScope.$broadcast("$ionicView.afterEnter"); expect($document[0].title).toEqual('Original Title'); - rootScope.$broadcast("viewState.viewEnter", {}); + rootScope.$broadcast("$ionicView.afterEnter", {}); expect($document[0].title).toEqual('Original Title'); - rootScope.$broadcast("viewState.viewEnter", { title: 'New Title' }); + rootScope.$broadcast("$ionicView.afterEnter", { title: 'New Title' }); expect($document[0].title).toEqual('New Title'); }));