Jump to content

User:Proteins/unindent.js: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Proteins (talk | contribs)
new background colors
Proteins (talk | contribs)
loop over the DD elements twice
Line 7: Line 7:


var indent_level = 0;
var indent_level = 0;
var indent_level_string = "";


var DL_elements;
var DL_elements;
Line 21: Line 22:


var top_node;
var top_node;
var child_node;
var parent_node;
var parent_node;
var top_DL_node;
var top_DL_node;
Line 35: Line 37:
top_node = document.getElementById('bodyContent');
top_node = document.getElementById('bodyContent');
DD_elements = top_node.getElementsByTagName("DD");
DD_elements = top_node.getElementsByTagName("DD");
num_DD_elements = DD_elements.length;


// First-pass loop colors the links and adds the level to the beginning
diagnostic_string = "";
diagnostic_string = "";
num_DD_elements = DD_elements.length;
for (DD_element_index=0; DD_element_index<num_DD_elements; DD_element_index++) {
for (DD_element_index=0; DD_element_index<num_DD_elements; DD_element_index++) {
temp_DD_element = DD_elements[DD_element_index];
temp_DD_element = DD_elements[DD_element_index];
Line 72: Line 75:
num_unindented_DD_elements++;
num_unindented_DD_elements++;
temp_DD_element.style.cssText = "background-color:" + DD_background_colors[indent_level%num_colors];
temp_DD_element.style.cssText = "background-color:" + DD_background_colors[indent_level%num_colors];

indent_level_string = "(Indent level " + indent_level + ") ";
child_node = temp_DD_element.firstChild;
if (child_node) {
temp_DD_element.insertBefore(document.createTextNode(indent_level_string), child_node);
} else {
indent_level_string += "No text in this DD element.\n";
temp_DD_element.appendChild(document.createTextNode(indent_level_string));
}
diagnostic_string += "DD element " + DD_element_index + " is indented to level " + indent_level + ".\n";
diagnostic_string += "DD element " + DD_element_index + " is indented to level " + indent_level + ".\n";
} // check for unindenting
} // closes loop over the DD elements of the document
window.alert(diagnostic_string);

// Second-pass loop changes the document tree structure
diagnostic_string = "";
for (DD_element_index=0; DD_element_index<num_DD_elements; DD_element_index++) {
temp_DD_element = DD_elements[DD_element_index];

indent_level = 0;

// Find the topmost DL element for this DD node
top_DL_node = null;
top_DD_node = null;
parent_node = temp_DD_element.parentNode;

while ((parent_node) && (parent_node != top_node)) {
if (parent_node.nodeType != 1) {
parent_node = parent_node.parentNode;
continue;
} // examine only Element nodes

if (parent_node.nodeName == "DL") {
// Check whether the parent DL element has any DT elements
num_DT_elements = parent_node.getElementsByTagName("DT").length;
if (num_DT_elements > 0) { break; } // if so, stop unindenting...

// ...else make this the new indent level
indent_level++;
top_DL_node = parent_node;
} // closes check for a parental DL element

if (parent_node.nodeName == "DD") { top_DD_node = parent_node; }

parent_node = parent_node.parentNode;
} // closes loop climbing up the document tree

if (indent_level > 0) {
if ((top_DD_node) && (top_DL_node)) {
if ((top_DD_node) && (top_DL_node)) {
sibling_node = top_DD_node.nextSibling;
sibling_node = top_DD_node.nextSibling;
Line 86: Line 136:
} // check that both the top_DD and top_DL elements are defined
} // check that both the top_DD and top_DL elements are defined
} // check for unindenting
} // check for unindenting

} // closes loop over the DD elements of the document
} // closes loop over the DD elements of the document
window.alert(diagnostic_string);
window.alert(diagnostic_string);

Revision as of 14:09, 23 October 2008

//<pre>
// Unindent discusions for accessibility
 
function unindent() {
	var alert_string = "";
	var diagnostic_string = "";

	var indent_level = 0;
	var indent_level_string = "";

	var DL_elements;
	var num_DL_elements = 0;

	var DT_elements;
	var num_DT_elements = 0;

	var DD_elements;
	var temp_DD_element;
	var num_DD_elements = 0;
	var DD_element_index = 0;
	var num_unindented_DD_elements = 0;

	var top_node;
	var child_node;
	var parent_node;
	var top_DL_node;
	var top_DD_node;

	// Colors to help sighted people after the unindenting
	var num_colors = 0;
	var DD_background_colors = ["white", "yellow", "greenyellow", "gold", "lawngreen", "orange", "lightskyblue", "darkorange"];


// Initialization
	num_colors = DD_background_colors.length;

	top_node = document.getElementById('bodyContent');
	DD_elements = top_node.getElementsByTagName("DD");
	num_DD_elements = DD_elements.length;

// First-pass loop colors the links and adds the level to the beginning
	diagnostic_string = "";
	for (DD_element_index=0; DD_element_index<num_DD_elements; DD_element_index++) { 
		temp_DD_element = DD_elements[DD_element_index];

		indent_level = 0;

		// Find the topmost DL element for this DD node
		top_DL_node = null;
		top_DD_node = null;
		parent_node = temp_DD_element.parentNode;

		while ((parent_node) && (parent_node != top_node)) {
			if (parent_node.nodeType != 1) {
				parent_node = parent_node.parentNode;
				continue; 
			} // examine only Element nodes

			if (parent_node.nodeName == "DL") { 
				// Check whether the parent DL element has any DT elements
				num_DT_elements = parent_node.getElementsByTagName("DT").length;
				if (num_DT_elements > 0) { break; } // if so, stop unindenting...

				// ...else make this the new indent level
				indent_level++;
				top_DL_node = parent_node;
			} // closes check for a parental DL element

			if (parent_node.nodeName == "DD") { top_DD_node = parent_node; }

			parent_node = parent_node.parentNode;
		} // closes loop climbing up the document tree

		if (indent_level > 0) { 
			num_unindented_DD_elements++;
			temp_DD_element.style.cssText = "background-color:" + DD_background_colors[indent_level%num_colors];

			indent_level_string = "(Indent level " + indent_level + ") ";
			child_node = temp_DD_element.firstChild;
			if (child_node) { 
				temp_DD_element.insertBefore(document.createTextNode(indent_level_string), child_node);
			} else {
				indent_level_string += "No text in this DD element.\n";
				temp_DD_element.appendChild(document.createTextNode(indent_level_string));
			}
			diagnostic_string += "DD element " + DD_element_index + " is indented to level " + indent_level + ".\n";
		} // check for unindenting
	} // closes loop over the DD elements of the document
	window.alert(diagnostic_string);

// Second-pass loop changes the document tree structure
	diagnostic_string = "";
	for (DD_element_index=0; DD_element_index<num_DD_elements; DD_element_index++) { 
		temp_DD_element = DD_elements[DD_element_index];

		indent_level = 0;

		// Find the topmost DL element for this DD node
		top_DL_node = null;
		top_DD_node = null;
		parent_node = temp_DD_element.parentNode;

		while ((parent_node) && (parent_node != top_node)) {
			if (parent_node.nodeType != 1) {
				parent_node = parent_node.parentNode;
				continue; 
			} // examine only Element nodes

			if (parent_node.nodeName == "DL") { 
				// Check whether the parent DL element has any DT elements
				num_DT_elements = parent_node.getElementsByTagName("DT").length;
				if (num_DT_elements > 0) { break; } // if so, stop unindenting...

				// ...else make this the new indent level
				indent_level++;
				top_DL_node = parent_node;
			} // closes check for a parental DL element

			if (parent_node.nodeName == "DD") { top_DD_node = parent_node; }

			parent_node = parent_node.parentNode;
		} // closes loop climbing up the document tree

		if (indent_level > 0) { 
			if ((top_DD_node) && (top_DL_node)) { 
				sibling_node = top_DD_node.nextSibling;
				while ((sibling_node) && (sibling_node.nodeType != 1)) { 
					sibling_node = sibling_node.nextSibling;
				}
				
				if (sibling_node) { 
					top_DL_node.insertBefore(temp_DD_element, sibling_code);
				} else { 
					top_DL_node.appendChild(temp_DD_element);
				}
			} // check that both the top_DD and top_DL elements are defined
		} // check for unindenting
	} // closes loop over the DD elements of the document
	window.alert(diagnostic_string);

//Acknowledgment
	alert_string = "Unindented " + num_unindented_DD_elements + " paragraphs of " + num_DD_elements + " possible.";
	window.alert(alert_string);

} // closes unindent() function

addOnloadHook(function () {
            addPortletLink('p-cactions', 'javascript:unindent()', 'unindent', 'ca-unindent', 'Unindent discussions', '', '');
});
 
//</pre>