github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/website/static/godocs.js (about)

     1  // Except as noted, this content is licensed under Creative Commons
     2  // Attribution 3.0
     3  
     4  /* A little code to ease navigation of these documents.
     5   *
     6   * On window load we:
     7   *  + Generate a table of contents (godocs_generateTOC)
     8   *  + Add links up to the top of the doc from each section (godocs_addTopLinks)
     9   */
    10  
    11  /* We want to do some stuff on page load (after the HTML is rendered).
    12     So listen for that:
    13   */
    14  function bindEvent(el, e, fn) {
    15    if (el.addEventListener){
    16      el.addEventListener(e, fn, false);
    17    } else if (el.attachEvent){
    18      el.attachEvent('on'+e, fn);
    19    }
    20  }
    21  
    22  function godocs_bindSearchEvents() {
    23    var search = document.getElementById('search');
    24    if (!search) {
    25      // no search box (index disabled)
    26      return;
    27    }
    28    function clearInactive() {
    29      if (search.className == "inactive") {
    30        search.value = "";
    31        search.className = "";
    32      }
    33    }
    34    function restoreInactive() {
    35      if (search.value !== "") {
    36        return;
    37      }
    38      if (search.type != "search") {
    39        search.value = search.getAttribute("placeholder");
    40      }
    41      search.className = "inactive";
    42    }
    43    restoreInactive();
    44    bindEvent(search, 'focus', clearInactive);
    45    bindEvent(search, 'blur', restoreInactive);
    46  }
    47  
    48  /* Returns the "This sweet header" from <h2>This <i>sweet</i> header</h2>.
    49   * Takes a node, returns a string.
    50   */
    51  function godocs_nodeToText(node) {
    52    var TEXT_NODE = 3; // Defined in Mozilla but not MSIE :(
    53  
    54    var text = '';
    55    for (var j = 0; j != node.childNodes.length; j++) {
    56      var child = node.childNodes[j];
    57      if (child.nodeType == TEXT_NODE) {
    58        if (child.nodeValue != '[Top]') { //ok, that's a hack, but it works.
    59          text = text + child.nodeValue;
    60        }
    61      } else {
    62        text = text + godocs_nodeToText(child);
    63      }
    64    }
    65    return text;
    66  }
    67  
    68  /* Generates a table of contents: looks for h2 and h3 elements and generates
    69   * links.  "Decorates" the element with id=="nav" with this table of contents.
    70   */
    71  function godocs_generateTOC() {
    72    if (document.getElementById('manual-nav')) { return; }
    73    var navbar = document.getElementById('nav');
    74    if (!navbar) { return; }
    75  
    76    var toc_items = [];
    77  
    78    var i;
    79    var seenNav = false;
    80    for (i = 0; i < navbar.parentNode.childNodes.length; i++) {
    81      var node = navbar.parentNode.childNodes[i];
    82      if (!seenNav) { 
    83        if (node.id == 'nav') {
    84          seenNav = true;
    85        }
    86        continue;
    87      }
    88      if ((node.tagName != 'h2') && (node.tagName != 'H2') &&
    89          (node.tagName != 'h3') && (node.tagName != 'H3')) {
    90        continue;
    91      }
    92      if (!node.id) {
    93        node.id = 'tmp_' + i;
    94      }
    95      var text = godocs_nodeToText(node);
    96      if (!text) { continue; }
    97  
    98      var textNode = document.createTextNode(text);
    99  
   100      var link = document.createElement('a');
   101      link.href = '#' + node.id;
   102      link.appendChild(textNode);
   103  
   104      // Then create the item itself
   105      var item;
   106      if ((node.tagName == 'h2') || (node.tagName == 'H2')) {
   107        item = document.createElement('dt');
   108      } else { // h3
   109        item = document.createElement('dd');
   110      }
   111  
   112      item.appendChild(link);
   113      toc_items.push(item);
   114    }
   115  
   116    if (toc_items.length <= 1) { return; }
   117  
   118    var dl1 = document.createElement('dl');
   119    var dl2 = document.createElement('dl');
   120  
   121    var split_index = (toc_items.length / 2) + 1;
   122    if (split_index < 8) {
   123      split_index = toc_items.length;
   124    }
   125  
   126    for (i = 0; i < split_index; i++) {
   127      dl1.appendChild(toc_items[i]);
   128    }
   129    for (/* keep using i */; i < toc_items.length; i++) {
   130      dl2.appendChild(toc_items[i]);
   131    }
   132  
   133    var tocTable = document.createElement('table');
   134    navbar.appendChild(tocTable);
   135    tocTable.className = 'unruled';
   136    var tocBody = document.createElement('tbody');
   137    tocTable.appendChild(tocBody);
   138  
   139    var tocRow = document.createElement('tr');
   140    tocBody.appendChild(tocRow);
   141  
   142    // 1st column
   143    var tocCell = document.createElement('td');
   144    tocCell.className = 'first';
   145    tocRow.appendChild(tocCell);
   146    tocCell.appendChild(dl1);
   147  
   148    // 2nd column
   149    tocCell = document.createElement('td');
   150    tocRow.appendChild(tocCell);
   151    tocCell.appendChild(dl2);
   152  }
   153  
   154  function getElementsByClassName(base, clazz) {
   155    if (base.getElementsByClassName) {
   156      return base.getElementsByClassName(clazz);
   157    }
   158    var elements = base.getElementsByTagName('*'), foundElements = [];
   159    for (var n in elements) {
   160      if (clazz == elements[n].className) {
   161        foundElements.push(elements[n]);
   162      }
   163    }
   164    return foundElements;
   165  }
   166  
   167  function godocs_bindToggle(el) {
   168    var button = getElementsByClassName(el, "toggleButton");
   169    var callback = function() {
   170      if (el.className == "toggle") {
   171        el.className = "toggleVisible";
   172      } else {
   173        el.className = "toggle";
   174      }
   175    };
   176    for (var i = 0; i < button.length; i++) {
   177      bindEvent(button[i], "click", callback);
   178    }
   179  }
   180  function godocs_bindToggles(className) {
   181    var els = getElementsByClassName(document, className);
   182    for (var i = 0; i < els.length; i++) {
   183      godocs_bindToggle(els[i]);
   184    }
   185  }
   186  function godocs_bindToggleLink(l, prefix) {
   187    bindEvent(l, "click", function() {
   188      var i = l.href.indexOf("#"+prefix);
   189      if (i < 0) {
   190        return;
   191      }
   192      var id = prefix + l.href.slice(i+1+prefix.length);
   193      var eg = document.getElementById(id);
   194      eg.className = "toggleVisible";
   195    });
   196  }
   197  function godocs_bindToggleLinks(className, prefix) {
   198    var links = getElementsByClassName(document, className);
   199    for (i = 0; i < links.length; i++) {
   200      godocs_bindToggleLink(links[i], prefix);
   201    }
   202  }
   203  
   204  function godocs_onload() {
   205    godocs_bindSearchEvents();
   206    godocs_generateTOC();
   207    godocs_bindToggles("toggle");
   208    godocs_bindToggles("toggleVisible");
   209    godocs_bindToggleLinks("exampleLink", "example_");
   210    godocs_bindToggleLinks("overviewLink", "");
   211  }
   212  
   213  bindEvent(window, 'load', godocs_onload);