using correct string
[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
61function lockoption(form,item) {
62 eval("document."+form+"."+item+".disabled=true");/* IE thing */
027d0485 63 if(document.forms[form].elements['h'+item]) {
64 eval("document."+form+".h"+item+".value=1");
65 }
63d28811 66}
67
68function unlockoption(form,item) {
69 eval("document."+form+"."+item+".disabled=false");/* IE thing */
027d0485 70 if(document.forms[form].elements['h'+item]) {
71 eval("document."+form+".h"+item+".value=0");
72 }
63d28811 73}
7678e65c 74
75function 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 }
86}
363cb62c 87
8ceb09e0 88function select_all_in(elTagName, elClass, elId) {
363cb62c 89 var inputs = document.getElementsByTagName('INPUT');
8ceb09e0 90 inputs = filterByParent(inputs, function(el) {return findParentNode(el, elTagName, elClass, elId);});
363cb62c 91 for(var i = 0; i < inputs.length; ++i) {
bee40515 92 if(inputs[i].type == 'checkbox' || inputs[i].type == 'radio') {
363cb62c 93 inputs[i].checked = 'checked';
94 }
95 }
96}
97
8ceb09e0 98function deselect_all_in(elTagName, elClass, elId) {
363cb62c 99 var inputs = document.getElementsByTagName('INPUT');
8ceb09e0 100 inputs = filterByParent(inputs, function(el) {return findParentNode(el, elTagName, elClass, elId);});
363cb62c 101 for(var i = 0; i < inputs.length; ++i) {
bee40515 102 if(inputs[i].type == 'checkbox' || inputs[i].type == 'radio') {
363cb62c 103 inputs[i].checked = '';
104 }
105 }
106}
107
108function confirm_if(expr, message) {
109 if(!expr) {
110 return true;
111 }
112 return confirm(message);
113}
47aa42e2 114
115
116/*
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.
123*/
124function 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;
136}
137
138/*
139 elementToggleHide (element, elementFinder)
140
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).
144
145 If persistent == true, also sets a cookie for this.
146*/
147function 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 }
162
163 if(persistent == true) {
164 new cookie('hide:' + obj.id, 1, (shown ? -1 : 356), '/').set();
165 }
166}
167
168
169function 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 }
175}
176
177function 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;
186}
187
7979105c 188/*
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.
191*/
192
193function 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 }
199}
200
201function fix_column_width(colName) {
202 if(column = document.getElementById(colName)) {
203 if(!column.offsetWidth) {
204 setTimeout("fix_column_width('" + colName + "')", 20);
205 return;
206 }
207
208 var width = 0;
209 var nodes = column.childNodes;
210
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 }
218
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 }
225}
d13c5938 226
227
228/*
229 Insert myValue at current cursor position
230*/
231function 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 }
247}