content/xml/tests/toc/toc.js

branch
TOR_BUG_9701
changeset 8
97036ab72558
equal deleted inserted replaced
-1:000000000000 0:e9b0a92aeefa
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 {
8 if (event.target.localName != "img")
9 return;
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 }
19 else {
20 div.style.display = "none";
21 img.src = "plus.gif";
22 }
23 }
24
25 // Function that recurses down the tree, looking for
26 // structural elements. For each structural element,
27 // a corresponding element is created in the table of
28 // contents.
29 var searchTags = new Array("book", "chapter", "section");
30 var tocTags = new Array("level1", "level2", "level3");
31 function addToToc(root, tocFrame)
32 {
33 var i;
34 var newTocFrame = tocFrame;
35 var newTocElement = null;
36 var newTocLink = null;
37
38 for (i=0; i < searchTags.length; i++) {
39 if (root.tagName == searchTags[i]) {
40 // If we've found a structural element, create the
41 // equivalent TOC element.
42 newTocElement = document.createElement(tocTags[i]);
43 // Create the toclink element that is a link to the
44 // corresponding structural element.
45 newTocLink = document.createElement("toclink");
46 newTocLink.setAttributeNS("http://www.w3.org/1999/xlink","xlink:type", "simple");
47 newTocLink.setAttributeNS("http://www.w3.org/1999/xlink","xlink:href", "#"+ root.getAttribute("id"));
48 newTocLink.setAttributeNS("http://www.w3.org/1999/xlink","xlink:show", "replace");
49 newTocElement.appendChild(newTocLink);
50
51 // Create the image and toggling container in the table of contents
52 if (i < searchTags.length-1) {
53 var img = document.createElementNS("http://www.w3.org/1999/xhtml","img");
54 img.src = "minus.gif";
55 newTocElement.insertBefore(img,newTocLink);
56
57 newTocFrame = document.createElementNS("http://www.w3.org/1999/xhtml","div");
58 newTocElement.appendChild(newTocFrame);
59 }
60 else {
61 newTocFrame = null;
62 }
63
64 tocFrame.appendChild(newTocElement);
65
66 break;
67 }
68 }
69
70 // Recurse down through the childNodes list
71 for (i=0; i < root.childNodes.length; i++) {
72 var child = root.childNodes[i];
73 if (child.nodeType == Node.ELEMENT_NODE) {
74 if ((newTocLink != null) && (child.tagName == "title")) {
75 var text = child.firstChild.cloneNode(true);
76 newTocLink.appendChild(text);
77 }
78 else {
79 addToToc(child, newTocFrame);
80 }
81 }
82 }
83 }
84
85 // Create the root table of contents element (a fixed element)
86 // and its contents.
87 function createToc()
88 {
89 if (document.getElementsByTagName("toc").length == 0) {
90 var toc = document.createElement("toc");
91 var title = document.createElement("title");
92 title.appendChild(document.createTextNode("Table of Contents"));
93 toc.appendChild(title);
94
95 // Recurse down and build up the document element
96 addToToc(document.documentElement, toc);
97
98 // Since we've created the toc element as a fixed element,
99 // insert a rule that shifts over the document element by
100 // the width of the toc element.
101 document.styleSheets[0].cssRules[0].style.marginLeft = "12em";
102 document.documentElement.appendChild(toc);
103
104 // Attach the event handler for table of contents buttons.
105 // This will only work for content that is already a part
106 // of a document, which is why we had to wait until here
107 // to do this.
108 toc.addEventListener("mouseup",toggleDisplay,1);
109 } else {
110 // Hide the table of contents.
111 // This is not very intelligent if we have a static document, we should
112 // just hide/show the toc via stylesheet mungling
113 document.documentElement.removeChild(document.getElementsByTagName("toc")[0]);
114 document.styleSheets[0].cssRules[0].style.marginLeft = "0em";
115 }
116 }
117

mercurial