MDL-9227 Inconsistent HTTPS for profile edit page; merged from MOODLE_17_STABLE
[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
d2ce367f 12/*
63d28811 13function popUpProperties(inobj) {
d2ce367f 14/// Legacy function
15 var op = window.open();
63d28811 16 op.document.open('text/plain');
17 for (objprop in inobj) {
18 op.document.write(objprop + ' => ' + inobj[objprop] + '\n');
19 }
20 op.document.close();
21}
22
23function fillmessagebox(text) {
d2ce367f 24/// Legacy function
63d28811 25 document.form.message.value = text;
26}
27
28function copyrichtext(textname) {
29/// Legacy stub for old editor - to be removed soon
30 return true;
31}
d2ce367f 32*/
63d28811 33
34function checkall() {
d2ce367f 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;
39 }
40 }
63d28811 41}
42
03f9425f 43function checknone() {
d2ce367f 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;
48 }
49 }
03f9425f 50}
51
d2ce367f 52function lockoptions(formid, master, subitems) {
027d0485 53 // Subitems is an array of names of sub items.
54 // Optionally, each item in subitems may have a
63d28811 55 // companion hidden item in the form with the
027d0485 56 // same name but prefixed by "h".
d2ce367f 57 var form = document.forms[formid];
58
59 if (eval("form."+master+".checked")) {
63d28811 60 for (i=0; i<subitems.length; i++) {
61 unlockoption(form, subitems[i]);
62 }
63 } else {
64 for (i=0; i<subitems.length; i++) {
65 lockoption(form, subitems[i]);
66 }
67 }
68 return(true);
69}
70
f07b9627 71function lockoption(form,item) {
d2ce367f 72 eval("form."+item+".disabled=true");/* IE thing */
73 if(form.elements['h'+item]) {
74 eval("form.h"+item+".value=1");
f07b9627 75 }
76}
77
78function unlockoption(form,item) {
d2ce367f 79 eval("form."+item+".disabled=false");/* IE thing */
80 if(form.elements['h'+item]) {
81 eval("form.h"+item+".value=0");
f07b9627 82 }
83}
dd07bbac 84
85
50ef8eb9 86function lockoptionsall(formid) {
dd07bbac 87 var form = document.forms[formid];
88 var dependons = eval(formid+'items');
632b88d5 89 var tolock = Array();
dd07bbac 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;
106 }
107 for (var ei in dependons[dependon][condition][value]) {
632b88d5 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;
114 }
dd07bbac 115 }
116 }
50ef8eb9 117 }
dd07bbac 118 }
632b88d5 119 for (var el in tolock){
120 var formelement = form[el];
121 formelement.disabled = tolock[el];
122 }
dd07bbac 123 return true;
50ef8eb9 124}
125
d01a38cb 126function lockoptionsallsetup(formid) {
dd07bbac 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'));};
134 }
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'));};
141 }
142 }
143 return lockoptionsall(formid);
d01a38cb 144}
50ef8eb9 145
7678e65c 146
147function submitFormById(id) {
148 var theform = document.getElementById(id);
149 if(!theform) {
150 return false;
151 }
bb006c95 152 if(theform.tagName.toLowerCase() != 'form') {
7678e65c 153 return false;
154 }
155 if(!theform.onsubmit || theform.onsubmit()) {
156 return theform.submit();
157 }
158}
363cb62c 159
8ceb09e0 160function select_all_in(elTagName, elClass, elId) {
d2ce367f 161 var inputs = document.getElementsByTagName('input');
8ceb09e0 162 inputs = filterByParent(inputs, function(el) {return findParentNode(el, elTagName, elClass, elId);});
363cb62c 163 for(var i = 0; i < inputs.length; ++i) {
bee40515 164 if(inputs[i].type == 'checkbox' || inputs[i].type == 'radio') {
363cb62c 165 inputs[i].checked = 'checked';
166 }
167 }
168}
169
8ceb09e0 170function deselect_all_in(elTagName, elClass, elId) {
363cb62c 171 var inputs = document.getElementsByTagName('INPUT');
8ceb09e0 172 inputs = filterByParent(inputs, function(el) {return findParentNode(el, elTagName, elClass, elId);});
363cb62c 173 for(var i = 0; i < inputs.length; ++i) {
bee40515 174 if(inputs[i].type == 'checkbox' || inputs[i].type == 'radio') {
363cb62c 175 inputs[i].checked = '';
176 }
177 }
178}
179
180function confirm_if(expr, message) {
181 if(!expr) {
182 return true;
183 }
184 return confirm(message);
185}
47aa42e2 186
187
188/*
189 findParentNode (start, elementName, elementClass, elementID)
50ef8eb9 190
47aa42e2 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.
195*/
196function findParentNode(el, elName, elClass, elId) {
ef4d42fd 197 while(el.nodeName.toUpperCase() != 'BODY') {
47aa42e2 198 if(
ef4d42fd 199 (!elName || el.nodeName.toUpperCase() == elName) &&
47aa42e2 200 (!elClass || el.className.indexOf(elClass) != -1) &&
201 (!elId || el.id == elId))
202 {
203 break;
204 }
205 el = el.parentNode;
206 }
207 return el;
208}
19194f82 209/*
210 findChildNode (start, elementName, elementClass, elementID)
211
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.
a23f0aaf 215 Doesn't examine children of matches.
19194f82 216*/
217function findChildNodes(start, tagName, elementClass, elementID, elementName) {
218 var children = new Array();
83c9a8a2 219 for (var i = 0; i < start.childNodes.length; i++) {
a23f0aaf 220 var classfound = false;
83c9a8a2 221 var child = start.childNodes[i];
a23f0aaf 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;
229 }
230 }
231 }
f07b9627 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))
237 {
238 children = children.concat(child);
239 } else {
240 children = children.concat(findChildNodes(child, tagName, elementClass, elementID, elementName));
241 }
19194f82 242 }
19194f82 243 }
244 return children;
245}
246/*
247 elementSetHide (elements, hide)
248
249 Adds or removes the "hide" class for the specified elements depending on boolean hide.
250*/
251function 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';
257 }
258 }
259}
260
261function showAdvancedOnClick(button, hidetext, showtext){
262 var toSet=findChildNodes(button.form, null, 'advanced');
263 var buttontext = '';
74d15d35 264 if (button.form.elements['mform_showadvanced_last'].value == '0' || button.form.elements['mform_showadvanced_last'].value == '' ) {
19194f82 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';
272 }
273 var formelements = button.form.elements;
274 for (var i in formelements){
9bf930bd 275 if (formelements[i] && formelements[i].name && (formelements[i].name=='mform_showadvanced')){
19194f82 276 formelements[i].value = buttontext;
277 }
278 }
279 //never submit the form if js is enabled.
280 return false;
281}
47aa42e2 282
283/*
284 elementToggleHide (element, elementFinder)
285
286 If elementFinder is not provided, toggles the "hidden" class for the specified element.
287 If elementFinder is provided, then the "hidden" class will be toggled for the object
288 returned by the function call elementFinder(element).
289
290 If persistent == true, also sets a cookie for this.
291*/
19194f82 292function elementToggleHide(el, persistent, elementFinder) {
47aa42e2 293 if(!elementFinder) {
294 var obj = el;
295 }
296 else {
297 var obj = elementFinder(el);
298 }
299 if(obj.className.indexOf('hidden') == -1) {
300 obj.className += ' hidden';
19194f82 301 var shown = 0;
47aa42e2 302 }
303 else {
19194f82 304 obj.className = obj.className.replace(new RegExp(' ?hidden'), '')
305 var shown = 1;
47aa42e2 306 }
19194f82 307
47aa42e2 308 if(persistent == true) {
309 new cookie('hide:' + obj.id, 1, (shown ? -1 : 356), '/').set();
310 }
311}
312
313
314function elementCookieHide(id) {
315 var obj = document.getElementById(id);
316 var cook = new cookie('hide:' + id).read();
317 if(cook != null) {
318 elementToggleHide(obj, false);
319 }
320}
321
322function filterByParent(elCollection, parentFinder) {
323 var filteredCollection = [];
324 for(var i = 0; i < elCollection.length; ++i) {
325 var findParent = parentFinder(elCollection[i]);
326 if(findParent.nodeName != 'BODY') {
327 filteredCollection.push(elCollection[i]);
328 }
329 }
330 return filteredCollection;
331}
332
7979105c 333/*
334 All this is here just so that IE gets to handle oversized blocks
335 in a visually pleasing manner. It does a browser detect. So sue me.
336*/
337
338function fix_column_widths() {
339 var agt = navigator.userAgent.toLowerCase();
340 if ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1)) {
341 fix_column_width('left-column');
342 fix_column_width('right-column');
343 }
344}
345
346function fix_column_width(colName) {
347 if(column = document.getElementById(colName)) {
348 if(!column.offsetWidth) {
349 setTimeout("fix_column_width('" + colName + "')", 20);
350 return;
351 }
352
353 var width = 0;
354 var nodes = column.childNodes;
355
356 for(i = 0; i < nodes.length; ++i) {
357 if(nodes[i].className.indexOf("sideblock") != -1 ) {
358 if(width < nodes[i].offsetWidth) {
359 width = nodes[i].offsetWidth;
360 }
361 }
362 }
363
364 for(i = 0; i < nodes.length; ++i) {
365 if(nodes[i].className.indexOf("sideblock") != -1 ) {
366 nodes[i].style.width = width + 'px';
367 }
368 }
369 }
370}
d13c5938 371
372
373/*
374 Insert myValue at current cursor position
375*/
376function insertAtCursor(myField, myValue) {
377 // IE support
378 if (document.selection) {
379 myField.focus();
380 sel = document.selection.createRange();
381 sel.text = myValue;
382 }
383 // Mozilla/Netscape support
384 else if (myField.selectionStart || myField.selectionStart == '0') {
385 var startPos = myField.selectionStart;
386 var endPos = myField.selectionEnd;
387 myField.value = myField.value.substring(0, startPos)
388 + myValue + myField.value.substring(endPos, myField.value.length);
389 } else {
390 myField.value += myValue;
391 }
392}