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