content/xml/tests/toc/toc.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/xml/tests/toc/toc.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,117 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +// Event handler for display togglers in Table of Contents
     1.9 +function toggleDisplay(event)
    1.10 +{
    1.11 +  if (event.target.localName != "img")
    1.12 +    return;
    1.13 +  var img = event.target;
    1.14 +  var div = img.nextSibling.nextSibling; 
    1.15 +
    1.16 +  // Change the display: property of the container to
    1.17 +  // hide and show the container.
    1.18 +  if (div.style.display == "none") {
    1.19 +    div.style.display = "block";
    1.20 +    img.src = "minus.gif";
    1.21 +  }
    1.22 +  else {
    1.23 +    div.style.display = "none";
    1.24 +    img.src = "plus.gif";
    1.25 +  }
    1.26 +}
    1.27 +
    1.28 +// Function that recurses down the tree, looking for 
    1.29 +// structural elements. For each structural element,
    1.30 +// a corresponding element is created in the table of
    1.31 +// contents.
    1.32 +var searchTags = new Array("book", "chapter", "section");
    1.33 +var tocTags = new Array("level1", "level2", "level3");
    1.34 +function addToToc(root, tocFrame)
    1.35 +{
    1.36 +  var i;
    1.37 +  var newTocFrame = tocFrame;
    1.38 +  var newTocElement = null;
    1.39 +  var newTocLink = null;
    1.40 +
    1.41 +  for (i=0; i < searchTags.length; i++) {
    1.42 +    if (root.tagName == searchTags[i]) {
    1.43 +      // If we've found a structural element, create the
    1.44 +      // equivalent TOC element.
    1.45 +      newTocElement = document.createElement(tocTags[i]);
    1.46 +      // Create the toclink element that is a link to the
    1.47 +      // corresponding structural element.
    1.48 +      newTocLink = document.createElement("toclink");
    1.49 +      newTocLink.setAttributeNS("http://www.w3.org/1999/xlink","xlink:type", "simple");
    1.50 +      newTocLink.setAttributeNS("http://www.w3.org/1999/xlink","xlink:href", "#"+ root.getAttribute("id"));
    1.51 +      newTocLink.setAttributeNS("http://www.w3.org/1999/xlink","xlink:show", "replace");
    1.52 +      newTocElement.appendChild(newTocLink);
    1.53 +
    1.54 +      // Create the image and toggling container in the table of contents
    1.55 +      if (i < searchTags.length-1) {
    1.56 +        var img = document.createElementNS("http://www.w3.org/1999/xhtml","img");
    1.57 +        img.src = "minus.gif";
    1.58 +        newTocElement.insertBefore(img,newTocLink);
    1.59 + 
    1.60 +        newTocFrame = document.createElementNS("http://www.w3.org/1999/xhtml","div");
    1.61 +        newTocElement.appendChild(newTocFrame);
    1.62 +      }
    1.63 +      else {
    1.64 +        newTocFrame = null;
    1.65 +      }
    1.66 +
    1.67 +      tocFrame.appendChild(newTocElement);
    1.68 +
    1.69 +      break;
    1.70 +    }
    1.71 +  }
    1.72 +
    1.73 +  // Recurse down through the childNodes list
    1.74 +  for (i=0; i < root.childNodes.length; i++) {
    1.75 +    var child = root.childNodes[i];
    1.76 +    if (child.nodeType == Node.ELEMENT_NODE) {
    1.77 +      if ((newTocLink != null) && (child.tagName == "title")) {
    1.78 +        var text = child.firstChild.cloneNode(true);
    1.79 +        newTocLink.appendChild(text);
    1.80 +      }
    1.81 +      else {
    1.82 +        addToToc(child, newTocFrame);
    1.83 +      }
    1.84 +    }
    1.85 +  }
    1.86 +}
    1.87 +
    1.88 +// Create the root table of contents element (a fixed element)
    1.89 +// and its contents.
    1.90 +function createToc()
    1.91 +{
    1.92 +  if (document.getElementsByTagName("toc").length == 0) {
    1.93 +    var toc = document.createElement("toc");
    1.94 +    var title = document.createElement("title");
    1.95 +    title.appendChild(document.createTextNode("Table of Contents"));
    1.96 +    toc.appendChild(title);
    1.97 +  
    1.98 +    // Recurse down and build up the document element
    1.99 +    addToToc(document.documentElement, toc);
   1.100 +    
   1.101 +    // Since we've created the toc element as a fixed element,
   1.102 +    // insert a rule that shifts over the document element by
   1.103 +    // the width of the toc element.
   1.104 +    document.styleSheets[0].cssRules[0].style.marginLeft = "12em";
   1.105 +    document.documentElement.appendChild(toc);
   1.106 +    
   1.107 +    // Attach the event handler for table of contents buttons.
   1.108 +    // This will only work for content that is already a part
   1.109 +    // of a document, which is why we had to wait until here
   1.110 +    // to do this.
   1.111 +    toc.addEventListener("mouseup",toggleDisplay,1);
   1.112 +  } else {
   1.113 +    // Hide the table of contents.
   1.114 +    // This is not very intelligent if we have a static document, we should
   1.115 +    // just hide/show the toc via stylesheet mungling
   1.116 +    document.documentElement.removeChild(document.getElementsByTagName("toc")[0]);
   1.117 +    document.styleSheets[0].cssRules[0].style.marginLeft = "0em";
   1.118 +  }
   1.119 +}
   1.120 +

mercurial