Matteo Scandolo | 7cd88ba | 2015-12-16 14:23:08 -0800 | [diff] [blame] | 1 | /** |
| 2 | * @ngdoc object |
| 3 | * @name ui.router.state.$uiViewScrollProvider |
| 4 | * |
| 5 | * @description |
| 6 | * Provider that returns the {@link ui.router.state.$uiViewScroll} service function. |
| 7 | */ |
| 8 | function $ViewScrollProvider() { |
| 9 | |
| 10 | var useAnchorScroll = false; |
| 11 | |
| 12 | /** |
| 13 | * @ngdoc function |
| 14 | * @name ui.router.state.$uiViewScrollProvider#useAnchorScroll |
| 15 | * @methodOf ui.router.state.$uiViewScrollProvider |
| 16 | * |
| 17 | * @description |
| 18 | * Reverts back to using the core [`$anchorScroll`](http://docs.angularjs.org/api/ng.$anchorScroll) service for |
| 19 | * scrolling based on the url anchor. |
| 20 | */ |
| 21 | this.useAnchorScroll = function () { |
| 22 | useAnchorScroll = true; |
| 23 | }; |
| 24 | |
| 25 | /** |
| 26 | * @ngdoc object |
| 27 | * @name ui.router.state.$uiViewScroll |
| 28 | * |
| 29 | * @requires $anchorScroll |
| 30 | * @requires $timeout |
| 31 | * |
| 32 | * @description |
| 33 | * When called with a jqLite element, it scrolls the element into view (after a |
| 34 | * `$timeout` so the DOM has time to refresh). |
| 35 | * |
| 36 | * If you prefer to rely on `$anchorScroll` to scroll the view to the anchor, |
| 37 | * this can be enabled by calling {@link ui.router.state.$uiViewScrollProvider#methods_useAnchorScroll `$uiViewScrollProvider.useAnchorScroll()`}. |
| 38 | */ |
| 39 | this.$get = ['$anchorScroll', '$timeout', function ($anchorScroll, $timeout) { |
| 40 | if (useAnchorScroll) { |
| 41 | return $anchorScroll; |
| 42 | } |
| 43 | |
| 44 | return function ($element) { |
| 45 | return $timeout(function () { |
| 46 | $element[0].scrollIntoView(); |
| 47 | }, 0, false); |
| 48 | }; |
| 49 | }]; |
| 50 | } |
| 51 | |
| 52 | angular.module('ui.router.state').provider('$uiViewScroll', $ViewScrollProvider); |