Javascript that expands and collapses Zwiki's subtopic lists.

/* script that expands and collapses nested unordered html lists. The idea is taken
from www.kryogenix.org, but the script has been completely rewritten by me (Benedikt Linse). This file can be used under the terms of the GNU public license.

*/

/* Javascript-datei zum expandieren und kollabieren geschachtelter
ungeordneter Listen. Das Prinzip stammt von www.kryogenix.org, das Skript wurde allerdings von mir komplett neu geschrieben. */

addEvent(window, "load", init_uls);

function addEvent(obj, evType, fn){
/* adds an eventListener for browsers which support it
Written by Scott Andrew: nice one, Scott */
if (obj.addEventListener){
obj.addEventListener(evType, fn, true); return true;
} else if (obj.attachEvent){
var r = obj.attachEvent("on"+evType, fn); return r;
} else {
return false;

}

}

function init_uls() {

var uls = document.getElementsByTagName("ul"); var n_uls = uls.length; for (var i = 0; i < n_uls; i++) {

var ul = uls[i]?; if (ul.className == "aqtree3clickable" && (ul.parentNode.nodeName !=
"SMALL")) { init_ul(ul); }
}

}

function init_ul(ul) {

var children = ul.childNodes; var n_cn = children.length; for (var i = 0; i < n_cn; i++) {

ch = children[i]?; if (ch.nodeType == 1) {

if (ch.nodeName == "LI") {
init_li(ch);

} else {

alert (ch.nodeName); return(0);

}

}

}

}

function get_sibling_with_name(elem, name) {

siblings = elem.parentNode.childNodes; for (i = 0; i < siblings.length; i++) {

if (siblings[i]?.nodeName == name) { return siblings[i]?; }

}

}

function get_child_with_name(elem, name) {

children = elem.childNodes; for (i = 0; i < children.length; i++) {

if (children[i]?.nodeName == name) { return children[i]?; }

}

}

function has_child_with_name(elem, name) {

children = elem.childNodes; for (i = 0; i < children.length; i++) {

if (children[i]?.nodeName == name) { return true; }

} return false;

}

function expand_li_IE() {
var img = window.event.srcElement; expand_li(img);

}

function expand_li(img) {

// der eventlistener geh�t zu einem Imageobjekt (bullet.gif, plus.gif // oder minus.gif). Nun muss die Liste expandiert werden, welche dem // Imageobjekt folgt. img.setAttribute('src', 'minus.gif'); if (img.attachEvent) { //this is for IE

img.detachEvent("onclick", expand_li_IE); img.attachEvent("onclick", collapse_li_IE);

} else { img.setAttribute('onClick', 'collapse_li(this)'); } var li = img.parentNode; li.className = 'open'; var ul = get_sibling_with_name(img, 'UL'); init_ul(ul);

}

function collapse_li_IE() {
var img = window.event.srcElement; collapse_li(img);

}

function collapse_li(img) {

li = img.parentNode; li.className = "closed"; img.setAttribute('src', 'plus.gif'); if (img.attachEvent) { //this is for IE

img.detachEvent("onclick", collapse_li_IE); img.attachEvent("onclick", expand_li_IE);

} else { img.setAttribute('onClick', "expand_li(this)"); }

}

function init_li(li) {
if (!has_child_with_name(li, 'IMG')) {

var img = document.createElement('IMG'); if (has_child_with_name(li, 'UL')) {

img.setAttribute('src', 'plus.gif'); if (img.attachEvent) { //this is for IE {

img.detachEvent("onclick", collapse_li_IE); img.attachEvent("onclick", expand_li_IE);

} else { img.setAttribute('onClick', "expand_li(this)"); } li.className = "closed";

} else {

img.setAttribute('src', 'bullet.gif'); li.className = "bullet";

} li.insertBefore(img, li.firstChild);

} else {

var img = get_child_with_name(li, 'IMG'); if (img.className == 'closed') { expand_li(img); }

}

}