@@ -66,7 +66,7 @@ angular.module('mm.core')
6666 } ;
6767
6868 this . $get = function ( $ionicLoading , $ionicPopup , $injector , $translate , $http , $log , $q , $mmLang , $mmFS , $timeout , $mmApp ,
69- $mmText , mmCoreWifiDownloadThreshold , mmCoreDownloadThreshold ) {
69+ $mmText , mmCoreWifiDownloadThreshold , mmCoreDownloadThreshold , $ionicScrollDelegate ) {
7070
7171 $log = $log . getInstance ( '$mmUtil' ) ;
7272
@@ -995,6 +995,50 @@ angular.module('mm.core')
995995 return div . html ( ) ;
996996 } ;
997997
998+ /**
999+ * Scroll to a certain element inside another element.
1000+ * This is done this way because using anchorScroll or $location.hash sticks the scroll to go upwards.
1001+ *
1002+ * @module mm.core
1003+ * @ngdoc method
1004+ * @name $mmUtil#scrollToElement
1005+ * @param {Object } container Element to search in.
1006+ * @param {String } [selector] Selector to find the element to scroll to. If not defined, scroll to the container.
1007+ * @param {Object } [scrollDelegate] Scroll delegate. If not defined, use $ionicScrollDelegate.
1008+ * @param {String } [scrollParentClass] Scroll Parent Class where to stop calculating the position. Default scroll-content.
1009+ * @return {Boolean } True if the element is found, false otherwise.
1010+ */
1011+ self . scrollToElement = function ( container , selector , scrollDelegate , scrollParentClass ) {
1012+ if ( ! scrollDelegate ) {
1013+ scrollDelegate = $ionicScrollDelegate ;
1014+ }
1015+
1016+ if ( ! scrollParentClass ) {
1017+ scrollParentClass = 'scroll-content' ;
1018+ }
1019+
1020+ var element = selector ? container . querySelector ( selector ) : container ,
1021+ positionTop = positionLeft = 0 ;
1022+
1023+ if ( ! element ) {
1024+ return false ;
1025+ }
1026+
1027+ while ( element ) {
1028+ positionLeft += ( element . offsetLeft - element . scrollLeft + element . clientLeft ) ;
1029+ positionTop += ( element . offsetTop - element . scrollTop + element . clientTop ) ;
1030+
1031+ element = element . offsetParent ;
1032+ // If scrolling element is reached, stop adding tops.
1033+ if ( angular . element ( element ) . hasClass ( scrollParentClass ) ) {
1034+ element = false ;
1035+ }
1036+ }
1037+
1038+ scrollDelegate . scrollTo ( positionLeft , positionTop ) ;
1039+ return true ;
1040+ } ;
1041+
9981042 return self ;
9991043 } ;
10001044} ) ;
0 commit comments