openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

activebookmark.js (1897B)


      1 // Kludge in HTML5 tag recognition in IE8
      2 document.createElement("section");
      3 document.createElement("article");
      4 
      5 (function() {
      6   if (!window.addEventListener) return;
      7   var pending = false, prevVal = null;
      8 
      9   function updateSoon() {
     10     if (!pending) {
     11       pending = true;
     12       setTimeout(update, 250);
     13     }
     14   }
     15 
     16   function update() {
     17     pending = false;
     18     var marks = document.getElementById("nav").getElementsByTagName("a"), found;
     19     for (var i = 0; i < marks.length; ++i) {
     20       var mark = marks[i], m;
     21       if (mark.getAttribute("data-default")) {
     22         if (found == null) found = i;
     23       } else if (m = mark.href.match(/#(.*)/)) {
     24         var ref = document.getElementById(m[1]);
     25         if (ref && ref.getBoundingClientRect().top < 50)
     26           found = i;
     27       }
     28     }
     29     if (found != null && found != prevVal) {
     30       prevVal = found;
     31       var lis = document.getElementById("nav").getElementsByTagName("li");
     32       for (var i = 0; i < lis.length; ++i) lis[i].className = "";
     33       for (var i = 0; i < marks.length; ++i) {
     34         if (found == i) {
     35           marks[i].className = "active";
     36           for (var n = marks[i]; n; n = n.parentNode)
     37             if (n.nodeName == "LI") n.className = "active";
     38         } else {
     39           marks[i].className = "";
     40         }
     41       }
     42     }
     43   }
     44 
     45   window.addEventListener("scroll", updateSoon);
     46   window.addEventListener("load", updateSoon);
     47   window.addEventListener("hashchange", function() {
     48     setTimeout(function() {
     49       var hash = document.location.hash, found = null, m;
     50       var marks = document.getElementById("nav").getElementsByTagName("a");
     51       for (var i = 0; i < marks.length; i++)
     52         if ((m = marks[i].href.match(/(#.*)/)) && m[1] == hash) { found = i; break; }
     53       if (found != null) for (var i = 0; i < marks.length; i++)
     54         marks[i].className = i == found ? "active" : "";
     55     }, 300);
     56   });
     57 })();