Fixed typo in ldap stuff
[moodle.git] / lib / javascript-static.js
CommitLineData
47aa42e2 1// Miscellaneous core Javascript functions for Moodle
2
63d28811 3function popUpProperties(inobj) {
4 op = window.open();
5 op.document.open('text/plain');
6 for (objprop in inobj) {
7 op.document.write(objprop + ' => ' + inobj[objprop] + '\n');
8 }
9 op.document.close();
10}
11
12function fillmessagebox(text) {
13 document.form.message.value = text;
14}
15
16function copyrichtext(textname) {
17/// Legacy stub for old editor - to be removed soon
18 return true;
19}
20
21function checkall() {
22 void(d=document);
23 void(el=d.getElementsByTagName('INPUT'));
24 for(i=0;i<el.length;i++)
25 void(el[i].checked=1)
26}
27
03f9425f 28function checknone() {
29 void(d=document);
30 void(el=d.getElementsByTagName('INPUT'));
31 for(i=0;i<el.length;i++)
32 void(el[i].checked=0)
33}
34
63d28811 35function lockoptions(form, master, subitems) {
36 // subitems is an array of names of sub items
37 // requires that each item in subitems has a
38 // companion hidden item in the form with the
39 // same name but prefixed by "h"
40 if (eval("document."+form+"."+master+".checked")) {
41 for (i=0; i<subitems.length; i++) {
42 unlockoption(form, subitems[i]);
43 }
44 } else {
45 for (i=0; i<subitems.length; i++) {
46 lockoption(form, subitems[i]);
47 }
48 }
49 return(true);
50}
51
52function lockoption(form,item) {
53 eval("document."+form+"."+item+".disabled=true");/* IE thing */
54 eval("document."+form+".h"+item+".value=1");
55}
56
57function unlockoption(form,item) {
58 eval("document."+form+"."+item+".disabled=false");/* IE thing */
59 eval("document."+form+".h"+item+".value=0");
60}
7678e65c 61
62function submitFormById(id) {
63 var theform = document.getElementById(id);
64 if(!theform) {
65 return false;
66 }
67 if(theform.tagName != 'FORM') {
68 return false;
69 }
70 if(!theform.onsubmit || theform.onsubmit()) {
71 return theform.submit();
72 }
73}
363cb62c 74
75function select_all_in(elTagName, elId, elClass) {
76 var inputs = document.getElementsByTagName('INPUT');
77 inputs = filterByParent(inputs, function(el) {return findParentNode(el, elTagName, elId, elClass);});
78 for(var i = 0; i < inputs.length; ++i) {
79 if(inputs[i].type == 'checkbox') {
80 inputs[i].checked = 'checked';
81 }
82 }
83}
84
85function deselect_all_in(elTagName, elId, elClass) {
86 var inputs = document.getElementsByTagName('INPUT');
87 inputs = filterByParent(inputs, function(el) {return findParentNode(el, elTagName, elId, elClass);});
88 for(var i = 0; i < inputs.length; ++i) {
89 if(inputs[i].type == 'checkbox') {
90 inputs[i].checked = '';
91 }
92 }
93}
94
95function confirm_if(expr, message) {
96 if(!expr) {
97 return true;
98 }
99 return confirm(message);
100}
47aa42e2 101
102
103/*
104 findParentNode (start, elementName, elementClass, elementID)
105
106 Travels up the DOM hierarchy to find a parent element with the
107 specified tag name, class, and id. All conditions must be met,
108 but any can be ommitted. Returns the BODY element if no match
109 found.
110*/
111function findParentNode(el, elName, elClass, elId) {
112 while(el.nodeName != 'BODY') {
113 if(
114 (!elName || el.nodeName == elName) &&
115 (!elClass || el.className.indexOf(elClass) != -1) &&
116 (!elId || el.id == elId))
117 {
118 break;
119 }
120 el = el.parentNode;
121 }
122 return el;
123}
124
125/*
126 elementToggleHide (element, elementFinder)
127
128 If elementFinder is not provided, toggles the "hidden" class for the specified element.
129 If elementFinder is provided, then the "hidden" class will be toggled for the object
130 returned by the function call elementFinder(element).
131
132 If persistent == true, also sets a cookie for this.
133*/
134function elementToggleHide(el, persistent, elementFinder) {
135 if(!elementFinder) {
136 var obj = el;
137 }
138 else {
139 var obj = elementFinder(el);
140 }
141 if(obj.className.indexOf('hidden') == -1) {
142 obj.className += ' hidden';
143 var shown = 0;
144 }
145 else {
146 obj.className = obj.className.replace(new RegExp(' ?hidden'), '')
147 var shown = 1;
148 }
149
150 if(persistent == true) {
151 new cookie('hide:' + obj.id, 1, (shown ? -1 : 356), '/').set();
152 }
153}
154
155
156function elementCookieHide(id) {
157 var obj = document.getElementById(id);
158 var cook = new cookie('hide:' + id).read();
159 if(cook != null) {
160 elementToggleHide(obj, false);
161 }
162}
163
164function filterByParent(elCollection, parentFinder) {
165 var filteredCollection = [];
166 for(var i = 0; i < elCollection.length; ++i) {
167 var findParent = parentFinder(elCollection[i]);
168 if(findParent.nodeName != 'BODY') {
169 filteredCollection.push(elCollection[i]);
170 }
171 }
172 return filteredCollection;
173}
174
175/*
176
177Is there some specific reason for this function? I think pretty
178much every browser worth considering supports getElementById() [Jon]
179
180function getObj(id)
181{
182 if (document.getElementById)
183 {
184 this.obj = document.getElementById(id);
185 this.style = document.getElementById(id).style;
186 }
187 else if (document.all)
188 {
189 this.obj = document.all[id];
190 this.style = document.all[id].style;
191 }
192 else if (document.layers)
193 {
194 this.obj = document.layers[id];
195 this.style = document.layers[id];
196 }
197}
198*/