Skip to content

Commit b1ac5a6

Browse files
committed
MOBILE-1513 navigation: Treat anchor links scrolling the current page
1 parent 5a57fdc commit b1ac5a6

File tree

2 files changed

+59
-6
lines changed

2 files changed

+59
-6
lines changed

www/core/directives/browser.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,28 @@ angular.module('mm.core')
2323
*
2424
* @param {Boolean} [captureLink=false] If the link needs to be captured by the app.
2525
*/
26-
.directive('mmBrowser', function($mmUtil, $mmContentLinksHelper) {
26+
.directive('mmBrowser', function($mmUtil, $mmContentLinksHelper, $location) {
2727

2828
/**
29-
* Convenience function to open file or url in the browser.
29+
* Convenience function to correctly navigate, open file or url in the browser.
3030
*
3131
* @param {String} href HREF to be opened
3232
*/
33-
function openInBrowser(href) {
33+
function navigate(href) {
3434
if (href.indexOf('cdvfile://') === 0 || href.indexOf('file://') === 0) {
3535
// We have a local file.
3636
$mmUtil.openFile(href).catch(function(error) {
3737
$mmUtil.showErrorModal(error);
3838
});
39+
} else if (href.charAt(0) == '#'){
40+
href = href.substr(1);
41+
// In site links
42+
if (href.charAt(0) == '/') {
43+
$location.url(href);
44+
} else {
45+
// Look for id or name
46+
$mmUtil.scrollToElement(document, "#" + href + ", [name='" + href + "']");
47+
}
3948
} else {
4049
// It's an external link, we will open with browser.
4150
$mmUtil.openInBrowser(href);
@@ -55,11 +64,11 @@ angular.module('mm.core')
5564
if (attrs.captureLink && attrs.captureLink !== 'false') {
5665
$mmContentLinksHelper.handleLink(href).then(function(treated) {
5766
if (!treated) {
58-
openInBrowser(href);
67+
navigate(href);
5968
}
6069
});
6170
} else {
62-
openInBrowser(href);
71+
navigate(href);
6372
}
6473
}
6574
});

www/core/lib/util.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)