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