tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

toc.js (4231B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 // Event handler for display togglers in Table of Contents
      6 function toggleDisplay(event) {
      7  if (event.target.localName != "img") {
      8    return;
      9  }
     10  var img = event.target;
     11  var div = img.nextSibling.nextSibling;
     12 
     13  // Change the display: property of the container to
     14  // hide and show the container.
     15  if (div.style.display == "none") {
     16    div.style.display = "block";
     17    img.src = "minus.gif";
     18  } else {
     19    div.style.display = "none";
     20    img.src = "plus.gif";
     21  }
     22 }
     23 
     24 // Function that recurses down the tree, looking for
     25 // structural elements. For each structural element,
     26 // a corresponding element is created in the table of
     27 // contents.
     28 var searchTags = ["book", "chapter", "section"];
     29 var tocTags = ["level1", "level2", "level3"];
     30 function addToToc(root, tocFrame) {
     31  var i;
     32  var newTocFrame = tocFrame;
     33  var newTocElement = null;
     34  var newTocLink = null;
     35 
     36  for (i = 0; i < searchTags.length; i++) {
     37    if (root.tagName == searchTags[i]) {
     38      // If we've found a structural element, create the
     39      // equivalent TOC element.
     40      newTocElement = document.createElement(tocTags[i]);
     41      // Create the toclink element that is a link to the
     42      // corresponding structural element.
     43      newTocLink = document.createElement("toclink");
     44      newTocLink.setAttributeNS(
     45        "http://www.w3.org/1999/xlink",
     46        "xlink:type",
     47        "simple"
     48      );
     49      newTocLink.setAttributeNS(
     50        "http://www.w3.org/1999/xlink",
     51        "xlink:href",
     52        "#" + root.getAttribute("id")
     53      );
     54      newTocLink.setAttributeNS(
     55        "http://www.w3.org/1999/xlink",
     56        "xlink:show",
     57        "replace"
     58      );
     59      newTocElement.appendChild(newTocLink);
     60 
     61      // Create the image and toggling container in the table of contents
     62      if (i < searchTags.length - 1) {
     63        var img = document.createElementNS(
     64          "http://www.w3.org/1999/xhtml",
     65          "img"
     66        );
     67        img.src = "minus.gif";
     68        newTocElement.insertBefore(img, newTocLink);
     69 
     70        newTocFrame = document.createElementNS(
     71          "http://www.w3.org/1999/xhtml",
     72          "div"
     73        );
     74        newTocElement.appendChild(newTocFrame);
     75      } else {
     76        newTocFrame = null;
     77      }
     78 
     79      tocFrame.appendChild(newTocElement);
     80 
     81      break;
     82    }
     83  }
     84 
     85  // Recurse down through the childNodes list
     86  for (i = 0; i < root.childNodes.length; i++) {
     87    var child = root.childNodes[i];
     88    if (child.nodeType == Node.ELEMENT_NODE) {
     89      if (newTocLink != null && child.tagName == "title") {
     90        var text = child.firstChild.cloneNode(true);
     91        newTocLink.appendChild(text);
     92      } else {
     93        addToToc(child, newTocFrame);
     94      }
     95    }
     96  }
     97 }
     98 
     99 // Create the root table of contents element (a fixed element)
    100 // and its contents.
    101 function createToc() {
    102  if (!document.getElementsByTagName("toc").length) {
    103    var toc = document.createElement("toc");
    104    var title = document.createElement("title");
    105    title.appendChild(document.createTextNode("Table of Contents"));
    106    toc.appendChild(title);
    107 
    108    // Recurse down and build up the document element
    109    addToToc(document.documentElement, toc);
    110 
    111    // Since we've created the toc element as a fixed element,
    112    // insert a rule that shifts over the document element by
    113    // the width of the toc element.
    114    document.styleSheets[0].cssRules[0].style.marginLeft = "12em";
    115    document.documentElement.appendChild(toc);
    116 
    117    // Attach the event handler for table of contents buttons.
    118    // This will only work for content that is already a part
    119    // of a document, which is why we had to wait until here
    120    // to do this.
    121    toc.addEventListener("mouseup", toggleDisplay, 1);
    122  } else {
    123    // Hide the table of contents.
    124    // This is not very intelligent if we have a static document, we should
    125    // just hide/show the toc via stylesheet mungling
    126    document.documentElement.removeChild(
    127      document.getElementsByTagName("toc")[0]
    128    );
    129    document.styleSheets[0].cssRules[0].style.marginLeft = "0em";
    130  }
    131 }