مستخدم:AhmadSherif/collapsible-list.js

ملاحظة: بعد الحفظ، قد يلزمك إفراغ الكاش لرؤية التغييرات.

CollapsibleList = {
	ec: function (element) {
		parentChilds = element.parentNode.childNodes;
		for (var i = 0; i < parentChilds.length; i++) {
			if (parentChilds[i].tagName == "UL") {
				if (parentChilds[i].style.display == "" || parentChilds[i].style.display == "table" || parentChilds[i].style.display == "block") {
					element.innerHTML = "<b>+</b>";
					parentChilds[i].style.display = "none";
				} else {
					element.innerHTML = "<b>-</b>";
					try {
						parentChilds[i].style.display = "table";
					} catch(e) {
						parentChilds[i].style.display = "block";
					}
				}
			}
		}
	},

	processUL: function (element) {
		if (element) {
			var li;
			for (var i = 0; i < element.childNodes.length; i++) {
				li = element.childNodes[i];
				for (var j = 0; j < li.childNodes.length; j++) {
					if (li.childNodes[j].tagName == "UL") {
						li.childNodes[j].style.display = "none";
						CollapsibleList.processUL(li.childNodes[j]);
						li.innerHTML = "<span onclick=\"if (this.href) this.href='javascript:void(0)'; CollapsibleList.ec(this);\" style=\"cursor: pointer;\"><b>+</b></span> " + li.innerHTML;
						break;
					}
				}
			}
		}
	},

	main: function() {
		var mainDiv = document.getElementById("collapsible-list");
		if (mainDiv && mainDiv.hasChildNodes()) {
			for (var i = 0; i < mainDiv.childNodes.length; i++) {
				if (mainDiv.childNodes[i].tagName == "UL") {
					CollapsibleList.processUL(mainDiv.childNodes[i]);
				}
			}
		}
	}
}

$(CollapsibleList.main);