michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // Event handler for display togglers in Table of Contents michael@0: function toggleDisplay(event) michael@0: { michael@0: if (event.target.localName != "img") michael@0: return; michael@0: var img = event.target; michael@0: var div = img.nextSibling.nextSibling; michael@0: michael@0: // Change the display: property of the container to michael@0: // hide and show the container. michael@0: if (div.style.display == "none") { michael@0: div.style.display = "block"; michael@0: img.src = "minus.gif"; michael@0: } michael@0: else { michael@0: div.style.display = "none"; michael@0: img.src = "plus.gif"; michael@0: } michael@0: } michael@0: michael@0: // Function that recurses down the tree, looking for michael@0: // structural elements. For each structural element, michael@0: // a corresponding element is created in the table of michael@0: // contents. michael@0: var searchTags = new Array("book", "chapter", "section"); michael@0: var tocTags = new Array("level1", "level2", "level3"); michael@0: function addToToc(root, tocFrame) michael@0: { michael@0: var i; michael@0: var newTocFrame = tocFrame; michael@0: var newTocElement = null; michael@0: var newTocLink = null; michael@0: michael@0: for (i=0; i < searchTags.length; i++) { michael@0: if (root.tagName == searchTags[i]) { michael@0: // If we've found a structural element, create the michael@0: // equivalent TOC element. michael@0: newTocElement = document.createElement(tocTags[i]); michael@0: // Create the toclink element that is a link to the michael@0: // corresponding structural element. michael@0: newTocLink = document.createElement("toclink"); michael@0: newTocLink.setAttributeNS("http://www.w3.org/1999/xlink","xlink:type", "simple"); michael@0: newTocLink.setAttributeNS("http://www.w3.org/1999/xlink","xlink:href", "#"+ root.getAttribute("id")); michael@0: newTocLink.setAttributeNS("http://www.w3.org/1999/xlink","xlink:show", "replace"); michael@0: newTocElement.appendChild(newTocLink); michael@0: michael@0: // Create the image and toggling container in the table of contents michael@0: if (i < searchTags.length-1) { michael@0: var img = document.createElementNS("http://www.w3.org/1999/xhtml","img"); michael@0: img.src = "minus.gif"; michael@0: newTocElement.insertBefore(img,newTocLink); michael@0: michael@0: newTocFrame = document.createElementNS("http://www.w3.org/1999/xhtml","div"); michael@0: newTocElement.appendChild(newTocFrame); michael@0: } michael@0: else { michael@0: newTocFrame = null; michael@0: } michael@0: michael@0: tocFrame.appendChild(newTocElement); michael@0: michael@0: break; michael@0: } michael@0: } michael@0: michael@0: // Recurse down through the childNodes list michael@0: for (i=0; i < root.childNodes.length; i++) { michael@0: var child = root.childNodes[i]; michael@0: if (child.nodeType == Node.ELEMENT_NODE) { michael@0: if ((newTocLink != null) && (child.tagName == "title")) { michael@0: var text = child.firstChild.cloneNode(true); michael@0: newTocLink.appendChild(text); michael@0: } michael@0: else { michael@0: addToToc(child, newTocFrame); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Create the root table of contents element (a fixed element) michael@0: // and its contents. michael@0: function createToc() michael@0: { michael@0: if (document.getElementsByTagName("toc").length == 0) { michael@0: var toc = document.createElement("toc"); michael@0: var title = document.createElement("title"); michael@0: title.appendChild(document.createTextNode("Table of Contents")); michael@0: toc.appendChild(title); michael@0: michael@0: // Recurse down and build up the document element michael@0: addToToc(document.documentElement, toc); michael@0: michael@0: // Since we've created the toc element as a fixed element, michael@0: // insert a rule that shifts over the document element by michael@0: // the width of the toc element. michael@0: document.styleSheets[0].cssRules[0].style.marginLeft = "12em"; michael@0: document.documentElement.appendChild(toc); michael@0: michael@0: // Attach the event handler for table of contents buttons. michael@0: // This will only work for content that is already a part michael@0: // of a document, which is why we had to wait until here michael@0: // to do this. michael@0: toc.addEventListener("mouseup",toggleDisplay,1); michael@0: } else { michael@0: // Hide the table of contents. michael@0: // This is not very intelligent if we have a static document, we should michael@0: // just hide/show the toc via stylesheet mungling michael@0: document.documentElement.removeChild(document.getElementsByTagName("toc")[0]); michael@0: document.styleSheets[0].cssRules[0].style.marginLeft = "0em"; michael@0: } michael@0: } michael@0: