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