Fix for missing "reply" links for guests doh! MDL-7393
[moodle.git] / lib / javascript-static.js
blobe63a3823564c660e58667606f15f6f6983118929
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();
9     }
12 function 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();
21 function fillmessagebox(text) {
22   document.form.message.value = text;
25 function copyrichtext(textname) {
26 /// Legacy stub for old editor - to be removed soon
27   return true;
30 function 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)
37 function 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)
44 function lockoptions(form, master, subitems) {
45   // Subitems is an array of names of sub items.
46   // Optionally, each item in subitems may have a
47   // companion hidden item in the form with the
48   // same name but prefixed by "h".
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);
61 function lockoption(form,item) {
62   eval("document."+form+"."+item+".disabled=true");/* IE thing */
63   if(document.forms[form].elements['h'+item]) {
64     eval("document."+form+".h"+item+".value=1");
65   }
68 function unlockoption(form,item) {
69   eval("document."+form+"."+item+".disabled=false");/* IE thing */
70   if(document.forms[form].elements['h'+item]) {
71     eval("document."+form+".h"+item+".value=0");
72   }
75 function submitFormById(id) {
76     var theform = document.getElementById(id);
77     if(!theform) {
78         return false;
79     }
80     if(theform.tagName != 'FORM') {
81         return false;
82     }
83     if(!theform.onsubmit || theform.onsubmit()) {
84         return theform.submit();
85     }
88 function select_all_in(elTagName, elClass, elId) {
89     var inputs = document.getElementsByTagName('INPUT');
90     inputs = filterByParent(inputs, function(el) {return findParentNode(el, elTagName, elClass, elId);});
91     for(var i = 0; i < inputs.length; ++i) {
92         if(inputs[i].type == 'checkbox' || inputs[i].type == 'radio') {
93             inputs[i].checked = 'checked';
94         }
95     }
98 function deselect_all_in(elTagName, elClass, elId) {
99     var inputs = document.getElementsByTagName('INPUT');
100     inputs = filterByParent(inputs, function(el) {return findParentNode(el, elTagName, elClass, elId);});
101     for(var i = 0; i < inputs.length; ++i) {
102         if(inputs[i].type == 'checkbox' || inputs[i].type == 'radio') {
103             inputs[i].checked = '';
104         }
105     }
108 function confirm_if(expr, message) {
109     if(!expr) {
110         return true;
111     }
112     return confirm(message);
117     findParentNode (start, elementName, elementClass, elementID)
118     
119     Travels up the DOM hierarchy to find a parent element with the
120     specified tag name, class, and id. All conditions must be met,
121     but any can be ommitted. Returns the BODY element if no match
122     found.
124 function findParentNode(el, elName, elClass, elId) {
125     while(el.nodeName != 'BODY') {
126         if(
127             (!elName || el.nodeName == elName) &&
128             (!elClass || el.className.indexOf(elClass) != -1) &&
129             (!elId || el.id == elId))
130         {
131             break;
132         }
133         el = el.parentNode;
134     }
135     return el;
139     elementToggleHide (element, elementFinder)
141     If elementFinder is not provided, toggles the "hidden" class for the specified element.
142     If elementFinder is provided, then the "hidden" class will be toggled for the object
143     returned by the function call elementFinder(element).
145     If persistent == true, also sets a cookie for this.
147 function elementToggleHide(el, persistent, elementFinder) {
148     if(!elementFinder) {
149         var obj = el;
150     }
151     else {
152         var obj = elementFinder(el);
153     }
154     if(obj.className.indexOf('hidden') == -1) {
155         obj.className += ' hidden';
156         var shown = 0;
157     }
158     else {
159         obj.className = obj.className.replace(new RegExp(' ?hidden'), '')
160         var shown = 1;
161     }
163     if(persistent == true) {
164         new cookie('hide:' + obj.id, 1, (shown ? -1 : 356), '/').set();
165     }
169 function elementCookieHide(id) {
170     var obj  = document.getElementById(id);
171     var cook = new cookie('hide:' + id).read();
172     if(cook != null) {
173         elementToggleHide(obj, false);
174     }
177 function filterByParent(elCollection, parentFinder) {
178     var filteredCollection = [];
179     for(var i = 0; i < elCollection.length; ++i) {
180         var findParent = parentFinder(elCollection[i]);
181         if(findParent.nodeName != 'BODY') {
182             filteredCollection.push(elCollection[i]);
183         }
184     }
185     return filteredCollection;
189     All this is here just so that IE gets to handle oversized blocks
190     in a visually pleasing manner. It does a browser detect. So sue me.
193 function fix_column_widths() {
194     var agt = navigator.userAgent.toLowerCase();
195     if ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1)) {
196         fix_column_width('left-column');
197         fix_column_width('right-column');
198     }
201 function fix_column_width(colName) {
202     if(column = document.getElementById(colName)) {
203         if(!column.offsetWidth) {
204             setTimeout("fix_column_width('" + colName + "')", 20);
205             return;
206         }
208         var width = 0;
209         var nodes = column.childNodes;
211         for(i = 0; i < nodes.length; ++i) {
212             if(nodes[i].className.indexOf("sideblock") != -1 ) {
213                 if(width < nodes[i].offsetWidth) {
214                     width = nodes[i].offsetWidth;
215                 }
216             }
217         }
219         for(i = 0; i < nodes.length; ++i) {
220             if(nodes[i].className.indexOf("sideblock") != -1 ) {
221                 nodes[i].style.width = width + 'px';
222             }
223         }
224     }
229         Insert myValue at current cursor position
231 function insertAtCursor(myField, myValue) {
232         // IE support
233         if (document.selection) {
234                 myField.focus();
235                 sel = document.selection.createRange();
236                 sel.text = myValue;
237         }
238         // Mozilla/Netscape support
239         else if (myField.selectionStart || myField.selectionStart == '0') {
240                 var startPos = myField.selectionStart;
241                 var endPos = myField.selectionEnd;
242                 myField.value = myField.value.substring(0, startPos)
243                         + myValue + myField.value.substring(endPos, myField.value.length);
244         } else {
245                 myField.value += myValue;
246         }