--- /dev/null
+/* Folding headings.
+ * Copyright (C) 2006 Merjis Ltd.
+ * $Id: folding.js,v 1.1 2006/08/17 11:19:58 rich Exp $
+ *
+ * For very long pages, you can use this Javascript to provide
+ * folding headings to make viewing the page more manageable.
+ *
+ * To install this code:
+ * (1) You must have the "manage site" permission.
+ * (2) Go to Sitewide settings, click Edit these settings, and in
+ * the Page bug section, add or append the following Javascript
+ * code:
+ * <script src="/_js/folding.js" type="text/javascript"></script>
+ * (3) Edit the page where you want folding to appear, and set
+ * the CSS class to "folding".
+ */
+
+// http://domscripting.com/blog/display/18
+function getElementsByClass (node, searchClass, tag)
+{
+ var classElements = new Array();
+ var els = node.getElementsByTagName (tag);
+ var pattern = new RegExp ("\\b"+searchClass+"\\b");
+ for (i = 0, j = 0; i < els.length; i++) {
+ if (pattern.test (els[i].className)) {
+ classElements[j] = els[i];
+ j++;
+ }
+ }
+ return classElements;
+}
+
+function setTextContent (text, content)
+{
+ // Not sure if this is even possible in IE6.
+ if (text.textContent) text.textContent = content;
+}
+
+function setOnClick (node, f)
+{
+ // if (!node.attachEvent) node.onclick = f;
+ // else node.attachEvent ("onclick", f);
+ node.onclick = f;
+}
+
+// http://joust.kano.net/weblog/archive/2005/08/08/a-huge-gotcha-with-javascript-closures/
+// http://calculist.blogspot.com/2005/12/gotcha-gotcha.html
+function createShowFunction (text, plus, nodes)
+{
+ return function (evt) {
+ // Restore the old display style.
+ for (var k = 0; k < nodes.length; ++k) {
+ var node = nodes[k];
+ if (node.style)
+ node.style.display = node.getAttribute ("displayold");
+ }
+ setTextContent (text, "[-]");
+ setOnClick (plus, createHideFunction (text, plus, nodes));
+ }
+}
+
+function createHideFunction (text, plus, nodes)
+{
+ return function (evt) {
+ // Set all the nodes to display none, but remember the old
+ // display style.
+ for (var k = 0; k < nodes.length; ++k) {
+ var node = nodes[k];
+ if (node.style) {
+ node.setAttribute ("displayold", node.style.display);
+ node.style.display = "none";
+ }
+ }
+ setTextContent (text, "[+]");
+ setOnClick (plus, createShowFunction (text, plus, nodes));
+ }
+}
+
+function set_up_folding ()
+{
+ var divs = getElementsByClass (document, "folding", "div");
+ for (var d = 0; d < divs.length; ++d) {
+ var div = divs[d];
+ var children = div.childNodes;
+ // List of child nodes is something like H3, n, n, H3, n, n, n, ...
+ // Add a button before each H3 which hides/shows the subsequent
+ // nodes up to but not including the next H3.
+ for (var i = 0; i < children.length; ++i) {
+ var h3 = children[i];
+ if (h3.nodeName == "H3") {
+ var j = i+1;
+ while (j < children.length && children[j].nodeName != "H3") j++;
+ // children i to j exclusive should be shown/hidden.
+ // Copy them to an array called 'nodes'.
+ var nodes = new Array ();
+ for (var k = i+1; k < j; ++k) {
+ var child = children[k];
+ nodes[k-(i+1)] = child;
+ }
+
+ // Create the +/- control.
+ var plus = document.createElement ("div");
+ plus.setAttribute ("class", "foldingcontrol");
+ var text = document.createTextNode ("[+]");
+ plus.appendChild (text);
+
+ // Create functions to show/hide the nodes.
+ var show = createShowFunction (text, plus, nodes);
+ var hide = createHideFunction (text, plus, nodes);
+
+ hide ();
+
+ // Place the '+' button before the h3.
+ div.insertBefore (plus, h3);
+ setOnClick (plus, show);
+
+ // Skip directly to next h3.
+ i = j-1;
+ }
+ }
+ }
+}
+
+set_up_folding ();