]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/OpenILS/global_util.js
LP#1086458: add way to clean up persist_helper event listeners
[working/Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / OpenILS / global_util.js
1     if(window.arguments && typeof window.arguments[0] == 'object' && typeof xulG == 'undefined') {
2         xulG = window.arguments[0];
3     }
4
5     function EventListenerList() {
6         this._listeners = [];
7         return this;
8     }
9
10     EventListenerList.prototype = {
11         'add' : function(node, type, listener, useCapture) {
12             try {
13                 node.addEventListener(type,listener,useCapture);
14                 this._listeners.push({
15                     'node' : node,
16                     'type' : type,
17                     'listener' : listener,
18                     'useCapture' : useCapture
19                 });
20             } catch(E) {
21                 alert(location.href + ' Error adding event listener ' + type + ': ' + E);
22             }
23         },
24
25         'removeAll' : function() {
26             try {
27                 if (typeof this._listeners != 'undefined') {
28                     for (var i = 0; i < this._listeners.length; i++) {
29                         this._listeners[i].node.removeEventListener(
30                             this._listeners[i].type,
31                             this._listeners[i].listener,
32                             this._listeners[i].useCapture
33                         );
34                     }
35                 }
36             } catch(E) {
37                 alert(location.href + ' Error in unloadEventListeners(): ' + E);
38             }
39         }
40     }
41
42     function $(id) { return document.getElementById(id); }
43
44     function oils_unsaved_data_V() {
45         JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
46         data.stash_retrieve();
47         if (typeof data.unsaved_data == 'undefined') { data.unsaved_data = 0; }
48         data.unsaved_data++;
49         window.oils_lock++;
50         data.stash('unsaved_data');
51         dump('\n=-=-=-=-=\n');
52         dump('oils_unsaved_data_V for ' + location.href + '\n');
53         dump('incrementing window.oils_lock\n');
54         dump('incrementing data.unsaved_data\n');
55         dump('\twindow.oils_lock == ' + window.oils_lock + '\n');
56         dump('\tdata.unsaved_data == ' + data.unsaved_data + '\n');
57     }
58
59     function oils_unsaved_data_P(count) {
60         dump('\n=-=-=-=-=\n');
61         dump('oils_unsaved_data_P for ' + location.href + '\n');
62         if (!count) { count = 1; }
63         dump('decrementing window.oils_lock by ' + count + '\n');
64         window.oils_lock -= count;
65         if (window.oils_lock < 0) { window.oils_lock = 0; }
66         JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
67         data.stash_retrieve();
68         if (typeof data.unsaved_data == 'undefined') { data.unsaved_data = 0; }
69         dump('decrementing data.unsaved_data by ' + count + '\n');
70         data.unsaved_data -= count;
71         if (data.unsaved_data < 0) { data.unsaved_data = 0; }
72         data.stash('unsaved_data');
73         dump('\twindow.oils_lock == ' + window.oils_lock + '\n');
74         dump('\tdata.unsaved_data == ' + data.unsaved_data + '\n');
75     }
76
77     function oils_lock_page(params) {
78         dump('\n=-=-=-=-=\n');
79         dump('oils_lock_page for ' + location.href + '\n');
80         if (!params) { params = {}; }
81         if (window.oils_lock > 0) {
82             if (!params.allow_multiple_locks) {
83                 return window.oils_lock;
84             }
85         }
86         if (typeof xulG != 'undefined') {
87             if (typeof xulG.unlock_tab == 'function') {
88                 dump('\twith xulG.lock_tab\n');
89                 xulG.lock_tab();
90                 window.oils_lock++; // different window scope than the chrome of xulG.lock_tab
91             } else {
92                 dump('\twithout xulG.lock_tab\n');
93                 oils_unsaved_data_V();
94             }
95         } else {
96             dump('\twithout xulG.lock_tab\n');
97             oils_unsaved_data_V();
98         }
99         return window.oils_lock;
100     }
101
102     function oils_unlock_page(params) {
103         dump('\n=-=-=-=-=\n');
104         dump('oils_unlock_page for ' + location.href + '\n');
105         if (typeof xulG != 'undefined') {
106             if (typeof xulG.unlock_tab == 'function') {
107                 dump('\twith xulG.unlock_tab\n');
108                 xulG.unlock_tab();
109                 window.oils_lock--; // different window scope than the chrome of xulG.unlock_tab
110                 if (window.oils_lock < 0) { window.oils_lock = 0; }
111             } else {
112                 dump('\twithout xulG.unlock_tab\n');
113                 oils_unsaved_data_P();
114             }
115         } else {
116             dump('\twithout xulG.unlock_tab\n');
117             oils_unsaved_data_P();
118         }
119         return window.oils_lock;
120     }
121
122     window.oils_lock = 0;
123     dump('\n=-=-=-=-=\n');
124     dump('init window.oils_lock == ' + window.oils_lock + ' for ' + location.href + '\n');
125     window.addEventListener(
126         'close',
127         function(ev) {
128             try {
129                 dump('\n=-=-=-=-=\n');
130                 dump('oils_lock_page/oils_unlock_page onclose handler for ' + location.href + '\n');
131                 if (window.oils_lock > 0) {
132                     var confirmation = window.confirm($('offlineStrings').getString('menu.close_window.unsaved_data_warning'));
133                     if (!confirmation) {
134                         ev.preventDefault();
135                         return false;
136                     }
137                 }
138
139                 if (typeof xulG != 'undefined') {
140                     if (typeof xulG.unlock_tab == 'function') {
141                         xulG.unlock_tab();
142                     } else {
143                         oils_unsaved_data_P( window.oils_lock );
144                     }
145                 } else {
146                     oils_unsaved_data_P( window.oils_lock );
147                 }
148                 window.oils_lock = 0;
149                 dump('forcing window.oils_lock == ' + window.oils_lock + '\n');
150
151                 // Dispatching the window close event doesn't always close the window, even though the event does happen
152                 setTimeout(
153                     function() {
154                         try {
155                             window.close();
156                         } catch(E) {
157                             dump('Error inside global_util.js, onclose handler, setTimeout window.close KLUDGE: ' + E + '\n');
158                         }
159                     }, 0
160                 );
161
162                 return true;
163             } catch(E) {
164                 dump('Error inside global_util.js, onclose handler: ' + E + '\n');
165                 return true;
166             }
167         },
168         false
169     );
170
171     function ses(a,params) {
172         try {
173             if (!params) params = {};
174             var data;
175             if (params.data) {
176                 data = params.data; data.stash_retrieve();
177             } else {
178                 // This has been breaking in certain contexts, with an internal instantiation of util.error failing because of util.error being an object instead of the constructor function it should be
179                 JSAN.use('OpenILS.data'); data = new OpenILS.data(); data.stash_retrieve();
180             }
181
182             switch(a) {
183                 case 'staff' : return data.list.au[0]; break;
184                 case 'staff_id' : return data.list.au[0].id(); break;
185                 case 'staff_usrname' : return data.list.au[0].usrname(); break;
186                 case 'ws_name':
187                     return data.ws_name;
188                 break;
189                 case 'ws_id' :
190                     return data.list.au[0].wsid();
191                 break;
192                 case 'ws_ou' :
193                     return data.list.au[0].ws_ou();
194                 break;
195                 case 'ws_ou_shortname' :
196                     return data.hash.aou[ data.list.au[0].ws_ou() ].shortname();
197                 break;
198                 case 'authtime' :
199                     return data.session.authtime;
200                 break;
201                 case 'key':
202                 default:
203                     return data.session.key;
204                 break;
205             }
206         } catch(E) {
207             alert(location.href + '\nError in global_utils.js, ses(): ' + E);
208             throw(E);
209         }
210     }
211
212     function font_helper() {
213         try {
214             JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
215             removeCSSClass(document.documentElement,'ALL_FONTS_LARGER');
216             removeCSSClass(document.documentElement,'ALL_FONTS_SMALLER');
217             removeCSSClass(document.documentElement,'ALL_FONTS_XX_SMALL');
218             removeCSSClass(document.documentElement,'ALL_FONTS_X_SMALL');
219             removeCSSClass(document.documentElement,'ALL_FONTS_SMALL');
220             removeCSSClass(document.documentElement,'ALL_FONTS_MEDIUM');
221             removeCSSClass(document.documentElement,'ALL_FONTS_LARGE');
222             removeCSSClass(document.documentElement,'ALL_FONTS_X_LARGE');
223             removeCSSClass(document.documentElement,'ALL_FONTS_XX_LARGE');
224             addCSSClass(document.documentElement,data.global_font_adjust);
225         } catch(E) {
226             var Strings = $('offlineStrings') || $('commonStrings');
227             alert(Strings.getFormattedString('openils.global_util.font_size.error', [E]));
228         }
229     }
230
231     function oils_persist(e,cancelable) {
232         try {
233             if (!e) { return; }
234             if (typeof cancelable == 'undefined') { cancelable = false; } 
235             var evt = document.createEvent("Events");
236             evt.initEvent( 'oils_persist', false, cancelable ); // event name, bubbles, cancelable
237             e.dispatchEvent(evt);
238         } catch(E) {
239             alert('Error with oils_persist():' + E);
240         }
241     }
242
243     function oils_persist_hostname() {
244         if(location.protocol == 'oils:') {
245             JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
246             return data.server_unadorned;
247         } else {
248             return location.hostname;
249         }
250     }
251
252     function persist_helper(base_key_suffix) {
253         try {
254             if (base_key_suffix) {
255                 base_key_suffix = base_key_suffix.replace(/[^A-Za-z]/g,'_') + '_';
256             } else {
257                 base_key_suffix = '';
258             }
259             window.persist_helper_event_listeners = new EventListenerList();
260
261             function gen_event_handler(etype,node) {
262                 return function(ev) {
263                     try {
264                         oils_persist(ev.target);
265                     } catch(E) {
266                         alert('Error in persist_helper, firing virtual event oils_persist after ' + etype + ' event on ' + node.nodeName + '.id = ' + node.id + ': ' + E);
267                     }
268                 };
269             };
270
271             function gen_oils_persist_handler(bk,node) {
272                 return function(ev) {
273                     try {
274                         var target;
275                         if (ev.target.nodeName == 'command') {
276                             target = node;
277                             if (ev.explicitOriginalTarget != node) return;
278                         } else {
279                             target = ev.target;
280                             if (target == window) {
281                                 target = window.document.documentElement;
282                             }
283                         }
284                         var filename = location.pathname.split('/')[ location.pathname.split('/').length - 1 ];
285                         var base_key = 'oils_persist_' + String(oils_persist_hostname() + '_' + filename + '_' + target.getAttribute('id')).replace('/','_','g') + '_' + base_key_suffix;
286                         var attribute_list = target.getAttribute('oils_persist').split(' ');
287                         dump('on_oils_persist: <<< ' + target.nodeName + '.id = ' + target.id + '\t' + bk + '\n');
288                         for (var j = 0; j < attribute_list.length; j++) {
289                             var key = base_key + attribute_list[j];
290                             var value;
291                             try {
292                                 value = encodeURI(target.getAttribute( attribute_list[j] ));
293                             } catch(E) {
294                                 dump('Error in persist_helper with encodeURI: ' + E + '\n');
295                                 value = target.getAttribute( attribute_list[j] );
296                             }
297                             if ( attribute_list[j] == 'checked' && ['checkbox','toolbarbutton'].indexOf( target.nodeName ) > -1 ) {
298                                 value = target.checked;
299                                 dump('\t' + value + ' <== .' + attribute_list[j] + '\n');
300                             } else if ( attribute_list[j] == 'value' && ['menulist'].indexOf( target.nodeName ) > -1 ) {
301                                 value = target.value;
302                                 dump('\t' + value + ' <== .' + attribute_list[j] + '\n');
303                             } else if ( attribute_list[j] == 'value' && ['textbox'].indexOf( target.nodeName ) > -1 ) {
304                                 value = target.value;
305                                 dump('\t' + value + ' <== .' + attribute_list[j] + '\n');
306                             } else if ( attribute_list[j] == 'sizemode' && ['window'].indexOf( target.nodeName ) > -1 ) {
307                                 value = window.windowState;
308                                 dump('\t' + value + ' <== window.windowState, @' + attribute_list[j] + '\n');
309                             } else if ( attribute_list[j] == 'height' && ['window'].indexOf( target.nodeName ) > -1 ) {
310                                 value = window.outerHeight;
311                                 dump('\t' + value + ' <== window.outerHeight, @' + attribute_list[j] + '\n');
312                             } else if ( attribute_list[j] == 'width' && ['window'].indexOf( target.nodeName ) > -1 ) {
313                                 value = window.outerWidth;
314                                 dump('\t' + value + ' <== window.outerWidth, @' + attribute_list[j] + '\n');
315                             } else {
316                                 dump('\t' + value + ' <== @' + attribute_list[j] + '\n');
317                             }
318                             prefs.setCharPref( key, value );
319                             // TODO: Need to add logic for splitter repositioning, grippy state, etc.
320                             // NOTE: oils_persist_peers and oils_persist="width" on those peers can help with the elements adjacent to a splitter
321                         }
322                         if (target.hasAttribute('oils_persist_peers') && ! ev.cancelable) { // We abuse the .cancelable field on the oils_persist event to prevent looping
323                             var peer_list = target.getAttribute('oils_persist_peers').split(' ');
324                             for (var j = 0; j < peer_list.length; j++) {
325                                 dump('on_oils_persist: dispatching oils_persist to peer ' + peer_list[j] + '\n');
326                                 oils_persist( document.getElementById( peer_list[j] ), true );
327                             } 
328                         }
329                     } catch(E) {
330                         alert('Error in persist_helper() event listener for ' + bk + ': ' + E);
331                     }
332                 };
333             }
334
335             var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
336             var nodes = document.getElementsByAttribute('oils_persist','*');
337             for (var i = 0; i < nodes.length; i++) {
338                 var filename = location.pathname.split('/')[ location.pathname.split('/').length - 1 ];
339                 var base_key = 'oils_persist_' + String(oils_persist_hostname() + '_' + filename + '_' + nodes[i].getAttribute('id')).replace('/','_','g') + '_' + base_key_suffix;
340                 var attribute_list = nodes[i].getAttribute('oils_persist').split(' ');
341                 dump('persist_helper: >>> ' + nodes[i].nodeName + '.id = ' + nodes[i].id + '\t' + base_key + '\n');
342                 for (var j = 0; j < attribute_list.length; j++) {
343                     var key = base_key + attribute_list[j];
344                     var has_key = prefs.prefHasUserValue(key);
345                     var value;
346                     try {
347                         value = has_key ? decodeURI(prefs.getCharPref(key)) : null;
348                     } catch(E) {
349                         dump('Error in persist_helper with decodeURI: ' + E + '\n');
350                         value = has_key ? prefs.getCharPref(key) : null;
351                     }
352                     if (value == 'true') { value = true; }
353                     if (value == 'false') { value = false; }
354                     if (has_key) {
355                         if ( attribute_list[j] == 'checked' && ['checkbox','toolbarbutton'].indexOf( nodes[i].nodeName ) > -1 ) {
356                             nodes[i].checked = value; 
357                             dump('\t' + value + ' ==> .' + attribute_list[j] + '\n');
358                             if (!value) {
359                                 nodes[i].removeAttribute('checked');
360                                 dump('\tremoving @checked\n');
361                             }
362                         } else if ( attribute_list[j] == 'value' && ['textbox'].indexOf( nodes[i].nodeName ) > -1 ) {
363                             nodes[i].value = value;
364                             dump('\t' + value + ' ==> .' + attribute_list[j] + '\n');
365                         } else if ( attribute_list[j] == 'value' && ['menulist'].indexOf( nodes[i].nodeName ) > -1 ) {
366                             nodes[i].value = value;
367                             dump('\t' + value + ' ==> .' + attribute_list[j] + '\n');       
368                         } else if ( attribute_list[j] == 'sizemode' && ['window'].indexOf( nodes[i].nodeName ) > -1 ) {
369                             switch(value) {
370                                 case window.STATE_MAXIMIZED:
371                                     window.maximize();
372                                     break;
373                                 case window.STATE_MINIMIZED:
374                                     window.minimize();
375                                     break;
376                             };
377                             dump('\t' + value + ' ==> window.windowState, @' + attribute_list[j] + '\n');
378                         } else if ( attribute_list[j] == 'height' && ['window'].indexOf( nodes[i].nodeName ) > -1 ) {
379                             window.outerHeight = value;
380                             dump('\t' + value + ' ==> window.outerHeight, @' + attribute_list[j] + '\n');
381                         } else if ( attribute_list[j] == 'width' && ['window'].indexOf( nodes[i].nodeName ) > -1 ) {
382                             window.outerWidth = value;
383                             dump('\t' + value + ' ==> window.outerWidth, @' + attribute_list[j] + '\n');
384                         } else {
385                             nodes[i].setAttribute( attribute_list[j], value);
386                             dump('\t' + value + ' ==> @' + attribute_list[j] + '\n');
387                         }
388                     }
389                 }
390                 var cmd = nodes[i].getAttribute('command');
391                 var cmd_el = document.getElementById(cmd);
392                 if (nodes[i].disabled == false && nodes[i].hidden == false) {
393                     var no_poke = nodes[i].getAttribute('oils_persist_no_poke');
394                     if (no_poke && no_poke == 'true') {
395                         // Timing issue for some checkboxes; don't poke them with an event
396                         dump('\tnot poking\n');
397                     } else {
398                         if (cmd_el) {
399                             dump('\tpoking @command\n');
400                             var evt = document.createEvent("Events");
401                             evt.initEvent( 'command', true, true );
402                             cmd_el.dispatchEvent(evt);
403                         } else {
404                             dump('\tpoking\n');
405                             var evt = document.createEvent("Events");
406                             evt.initEvent( 'command', true, true );
407                             nodes[i].dispatchEvent(evt);
408                         }
409                     }
410                 }
411                 if (cmd_el) {
412                     window.persist_helper_event_listeners.add(cmd_el, 
413                         'command',
414                         gen_event_handler('command',cmd_el),
415                         false
416                     );
417                     window.persist_helper_event_listeners.add(cmd_el, 
418                         'oils_persist',
419                         gen_oils_persist_handler( base_key, nodes[i] ),
420                         false
421                     );
422                 } else {
423                     var node = nodes[i];
424                     var event_types = [];
425                     if (node.hasAttribute('oils_persist_events')) {
426                         var event_type_list = node.getAttribute('oils_persist_events').split(' ');
427                         for (var j = 0; j < event_type_list.length; j++) {
428                             event_types.push( event_type_list[j] );
429                         }
430                     } else {
431                         if (node.nodeName == 'textbox') { 
432                             event_types.push('change');
433                         } else if (node.nodeName == 'menulist') { 
434                             event_types.push('select');  
435                         } else if (node.nodeName == 'window') {
436                             event_types.push('resize'); 
437                             node = window; // xul window is an element of window.document
438                         } else {
439                             event_types.push('command'); 
440                         }
441                     }
442                     for (var j = 0; j < event_types.length; j++) {
443                         window.persist_helper_event_listeners.add(node, 
444                             event_types[j],
445                             gen_event_handler(event_types[j],node),
446                             false
447                         );
448                     }
449                     window.persist_helper_event_listeners.add(node, 
450                         'oils_persist',
451                         gen_oils_persist_handler( base_key, node ),
452                         false
453                     );
454                 }
455             }
456         } catch(E) {
457             alert('Error in persist_helper(): ' + E);
458         }
459     }
460
461     function persist_helper_cleanup() {
462         try {
463             window.persist_helper_event_listeners.removeAll();
464         } catch(E) {
465             alert('Error in persist_helper_cleanup(): ' + E);
466         }
467     }
468
469     function getKeys(o) {
470         var keys = [];
471         for (var k in o) keys.push(k);
472         return keys;
473     }
474
475     function get_contentWindow(frame) {
476         try {
477             if (frame && frame.contentWindow) {
478                 try {
479                     if (typeof frame.contentWindow.wrappedJSObject != 'undefined') {
480                                      return frame.contentWindow.wrappedJSObject;
481                           }
482                 } catch(E) {
483                     var Strings = $('offlineStrings') || $('commonStrings');
484                     alert(Strings.getFormattedString('openils.global_util.content_window_jsobject.error', [frame, E]));
485                 }
486                 return frame.contentWindow;
487             } else {
488                 return null;
489             }
490         } catch(E) {
491             var Strings = $('offlineStrings') || $('commonStrings');
492             alert(Strings.getFormattedString('openils.global_util.content_window.error', [frame, E]));
493         }
494     }
495
496     function xul_param(param_name,_params) {
497         /* By default, this function looks for a CGI-style query param identified by param_name.  If one isn't found, it then looks in xulG.  If one still isn't found, and _params.stash_name is true, it looks in the global xpcom stash for the field identified by stash_name.  If _params.concat is true, then it looks in all these places and concatenates the results.  There are also options for converting JSON to javascript objects, and clearing the xpcom stash_name field after retrieval.  Also added, ability to search a specific spot in the xpcom stash that implements a stack to hold xulG's for modal windows */
498         try {
499             //dump('xul_param('+param_name+','+js2JSON(_params)+')\n');
500             var value = undefined; if (!_params) _params = {};
501             if (typeof _params.no_cgi == 'undefined') {
502                 var cgi = new CGI();
503                 if (cgi.param(param_name)) {
504                     var x = cgi.param(param_name);
505                     //dump('\tfound via location.href = ' + x + '\n');
506                     if (typeof _params.JSON2js_if_cgi != 'undefined') {
507                         x = JSON2js( x );
508                         //dump('\tJSON2js = ' + x + '\n');
509                     }
510                     if (typeof _params.concat == 'undefined') {
511                         //alert(param_name + ' x = ' + x);
512                         return x; // value
513                     } else {
514                         if (value) {
515                             if (value.constructor != Array) value = [ value ];
516                             value = value.concat(x);
517                         } else {
518                             value = x;
519                         }
520                     }
521                 }
522             }
523             if (typeof _params.no_xulG == 'undefined') {
524                 if (typeof xulG == 'object' && typeof xulG[ param_name ] != 'undefined') {
525                     var x = xulG[ param_name ];
526                     //dump('\tfound via xulG = ' + x + '\n');
527                     if (typeof _params.JSON2js_if_xulG != 'undefined') {
528                         x = JSON2js( x );
529                         //dump('\tJSON2js = ' + x + '\n');
530                     }
531                     if (typeof _params.concat == 'undefined') {
532                         //alert(param_name + ' x = ' + x);
533                         return x; // value
534                     } else {
535                         if (value) {
536                             if (value.constructor != Array) value = [ value ];
537                             value = value.concat(x);
538                         } else {
539                             value = x;
540                         }
541                     }
542                 }
543             }
544             if (typeof _params.no_xpcom == 'undefined') {
545                 /* the field names used for temp variables in the global stash tend to be more unique than xuLG or CGI param names, to avoid collisions */
546                 if (typeof _params.stash_name != 'undefined') { 
547                     JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
548                     if (typeof data[ _params.stash_name ] != 'undefined') {
549                         var x = data[ _params.stash_name ];
550                         //dump('\tfound via xpcom = ' + x + '\n');
551                         if (typeof _params.JSON2js_if_xpcom != 'undefined') {
552                             x = JSON2js( x );
553                             //dump('\tJSON2js = ' + x + '\n');
554                         }
555                         if (_params.clear_xpcom) { 
556                             data[ _params.stash_name ] = undefined; data.stash( _params.stash_name ); 
557                         }
558                         if (typeof _params.concat == 'undefined') {
559                             //alert(param_name + ' x = ' + x);
560                             return x; // value
561                         } else {
562                             if (value) {
563                                 if (value.constructor != Array) value = [ value ];
564                                 value = value.concat(x);
565                             } else {
566                                 value = x;
567                             }
568                         }
569                     }
570                 }
571             }
572             //alert(param_name + ' value = ' + value);
573             return value;
574         } catch(E) {
575             dump('xul_param error: ' + E + '\n');
576         }
577     }
578
579     function get_bool(a) {
580         // Normal javascript interpretation except 'f' == false, per postgres, and 'F' == false, and '0' == false (newer JSON is returning '0' instead of 0 in cases)
581         // So false includes 'f', '', '0', 0, null, and undefined
582         if (a == 'f') return false;
583         if (a == 'F') return false;
584         if (a == '0') return false;
585         if (a) return true; else return false;
586     }
587
588     function get_localized_bool(a) {
589         var Strings = $('offlineStrings') || $('commonStrings');
590         return get_bool(a) ? Strings.getString('common.yes') : Strings.getString('common.no');
591     }
592
593     function get_db_true() {
594         return 't';
595     }
596
597     function get_db_false() {
598         return 'f';
599     }
600
601     function copy_to_clipboard(ev) {
602         try {
603             var text;
604             if (typeof ev == 'object') {
605                 if (typeof ev.target != 'undefined') {
606                     if (typeof ev.target.textContent != 'undefined') if (ev.target.textContent) text = ev.target.textContent;
607                     if (typeof ev.target.value != 'undefined') if (ev.target.value) text = ev.target.value;
608                 }
609             } else if (typeof ev == 'string') {
610                 text = ev;
611             }
612             const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"]
613                 .getService(Components.interfaces.nsIClipboardHelper);
614             gClipboardHelper.copyString(text);
615             var Strings = $('offlineStrings') || $('commonStrings');
616             // alert(Strings.getFormattedString('openils.global_util.clipboard', [text]));
617         } catch(E) {
618             var Strings = $('offlineStrings') || $('commonStrings');
619             alert(Strings.getFormattedString('openils.global_util.clipboard.error', [E]));    
620         }
621     }
622
623     function clear_the_cache() {
624         try {
625             var cacheClass         = Components.classes["@mozilla.org/network/cache-service;1"];
626             var cacheService    = cacheClass.getService(Components.interfaces.nsICacheService);
627             cacheService.evictEntries(Components.interfaces.nsICache.STORE_ON_DISK);
628             cacheService.evictEntries(Components.interfaces.nsICache.STORE_IN_MEMORY);
629         } catch(E) {
630             var Strings = $('offlineStrings') || $('commonStrings');
631             alert(Strings.getFormattedString('openils.global_util.clear_cache.error', [E]));
632         }
633     }
634
635     function toOpenWindowByType(inType, uri) {
636         var winopts = "chrome,extrachrome,menubar,resizable,scrollbars,status,toolbar";
637         window.open(uri, "_blank", winopts);
638     }
639
640     function url_prefix(url) {
641         var base_url = url.match(/^[^?/|]+/);
642         if(base_url) {
643             base_url = base_url[0];
644             if(urls[base_url])
645                 url = url.replace(/^[^?/|]+\|?/, urls[base_url]);
646         }
647         if (url.match(/^\//)) url = urls.remote + url;
648         if (! url.match(/^(http|https|chrome|oils):\/\//) && ! url.match(/^data:/) ) url = 'http://' + url;
649         dump('url_prefix = ' + url + '\n');
650         return url;
651     }
652
653     function widget_prompt(node,args) {
654         // args is an object that may contain the following keys: title, desc, ok_label, ok_accesskey, cancel_label, cancel_accesskey, access, method
655         // access may contain 'property' or 'attribute' or 'method' for retrieving the value from the node
656         // if 'method', then the method key will reference a function that returns the value
657         try {
658             if (!node) { return false; }
659             if (!args) { args = {}; }
660             args[ 'widget' ] = node;
661
662             var url = location.protocol == 'chrome'
663                 ? 'chrome://open_ils_staff_client/content/util/widget_prompt.xul'
664                 : '/xul/server/util/widget_prompt.xul';
665
666             JSAN.use('util.window'); var win = new util.window();
667             var my_xulG = win.open(
668                 url,
669                 args.title || 'widget_prompt',
670                 'chrome,modal',
671                 args
672             );
673
674             if (my_xulG.status == 'incomplete') {
675                 return false;
676             } else {
677                 return my_xulG.value;
678             }
679         } catch(E) {
680             alert('Error in global_utils.js, widget_prompt(): ' + E);
681         }
682     }