MDL-10234
[moodle-linuxchix.git] / lib / javascript-static.js
bloba5f3194b0f27d21054378dcc3b628d67c10a5535
1 // Miscellaneous core Javascript functions for Moodle
3 function 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();
13 function popUpProperties(inobj) {
14 /// Legacy function
15 var op = window.open();
16 op.document.open('text/plain');
17 for (objprop in inobj) {
18 op.document.write(objprop + ' => ' + inobj[objprop] + '\n');
20 op.document.close();
23 function fillmessagebox(text) {
24 /// Legacy function
25 document.form.message.value = text;
28 function copyrichtext(textname) {
29 /// Legacy stub for old editor - to be removed soon
30 return true;
34 function checkall() {
35 var el = document.getElementsByTagName('input');
36 for(var i=0; i<el.length; i++) {
37 if(el[i].type == 'checkbox') {
38 el[i].checked = true;
43 function checknone() {
44 var el = document.getElementsByTagName('input');
45 for(var i=0; i<el.length; i++) {
46 if(el[i].type == 'checkbox') {
47 el[i].checked = false;
52 function lockoptions(formid, master, subitems) {
53 // Subitems is an array of names of sub items.
54 // Optionally, each item in subitems may have a
55 // companion hidden item in the form with the
56 // same name but prefixed by "h".
57 var form = document.forms[formid];
59 if (eval("form."+master+".checked")) {
60 for (i=0; i<subitems.length; i++) {
61 unlockoption(form, subitems[i]);
63 } else {
64 for (i=0; i<subitems.length; i++) {
65 lockoption(form, subitems[i]);
68 return(true);
71 function lockoption(form,item) {
72 eval("form."+item+".disabled=true");/* IE thing */
73 if(form.elements['h'+item]) {
74 eval("form.h"+item+".value=1");
78 function unlockoption(form,item) {
79 eval("form."+item+".disabled=false");/* IE thing */
80 if(form.elements['h'+item]) {
81 eval("form.h"+item+".value=0");
86 function lockoptionsall(formid) {
87 var form = document.forms[formid];
88 var dependons = eval(formid+'items');
89 var tolock = Array();
90 for (var dependon in dependons) {
91 var master = form[dependon];
92 for (var condition in dependons[dependon]) {
93 for (var value in dependons[dependon][condition]) {
94 var lock;
95 switch (condition) {
96 case 'notchecked':
97 lock = !master.checked; break;
98 case 'checked':
99 lock = master.checked; break;
100 case 'noitemselected':
101 lock = master.selectedIndex==-1; break;
102 case 'eq':
103 lock = master.value==value; break;
104 default:
105 lock = master.value!=value; break;
107 for (var ei in dependons[dependon][condition][value]) {
108 var eltolock = dependons[dependon][condition][value][ei];
109 if (tolock[eltolock] != null){
110 tolock[eltolock] =
111 lock || tolock[eltolock];
112 } else {
113 tolock[eltolock] = lock;
119 for (var el in tolock){
120 var formelement = form[el];
121 formelement.disabled = tolock[el];
123 return true;
126 function lockoptionsallsetup(formid) {
127 var form = document.forms[formid];
128 var dependons = eval(formid+'items');
129 for (var dependon in dependons) {
130 var master = form[dependon];
131 master.onclick = function() {return lockoptionsall(this.form.getAttribute('id'));};
132 master.onblur = function() {return lockoptionsall(this.form.getAttribute('id'));};
133 master.onchange = function() {return lockoptionsall(this.form.getAttribute('id'));};
135 for (var i = 0; i < form.elements.length; i++){
136 var formelement = form.elements[i];
137 if (formelement.type=='reset') {
138 formelement.onclick = function() {this.form.reset();return lockoptionsall(this.form.getAttribute('id'));};
139 formelement.onblur = function() {this.form.reset();return lockoptionsall(this.form.getAttribute('id'));};
140 formelement.onchange = function() {this.form.reset();return lockoptionsall(this.form.getAttribute('id'));};
143 return lockoptionsall(formid);
147 function submitFormById(id) {
148 var theform = document.getElementById(id);
149 if(!theform) {
150 return false;
152 if(theform.tagName.toLowerCase() != 'form') {
153 return false;
155 if(!theform.onsubmit || theform.onsubmit()) {
156 return theform.submit();
160 function select_all_in(elTagName, elClass, elId) {
161 var inputs = document.getElementsByTagName('input');
162 inputs = filterByParent(inputs, function(el) {return findParentNode(el, elTagName, elClass, elId);});
163 for(var i = 0; i < inputs.length; ++i) {
164 if(inputs[i].type == 'checkbox' || inputs[i].type == 'radio') {
165 inputs[i].checked = 'checked';
170 function deselect_all_in(elTagName, elClass, elId) {
171 var inputs = document.getElementsByTagName('INPUT');
172 inputs = filterByParent(inputs, function(el) {return findParentNode(el, elTagName, elClass, elId);});
173 for(var i = 0; i < inputs.length; ++i) {
174 if(inputs[i].type == 'checkbox' || inputs[i].type == 'radio') {
175 inputs[i].checked = '';
180 function confirm_if(expr, message) {
181 if(!expr) {
182 return true;
184 return confirm(message);
189 findParentNode (start, elementName, elementClass, elementID)
191 Travels up the DOM hierarchy to find a parent element with the
192 specified tag name, class, and id. All conditions must be met,
193 but any can be ommitted. Returns the BODY element if no match
194 found.
196 function findParentNode(el, elName, elClass, elId) {
197 while(el.nodeName.toUpperCase() != 'BODY') {
199 (!elName || el.nodeName.toUpperCase() == elName) &&
200 (!elClass || el.className.indexOf(elClass) != -1) &&
201 (!elId || el.id == elId))
203 break;
205 el = el.parentNode;
207 return el;
210 findChildNode (start, elementName, elementClass, elementID)
212 Travels down the DOM hierarchy to find all child elements with the
213 specified tag name, class, and id. All conditions must be met,
214 but any can be ommitted.
215 Doesn't examine children of matches.
217 function findChildNodes(start, tagName, elementClass, elementID, elementName) {
218 var children = new Array();
219 for (var i = 0; i < start.childNodes.length; i++) {
220 var classfound = false;
221 var child = start.childNodes[i];
222 if((child.nodeType == 1) &&//element node type
223 (elementClass && (typeof(child.className)=='string'))){
224 var childClasses = child.className.split(/\s+/);
225 for (var childClassIndex in childClasses){
226 if (childClasses[childClassIndex]==elementClass){
227 classfound = true;
228 break;
232 if(child.nodeType == 1) { //element node type
233 if ( (!tagName || child.nodeName == tagName) &&
234 (!elementClass || classfound)&&
235 (!elementID || child.id == elementID) &&
236 (!elementName || child.name == elementName))
238 children = children.concat(child);
239 } else {
240 children = children.concat(findChildNodes(child, tagName, elementClass, elementID, elementName));
244 return children;
247 elementSetHide (elements, hide)
249 Adds or removes the "hide" class for the specified elements depending on boolean hide.
251 function elementShowAdvanced(elements, show) {
252 for (var elementIndex in elements){
253 element = elements[elementIndex];
254 element.className = element.className.replace(new RegExp(' ?hide'), '')
255 if(!show) {
256 element.className += ' hide';
261 function showAdvancedOnClick(button, hidetext, showtext){
262 var toSet=findChildNodes(button.form, null, 'advanced');
263 var buttontext = '';
264 if (button.form.elements['mform_showadvanced_last'].value == '0' || button.form.elements['mform_showadvanced_last'].value == '' ) {
265 elementShowAdvanced(toSet, true);
266 buttontext = hidetext;
267 button.form.elements['mform_showadvanced_last'].value = '1';
268 } else {
269 elementShowAdvanced(toSet, false);
270 buttontext = showtext;
271 button.form.elements['mform_showadvanced_last'].value = '0';
273 var formelements = button.form.elements;
274 for (var i in formelements){
275 if (formelements[i] && formelements[i].name && (formelements[i].name=='mform_showadvanced')){
276 formelements[i].value = buttontext;
279 //never submit the form if js is enabled.
280 return false;
283 function unmaskPassword(id) {
284 var pw = document.getElementById(id);
285 var chb = document.getElementById(id+'unmask');
287 try {
288 // first try IE way - it can not set name attribute later
289 if (chb.checked) {
290 var newpw = document.createElement('<input type="text" name="'+pw.name+'">');
291 } else {
292 var newpw = document.createElement('<input type="password" name="'+pw.name+'">');
294 newpw.attributes['class'].nodeValue = pw.attributes['class'].nodeValue;
295 } catch (e) {
296 var newpw = document.createElement('input');
297 newpw.setAttribute('name', pw.name);
298 if (chb.checked) {
299 newpw.setAttribute('type', 'text');
300 } else {
301 newpw.setAttribute('type', 'password');
303 newpw.setAttribute('class', pw.getAttribute('class'));
305 newpw.id = pw.id;
306 newpw.size = pw.size;
307 newpw.onblur = pw.onblur;
308 newpw.onchange = pw.onchange;
309 newpw.value = pw.value;
310 pw.parentNode.replaceChild(newpw, pw);
314 elementToggleHide (element, elementFinder)
316 If elementFinder is not provided, toggles the "hidden" class for the specified element.
317 If elementFinder is provided, then the "hidden" class will be toggled for the object
318 returned by the function call elementFinder(element).
320 If persistent == true, also sets a cookie for this.
322 function elementToggleHide(el, persistent, elementFinder) {
323 if(!elementFinder) {
324 var obj = el;
326 else {
327 var obj = elementFinder(el);
329 if(obj.className.indexOf('hidden') == -1) {
330 obj.className += ' hidden';
331 var shown = 0;
333 else {
334 obj.className = obj.className.replace(new RegExp(' ?hidden'), '')
335 var shown = 1;
338 if(persistent == true) {
339 new cookie('hide:' + obj.id, 1, (shown ? -1 : 356), '/').set();
344 function elementCookieHide(id) {
345 var obj = document.getElementById(id);
346 var cook = new cookie('hide:' + id).read();
347 if(cook != null) {
348 elementToggleHide(obj, false);
352 function filterByParent(elCollection, parentFinder) {
353 var filteredCollection = [];
354 for(var i = 0; i < elCollection.length; ++i) {
355 var findParent = parentFinder(elCollection[i]);
356 if(findParent.nodeName != 'BODY') {
357 filteredCollection.push(elCollection[i]);
360 return filteredCollection;
364 All this is here just so that IE gets to handle oversized blocks
365 in a visually pleasing manner. It does a browser detect. So sue me.
368 function fix_column_widths() {
369 var agt = navigator.userAgent.toLowerCase();
370 if ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1)) {
371 fix_column_width('left-column');
372 fix_column_width('right-column');
376 function fix_column_width(colName) {
377 if(column = document.getElementById(colName)) {
378 if(!column.offsetWidth) {
379 setTimeout("fix_column_width('" + colName + "')", 20);
380 return;
383 var width = 0;
384 var nodes = column.childNodes;
386 for(i = 0; i < nodes.length; ++i) {
387 if(nodes[i].className.indexOf("sideblock") != -1 ) {
388 if(width < nodes[i].offsetWidth) {
389 width = nodes[i].offsetWidth;
394 for(i = 0; i < nodes.length; ++i) {
395 if(nodes[i].className.indexOf("sideblock") != -1 ) {
396 nodes[i].style.width = width + 'px';
404 Insert myValue at current cursor position
406 function insertAtCursor(myField, myValue) {
407 // IE support
408 if (document.selection) {
409 myField.focus();
410 sel = document.selection.createRange();
411 sel.text = myValue;
413 // Mozilla/Netscape support
414 else if (myField.selectionStart || myField.selectionStart == '0') {
415 var startPos = myField.selectionStart;
416 var endPos = myField.selectionEnd;
417 myField.value = myField.value.substring(0, startPos)
418 + myValue + myField.value.substring(endPos, myField.value.length);
419 } else {
420 myField.value += myValue;
426 Call instead of setting window.onload directly or setting body onload=.
427 Adds your function to a chain of functions rather than overwriting anything
428 that exists.
430 function addonload(fn) {
431 var oldhandler=window.onload;
432 window.onload=function() {
433 if(oldhandler) oldhandler();
434 fn();