Changed use_html_editor and print_editor_config functions so that we have an
[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) {
45 // subitems is an array of names of sub items
46 // requires that each item in subitems has 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);
59}
60
61function lockoption(form,item) {
62 eval("document."+form+"."+item+".disabled=true");/* IE thing */
63 eval("document."+form+".h"+item+".value=1");
64}
65
66function unlockoption(form,item) {
67 eval("document."+form+"."+item+".disabled=false");/* IE thing */
68 eval("document."+form+".h"+item+".value=0");
69}
7678e65c 70
71function submitFormById(id) {
72 var theform = document.getElementById(id);
73 if(!theform) {
74 return false;
75 }
76 if(theform.tagName != 'FORM') {
77 return false;
78 }
79 if(!theform.onsubmit || theform.onsubmit()) {
80 return theform.submit();
81 }
82}
363cb62c 83
8ceb09e0 84function select_all_in(elTagName, elClass, elId) {
363cb62c 85 var inputs = document.getElementsByTagName('INPUT');
8ceb09e0 86 inputs = filterByParent(inputs, function(el) {return findParentNode(el, elTagName, elClass, elId);});
363cb62c 87 for(var i = 0; i < inputs.length; ++i) {
88 if(inputs[i].type == 'checkbox') {
89 inputs[i].checked = 'checked';
90 }
91 }
92}
93
8ceb09e0 94function deselect_all_in(elTagName, elClass, elId) {
363cb62c 95 var inputs = document.getElementsByTagName('INPUT');
8ceb09e0 96 inputs = filterByParent(inputs, function(el) {return findParentNode(el, elTagName, elClass, elId);});
363cb62c 97 for(var i = 0; i < inputs.length; ++i) {
98 if(inputs[i].type == 'checkbox') {
99 inputs[i].checked = '';
100 }
101 }
102}
103
104function confirm_if(expr, message) {
105 if(!expr) {
106 return true;
107 }
108 return confirm(message);
109}
47aa42e2 110
111
112/*
113 findParentNode (start, elementName, elementClass, elementID)
114
115 Travels up the DOM hierarchy to find a parent element with the
116 specified tag name, class, and id. All conditions must be met,
117 but any can be ommitted. Returns the BODY element if no match
118 found.
119*/
120function findParentNode(el, elName, elClass, elId) {
121 while(el.nodeName != 'BODY') {
122 if(
123 (!elName || el.nodeName == elName) &&
124 (!elClass || el.className.indexOf(elClass) != -1) &&
125 (!elId || el.id == elId))
126 {
127 break;
128 }
129 el = el.parentNode;
130 }
131 return el;
132}
133
134/*
135 elementToggleHide (element, elementFinder)
136
137 If elementFinder is not provided, toggles the "hidden" class for the specified element.
138 If elementFinder is provided, then the "hidden" class will be toggled for the object
139 returned by the function call elementFinder(element).
140
141 If persistent == true, also sets a cookie for this.
142*/
143function elementToggleHide(el, persistent, elementFinder) {
144 if(!elementFinder) {
145 var obj = el;
146 }
147 else {
148 var obj = elementFinder(el);
149 }
150 if(obj.className.indexOf('hidden') == -1) {
151 obj.className += ' hidden';
152 var shown = 0;
153 }
154 else {
155 obj.className = obj.className.replace(new RegExp(' ?hidden'), '')
156 var shown = 1;
157 }
158
159 if(persistent == true) {
160 new cookie('hide:' + obj.id, 1, (shown ? -1 : 356), '/').set();
161 }
162}
163
164
165function elementCookieHide(id) {
166 var obj = document.getElementById(id);
167 var cook = new cookie('hide:' + id).read();
168 if(cook != null) {
169 elementToggleHide(obj, false);
170 }
171}
172
173function filterByParent(elCollection, parentFinder) {
174 var filteredCollection = [];
175 for(var i = 0; i < elCollection.length; ++i) {
176 var findParent = parentFinder(elCollection[i]);
177 if(findParent.nodeName != 'BODY') {
178 filteredCollection.push(elCollection[i]);
179 }
180 }
181 return filteredCollection;
182}
183
7979105c 184/*
185 All this is here just so that IE gets to handle oversized blocks
186 in a visually pleasing manner. It does a browser detect. So sue me.
187*/
188
189function fix_column_widths() {
190 var agt = navigator.userAgent.toLowerCase();
191 if ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1)) {
192 fix_column_width('left-column');
193 fix_column_width('right-column');
194 }
195}
196
197function fix_column_width(colName) {
198 if(column = document.getElementById(colName)) {
199 if(!column.offsetWidth) {
200 setTimeout("fix_column_width('" + colName + "')", 20);
201 return;
202 }
203
204 var width = 0;
205 var nodes = column.childNodes;
206
207 for(i = 0; i < nodes.length; ++i) {
208 if(nodes[i].className.indexOf("sideblock") != -1 ) {
209 if(width < nodes[i].offsetWidth) {
210 width = nodes[i].offsetWidth;
211 }
212 }
213 }
214
215 for(i = 0; i < nodes.length; ++i) {
216 if(nodes[i].className.indexOf("sideblock") != -1 ) {
217 nodes[i].style.width = width + 'px';
218 }
219 }
220 }
221}