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