]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/OpenILS/global_util.js
Revert "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                 var cmd_el = document.getElementById(cmd);
355                 if (nodes[i].disabled == false && nodes[i].hidden == false) {
356                     var no_poke = nodes[i].getAttribute('oils_persist_no_poke');
357                     if (no_poke && no_poke == 'true') {
358                         // Timing issue for some checkboxes; don't poke them with an event
359                         dump('\tnot poking\n');
360                     } else {
361                         if (cmd_el) {
362                             dump('\tpoking @command\n');
363                             var evt = document.createEvent("Events");
364                             evt.initEvent( 'command', true, true );
365                             cmd_el.dispatchEvent(evt);
366                         } else {
367                             dump('\tpoking\n');
368                             var evt = document.createEvent("Events");
369                             evt.initEvent( 'command', true, true );
370                             nodes[i].dispatchEvent(evt);
371                         }
372                     }
373                 }
374                 if (cmd_el) {
375                     window.persist_helper_event_listeners.add(cmd_el, 
376                         'command',
377                         gen_event_handler('command',cmd_el),
378                         false
379                     );
380                     window.persist_helper_event_listeners.add(cmd_el, 
381                         'oils_persist',
382                         gen_oils_persist_handler( base_key, nodes[i] ),
383                         false
384                     );
385                 } else {
386                     var node = nodes[i];
387                     var event_types = [];
388                     if (node.hasAttribute('oils_persist_events')) {
389                         var event_type_list = node.getAttribute('oils_persist_events').split(' ');
390                         for (var j = 0; j < event_type_list.length; j++) {
391                             event_types.push( event_type_list[j] );
392                         }
393                     } else {
394                         if (node.nodeName == 'textbox') { 
395                             event_types.push('change');
396                         } else if (node.nodeName == 'menulist') { 
397                             event_types.push('select');  
398                         } else if (node.nodeName == 'window') {
399                             event_types.push('resize'); 
400                             node = window; // xul window is an element of window.document
401                         } else {
402                             event_types.push('command'); 
403                         }
404                     }
405                     for (var j = 0; j < event_types.length; j++) {
406                         window.persist_helper_event_listeners.add(node, 
407                             event_types[j],
408                             gen_event_handler(event_types[j],node),
409                             false
410                         );
411                     }
412                     window.persist_helper_event_listeners.add(node, 
413                         'oils_persist',
414                         gen_oils_persist_handler( base_key, node ),
415                         false
416                     );
417                 }
418             }
419         } catch(E) {
420             alert('Error in persist_helper(): ' + E);
421         }
422     }
423
424     function persist_helper_cleanup() {
425         try {
426             window.persist_helper_event_listeners.removeAll();
427         } catch(E) {
428             alert('Error in persist_helper_cleanup(): ' + E);
429         }
430     }
431
432     function getKeys(o) {
433         var keys = [];
434         for (var k in o) keys.push(k);
435         return keys;
436     }
437
438     function get_contentWindow(frame) {
439         try {
440             if (frame && frame.contentWindow) {
441                 try {
442                     if (typeof frame.contentWindow.wrappedJSObject != 'undefined') {
443                                      return frame.contentWindow.wrappedJSObject;
444                           }
445                 } catch(E) {
446                     var Strings = $('offlineStrings') || $('commonStrings');
447                     alert(Strings.getFormattedString('openils.global_util.content_window_jsobject.error', [frame, E]));
448                 }
449                 return frame.contentWindow;
450             } else {
451                 return null;
452             }
453         } catch(E) {
454             var Strings = $('offlineStrings') || $('commonStrings');
455             alert(Strings.getFormattedString('openils.global_util.content_window.error', [frame, E]));
456         }
457     }
458
459     function xul_param(param_name,_params) {
460         /* 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 */
461         try {
462             //dump('xul_param('+param_name+','+js2JSON(_params)+')\n');
463             var value = undefined; if (!_params) _params = {};
464             if (typeof _params.no_cgi == 'undefined') {
465                 var cgi = new CGI();
466                 if (cgi.param(param_name)) {
467                     var x = cgi.param(param_name);
468                     //dump('\tfound via location.href = ' + x + '\n');
469                     if (typeof _params.JSON2js_if_cgi != 'undefined') {
470                         x = JSON2js( x );
471                         //dump('\tJSON2js = ' + x + '\n');
472                     }
473                     if (typeof _params.concat == 'undefined') {
474                         //alert(param_name + ' x = ' + x);
475                         return x; // value
476                     } else {
477                         if (value) {
478                             if (value.constructor != Array) value = [ value ];
479                             value = value.concat(x);
480                         } else {
481                             value = x;
482                         }
483                     }
484                 }
485             }
486             if (typeof _params.no_xulG == 'undefined') {
487                 if (typeof xulG == 'object' && typeof xulG[ param_name ] != 'undefined') {
488                     var x = xulG[ param_name ];
489                     //dump('\tfound via xulG = ' + x + '\n');
490                     if (typeof _params.JSON2js_if_xulG != 'undefined') {
491                         x = JSON2js( x );
492                         //dump('\tJSON2js = ' + x + '\n');
493                     }
494                     if (typeof _params.concat == 'undefined') {
495                         //alert(param_name + ' x = ' + x);
496                         return x; // value
497                     } else {
498                         if (value) {
499                             if (value.constructor != Array) value = [ value ];
500                             value = value.concat(x);
501                         } else {
502                             value = x;
503                         }
504                     }
505                 }
506             }
507             if (typeof _params.no_xpcom == 'undefined') {
508                 /* 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 */
509                 if (typeof _params.stash_name != 'undefined') { 
510                     JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
511                     if (typeof data[ _params.stash_name ] != 'undefined') {
512                         var x = data[ _params.stash_name ];
513                         //dump('\tfound via xpcom = ' + x + '\n');
514                         if (typeof _params.JSON2js_if_xpcom != 'undefined') {
515                             x = JSON2js( x );
516                             //dump('\tJSON2js = ' + x + '\n');
517                         }
518                         if (_params.clear_xpcom) { 
519                             data[ _params.stash_name ] = undefined; data.stash( _params.stash_name ); 
520                         }
521                         if (typeof _params.concat == 'undefined') {
522                             //alert(param_name + ' x = ' + x);
523                             return x; // value
524                         } else {
525                             if (value) {
526                                 if (value.constructor != Array) value = [ value ];
527                                 value = value.concat(x);
528                             } else {
529                                 value = x;
530                             }
531                         }
532                     }
533                 }
534             }
535             //alert(param_name + ' value = ' + value);
536             return value;
537         } catch(E) {
538             dump('xul_param error: ' + E + '\n');
539         }
540     }
541
542     function get_bool(a) {
543         // Normal javascript interpretation except 'f' == false, per postgres, and 'F' == false, and '0' == false (newer JSON is returning '0' instead of 0 in cases)
544         // So false includes 'f', '', '0', 0, null, and undefined
545         if (a == 'f') return false;
546         if (a == 'F') return false;
547         if (a == '0') return false;
548         if (a) return true; else return false;
549     }
550
551     function get_localized_bool(a) {
552         var Strings = $('offlineStrings') || $('commonStrings');
553         return get_bool(a) ? Strings.getString('common.yes') : Strings.getString('common.no');
554     }
555
556     function get_db_true() {
557         return 't';
558     }
559
560     function get_db_false() {
561         return 'f';
562     }
563
564     function copy_to_clipboard(ev) {
565         try {
566             var text;
567             if (typeof ev == 'object') {
568                 if (typeof ev.target != 'undefined') {
569                     if (typeof ev.target.textContent != 'undefined') if (ev.target.textContent) text = ev.target.textContent;
570                     if (typeof ev.target.value != 'undefined') if (ev.target.value) text = ev.target.value;
571                 }
572             } else if (typeof ev == 'string') {
573                 text = ev;
574             }
575             const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"]
576                 .getService(Components.interfaces.nsIClipboardHelper);
577             gClipboardHelper.copyString(text);
578             var Strings = $('offlineStrings') || $('commonStrings');
579             // alert(Strings.getFormattedString('openils.global_util.clipboard', [text]));
580         } catch(E) {
581             var Strings = $('offlineStrings') || $('commonStrings');
582             alert(Strings.getFormattedString('openils.global_util.clipboard.error', [E]));    
583         }
584     }
585
586     function clear_the_cache() {
587         try {
588             var cacheClass         = Components.classes["@mozilla.org/network/cache-service;1"];
589             var cacheService    = cacheClass.getService(Components.interfaces.nsICacheService);
590             cacheService.evictEntries(Components.interfaces.nsICache.STORE_ON_DISK);
591             cacheService.evictEntries(Components.interfaces.nsICache.STORE_IN_MEMORY);
592         } catch(E) {
593             var Strings = $('offlineStrings') || $('commonStrings');
594             alert(Strings.getFormattedString('openils.global_util.clear_cache.error', [E]));
595         }
596     }
597
598     function toOpenWindowByType(inType, uri) {
599         var winopts = "chrome,extrachrome,menubar,resizable,scrollbars,status,toolbar";
600         window.open(uri, "_blank", winopts);
601     }
602
603     function url_prefix(url) {
604         var base_url = url.match(/^[^?/|]+/);
605         if(base_url) {
606             base_url = base_url[0];
607             if(urls[base_url])
608                 url = url.replace(/^[^?/|]+\|?/, urls[base_url]);
609         }
610         if (url.match(/^\//)) url = urls.remote + url;
611         if (! url.match(/^(http|https|chrome|oils):\/\//) && ! url.match(/^data:/) ) url = 'http://' + url;
612         dump('url_prefix = ' + url + '\n');
613         return url;
614     }
615
616     function widget_prompt(node,args) {
617         // args is an object that may contain the following keys: title, desc, ok_label, ok_accesskey, cancel_label, cancel_accesskey, access, method
618         // access may contain 'property' or 'attribute' or 'method' for retrieving the value from the node
619         // if 'method', then the method key will reference a function that returns the value
620         try {
621             if (!node) { return false; }
622             if (!args) { args = {}; }
623             args[ 'widget' ] = node;
624
625             var url = location.protocol == 'chrome'
626                 ? 'chrome://open_ils_staff_client/content/util/widget_prompt.xul'
627                 : '/xul/server/util/widget_prompt.xul';
628
629             JSAN.use('util.window'); var win = new util.window();
630             var my_xulG = win.open(
631                 url,
632                 args.title || 'widget_prompt',
633                 'chrome,modal',
634                 args
635             );
636
637             if (my_xulG.status == 'incomplete') {
638                 return false;
639             } else {
640                 return my_xulG.value;
641             }
642         } catch(E) {
643             alert('Error in global_utils.js, widget_prompt(): ' + E);
644         }
645     }
646
647
648     window.addEventListener(
649         'load',
650         function(ev) {
651             try {
652                 if (window.oils_autoloaded) { return; }
653                 JSAN.use('addon.autoloader');
654                 window.oils_autoloaded = new addon.autoloader();
655             } catch(E) {
656                 dump('Error in global_util.js with addon.autoloader: ' + E + '\n');
657             }
658         },
659         false
660     );