From cc0a4ef775c1fde3ca3591fcf42f1fc5a23213d8 Mon Sep 17 00:00:00 2001 From: Andy Joslin Date: Mon, 17 Mar 2014 07:48:52 -0600 Subject: [PATCH] test(sideMenu2): update test to reflect changes --- .../src/controller/ionicScrollController.js | 12 ++++++---- js/ext/angular/src/directive/ionicContent.js | 17 ++------------ .../service/delegates/ionicScrollDelegate.js | 23 ++++++++++++++++++- js/ext/angular/src/service/ionicView.js | 2 +- js/ext/angular/test/sideMenu2.html | 4 ++-- 5 files changed, 35 insertions(+), 23 deletions(-) diff --git a/js/ext/angular/src/controller/ionicScrollController.js b/js/ext/angular/src/controller/ionicScrollController.js index 23a858603c..1318628f92 100644 --- a/js/ext/angular/src/controller/ionicScrollController.js +++ b/js/ext/angular/src/controller/ionicScrollController.js @@ -6,7 +6,7 @@ angular.module('ionic.ui.scroll') /** * @private */ -.controller('$ionicScroll', ['$scope', 'scrollViewOptions', '$timeout', '$ionicScrollDelegate', '$window', function($scope, scrollViewOptions, $timeout, $ionicScrollDelegate, $window) { +.controller('$ionicScroll', ['$scope', 'scrollViewOptions', '$timeout', '$ionicScrollDelegate', '$window', '$ionicViewService', function($scope, scrollViewOptions, $timeout, $ionicScrollDelegate, $window, $ionicViewService) { var self = this; @@ -31,13 +31,17 @@ angular.module('ionic.ui.scroll') //Register delegate for event handling $ionicScrollDelegate.register($scope, $element, scrollView); + var resize = angular.bind(scrollView, scrollView.resize); $window.addEventListener('resize', resize); + $scope.$on('$destroy', function() { $window.removeEventListener('resize', resize); + + var view = $ionicViewService.getCurrentView(); + if (view) { + view.rememberedScrollValues = scrollView.getValues(); + } }); - function resize() { - scrollView.resize(); - } this.setRefresher = function(refresherScope, refresherElement) { var refresher = this.refresher = refresherElement; diff --git a/js/ext/angular/src/directive/ionicContent.js b/js/ext/angular/src/directive/ionicContent.js index 30f12bf22d..2b017ebfe4 100644 --- a/js/ext/angular/src/directive/ionicContent.js +++ b/js/ext/angular/src/directive/ionicContent.js @@ -136,21 +136,8 @@ function($parse, $timeout, $ionicScrollDelegate, $controller, $ionicBind) { //Publish scrollView to parent so children can access it scrollView = $scope.$parent.scrollView = scrollCtrl.scrollView; - $scope.$on('$viewContentLoaded', function(e, viewHistoryData) { - viewHistoryData || (viewHistoryData = {}); - var scroll = viewHistoryData.scrollValues; - if (scroll) { - $timeout(function() { - scrollView.scrollTo(+scroll.left || null, +scroll.top || null); - }, 0); - } - - //Save scroll onto viewHistoryData when scope is destroyed - $scope.$on('$destroy', function() { - viewHistoryData.scrollValues = scrollView.getValues(); - }); - }); - + var delegate = $ionicScrollDelegate($scope); + delegate.rememberScrollPosition(); } transclude($scope, function(clone) { diff --git a/js/ext/angular/src/service/delegates/ionicScrollDelegate.js b/js/ext/angular/src/service/delegates/ionicScrollDelegate.js index 1f58595bf3..7d2cd8747c 100644 --- a/js/ext/angular/src/service/delegates/ionicScrollDelegate.js +++ b/js/ext/angular/src/service/delegates/ionicScrollDelegate.js @@ -34,7 +34,7 @@ angular.module('ionic.ui.service.scrollDelegate', []) * } * ``` */ -.factory('$ionicScrollDelegate', ['$rootScope', '$timeout', '$location', function($rootScope, $timeout, $location) { +.factory('$ionicScrollDelegate', ['$rootScope', '$timeout', '$location', '$ionicViewService', function($rootScope, $timeout, $location, $ionicViewService) { function getScrollCtrl($scope) { var ctrl; @@ -131,6 +131,18 @@ angular.module('ionic.ui.service.scrollDelegate', []) }, element[0]); }, + /** + * @ngdoc method + * @name $ionicScrollDelegate#rememberScrollPosition + * @description Used on an instance of $ionicScrollDelegate. + * + * If this scroll area is associated with a view in the history, + * load the last scroll position from the last time this view was shown. + */ + rememberScrollPosition: function(animate) { + scrollScope.$broadcast('scroll.rememberPosition', !!animate); + }, + /** * @private * Attempt to get the current scroll view in scope (if any) @@ -204,6 +216,15 @@ angular.module('ionic.ui.service.scrollDelegate', []) } }); }); + $scope.$on('scroll.rememberPosition', function(e, animate) { + scrollViewResize().then(function() { + var view = $ionicViewService.getCurrentView(); + var values = view && view.rememberedScrollValues; + if (view && values) { + scrollView.scrollTo(+values.left || null, +values.top || null, animate); + } + }); + }); }; return ionicScrollDelegate; diff --git a/js/ext/angular/src/service/ionicView.js b/js/ext/angular/src/service/ionicView.js index f43556e421..a5d0fb7de0 100644 --- a/js/ext/angular/src/service/ionicView.js +++ b/js/ext/angular/src/service/ionicView.js @@ -231,7 +231,7 @@ angular.module('ionic.service.view', ['ui.router', 'ionic.service.platform']) stateName: this.getCurrentStateName(), stateParams: this.getCurrentStateParams(), url: $location.url(), - scrollValues: null + rememberedScrollValues: null }); // add the new view to this history's stack diff --git a/js/ext/angular/test/sideMenu2.html b/js/ext/angular/test/sideMenu2.html index fd84d4be88..d05ef55e0e 100644 --- a/js/ext/angular/test/sideMenu2.html +++ b/js/ext/angular/test/sideMenu2.html @@ -190,7 +190,7 @@ $urlRouterProvider.otherwise("/event/home"); }) - .controller('MainCtrl', function($scope, $ionicSideMenuDelegate) { + .controller('MainCtrl', function($scope) { console.log('MainCtrl'); $scope.data = { @@ -198,7 +198,7 @@ }; $scope.closeMenu = function() { - $ionicSideMenuDelegate.close($scope); + $scope.sideMenuController.toggleLeft(false); }; setTimeout(function(){