1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
/*!
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
/**
* See https://github.com/piwik/piwik/issues/4795 "linking to #hash tag does not work after merging AngularJS"
*/
(function () {
function scrollToAnchorNode($node)
{
$.scrollTo($node, 20);
}
function preventDefaultIfEventExists(event)
{
if (event) {
event.preventDefault();
}
}
function scrollToAnchorIfPossible(hash, event)
{
if (!hash) {
return;
}
if (-1 !== hash.indexOf('&')) {
return;
}
try {
var $node = $('#' + hash);
} catch (err) {
// on jquery syntax error, ignore so nothing is logged to the console
return;
}
if ($node && $node.length) {
scrollToAnchorNode($node);
preventDefaultIfEventExists(event);
return;
}
$node = $('a[name='+ hash + ']');
if ($node && $node.length) {
scrollToAnchorNode($node);
preventDefaultIfEventExists(event);
}
}
function isLinkWithinSamePage(location, newUrl)
{
if (location && location.origin && -1 === newUrl.indexOf(location.origin)) {
// link to different domain
return false;
}
if (location && location.pathname && -1 === newUrl.indexOf(location.pathname)) {
// link to different path
return false;
}
if (location && location.search && -1 === newUrl.indexOf(location.search)) {
// link with different search
return false;
}
return true;
}
function handleScrollToAnchorIfPresentOnPageLoad()
{
if (location.hash.substr(0, 2) == '#/') {
var hash = location.hash.substr(2);
scrollToAnchorIfPossible(hash, null);
}
}
function handleScrollToAnchorAfterPageLoad()
{
angular.module('piwikApp').run(['$rootScope', function ($rootScope) {
$rootScope.$on('$locationChangeStart', onLocationChangeStart);
function onLocationChangeStart (event, newUrl, oldUrl, $location) {
if (!newUrl) {
return;
}
var hashPos = newUrl.indexOf('#/');
if (-1 === hashPos) {
return;
}
if (!isLinkWithinSamePage(this.location, newUrl)) {
return;
}
var hash = newUrl.substr(hashPos + 2);
scrollToAnchorIfPossible(hash, event);
}
}]);
}
handleScrollToAnchorAfterPageLoad();
$(handleScrollToAnchorIfPresentOnPageLoad);
window.anchorLinkFix = {
scrollToAnchorInUrl: function () {
// may be called when page is only fully loaded after some additional requests
// timeout needed to ensure angular rendered fully
var $timeout = piwikHelper.getAngularDependency('$timeout');
$timeout(handleScrollToAnchorIfPresentOnPageLoad);
}
};
})();
|