~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/content/xml/tests/toc/toc.js

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

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