From 37de7c86f0c1ef49af8997ee7822bdb0353cf5e9 Mon Sep 17 00:00:00 2001
From: Thomas Steur <thomas.steur@googlemail.com>
Date: Wed, 12 Mar 2014 22:32:13 +0100
Subject: [PATCH] fix #4796 anchor links do not work after merging AngularJS

---
 plugins/CoreHome/CoreHome.php               |   1 +
 plugins/CoreHome/angularjs/anchorLinkFix.js | 108 ++++++++++++++++++++
 plugins/CoreHome/angularjs/piwikApp.js      |   9 +-
 3 files changed, 117 insertions(+), 1 deletion(-)
 create mode 100644 plugins/CoreHome/angularjs/anchorLinkFix.js

diff --git a/plugins/CoreHome/CoreHome.php b/plugins/CoreHome/CoreHome.php
index 7128ddc677..5e0fa821ef 100644
--- a/plugins/CoreHome/CoreHome.php
+++ b/plugins/CoreHome/CoreHome.php
@@ -111,6 +111,7 @@ class CoreHome extends \Piwik\Plugin
         $jsFiles[] = "plugins/CoreHome/angularjs/common/directives/focusif.js";
 
         $jsFiles[] = "plugins/CoreHome/angularjs/piwikApp.js";
+        $jsFiles[] = "plugins/CoreHome/angularjs/anchorLinkFix.js";
 
         $jsFiles[] = "plugins/CoreHome/angularjs/siteselector/siteselector-model.js";
         $jsFiles[] = "plugins/CoreHome/angularjs/siteselector/siteselector-controller.js";
diff --git a/plugins/CoreHome/angularjs/anchorLinkFix.js b/plugins/CoreHome/angularjs/anchorLinkFix.js
new file mode 100644
index 0000000000..5abe2f3a22
--- /dev/null
+++ b/plugins/CoreHome/angularjs/anchorLinkFix.js
@@ -0,0 +1,108 @@
+/*!
+ * Piwik - Web Analytics
+ *
+ * @link http://piwik.org
+ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
+ */
+
+/**
+ * See http://dev.piwik.org/trac/ticket/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;
+        }
+
+        var $node = $('#' + hash);
+
+        if ($node && $node.length) {
+            scrollToAnchorNode($node);
+            preventDefaultIfEventExists(event);
+            return;
+        }
+
+        var $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', function (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);
+
+})();
\ No newline at end of file
diff --git a/plugins/CoreHome/angularjs/piwikApp.js b/plugins/CoreHome/angularjs/piwikApp.js
index 87079ecb36..a678168f3e 100644
--- a/plugins/CoreHome/angularjs/piwikApp.js
+++ b/plugins/CoreHome/angularjs/piwikApp.js
@@ -1,3 +1,10 @@
+/*!
+ * Piwik - Web Analytics
+ *
+ * @link http://piwik.org
+ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
+ */
+
 angular.module('piwikApp', [
     'ngSanitize',
     'ngAnimate',
@@ -6,4 +13,4 @@ angular.module('piwikApp', [
     'piwikApp.directive',
     'piwikApp.filter'
 ]);
-angular.module('app', []);
+angular.module('app', []);
\ No newline at end of file
-- 
GitLab