PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 7ms RepoModel.GetById 33ms app.codeStats 0ms

/Documentation/Help/scripts/ElementCollection.js

#
JavaScript | 91 lines | 71 code | 14 blank | 6 comment | 39 complexity | 9a0f0ad4e0149ada894265fd76f45f54 MD5 | raw file
 1
 2	// a collection of elements whoose style can be changed based on the values of attributes
 3
 4	function ElementCollection(parentId) {
 5
 6		// get the tabbed region
 7		this.parent = document.getElementById(parentId);
 8
 9
10		// get the children
11		this.elements = new Array();
12		if (this.parent.tagName == 'TABLE') {
13			// special handling for tables
14			var bodies = this.parent.tBodies;
15			for(i=0; i<bodies.length; i++) {
16				var rows = bodies[i].rows;
17				for(j=0; j<rows.length; j++) {
18					if (rows[j].nodeType == 1) this.elements.push(rows[j]);
19				}
20			}
21			// this.elements = this.parent.tBodies[0].rows;
22		} else {
23			// all other cases
24			var nodes = this.parent.childNodes;
25			for(i=0; i<nodes.length; i++) {
26				if (nodes[i].nodeType == 1) this.elements.push(nodes[i]);
27			}
28		}
29
30	}
31
32	ElementCollection.prototype.process = function(processFunction) {
33		for(var i=0; i<this.elements.length; i++) {
34			var element = this.elements[i];
35			processFunction(element);
36		}
37	}
38
39	ElementCollection.prototype.changeStyle = function(attributeName, attributeValue, styleName, styleValue) {
40		for(var i=0; i<this.elements.length; i++) {
41			var element = this.elements[i];
42			var value = element.getAttribute(attributeName);
43			if (value != null) {
44				if (value == attributeValue) {
45					element.style[styleName] = styleValue;
46				}
47			}
48		}
49	}
50
51
52	ElementCollection.prototype.toggleStyle = function(attributeName, attributeValue, styleName, trueStyleValue, falseStyleValue) {
53		for(var i=0; i<this.elements.length; i++) {
54			var element = this.elements[i];
55			if (element.nodeType != 1) continue;
56			var value = element.getAttribute(attributeName);
57			if (value == null) continue;
58
59			if (value == attributeValue) {
60				element.style[styleName] = trueStyleValue;
61			} else {
62				element.style[styleName] = falseStyleValue;
63			}
64		}
65	}
66
67	ElementCollection.prototype.toggleClass = function(attributeName, attributeValue, trueClass, falseClass) {
68		for(var i=0; i<this.elements.length; i++) {
69			var element = this.elements[i];
70			if (element.nodeType != 1) continue;
71			var value = element.getAttribute(attributeName);
72			if (value == null) continue;
73
74			if (value == attributeValue) {
75				element.className = trueClass;
76			} else {
77				element.className = falseClass;
78			}
79		}
80	}
81
82	function useShowAttribute(element) {
83		if (element == null) return;
84		var value = element.getAttribute("show");
85		if (value == null) return;
86		if (value == "true") {
87			element.style["display"] = "block";
88		} else {
89			element.style["display"] = "none";
90		}
91	}