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)
12 function popUpProperties(inobj) {
14 op.document.open('text/plain');
15 for (objprop in inobj) {
16 op.document.write(objprop + ' => ' + inobj[objprop] + '\n');
21 function fillmessagebox(text) {
22 document.form.message.value = text;
25 function copyrichtext(textname) {
26 /// Legacy stub for old editor - to be removed soon
32 void(el=d.getElementsByTagName('INPUT'));
33 for(i=0;i<el.length;i++)
37 function checknone() {
39 void(el=d.getElementsByTagName('INPUT'));
40 for(i=0;i<el.length;i++)
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]);
54 for (i=0; i<subitems.length; i++) {
55 lockoption(form, subitems[i]);
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");
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");
75 function submitFormById(id) {
76 var theform = document.getElementById(id);
80 if(theform.tagName != 'FORM') {
83 if(!theform.onsubmit || theform.onsubmit()) {
84 return theform.submit();
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';
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 = '';
108 function confirm_if(expr, message) {
112 return confirm(message);
117 findParentNode (start, elementName, elementClass, elementID)
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
124 function findParentNode(el, elName, elClass, elId) {
125 while(el.nodeName != 'BODY') {
127 (!elName || el.nodeName == elName) &&
128 (!elClass || el.className.indexOf(elClass) != -1) &&
129 (!elId || el.id == elId))
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) {
152 var obj = elementFinder(el);
154 if(obj.className.indexOf('hidden') == -1) {
155 obj.className += ' hidden';
159 obj.className = obj.className.replace(new RegExp(' ?hidden'), '')
163 if(persistent == true) {
164 new cookie('hide:' + obj.id, 1, (shown ? -1 : 356), '/').set();
169 function elementCookieHide(id) {
170 var obj = document.getElementById(id);
171 var cook = new cookie('hide:' + id).read();
173 elementToggleHide(obj, false);
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]);
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');
201 function fix_column_width(colName) {
202 if(column = document.getElementById(colName)) {
203 if(!column.offsetWidth) {
204 setTimeout("fix_column_width('" + colName + "')", 20);
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;
219 for(i = 0; i < nodes.length; ++i) {
220 if(nodes[i].className.indexOf("sideblock") != -1 ) {
221 nodes[i].style.width = width + 'px';
229 Insert myValue at current cursor position
231 function insertAtCursor(myField, myValue) {
233 if (document.selection) {
235 sel = document.selection.createRange();
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);
245 myField.value += myValue;