fix for auto-scroll-detecting side menu to take a link if it is clicked

Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/1e6f8b42
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/1e6f8b42
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/1e6f8b42

Branch: refs/heads/master
Commit: 1e6f8b424edb3bc9e860cf8fb4124c640a06a720
Parents: a8a9f28
Author: Alex Heneveld <[email protected]>
Authored: Mon Jan 19 13:38:20 2015 +0000
Committer: Alex Heneveld <[email protected]>
Committed: Mon Jan 19 22:57:24 2015 +0000

----------------------------------------------------------------------
 docs/_includes/sidemenu.html | 81 +++++++++++++++++++++++++++++++++++----
 1 file changed, 73 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/1e6f8b42/docs/_includes/sidemenu.html
----------------------------------------------------------------------
diff --git a/docs/_includes/sidemenu.html b/docs/_includes/sidemenu.html
index f37ce66..808d9e2 100644
--- a/docs/_includes/sidemenu.html
+++ b/docs/_includes/sidemenu.html
@@ -105,7 +105,7 @@ var sideMenu = $("#side-menu"),
 var highlight_section_last_top = -1;
 var highlight_section_completed = false;
 
-var highlight_section = function(data) {
+var highlight_section = function() {
    // Get container scroll position
    var highlight_section_new_top = $(this).scrollTop();
    if (highlight_section_new_top == highlight_section_last_top) return;
@@ -114,13 +114,16 @@ var highlight_section = function(data) {
 
    var last_item = null, active_item = $("#side-menu a.section#active");
    
+   var found_top = false;
    var displayable_items = scrollItems.map(function(itemI){
      item = $(scrollItems[itemI]);
      if (item && item.length) {
        if (highlight_section_last_top == -1 || !highlight_section_completed) {
-         // just opening page - take item at top of page
-         if (item.offset().top < highlight_section_new_top + 15)
-           return item;         
+         // just opening page - take item matching hash, or otherwise the 
first item visible
+         if (item.selector === window.location.hash || (item.offset().top > 
highlight_section_new_top - 15 && !found_top)) {
+           found_top = true;
+           return item;
+         }
        } else if (scroll_advancing) {
          // if scrolling advance, pick up a section when title starts before 
1/3 height 
          if (item.offset().top < highlight_section_new_top + 
$(window).height()/3)
@@ -146,12 +149,12 @@ var highlight_section = function(data) {
            return item;
          
        }
-       if (!highlight_section_completed && document.readyState === "complete") 
{
-         highlight_section_completed = true;
-       }
        last_item = item;
      }
    });
+   if (!highlight_section_completed && document.readyState === "complete") {
+     highlight_section_completed = true;
+   }
    if (!displayable_items.length) {
      $("#side-menu a.section").removeClass("active");
    } else {
@@ -169,7 +172,69 @@ var highlight_section = function(data) {
    
    highlight_section_last_top = highlight_section_new_top;
 };
+var highlight_new_section = function() {
+  highlight_section_completed = false;
+  highlight_section_last_top = -1;
+  highlight_section();
+}
 
 $(window).scroll(highlight_section);
-$(highlight_section);
+$(highlight_new_section);
+
+// detect link change - courtesy 
http://www.bennadel.com/blog/1520-binding-events-to-non-dom-objects-with-jquery.htm
+    (
+        function( $ ){
+            // Default to the current location.
+            var strLocation = window.location.href;
+            var strHash = window.location.hash;
+            var strPrevLocation = "";
+            var strPrevHash = "";
+
+            // This is how often we will be checkint for
+            // changes on the location.
+            var intIntervalTime = 100;
+
+            // This method removes the pound from the hash.
+            var fnCleanHash = function( strHash ){
+                return(
+                    strHash.substring( 1, strHash.length )
+                    );
+            }
+
+            // This will be the method that we use to check
+            // changes in the window location.
+            var fnCheckLocation = function(){
+                // Check to see if the location has changed.
+                if (strLocation != window.location.href){
+
+                    // Store the new and previous locations.
+                    strPrevLocation = strLocation;
+                    strPrevHash = strHash;
+                    strLocation = window.location.href;
+                    strHash = window.location.hash;
+
+                    // The location has changed. Trigger a
+                    // change event on the location object,
+                    // passing in the current and previous
+                    // location values.
+                    $( window.location ).trigger(
+                        "change",
+                        {
+                            currentHref: strLocation,
+                            currentHash: fnCleanHash( strHash ),
+                            previousHref: strPrevLocation,
+                            previousHash: fnCleanHash( strPrevHash )
+                        }
+                        );
+
+                }
+            }
+
+            // Set an interval to check the location changes.
+            setInterval( fnCheckLocation, intIntervalTime );
+        }
+    )( jQuery );
+// and trigger highlight section on link change
+$(window.location).bind("change", highlight_new_section);
+
 </script>

Reply via email to