added pop up blocker checker
[moodle.git] / lib / javascript-static.js
CommitLineData
47aa42e2 1// Miscellaneous core Javascript functions for Moodle
2
63d28811 3function popUpProperties(inobj) {
4 op = window.open();
5 op.document.open('text/plain');
6 for (objprop in inobj) {
7 op.document.write(objprop + ' => ' + inobj[objprop] + '\n');
8 }
9 op.document.close();
10}
11
12function fillmessagebox(text) {
13 document.form.message.value = text;
14}
15
16function copyrichtext(textname) {
17/// Legacy stub for old editor - to be removed soon
18 return true;
19}
20
21function checkall() {
22 void(d=document);
23 void(el=d.getElementsByTagName('INPUT'));
24 for(i=0;i<el.length;i++)
25 void(el[i].checked=1)
26}
27
03f9425f 28function checknone() {
29 void(d=document);
30 void(el=d.getElementsByTagName('INPUT'));
31 for(i=0;i<el.length;i++)
32 void(el[i].checked=0)
33}
34
63d28811 35function lockoptions(form, master, subitems) {
36 // subitems is an array of names of sub items
37 // requires that each item in subitems has a
38 // companion hidden item in the form with the
39 // same name but prefixed by "h"
40 if (eval("document."+form+"."+master+".checked")) {
41 for (i=0; i<subitems.length; i++) {
42 unlockoption(form, subitems[i]);
43 }
44 } else {
45 for (i=0; i<subitems.length; i++) {
46 lockoption(form, subitems[i]);
47 }
48 }
49 return(true);
50}
51
52function lockoption(form,item) {
53 eval("document."+form+"."+item+".disabled=true");/* IE thing */
54 eval("document."+form+".h"+item+".value=1");
55}
56
57function unlockoption(form,item) {
58 eval("document."+form+"."+item+".disabled=false");/* IE thing */
59 eval("document."+form+".h"+item+".value=0");
60}
7678e65c 61
62function submitFormById(id) {
63 var theform = document.getElementById(id);
64 if(!theform) {
65 return false;
66 }
67 if(theform.tagName != 'FORM') {
68 return false;
69 }
70 if(!theform.onsubmit || theform.onsubmit()) {
71 return theform.submit();
72 }
73}
363cb62c 74
8ceb09e0 75function select_all_in(elTagName, elClass, elId) {
363cb62c 76 var inputs = document.getElementsByTagName('INPUT');
8ceb09e0 77 inputs = filterByParent(inputs, function(el) {return findParentNode(el, elTagName, elClass, elId);});
363cb62c 78 for(var i = 0; i < inputs.length; ++i) {
79 if(inputs[i].type == 'checkbox') {
80 inputs[i].checked = 'checked';
81 }
82 }
83}
84
8ceb09e0 85function deselect_all_in(elTagName, elClass, elId) {
363cb62c 86 var inputs = document.getElementsByTagName('INPUT');
8ceb09e0 87 inputs = filterByParent(inputs, function(el) {return findParentNode(el, elTagName, elClass, elId);});
363cb62c 88 for(var i = 0; i < inputs.length; ++i) {
89 if(inputs[i].type == 'checkbox') {
90 inputs[i].checked = '';
91 }
92 }
93}
94
95function confirm_if(expr, message) {
96 if(!expr) {
97 return true;
98 }
99 return confirm(message);
100}
47aa42e2 101
102
103/*
104 findParentNode (start, elementName, elementClass, elementID)
105
106 Travels up the DOM hierarchy to find a parent element with the
107 specified tag name, class, and id. All conditions must be met,
108 but any can be ommitted. Returns the BODY element if no match
109 found.
110*/
111function findParentNode(el, elName, elClass, elId) {
112 while(el.nodeName != 'BODY') {
113 if(
114 (!elName || el.nodeName == elName) &&
115 (!elClass || el.className.indexOf(elClass) != -1) &&
116 (!elId || el.id == elId))
117 {
118 break;
119 }
120 el = el.parentNode;
121 }
122 return el;
123}
124
125/*
126 elementToggleHide (element, elementFinder)
127
128 If elementFinder is not provided, toggles the "hidden" class for the specified element.
129 If elementFinder is provided, then the "hidden" class will be toggled for the object
130 returned by the function call elementFinder(element).
131
132 If persistent == true, also sets a cookie for this.
133*/
134function elementToggleHide(el, persistent, elementFinder) {
135 if(!elementFinder) {
136 var obj = el;
137 }
138 else {
139 var obj = elementFinder(el);
140 }
141 if(obj.className.indexOf('hidden') == -1) {
142 obj.className += ' hidden';
143 var shown = 0;
144 }
145 else {
146 obj.className = obj.className.replace(new RegExp(' ?hidden'), '')
147 var shown = 1;
148 }
149
150 if(persistent == true) {
151 new cookie('hide:' + obj.id, 1, (shown ? -1 : 356), '/').set();
152 }
153}
154
155
156function elementCookieHide(id) {
157 var obj = document.getElementById(id);
158 var cook = new cookie('hide:' + id).read();
159 if(cook != null) {
160 elementToggleHide(obj, false);
161 }
162}
163
164function filterByParent(elCollection, parentFinder) {
165 var filteredCollection = [];
166 for(var i = 0; i < elCollection.length; ++i) {
167 var findParent = parentFinder(elCollection[i]);
168 if(findParent.nodeName != 'BODY') {
169 filteredCollection.push(elCollection[i]);
170 }
171 }
172 return filteredCollection;
173}
174
7979105c 175/*
176 All this is here just so that IE gets to handle oversized blocks
177 in a visually pleasing manner. It does a browser detect. So sue me.
178*/
179
180function fix_column_widths() {
181 var agt = navigator.userAgent.toLowerCase();
182 if ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1)) {
183 fix_column_width('left-column');
184 fix_column_width('right-column');
185 }
186}
187
188function fix_column_width(colName) {
189 if(column = document.getElementById(colName)) {
190 if(!column.offsetWidth) {
191 setTimeout("fix_column_width('" + colName + "')", 20);
192 return;
193 }
194
195 var width = 0;
196 var nodes = column.childNodes;
197
198 for(i = 0; i < nodes.length; ++i) {
199 if(nodes[i].className.indexOf("sideblock") != -1 ) {
200 if(width < nodes[i].offsetWidth) {
201 width = nodes[i].offsetWidth;
202 }
203 }
204 }
205
206 for(i = 0; i < nodes.length; ++i) {
207 if(nodes[i].className.indexOf("sideblock") != -1 ) {
208 nodes[i].style.width = width + 'px';
209 }
210 }
211 }
212}