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