Making sure that restore views work in xhtml strict 1.0. MDL-7861.
[moodle.git] / lib / javascript-static.js
CommitLineData
47aa42e2 1// Miscellaneous core Javascript functions for Moodle
2
b48fd7b5 3function popupchecker(msg) {
4 var testwindow = window.open('itestwin.html', '', 'width=1,height=1,left=0,top=0,scrollbars=no');
5 if (testwindow == null)
6 {alert(msg);}
7 else {
8 testwindow.close();
9 }
10}
11
63d28811 12function popUpProperties(inobj) {
13 op = window.open();
14 op.document.open('text/plain');
15 for (objprop in inobj) {
16 op.document.write(objprop + ' => ' + inobj[objprop] + '\n');
17 }
18 op.document.close();
19}
20
21function fillmessagebox(text) {
22 document.form.message.value = text;
23}
24
25function copyrichtext(textname) {
26/// Legacy stub for old editor - to be removed soon
27 return true;
28}
29
30function checkall() {
31 void(d=document);
32 void(el=d.getElementsByTagName('INPUT'));
33 for(i=0;i<el.length;i++)
34 void(el[i].checked=1)
35}
36
03f9425f 37function checknone() {
38 void(d=document);
39 void(el=d.getElementsByTagName('INPUT'));
40 for(i=0;i<el.length;i++)
41 void(el[i].checked=0)
42}
43
63d28811 44function lockoptions(form, master, subitems) {
027d0485 45 // Subitems is an array of names of sub items.
46 // Optionally, each item in subitems may have a
63d28811 47 // companion hidden item in the form with the
027d0485 48 // same name but prefixed by "h".
63d28811 49 if (eval("document."+form+"."+master+".checked")) {
50 for (i=0; i<subitems.length; i++) {
51 unlockoption(form, subitems[i]);
52 }
53 } else {
54 for (i=0; i<subitems.length; i++) {
55 lockoption(form, subitems[i]);
56 }
57 }
58 return(true);
59}
60
50ef8eb9 61function lockoptionsall(formid) {
62 var lock=new Object();
63 var items=eval(formid+'items');
64 var form=document.forms[formid];
65 for (var master in items){
66 var subitems=items[master].dependents;
67 var thislock;
9c03bbaa 68 if (items[master].condition=='notchecked'){
50ef8eb9 69 thislock=!form.elements[master].checked;
9c03bbaa 70 } else if (items[master].condition=='checked'){
71 thislock=form.elements[master].checked;
e24b7f85 72 } else if (items[master].condition=='eq'){
73 thislock=(form.elements[master].value==items[master].value);
9c03bbaa 74 } else {
e24b7f85 75 thislock=(form.elements[master].value!=items[master].value);
76
50ef8eb9 77 }
78
79 for (var i=0; i<subitems.length; i++) {
80 if (lock[subitems[i]]!=null){
81 lock[subitems[i]] = thislock || lock[subitems[i]];
82 }else{
83 lock[subitems[i]] = thislock;
84 }
85 }
86
87 for (var item in lock) {
88 form.elements[item].disabled=lock[item];
89 }
90 }
91 return(true);
92}
93
d01a38cb 94function lockoptionsallsetup(formid) {
95 var items=eval(formid+'items');
96 var form=document.forms[formid];
19110c57 97 for (var i = 0; i < form.elements.length; i++){
98 var found=false;
99 var formelement = form.elements[i];
100 if (formelement.type=='reset'){
101 formelement.onclick = function(){this.form.reset();return lockoptionsall(this.form.getAttribute('id'));};
102 formelement.onblur = function(){this.form.reset();return lockoptionsall(this.form.getAttribute('id'));};
103 formelement.onchange = function(){this.form.reset();return lockoptionsall(this.form.getAttribute('id'));};
104 } else {
105 for (var master in items){
106 if (master==formelement.name){
107 formelement.onclick = function(){return lockoptionsall(this.form.getAttribute('id'));};
108 formelement.onblur = function(){return lockoptionsall(this.form.getAttribute('id'));};
109 formelement.onchange = function(){return lockoptionsall(this.form.getAttribute('id'));};
110 break;
111 }
112 }
113 }
114 if (found){
115 }
d01a38cb 116 }
19110c57 117
d01a38cb 118 return lockoptionsall(formid);
119}
50ef8eb9 120
63d28811 121function lockoption(form,item) {
122 eval("document."+form+"."+item+".disabled=true");/* IE thing */
027d0485 123 if(document.forms[form].elements['h'+item]) {
124 eval("document."+form+".h"+item+".value=1");
125 }
63d28811 126}
127
128function unlockoption(form,item) {
129 eval("document."+form+"."+item+".disabled=false");/* IE thing */
027d0485 130 if(document.forms[form].elements['h'+item]) {
131 eval("document."+form+".h"+item+".value=0");
132 }
63d28811 133}
7678e65c 134
135function submitFormById(id) {
136 var theform = document.getElementById(id);
137 if(!theform) {
138 return false;
139 }
140 if(theform.tagName != 'FORM') {
141 return false;
142 }
143 if(!theform.onsubmit || theform.onsubmit()) {
144 return theform.submit();
145 }
146}
363cb62c 147
8ceb09e0 148function select_all_in(elTagName, elClass, elId) {
363cb62c 149 var inputs = document.getElementsByTagName('INPUT');
8ceb09e0 150 inputs = filterByParent(inputs, function(el) {return findParentNode(el, elTagName, elClass, elId);});
363cb62c 151 for(var i = 0; i < inputs.length; ++i) {
bee40515 152 if(inputs[i].type == 'checkbox' || inputs[i].type == 'radio') {
363cb62c 153 inputs[i].checked = 'checked';
154 }
155 }
156}
157
8ceb09e0 158function deselect_all_in(elTagName, elClass, elId) {
363cb62c 159 var inputs = document.getElementsByTagName('INPUT');
8ceb09e0 160 inputs = filterByParent(inputs, function(el) {return findParentNode(el, elTagName, elClass, elId);});
363cb62c 161 for(var i = 0; i < inputs.length; ++i) {
bee40515 162 if(inputs[i].type == 'checkbox' || inputs[i].type == 'radio') {
363cb62c 163 inputs[i].checked = '';
164 }
165 }
166}
167
168function confirm_if(expr, message) {
169 if(!expr) {
170 return true;
171 }
172 return confirm(message);
173}
47aa42e2 174
175
176/*
177 findParentNode (start, elementName, elementClass, elementID)
50ef8eb9 178
47aa42e2 179 Travels up the DOM hierarchy to find a parent element with the
180 specified tag name, class, and id. All conditions must be met,
181 but any can be ommitted. Returns the BODY element if no match
182 found.
183*/
184function findParentNode(el, elName, elClass, elId) {
185 while(el.nodeName != 'BODY') {
186 if(
187 (!elName || el.nodeName == elName) &&
188 (!elClass || el.className.indexOf(elClass) != -1) &&
189 (!elId || el.id == elId))
190 {
191 break;
192 }
193 el = el.parentNode;
194 }
195 return el;
196}
19194f82 197/*
198 findChildNode (start, elementName, elementClass, elementID)
199
200 Travels down the DOM hierarchy to find all child elements with the
201 specified tag name, class, and id. All conditions must be met,
202 but any can be ommitted.
203*/
204function findChildNodes(start, tagName, elementClass, elementID, elementName) {
205 var children = new Array();
206 for (var childIndex in start.childNodes) {
207 var child = start.childNodes[childIndex];
208 if(
209 (child.nodeType == 1) &&//element node type
210 (!tagName || child.nodeName == tagName) &&
211 (!elementClass || child.className.indexOf(elementClass) != -1) &&
212 (!elementID || child.id == elementID) &&
213 (!elementName || child.name == elementName))
214 {
215 children = children.concat(child);
216 }
217 children = children.concat(findChildNodes(child, tagName, elementClass, elementID, elementName));
218 }
219 return children;
220}
221/*
222 elementSetHide (elements, hide)
223
224 Adds or removes the "hide" class for the specified elements depending on boolean hide.
225*/
226function elementShowAdvanced(elements, show) {
227 for (var elementIndex in elements){
228 element = elements[elementIndex];
229 element.className = element.className.replace(new RegExp(' ?hide'), '')
230 if(!show) {
231 element.className += ' hide';
232 }
233 }
234}
235
236function showAdvancedOnClick(button, hidetext, showtext){
237 var toSet=findChildNodes(button.form, null, 'advanced');
238 var buttontext = '';
239 if (button.form.elements['mform_showadvanced_last'].value == '0') {
240 elementShowAdvanced(toSet, true);
241 buttontext = hidetext;
242 button.form.elements['mform_showadvanced_last'].value = '1';
243 } else {
244 elementShowAdvanced(toSet, false);
245 buttontext = showtext;
246 button.form.elements['mform_showadvanced_last'].value = '0';
247 }
248 var formelements = button.form.elements;
249 for (var i in formelements){
250 if (formelements[i].name=='mform_showadvanced'){
251 formelements[i].value = buttontext;
252 }
253 }
254 //never submit the form if js is enabled.
255 return false;
256}
47aa42e2 257
258/*
259 elementToggleHide (element, elementFinder)
260
261 If elementFinder is not provided, toggles the "hidden" class for the specified element.
262 If elementFinder is provided, then the "hidden" class will be toggled for the object
263 returned by the function call elementFinder(element).
264
265 If persistent == true, also sets a cookie for this.
266*/
19194f82 267function elementToggleHide(el, persistent, elementFinder) {
47aa42e2 268 if(!elementFinder) {
269 var obj = el;
270 }
271 else {
272 var obj = elementFinder(el);
273 }
274 if(obj.className.indexOf('hidden') == -1) {
275 obj.className += ' hidden';
19194f82 276 var shown = 0;
47aa42e2 277 }
278 else {
19194f82 279 obj.className = obj.className.replace(new RegExp(' ?hidden'), '')
280 var shown = 1;
47aa42e2 281 }
19194f82 282
47aa42e2 283 if(persistent == true) {
284 new cookie('hide:' + obj.id, 1, (shown ? -1 : 356), '/').set();
285 }
286}
287
288
289function elementCookieHide(id) {
290 var obj = document.getElementById(id);
291 var cook = new cookie('hide:' + id).read();
292 if(cook != null) {
293 elementToggleHide(obj, false);
294 }
295}
296
297function filterByParent(elCollection, parentFinder) {
298 var filteredCollection = [];
299 for(var i = 0; i < elCollection.length; ++i) {
300 var findParent = parentFinder(elCollection[i]);
301 if(findParent.nodeName != 'BODY') {
302 filteredCollection.push(elCollection[i]);
303 }
304 }
305 return filteredCollection;
306}
307
7979105c 308/*
309 All this is here just so that IE gets to handle oversized blocks
310 in a visually pleasing manner. It does a browser detect. So sue me.
311*/
312
313function fix_column_widths() {
314 var agt = navigator.userAgent.toLowerCase();
315 if ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1)) {
316 fix_column_width('left-column');
317 fix_column_width('right-column');
318 }
319}
320
321function fix_column_width(colName) {
322 if(column = document.getElementById(colName)) {
323 if(!column.offsetWidth) {
324 setTimeout("fix_column_width('" + colName + "')", 20);
325 return;
326 }
327
328 var width = 0;
329 var nodes = column.childNodes;
330
331 for(i = 0; i < nodes.length; ++i) {
332 if(nodes[i].className.indexOf("sideblock") != -1 ) {
333 if(width < nodes[i].offsetWidth) {
334 width = nodes[i].offsetWidth;
335 }
336 }
337 }
338
339 for(i = 0; i < nodes.length; ++i) {
340 if(nodes[i].className.indexOf("sideblock") != -1 ) {
341 nodes[i].style.width = width + 'px';
342 }
343 }
344 }
345}
d13c5938 346
347
348/*
349 Insert myValue at current cursor position
350*/
351function insertAtCursor(myField, myValue) {
352 // IE support
353 if (document.selection) {
354 myField.focus();
355 sel = document.selection.createRange();
356 sel.text = myValue;
357 }
358 // Mozilla/Netscape support
359 else if (myField.selectionStart || myField.selectionStart == '0') {
360 var startPos = myField.selectionStart;
361 var endPos = myField.selectionEnd;
362 myField.value = myField.value.substring(0, startPos)
363 + myValue + myField.value.substring(endPos, myField.value.length);
364 } else {
365 myField.value += myValue;
366 }
367}