]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/javascript/util/webutils.js
*** empty log message ***
[working/Evergreen.git] / Open-ILS / src / javascript / util / webutils.js
1
2 var isMac
3 var NS;
4 var NS4;
5 var NS6;
6 var IE;
7 var IE4;
8 var IE4mac;
9 var IE4plus;
10 var IE5;
11 var IE5plus;
12 var IE6;
13 var IEMajor;
14 var ver4;
15
16 function detect_browser() {       
17
18         isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
19         NS = (navigator.appName == "Netscape") ? true : false;
20         NS4 = (document.layers) ? true : false;
21         IE = (navigator.appName == "Microsoft Internet Explorer") ? true : false;
22         IEmac = ((document.all)&&(isMac)) ? true : false;
23         IE4plus = (document.all) ? true : false;
24         IE4 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 4.")!=-1)) ? true : false;
25         IE5 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 5.")!=-1)) ? true : false;
26         IE6 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 6.")!=-1)) ? true : false;
27         ver4 = (NS4 || IE4plus) ? true : false;
28         NS6 = (!document.layers) && (navigator.userAgent.indexOf('Netscape')!=-1)?true:false;
29
30         IE5plus = IE5 || IE6;
31         IEMajor = 0;
32
33         if (IE4plus) {
34                 var start = navigator.appVersion.indexOf("MSIE");
35                 var end = navigator.appVersion.indexOf(".",start);
36                 IEMajor = parseInt(navigator.appVersion.substring(start+5,end));
37                 IE5plus = (IEMajor>=5) ? true : false;
38         }
39 }
40
41 detect_browser();
42
43
44
45
46
47 function hideMe(obj) {
48         if(!obj)  return;
49         add_css_class( obj, "hide_me");
50         remove_css_class( obj, "show_me");
51 }
52
53 function showMe(obj) {
54         if(!obj)  return;
55         add_css_class( obj, "show_me");
56         remove_css_class( obj, "hide_me");
57 }
58
59 function grabCharCode( evt ) {
60         evt = (evt) ? evt : ((window.event) ? event : null); /* for mozilla and IE */
61         if( evt ) {
62                 return (evt.charCode ? evt.charCode : 
63                         ((evt.which) ? evt.which : evt.keyCode ));
64         } else { 
65                 return -1;
66         }
67 }
68
69 function getById(id) {
70
71         var obj = document.getElementById(id);
72         if(obj != null) return obj;
73
74         try {
75                 if(globalAppFrame) {
76                         obj = globalAppFrame.document.getElementById(id);
77                 }
78         } catch(E) {
79                 alert("We need a globalAppFrame to function");
80         }
81
82         return obj;
83 }
84
85 function createAppElement(name) {
86         if(globalAppFrame)
87                 return globalAppFrame.document.createElement(name);
88         return document.createElement(name);
89 }
90
91 function createAppTextNode(text) {
92         if(globalAppFrame)
93                 return globalAppFrame.document.createTextNode(text);
94         return document.createTextNode(text);
95 }
96
97
98
99 function normalize(val) {
100         var newVal = '';
101         val = val.split(' ');
102         for(var c=0; c < val.length; c++) {
103                 var string = val[c];
104
105                 for(var x = 0; x != string.length; x++) {
106                         if(x==0)
107                                 newVal += string.charAt(x).toUpperCase();
108                         else
109                                 newVal += string.charAt(x).toLowerCase();
110                 }
111                 if(c < (val.length-1)) newVal += " ";
112         }
113         return newVal;
114 }
115
116
117
118
119 var reg = /Mozilla/;
120 var ismoz = false;
121 if(reg.exec(navigator.userAgent)) 
122         ismoz = true;
123
124 var DEBUG = true;
125
126 function debug(message) {
127         if(DEBUG) {
128                 try {
129                         dump(" -|*|- Debug: " + message + "\n");
130                 } catch(E) {}
131         }
132 }
133
134 /* finds or builds the requested row id, adding any intermediate rows along the way */
135 function table_row_find_or_create( table, index ) {
136
137         if(table == null || index == null || index < 0 || index > 10000 ) {
138                 throw "table_row_find_or_create with invalid " +
139                         "params.  table: " + table + " index: " + index + "\n";
140         }
141
142         var tbody = table.getElementsByTagName("tbody")[0];
143
144         if(tbody == null)
145                 tbody = table.appendChild(createAppElement("tbody"));
146
147
148         if(table.rows[index] != null)
149                 return table.rows[index];
150
151         var row;
152         for( var x = 0; x <= index; x++ ) {
153                 if(table.rows[x] == null) {
154                         row = tbody.appendChild(createAppElement("tr"));
155                         //row = table.appendChild(document.createElement("tr"));
156                         //row = document.createElement("tr");
157                         //var tbody = table.getElementsByTagName("tbody")[0];
158                         //debug(tbody);
159                         //tbody.appendChild(row);
160                         //table.childNodes[x] = row;
161                         //table.rows[x] = row;
162                         //row = table.insertRow(x);
163                 }
164         }
165         return row;
166 }
167
168 /* finds or builds the requested cell,  adding any intermediate cells along the way */
169 function table_cell_find_or_create( row, index ) {
170
171         if(row == null || index == null || index < 0 || index > 10000 ) {
172                 throw "table_cell_find_or_create with invalid " +
173                         "params.  row: " + row + " index: " + index + "\n";
174         }
175
176         if(row.cells[index] != null)
177                 return row.cells[index];
178
179         for( var x = 0; x!= index; x++ ) {
180                 if(row.cells[x] == null) 
181                         row.insertCell(x);
182         }
183
184         return row.insertCell(index);
185 }
186         
187
188
189 // -----------------------------------------------------------------------
190 // Generic cookie libarary copied from 
191 // http://webreference.com/js/column8/functions.html
192 // -----------------------------------------------------------------------
193
194 /*
195         name - name of the cookie
196    value - value of the cookie
197    [expires] - expiration date of the cookie
198    (defaults to end of current session)
199    [path] - path for which the cookie is valid
200    (defaults to path of calling document)
201    [domain] - domain for which the cookie is valid
202    (defaults to domain of calling document)
203         [secure] - Boolean value indicating if the cookie transmission requires
204         a secure transmission
205    * an argument defaults when it is assigned null as a placeholder
206    * a null placeholder is not required for trailing omitted arguments
207         */
208
209 function setCookie(name, value, expires, path, domain, secure) {
210         var curCookie = name + "=" + escape(value) +
211    ((expires) ? "; expires=" + expires.toGMTString() : "") +
212    ((path) ? "; path=" + path : "") +
213    ((domain) ? "; domain=" + domain : "") +
214    ((secure) ? "; secure" : "");
215         document.cookie = curCookie;
216 }
217
218
219 /*
220         name - name of the desired cookie
221    return string containing value of specified cookie or null
222    if cookie does not exist
223 */
224
225 function getCookie(name) {
226         var dc = document.cookie;
227         var prefix = name + "=";
228         var begin = dc.indexOf("; " + prefix);
229         if (begin == -1) {
230                 begin = dc.indexOf(prefix);
231            if (begin != 0) return null;
232         } else {
233             begin += 2;
234         }
235
236         var end = document.cookie.indexOf(";", begin);
237    if (end == -1) end = dc.length;
238         return unescape(dc.substring(begin + prefix.length, end));
239 }
240
241
242 /*
243         name - name of the cookie
244         [path] - path of the cookie (must be same as path used to create cookie)
245         [domain] - domain of the cookie (must be same as domain used to
246    create cookie)
247    path and domain default if assigned null or omitted if no explicit
248    argument proceeds
249 */
250
251 function deleteCookie(name, path, domain) {
252         if (getCookie(name)) {
253                 var string = name + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
254                 debug("Delete cookie string: "+ string );
255                 document.cookie = string;
256                 debug("Delete Cookie: " + document.cookie );
257                 /*
258                 document.cookie = name + "=" +
259                 ((path) ? "; path=" + path : "") +
260                 ((domain) ? "; domain=" + domain : "") +
261                 "; expires=Thu, 01-Jan-70 00:00:01 GMT";
262                 */
263         }
264 }
265
266 // date - any instance of the Date object
267 // * hand all instances of the Date object to this function for "repairs"
268
269 function fixDate(date) {
270         var base = new Date(0);
271         var skew = base.getTime();
272         if (skew > 0)
273                 date.setTime(date.getTime() - skew);
274 }
275
276 // -----------------------------------------------------------------------
277
278 var globalProgressBar = null;
279 function ProgressBar( div, color, interval ) {
280
281         this.progressEnd                        = 9;                            
282
283         if( color != null)
284                 this.progressColor              = color;
285         else 
286                 this.progressColor              = 'blue';       
287
288         if(interval != null)
289                 this.progressInterval   = interval;
290         else
291                 this.progressInterval   = 50;   
292
293         this.progressAt = this.progressEnd;
294         this.progressTimer;
295
296         for( var x = 0; x!= this.progressEnd; x++ ) {
297                 var newdiv = createAppElement("span");
298                 newdiv.id = "progress" + x;
299                 newdiv.appendChild(document.createTextNode("   "));
300                 div.appendChild(newdiv);
301         }
302         globalProgressBar = this;
303 }
304
305 ProgressBar.prototype.progressStart = function() {
306         this.progressUpdate();
307 }
308
309 ProgressBar.prototype.progressClear = function() {
310         for (var i = 0; i < this.progressEnd; i++) {
311                 getById('progress' + i).style.backgroundColor = 'transparent';
312         }
313         progressAt = 0;
314 }
315
316 ProgressBar.prototype.progressUpdate = function() {
317         debug(" -3-3-3-3- Updating Progress Bar");
318         this.progressAt++;
319         if (this.progressAt > this.progressEnd) 
320                 this.progressClear();
321         else 
322                 getById('progress'+progressAt).style.backgroundColor = this.progressColor;
323         this.progressTimer = setTimeout('globalProgressBar.progressUpdate()', this.progressInterval);
324         debug("Timer is set at " + this.progressInterval);
325 }
326
327 ProgressBar.prototype.progressStop = function() {
328         clearTimeout(this.progressTimer);
329         this.progressClear();
330 }
331
332 function add_css_class(w,c) {
333         var e;
334         if (typeof(w) == 'object') {
335                 e = w;
336         } else {
337                 e = getById(w);
338         }
339         var css_class_string = e.className;
340         var css_class_array;
341
342         if(css_class_string)
343                 css_class_array = css_class_string.split(/\s+/);
344
345         var string_ip = ""; /*strip out nulls*/
346         for (var css_class in css_class_array) {
347                 if (css_class_array[css_class] == c) { return; }
348                 if(css_class_array[css_class] !=null)
349                         string_ip += css_class_array[css_class] + " ";
350         }
351         string_ip = string_ip + c;
352         e.className = string_ip;
353 }
354
355 function remove_css_class(w,c) {
356         var e;
357         if(w==null)
358                 return;
359
360         if (typeof(w) == 'object') {
361                 e = w;
362         } else {
363                 e = getById(w);
364         }
365         var css_class_string = '';
366
367         var css_class_array = e.className;
368         if( css_class_array )
369                 css_class_array = css_class_array.split(/\s+/);
370
371         var first = 1;
372         for (var css_class in css_class_array) {
373                 if (css_class_array[css_class] != c) {
374                         if (first == 1) {
375                                 css_class_string = css_class_array[css_class];
376                                 first = 0;
377                         } else {
378                                 css_class_string = css_class_string + ' ' +
379                                         css_class_array[css_class];
380                         }
381                 }
382         }
383         e.className = css_class_string;
384 }
385
386
387
388
389 /* takes an array of the form [ key, value, key, value, ..] and 
390         redirects the page to the current host/path plus the key
391         value pairs provided 
392         */
393 function url_redirect(key_value_array) {
394
395         if( key_value_array == null || 
396                         (key_value_array.length %2))  {
397                 throw new EXArg( 
398                                 "AdvancedSearchPage.redirect has invalid args" );
399         }
400
401         var fullpath = globalRootPath;
402         var x = 0;
403
404         debug("Redirecting...");
405
406         for( var x = 0; x!= key_value_array.length; x++ ) {
407                 debug("Checking key_value_array " + x + " : " + key_value_array[x] );
408                 if( x == 0 )
409                         fullpath += "?" + encodeURIComponent(key_value_array[x]);
410                 else {
411                         if((x%2) == 0)
412                                 fullpath += "&" + encodeURIComponent(key_value_array[x]);
413                         if((x%2) != 0)
414                                 fullpath += "=" + encodeURIComponent(key_value_array[x]);
415                 }
416         }
417
418         debug("Redirecting to " + fullpath );
419         globalAppFrame.location.href = fullpath;
420
421 }
422         
423
424
425 /* 
426         the paramObj contains cgi params as object attributes 
427         -> paramObj.__paramName
428         paramName is the name of the parameter.  the '__' is there to
429         differentiate the paramName from other object attributes.
430         */
431 function build_param_array() {
432         var paramArray = new Array();
433         for( var p in paramObj ) {
434                 if( p.substr(0,2) == "__" ) {
435                         var name = p.substr(2,p.length - 1);
436                         paramArray.push(name)
437                         paramArray.push(paramObj[p])
438                 }
439         }
440         return paramArray;
441 }
442
443
444 var evtCache = new Object();
445 function EventListener(bool_callback, done_callback, name, usr_object) {
446         this.bool_callback = bool_callback;
447         this.done_callback = done_callback;
448         this.interval = 100;
449         this.obj = usr_object;
450         this.complete = false;
451         evtCache["___" + name] = this;
452 }
453
454
455 //EventListener.prototype.poll = function() {
456 function eventPoll(name) {
457         var obj = evtCache["___" + name];
458         if(obj == null)
459                 throw "No Listener by that name";
460
461         obj.complete = obj.bool_callback(obj.obj);
462         if(obj.complete)
463                 obj.done_callback(obj.obj);
464         else {
465                 debug("Setting timeout for next poll..");
466                 setTimeout("eventPoll('" + name + "')", obj.interval );
467         }
468 }
469
470         
471
472
473 function swapClass(obj, class1, class2 ) {
474         if( obj.className.indexOf(class1) != -1 ) {
475                 remove_css_class(obj, class1);
476                 add_css_class(obj,class2);
477         } else {
478                 remove_css_class(obj, class2);
479                 add_css_class(obj,class1);
480         }
481 }
482
483
484
485
486
487
488 function findPosX(obj)
489 {
490                 var curleft = 0;
491                         if (obj.offsetParent)
492                                         {
493                                                                 while (obj.offsetParent)
494                                                                                         {
495                                                                                                                         curleft += obj.offsetLeft
496                                                                                                                                                         obj = obj.offsetParent;
497                                                                                                                                         }
498                                                                         }
499                                 else if (obj.x)
500                                                         curleft += obj.x;
501                                         return curleft;
502 }
503
504 function findPosY(obj)
505 {
506                 var curtop = 0;
507                         if (obj.offsetParent)
508                                         {
509                                                                 while (obj.offsetParent)
510                                                                                         {
511                                                                                                                         curtop += obj.offsetTop
512                                                                                                                                                         obj = obj.offsetParent;
513                                                                                                                                         }
514                                                                         }
515                                 else if (obj.y)
516                                                         curtop += obj.y;
517                                         return curtop;
518 }
519
520
521
522 function getObjectHeight(obj)  {
523             var elem = obj;
524                      var result = 0;
525                               if (elem.offsetHeight) {
526                                                         result = elem.offsetHeight;
527                                                                       } else if (elem.clip && elem.clip.height) {
528                                                                                                 result = elem.clip.height;
529                                                                                                               } else if (elem.style && elem.style.pixelHeight) {
530                                                                                                                                         result = elem.style.pixelHeight;
531                                                                                                                                                       }
532                                             return parseInt(result);
533 }
534
535
536 function getObjectWidth(obj)  {
537             var elem = obj;
538                      var result = 0;
539                               if (elem.offsetWidth) {
540                                                         result = elem.offsetWidth;
541                                                                       } else if (elem.clip && elem.clip.width) {
542                                                                                                 result = elem.clip.width;
543                                                                                                               } else if (elem.style && elem.style.pixelWidth) {
544                                                                                                                                         result = elem.style.pixelWidth;
545                                                                                                                                                       }
546                                             return parseInt(result);
547 }
548
549
550
551 function getAppWindow() {
552         if( globalAppFrame)
553                 return globalAppFrame.window;
554         return window;
555 }
556
557 function getDocument() {
558         if(globalAppFrame)
559                 return globalAppFrame.document;
560         return document;
561 }
562
563 /* returns [x, y] coords of the mouse */
564 function getMousePos(e) {
565
566         var posx = 0;
567         var posy = 0;
568         if (!e) e = getAppWindow().event;
569         if (e.pageX || e.pageY) {
570                 posx = e.pageX;
571                 posy = e.pageY;
572         }
573         else if (e.clientX || e.clientY) {
574                 posx = e.clientX + getDocument().body.scrollLeft;
575                 posy = e.clientY + getDocument().body.scrollTop;
576         }
577
578         return [ posx, posy ];
579 }
580
581 function buildFunction(string) {
582         return eval("new Function(" + string + ")");
583 }
584
585
586
587
588