]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/common/js/utils.js
Many random tweaks to get IE to behave
[Evergreen.git] / Open-ILS / web / opac / common / js / utils.js
1 function $(id) { return getId(id); }
2 function getId(id) {
3         return document.getElementById(id);
4 }
5
6 function swapCSSClass(obj, old, newc ) {
7         removeCSSClass(obj, old );
8         addCSSClass(obj, newc );
9 }
10
11
12 function addCSSClass(e,c) {
13         if(!e || !c) return;
14
15         var css_class_string = e.className;
16         var css_class_array;
17
18         if(css_class_string)
19                 css_class_array = css_class_string.split(/\s+/);
20
21         var string_ip = ""; /*strip out nulls*/
22         for (var css_class in css_class_array) {
23                 if (css_class_array[css_class] == c) { return; }
24                 if(css_class_array[css_class] !=null)
25                         string_ip += css_class_array[css_class] + " ";
26         }
27         string_ip += c;
28         e.className = string_ip;
29 }
30
31 function removeCSSClass(e, c) {
32         if(!e || !c) return;
33
34         var css_class_string = '';
35
36         var css_class_array = e.className;
37         if( css_class_array )
38                 css_class_array = css_class_array.split(/\s+/);
39
40         var first = 1;
41         for (var css_class in css_class_array) {
42                 if (css_class_array[css_class] != c) {
43                         if (first == 1) {
44                                 css_class_string = css_class_array[css_class];
45                                 first = 0;
46                         } else {
47                                 css_class_string = css_class_string + ' ' +
48                                         css_class_array[css_class];
49                         }
50                 }
51         }
52         e.className = css_class_string;
53 }
54
55
56 /*returns the character code pressed that caused the event */
57 function grabCharCode(evt) {
58    evt = (evt) ? evt : ((window.event) ? event : null); 
59    if( evt ) {
60       return (evt.charCode ? evt.charCode : 
61          ((evt.which) ? evt.which : evt.keyCode ));
62    } else { return -1; }
63 }       
64
65
66 /* returns true if the user pressed enter */
67 function userPressedEnter(evt) {
68    var code = grabCharCode(evt);
69    if(code==13||code==3) return true;
70    return false;
71 }   
72
73
74 function goTo(url) {
75         /* setTimeout because ie sux */
76         setTimeout( function(){ location.href = url; }, 0 );
77 }
78
79
80 function removeChildren(dom) {
81         if(!dom) return;
82         while(dom.childNodes[0])
83                 dom.removeChild(dom.childNodes[0]);
84 }
85
86 function appendClear(node, child) {
87         removeChildren(node);
88         node.appendChild(child);
89 }
90
91
92 function instanceOf(object, constructorFunction) {
93
94    if(!IE) {
95       while (object != null) {
96          if (object == constructorFunction.prototype)
97             return true;
98          object = object.__proto__;
99       }
100    } else {
101       while(object != null) {
102          if( object instanceof constructorFunction )
103             return true;
104          object = object.__proto__;
105       }
106    }
107    return false;
108 }         
109
110
111 /* ------------------------------------------------------------------------------------------- */
112 /* detect my browser */
113 var isMac, NS, NS4, NS6, IE, IE4, IE4mac, IE4plus, IE5, IE5plus, IE6, IEMajor, ver4;
114 function detect_browser() {       
115
116    isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
117    NS = (navigator.appName == "Netscape") ? true : false;
118    NS4 = (document.layers) ? true : false;
119    IE = (navigator.appName == "Microsoft Internet Explorer") ? true : false;
120    IEmac = ((document.all)&&(isMac)) ? true : false;
121    IE4plus = (document.all) ? true : false;
122    IE4 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 4.")!=-1)) ? true : false;
123    IE5 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 5.")!=-1)) ? true : false;
124    IE6 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 6.")!=-1)) ? true : false;
125    ver4 = (NS4 || IE4plus) ? true : false;
126    NS6 = (!document.layers) && (navigator.userAgent.indexOf('Netscape')!=-1)?true:false;
127
128    IE5plus = IE5 || IE6;
129    IEMajor = 0;
130
131    if (IE4plus) {
132       var start = navigator.appVersion.indexOf("MSIE");
133       var end = navigator.appVersion.indexOf(".",start);
134       IEMajor = parseInt(navigator.appVersion.substring(start+5,end));
135       IE5plus = (IEMajor>=5) ? true : false;
136    }
137 }  
138 detect_browser();
139 /* ------------------------------------------------------------------------------------------- */
140
141
142 function text(t) {
143         if(t == null) t = "";
144         return document.createTextNode(t);
145 }
146
147 function elem(name, attrs, txt) {
148     var e = document.createElement(name);
149     if (attrs) {
150         for (key in attrs) {
151                           if( key == 'id') e.id = attrs[key];
152                           else e.setAttribute(key, attrs[key]);
153         }
154     }
155     if (txt) e.appendChild(text(txt));
156     return e;
157 }                   
158
159
160 /* sel is the selector object, sets selected on the 
161         option with the given value. case does not matter...*/
162 function setSelector( sel, value ) {
163         if(sel && value != null) {
164                 for( var i = 0; i!= sel.options.length; i++ ) { 
165                         if( sel.options[i] ) {
166                                 var val = sel.options[i].value;
167                                 if( val == null || val == "" ) /* for IE */
168                                         val = sel.options[i].innerHTML;
169                                 value += ""; /* in case of number */ 
170                                 if( val && val.toLowerCase() == value.toLowerCase() ) {
171                                         sel.selectedIndex = i;
172                                         sel.options[i].selected = true;
173                                 }
174                         }
175                 }
176         }
177 }
178
179 function getSelectorVal( sel ) {
180         if(!sel) return null;
181         var o = sel.options[sel.selectedIndex];
182         var v = o.value; 
183         if(v == null || v == "") v = o.innerHTML;
184         return v;
185 }
186
187 function debugSelector(sel) {
188         var s = 'Selector\n';
189         for( var i = 0; i != sel.options.length; i++ ) {
190                 var o = sel.options[i];
191                 s += "\t" + o.innerHTML + "\n";
192         }
193         return s;
194 }
195
196 function doSelectorActions(sel) {
197         if(IE && sel) { 
198                 sel.onchange = function() {
199                         var o = sel.options[sel.selectedIndex];
200                         if(o && o.onclick) o.onclick()
201                 }
202         }
203 }
204
205 /* if index < 0, the item is pushed onto the end */
206 function insertSelectorVal( selector, index, name, value, action, indent ) {
207         if( index < 0 ) index = selector.options.length;
208         var a = [];
209         for( var i = selector.options.length; i != index; i-- ) 
210                 a[i] = selector.options[i-1];
211
212         setSelectorVal( selector, index, name, value, action, indent );
213
214         for( var i = index + 1; i != a.length; i++ ) 
215                 selector.options[i] = a[i];
216 }
217
218 function setSelectorVal( selector, index, name, value, action, indent ) {
219         if(!indent || indent < 0) indent = 0;
220         indent = parseInt(indent);
221
222         var option;
223         if(IE) {
224                 var pre = elem("pre");
225                 for( var i = 0; i != indent; i++ )
226                         pre.appendChild(text("   "));
227
228                 pre.appendChild(text(name));
229                 option = new Option("", value);
230                 selector.options[index] = option;
231                 option.appendChild(pre);
232         
233         } else {
234                 indent = indent * 14;
235                 option= new Option(name, value);
236                 option.setAttribute("style", "padding-left: "+indent+'px;');
237                 selector.options[index] = option;
238                 if(action) option.onclick = action;
239         }
240
241         option.onclick = action;
242 }
243
244
245 /* split on spaces.  capitalize the first /\w/ character in
246    each substring */
247 function normalize(val) {
248
249    if(!val) return ""; 
250
251    var newVal = '';
252    try {val = val.split(' ');} catch(E) {return val;}
253    var reg = /\w/;
254
255    for( var c = 0; c < val.length; c++) {
256
257       var string = val[c];
258       var cap = false; 
259       for(var x = 0; x != string.length; x++) {
260
261          if(!cap) {
262             var ch = string.charAt(x);
263             if(reg.exec(ch + "")) {
264                newVal += string.charAt(x).toUpperCase();
265                cap = true;
266                continue;
267             }
268          }
269
270          newVal += string.charAt(x).toLowerCase();
271       }
272       if(c < (val.length-1)) newVal += " ";
273    }
274
275    newVal = newVal.replace(/\s*\.\s*$/,'');
276    newVal = newVal.replace(/\s*\/\s*\/\s*$/,' / ');
277    newVal = newVal.replace(/\s*\/\s*$/,'');
278
279    return newVal;
280 }
281
282
283 /* returns true if n is null or stringifies to 'undefined' */
284 function isNull(n) {
285         if( n == null || n == undefined || n.toString().toLowerCase() == "undefined" 
286                 || n.toString().toLowerCase() == "null" )
287                 return true;
288         return false;
289 }
290
291
292 /* find nodes with an attribute of 'name' that equals nodeName */
293
294 function $n( root, nodeName ) { return findNodeByName(root,nodeName); }
295
296 function findNodeByName(root, nodeName) {
297         if( !root || !nodeName) return null;
298
299         if(root.nodeType != 1) return null;
300
301         if(root.getAttribute("name") == nodeName || root.name == nodeName ) 
302                 return root;
303
304         var children = root.childNodes;
305
306         for( var i = 0; i != children.length; i++ ) {
307                 var n = findNodeByName(children[i], nodeName);
308                 if(n) return n;
309         }
310
311         return null;
312 }
313
314
315 /* truncates the string at 'size' characters and appends a '...' to the end */
316 function truncate(string, size) {
317         if(string && size != null && 
318                         size > -1 && string.length > size) 
319                 return string.substr(0, size) + "... "; 
320         return string;
321 }
322
323
324 /* style sheets must have a 'name' attribute for these functions to work */
325 function setActivateStyleSheet(name) {
326         var i, a, main;
327         for (i = 0; (a = document.getElementsByTagName ("link")[i]); i++) {
328                 if (a.getAttribute ("rel").indexOf ("style") != -1 && a.getAttribute ("name")) {
329                         a.disabled = true;
330                         if (a.getAttribute ("name").indexOf(name) != -1)
331                                 a.disabled = false;
332                 }
333         }
334 }
335
336
337 /* ----------------------------------------------------- */
338 function scaleFonts(type) {
339
340         var size = "";
341         var ssize = "";
342         var a;
343
344         switch(type) {
345                 case "large": 
346                         size = "148%"; 
347                         ssize = "94%";
348                         break;
349         }
350
351         document.getElementsByTagName('body')[0].style.fontSize = size;
352         for (i = 0; (a = document.getElementsByTagName ("td")[i]); i++) a.style.fontSize = size;;
353         for (i = 0; (a = document.getElementsByTagName ("div")[i]); i++) a.style.fontSize = ssize;
354         for (i = 0; (a = document.getElementsByTagName ("option")[i]); i++) a.style.fontSize = ssize;
355         for (i = 0; (a = document.getElementsByTagName ("li")[i]); i++) a.style.fontSize = ssize;
356         for (i = 0; (a = document.getElementsByTagName ("span")[i]); i++) a.style.fontSize = ssize;
357 }
358
359
360 function sortWordsIgnoreCase(a, b) {
361         a = a.toLowerCase();
362         b = b.toLowerCase();
363         if(a>b) return 1;
364         if(a<b) return -1;
365         return 0;
366 }
367
368
369
370
371
372 function setEnterFunc(node, func) {
373         if(!(node && func)) return;
374         node.onkeydown = function(evt) {
375                 if( userPressedEnter(evt)) func();
376         }
377 }