kettek/javascript/folder.js

79 lines
2.1 KiB
JavaScript

/*
This file provides folding of sections of text
*/
var toggleHidden = function(arrow, target) {
if (target.style.display == "none") {
target.style.display = "block";
arrow.className = "arrow_open";
} else {
target.style.display = "none";
arrow.className = "arrow";
}
};
var createFolds = function() {
var context = document.getElementById("content");
foldHeader(context, "H2");
};
var foldHeader = function(context, type) {
if (!context.hasChildNodes()) return;
var child = context.firstChild;
var prev_child = child;
var header = null;
var section = null;
var open = 0;
while (child) {
var next_child = child.nextSibling;
if (child.nodeName == type) {
if (section == null) {
header = child;
var arrow = document.createElement("div");
arrow.className = "arrow_open";
header.appendChild(arrow);
section = document.createElement("div");
header.onclick = (function(header, arrow, section) {
return function(e) {
toggleHidden(arrow, section);
}
})(header, arrow, section);
toggleHidden(arrow, section);
open = 1;
} else {
if (header.nextSibling) {
context.insertBefore(section, header.nextSibling);
} else {
context.appendChild(section);
}
header = child;
var arrow = document.createElement("div");
arrow.className = "arrow_open";
header.appendChild(arrow);
section = document.createElement("div");
header.onclick = (function(header, arrow, section) {
return function(e) {
toggleHidden(arrow, section);
}
})(header, arrow, section);
toggleHidden(arrow, section);
open = 1;
}
} else {
if (section != null) {
section.appendChild(child);
}
}
prev_child = child;
child = next_child;
}
if (open == 1) {
if (header.nextSibling) {
context.insertBefore(section, header.nextSibling);
} else {
context.appendChild(section);
}
}
};
window.onload = function() { createFolds() };