]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/OpenILS/global_util.js
LP#1086458: add class to manage 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
260             function gen_event_handler(etype,node) {
261                 return function(ev) {
262                     try {
263                         oils_persist(ev.target);
264                     } catch(E) {
265                         alert('Error in persist_helper, firing virtual event oils_persist after ' + etype + ' event on ' + node.nodeName + '.id = ' + node.id + ': ' + E);
266                     }
267                 };
268             };
269
270             function gen_oils_persist_handler(bk,node) {
271                 return function(ev) {
272                     try {
273                         var target;
274                         if (ev.target.nodeName == 'command') {
275                             target = node;
276                             if (ev.explicitOriginalTarget != node) return;
277                         } else {
278                             target = ev.target;
279                             if (target == window) {
280                                 target = window.document.documentElement;
281                             }
282                         }
283                         var filename = location.pathname.split('/')[ location.pathname.split('/').length - 1 ];
284                         var base_key = 'oils_persist_' + String(oils_persist_hostname() + '_' + filename + '_' + target.getAttribute('id')).replace('/','_','g') + '_' + base_key_suffix;
285                         var attribute_list = target.getAttribute('oils_persist').split(' ');
286                         dump('on_oils_persist: <<< ' + target.nodeName + '.id = ' + target.id + '\t' + bk + '\n');
287                         for (var j = 0; j < attribute_list.length; j++) {
288                             var key = base_key + attribute_list[j];
289                             var value;
290                             try {
291                                 value = encodeURI(target.getAttribute( attribute_list[j] ));
292                             } catch(E) {
293                                 dump('Error in persist_helper with encodeURI: ' + E + '\n');
294                                 value = target.getAttribute( attribute_list[j] );
295                             }
296                             if ( attribute_list[j] == 'checked' && ['checkbox','toolbarbutton'].indexOf( target.nodeName ) > -1 ) {
297                                 value = target.checked;
298                                 dump('\t' + value + ' <== .' + attribute_list[j] + '\n');
299                             } else if ( attribute_list[j] == 'value' && ['menulist'].indexOf( target.nodeName ) > -1 ) {
300                                 value = target.value;
301                                 dump('\t' + value + ' <== .' + attribute_list[j] + '\n');
302                             } else if ( attribute_list[j] == 'value' && ['textbox'].indexOf( target.nodeName ) > -1 ) {
303                                 value = target.value;
304                                 dump('\t' + value + ' <== .' + attribute_list[j] + '\n');
305                             } else if ( attribute_list[j] == 'sizemode' && ['window'].indexOf( target.nodeName ) > -1 ) {
306                                 value = window.windowState;
307                                 dump('\t' + value + ' <== window.windowState, @' + attribute_list[j] + '\n');
308                             } else if ( attribute_list[j] == 'height' && ['window'].indexOf( target.nodeName ) > -1 ) {
309                                 value = window.outerHeight;
310                                 dump('\t' + value + ' <== window.outerHeight, @' + attribute_list[j] + '\n');
311                             } else if ( attribute_list[j] == 'width' && ['window'].indexOf( target.nodeName ) > -1 ) {
312                                 value = window.outerWidth;
313                                 dump('\t' + value + ' <== window.outerWidth, @' + attribute_list[j] + '\n');
314                             } else {
315                                 dump('\t' + value + ' <== @' + attribute_list[j] + '\n');
316                             }
317                             prefs.setCharPref( key, value );
318                             // TODO: Need to add logic for splitter repositioning, grippy state, etc.
319                             // NOTE: oils_persist_peers and oils_persist="width" on those peers can help with the elements adjacent to a splitter
320                         }
321                         if (target.hasAttribute('oils_persist_peers') && ! ev.cancelable) { // We abuse the .cancelable field on the oils_persist event to prevent looping
322                             var peer_list = target.getAttribute('oils_persist_peers').split(' ');
323                             for (var j = 0; j < peer_list.length; j++) {
324                                 dump('on_oils_persist: dispatching oils_persist to peer ' + peer_list[j] + '\n');
325                                 oils_persist( document.getElementById( peer_list[j] ), true );
326                             } 
327                         }
328                     } catch(E) {
329                         alert('Error in persist_helper() event listener for ' + bk + ': ' + E);
330                     }
331                 };
332             }
333
334             var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
335             var nodes = document.getElementsByAttribute('oils_persist','*');
336             for (var i = 0; i < nodes.length; i++) {
337                 var filename = location.pathname.split('/')[ location.pathname.split('/').length - 1 ];
338                 var base_key = 'oils_persist_' + String(oils_persist_hostname() + '_' + filename + '_' + nodes[i].getAttribute('id')).replace('/','_','g') + '_' + base_key_suffix;
339                 var attribute_list = nodes[i].getAttribute('oils_persist').split(' ');
340                 dump('persist_helper: >>> ' + nodes[i].nodeName + '.id = ' + nodes[i].id + '\t' + base_key + '\n');
341                 for (var j = 0; j < attribute_list.length; j++) {
342                     var key = base_key + attribute_list[j];
343                     var has_key = prefs.prefHasUserValue(key);
344                     var value;
345                     try {
346                         value = has_key ? decodeURI(prefs.getCharPref(key)) : null;
347                     } catch(E) {
348                         dump('Error in persist_helper with decodeURI: ' + E + '\n');
349                         value = has_key ? prefs.getCharPref(key) : null;
350                     }
351                     if (value == 'true') { value = true; }
352                     if (value == 'false') { value = false; }
353                     if (has_key) {
354                         if ( attribute_list[j] == 'checked' && ['checkbox','toolbarbutton'].indexOf( nodes[i].nodeName ) > -1 ) {
355                             nodes[i].checked = value; 
356                             dump('\t' + value + ' ==> .' + attribute_list[j] + '\n');
357                             if (!value) {
358                                 nodes[i].removeAttribute('checked');
359                                 dump('\tremoving @checked\n');
360                             }
361                         } else if ( attribute_list[j] == 'value' && ['textbox'].indexOf( nodes[i].nodeName ) > -1 ) {
362                             nodes[i].value = value;
363                             dump('\t' + value + ' ==> .' + attribute_list[j] + '\n');
364                         } else if ( attribute_list[j] == 'value' && ['menulist'].indexOf( nodes[i].nodeName ) > -1 ) {
365                             nodes[i].value = value;
366                             dump('\t' + value + ' ==> .' + attribute_list[j] + '\n');       
367                         } else if ( attribute_list[j] == 'sizemode' && ['window'].indexOf( nodes[i].nodeName ) > -1 ) {
368                             switch(value) {
369                                 case window.STATE_MAXIMIZED:
370                                     window.maximize();
371                                     break;
372                                 case window.STATE_MINIMIZED:
373                                     window.minimize();
374                                     break;
375                             };
376                             dump('\t' + value + ' ==> window.windowState, @' + attribute_list[j] + '\n');
377                         } else if ( attribute_list[j] == 'height' && ['window'].indexOf( nodes[i].nodeName ) > -1 ) {
378                             window.outerHeight = value;
379                             dump('\t' + value + ' ==> window.outerHeight, @' + attribute_list[j] + '\n');
380                         } else if ( attribute_list[j] == 'width' && ['window'].indexOf( nodes[i].nodeName ) > -1 ) {
381                             window.outerWidth = value;
382                             dump('\t' + value + ' ==> window.outerWidth, @' + attribute_list[j] + '\n');
383                         } else {
384                             nodes[i].setAttribute( attribute_list[j], value);
385                             dump('\t' + value + ' ==> @' + attribute_list[j] + '\n');
386                         }
387                     }
388                 }
389                 var cmd = nodes[i].getAttribute('command');
390                 var cmd_el = document.getElementById(cmd);
391                 if (nodes[i].disabled == false && nodes[i].hidden == false) {
392                     var no_poke = nodes[i].getAttribute('oils_persist_no_poke');
393                     if (no_poke && no_poke == 'true') {
394                         // Timing issue for some checkboxes; don't poke them with an event
395                         dump('\tnot poking\n');
396                     } else {
397                         if (cmd_el) {
398                             dump('\tpoking @command\n');
399                             var evt = document.createEvent("Events");
400                             evt.initEvent( 'command', true, true );
401                             cmd_el.dispatchEvent(evt);
402                         } else {
403                             dump('\tpoking\n');
404                             var evt = document.createEvent("Events");
405                             evt.initEvent( 'command', true, true );
406                             nodes[i].dispatchEvent(evt);
407                         }
408                     }
409                 }
410                 if (cmd_el) {
411                     cmd_el.addEventListener(
412                         'command',
413                         gen_event_handler('command',cmd_el),
414                         false
415                     );
416                     cmd_el.addEventListener(
417                         'oils_persist',
418                         gen_oils_persist_handler( base_key, nodes[i] ),
419                         false
420                     );
421                 } else {
422                     var node = nodes[i];
423                     var event_types = [];
424                     if (node.hasAttribute('oils_persist_events')) {
425                         var event_type_list = node.getAttribute('oils_persist_events').split(' ');
426                         for (var j = 0; j < event_type_list.length; j++) {
427                             event_types.push( event_type_list[j] );
428                         }
429                     } else {
430                         if (node.nodeName == 'textbox') { 
431                             event_types.push('change');
432                         } else if (node.nodeName == 'menulist') { 
433                             event_types.push('select');  
434                         } else if (node.nodeName == 'window') {
435                             event_types.push('resize'); 
436                             node = window; // xul window is an element of window.document
437                         } else {
438                             event_types.push('command'); 
439                         }
440                     }
441                     for (var j = 0; j < event_types.length; j++) {
442                         node.addEventListener(
443                             event_types[j],
444                             gen_event_handler(event_types[j],node),
445                             false
446                         );
447                     }
448                     node.addEventListener(
449                         'oils_persist',
450                         gen_oils_persist_handler( base_key, node ),
451                         false
452                     );
453                 }
454             }
455         } catch(E) {
456             alert('Error in persist_helper(): ' + E);
457         }
458     }
459
460     function getKeys(o) {
461         var keys = [];
462         for (var k in o) keys.push(k);
463         return keys;
464     }
465
466     function get_contentWindow(frame) {
467         try {
468             if (frame && frame.contentWindow) {
469                 try {
470                     if (typeof frame.contentWindow.wrappedJSObject != 'undefined') {
471                                      return frame.contentWindow.wrappedJSObject;
472                           }
473                 } catch(E) {
474                     var Strings = $('offlineStrings') || $('commonStrings');
475                     alert(Strings.getFormattedString('openils.global_util.content_window_jsobject.error', [frame, E]));
476                 }
477                 return frame.contentWindow;
478             } else {
479                 return null;
480             }
481         } catch(E) {
482             var Strings = $('offlineStrings') || $('commonStrings');
483             alert(Strings.getFormattedString('openils.global_util.content_window.error', [frame, E]));
484         }
485     }
486
487     function xul_param(param_name,_params) {
488         /* 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 */
489         try {
490             //dump('xul_param('+param_name+','+js2JSON(_params)+')\n');
491             var value = undefined; if (!_params) _params = {};
492             if (typeof _params.no_cgi == 'undefined') {
493                 var cgi = new CGI();
494                 if (cgi.param(param_name)) {
495                     var x = cgi.param(param_name);
496                     //dump('\tfound via location.href = ' + x + '\n');
497                     if (typeof _params.JSON2js_if_cgi != 'undefined') {
498                         x = JSON2js( x );
499                         //dump('\tJSON2js = ' + x + '\n');
500                     }
501                     if (typeof _params.concat == 'undefined') {
502                         //alert(param_name + ' x = ' + x);
503                         return x; // value
504                     } else {
505                         if (value) {
506                             if (value.constructor != Array) value = [ value ];
507                             value = value.concat(x);
508                         } else {
509                             value = x;
510                         }
511                     }
512                 }
513             }
514             if (typeof _params.no_xulG == 'undefined') {
515                 if (typeof xulG == 'object' && typeof xulG[ param_name ] != 'undefined') {
516                     var x = xulG[ param_name ];
517                     //dump('\tfound via xulG = ' + x + '\n');
518                     if (typeof _params.JSON2js_if_xulG != 'undefined') {
519                         x = JSON2js( x );
520                         //dump('\tJSON2js = ' + x + '\n');
521                     }
522                     if (typeof _params.concat == 'undefined') {
523                         //alert(param_name + ' x = ' + x);
524                         return x; // value
525                     } else {
526                         if (value) {
527                             if (value.constructor != Array) value = [ value ];
528                             value = value.concat(x);
529                         } else {
530                             value = x;
531                         }
532                     }
533                 }
534             }
535             if (typeof _params.no_xpcom == 'undefined') {
536                 /* 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 */
537                 if (typeof _params.stash_name != 'undefined') { 
538                     JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
539                     if (typeof data[ _params.stash_name ] != 'undefined') {
540                         var x = data[ _params.stash_name ];
541                         //dump('\tfound via xpcom = ' + x + '\n');
542                         if (typeof _params.JSON2js_if_xpcom != 'undefined') {
543                             x = JSON2js( x );
544                             //dump('\tJSON2js = ' + x + '\n');
545                         }
546                         if (_params.clear_xpcom) { 
547                             data[ _params.stash_name ] = undefined; data.stash( _params.stash_name ); 
548                         }
549                         if (typeof _params.concat == 'undefined') {
550                             //alert(param_name + ' x = ' + x);
551                             return x; // value
552                         } else {
553                             if (value) {
554                                 if (value.constructor != Array) value = [ value ];
555                                 value = value.concat(x);
556                             } else {
557                                 value = x;
558                             }
559                         }
560                     }
561                 }
562             }
563             //alert(param_name + ' value = ' + value);
564             return value;
565         } catch(E) {
566             dump('xul_param error: ' + E + '\n');
567         }
568     }
569
570     function get_bool(a) {
571         // Normal javascript interpretation except 'f' == false, per postgres, and 'F' == false, and '0' == false (newer JSON is returning '0' instead of 0 in cases)
572         // So false includes 'f', '', '0', 0, null, and undefined
573         if (a == 'f') return false;
574         if (a == 'F') return false;
575         if (a == '0') return false;
576         if (a) return true; else return false;
577     }
578
579     function get_localized_bool(a) {
580         var Strings = $('offlineStrings') || $('commonStrings');
581         return get_bool(a) ? Strings.getString('common.yes') : Strings.getString('common.no');
582     }
583
584     function get_db_true() {
585         return 't';
586     }
587
588     function get_db_false() {
589         return 'f';
590     }
591
592     function copy_to_clipboard(ev) {
593         try {
594             var text;
595             if (typeof ev == 'object') {
596                 if (typeof ev.target != 'undefined') {
597                     if (typeof ev.target.textContent != 'undefined') if (ev.target.textContent) text = ev.target.textContent;
598                     if (typeof ev.target.value != 'undefined') if (ev.target.value) text = ev.target.value;
599                 }
600             } else if (typeof ev == 'string') {
601                 text = ev;
602             }
603             const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"]
604                 .getService(Components.interfaces.nsIClipboardHelper);
605             gClipboardHelper.copyString(text);
606             var Strings = $('offlineStrings') || $('commonStrings');
607             // alert(Strings.getFormattedString('openils.global_util.clipboard', [text]));
608         } catch(E) {
609             var Strings = $('offlineStrings') || $('commonStrings');
610             alert(Strings.getFormattedString('openils.global_util.clipboard.error', [E]));    
611         }
612     }
613
614     function clear_the_cache() {
615         try {
616             var cacheClass         = Components.classes["@mozilla.org/network/cache-service;1"];
617             var cacheService    = cacheClass.getService(Components.interfaces.nsICacheService);
618             cacheService.evictEntries(Components.interfaces.nsICache.STORE_ON_DISK);
619             cacheService.evictEntries(Components.interfaces.nsICache.STORE_IN_MEMORY);
620         } catch(E) {
621             var Strings = $('offlineStrings') || $('commonStrings');
622             alert(Strings.getFormattedString('openils.global_util.clear_cache.error', [E]));
623         }
624     }
625
626     function toOpenWindowByType(inType, uri) {
627         var winopts = "chrome,extrachrome,menubar,resizable,scrollbars,status,toolbar";
628         window.open(uri, "_blank", winopts);
629     }
630
631     function url_prefix(url) {
632         var base_url = url.match(/^[^?/|]+/);
633         if(base_url) {
634             base_url = base_url[0];
635             if(urls[base_url])
636                 url = url.replace(/^[^?/|]+\|?/, urls[base_url]);
637         }
638         if (url.match(/^\//)) url = urls.remote + url;
639         if (! url.match(/^(http|https|chrome|oils):\/\//) && ! url.match(/^data:/) ) url = 'http://' + url;
640         dump('url_prefix = ' + url + '\n');
641         return url;
642     }
643
644     function widget_prompt(node,args) {
645         // args is an object that may contain the following keys: title, desc, ok_label, ok_accesskey, cancel_label, cancel_accesskey, access, method
646         // access may contain 'property' or 'attribute' or 'method' for retrieving the value from the node
647         // if 'method', then the method key will reference a function that returns the value
648         try {
649             if (!node) { return false; }
650             if (!args) { args = {}; }
651             args[ 'widget' ] = node;
652
653             var url = location.protocol == 'chrome'
654                 ? 'chrome://open_ils_staff_client/content/util/widget_prompt.xul'
655                 : '/xul/server/util/widget_prompt.xul';
656
657             JSAN.use('util.window'); var win = new util.window();
658             var my_xulG = win.open(
659                 url,
660                 args.title || 'widget_prompt',
661                 'chrome,modal',
662                 args
663             );
664
665             if (my_xulG.status == 'incomplete') {
666                 return false;
667             } else {
668                 return my_xulG.value;
669             }
670         } catch(E) {
671             alert('Error in global_utils.js, widget_prompt(): ' + E);
672         }
673     }