From 1eee8d2ef7dc0f1ebde7501a286fd0a62d21a367 Mon Sep 17 00:00:00 2001 From: kiswa Date: Sat, 25 Oct 2014 14:36:14 -0400 Subject: [PATCH] Missed the directive file. --- js/directives/keepScrollPos.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 js/directives/keepScrollPos.js diff --git a/js/directives/keepScrollPos.js b/js/directives/keepScrollPos.js new file mode 100644 index 0000000..c64fc6a --- /dev/null +++ b/js/directives/keepScrollPos.js @@ -0,0 +1,28 @@ +// Source: http://stackoverflow.com/a/25073496/1110183 +taskBoardDirectives.directive("keepScrollPos", +['$route', '$window', '$timeout', '$location', '$anchorScroll', +function($route, $window, $timeout, $location, $anchorScroll) { + // Cache scroll position of each route's templateUrl. + var scrollPosCache = {}; + + // Compile function + return function(scope, element, attrs) { + scope.$on('$routeChangeStart', function() { + if ($route.current) { + scrollPosCache[$route.current.loadedTemplateUrl] = [$window.pageXOffset, $window.pageYOffset]; + } + }); + + scope.$on('$routeChangeSuccess', function() { + // If hash is specified explicitly, it trumps previously stored scroll position. + if ($location.hash()) { + $anchorScroll(); + } else { + var prevScrollPos = scrollPosCache[$route.current.loadedTemplateUrl] || [0, 0]; + $timeout(function() { + $window.scrollTo(prevScrollPos[0], prevScrollPos[1]); + }, 0); + } + }); + } +}]);