3
3
4
4
angular . module ( 'ionic.ui.service.scrollDelegate' , [ ] )
5
5
6
- . factory ( '$ionicScrollDelegate' , [ '$rootScope' , '$timeout' , '$q' , function ( $rootScope , $timeout , $q ) {
6
+ . factory ( '$ionicScrollDelegate' , [ '$rootScope' , '$timeout' , '$q' , '$anchorScroll' , '$location' , '$document' , function ( $rootScope , $timeout , $q , $anchorScroll , $location , $document ) {
7
7
return {
8
8
/**
9
9
* Trigger a scroll-to-top event on child scrollers.
@@ -17,6 +17,9 @@ angular.module('ionic.ui.service.scrollDelegate', [])
17
17
resize : function ( ) {
18
18
$rootScope . $broadcast ( 'scroll.resize' ) ;
19
19
} ,
20
+ anchorScroll : function ( ) {
21
+ $rootScope . $broadcast ( 'scroll.anchorScroll' ) ;
22
+ } ,
20
23
tapScrollToTop : function ( element ) {
21
24
var _this = this ;
22
25
@@ -47,29 +50,46 @@ angular.module('ionic.ui.service.scrollDelegate', [])
47
50
* $scope {Scope} the scope to register and listen for events
48
51
*/
49
52
register : function ( $scope , $element ) {
53
+ //Get scroll controller from parent
54
+ var scrollCtrl = $element . controller ( '$ionicScroll' ) ;
55
+ if ( ! scrollCtrl ) {
56
+ return ;
57
+ }
58
+ var scrollView = scrollCtrl . scrollView ;
59
+ var scrollEl = scrollCtrl . element ;
50
60
51
61
function scrollViewResize ( ) {
52
62
// Run the resize after this digest
53
63
return $timeout ( function ( ) {
54
- $scope . $parent . scrollView && $scope . $parent . scrollView . resize ( ) ;
64
+ scrollView . resize ( ) ;
55
65
} ) ;
56
66
}
57
67
58
68
$element . bind ( 'scroll' , function ( e ) {
59
- $scope . onScroll ( {
69
+ $scope . onScroll && $scope . onScroll ( {
60
70
event : e ,
61
71
scrollTop : e . detail ? e . detail . scrollTop : e . originalEvent ? e . originalEvent . detail . scrollTop : 0 ,
62
72
scrollLeft : e . detail ? e . detail . scrollLeft : e . originalEvent ? e . originalEvent . detail . scrollLeft : 0
63
73
} ) ;
64
74
} ) ;
65
75
66
- $scope . $parent . $on ( 'scroll.resize' , function ( e ) {
67
- scrollViewResize ( ) ;
68
- } ) ;
76
+ $scope . $parent . $on ( 'scroll.resize' , scrollViewResize ) ;
69
77
70
78
// Called to stop refreshing on the scroll view
71
79
$scope . $parent . $on ( 'scroll.refreshComplete' , function ( e ) {
72
- $scope . $parent . scrollView && $scope . $parent . scrollView . finishPullToRefresh ( ) ;
80
+ scrollView . finishPullToRefresh ( ) ;
81
+ } ) ;
82
+
83
+ $scope . $parent . $on ( 'scroll.anchorScroll' , function ( ) {
84
+ var hash = $location . hash ( ) ;
85
+ var elm ;
86
+ //If there are multiple with this id, go to first one
87
+ if ( hash && ( elm = $document . body . querySelectorAll ( '#' + hash ) [ 0 ] ) ) {
88
+ var scroll = ionic . DomUtil . getPositionInParent ( elm , scrollEl ) ;
89
+ scrollView . scrollTo ( scroll . left , scroll . top ) ;
90
+ } else {
91
+ scrollView . scrollTo ( 0 , 0 ) ;
92
+ }
73
93
} ) ;
74
94
75
95
/**
@@ -79,12 +99,12 @@ angular.module('ionic.ui.service.scrollDelegate', [])
79
99
*/
80
100
$scope . $parent . $on ( 'scroll.scrollTop' , function ( e , animate ) {
81
101
scrollViewResize ( ) . then ( function ( ) {
82
- $scope . $parent . scrollView && $scope . $parent . scrollView . scrollTo ( 0 , 0 , animate === false ? false : true ) ;
102
+ scrollView . scrollTo ( 0 , 0 , animate === false ? false : true ) ;
83
103
} ) ;
84
104
} ) ;
85
105
$scope . $parent . $on ( 'scroll.scrollBottom' , function ( e , animate ) {
86
106
scrollViewResize ( ) . then ( function ( ) {
87
- var sv = $scope . $parent . scrollView ;
107
+ var sv = scrollView ;
88
108
if ( sv ) {
89
109
var max = sv . getScrollMax ( ) ;
90
110
sv . scrollTo ( 0 , max . top , animate === false ? false : true ) ;
0 commit comments