refactor($ionicHistory): add nextViewOptions

This commit is contained in:
Adam Bradley
2014-11-13 21:14:40 -06:00
parent 6b4cde3098
commit 08353f6703
3 changed files with 95 additions and 21 deletions

View File

@@ -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();
}
});

View File

@@ -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;
}

View File

@@ -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');
}));