1 if(window.arguments && typeof window.arguments[0] == 'object' && typeof xulG == 'undefined') {
2 xulG = window.arguments[0];
5 function EventListenerList() {
10 EventListenerList.prototype = {
11 'add' : function(node, type, listener, useCapture) {
13 node.addEventListener(type,listener,useCapture);
14 this._listeners.push({
17 'listener' : listener,
18 'useCapture' : useCapture
21 alert(location.href + ' Error adding event listener ' + type + ': ' + E);
25 'removeAll' : function() {
27 if (typeof this._listeners != 'undefined') {
28 for (var i = 0; i < this._listeners.length; i++) {
29 this._listeners[i].node.removeEventListener(
30 this._listeners[i].type,
31 this._listeners[i].listener,
32 this._listeners[i].useCapture
37 alert(location.href + ' Error in unloadEventListeners(): ' + E);
42 function $(id) { return document.getElementById(id); }
44 function oils_unsaved_data_V() {
45 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
46 data.stash_retrieve();
47 if (typeof data.unsaved_data == 'undefined') { data.unsaved_data = 0; }
50 data.stash('unsaved_data');
51 dump('\n=-=-=-=-=\n');
52 dump('oils_unsaved_data_V for ' + location.href + '\n');
53 dump('incrementing window.oils_lock\n');
54 dump('incrementing data.unsaved_data\n');
55 dump('\twindow.oils_lock == ' + window.oils_lock + '\n');
56 dump('\tdata.unsaved_data == ' + data.unsaved_data + '\n');
59 function oils_unsaved_data_P(count) {
60 dump('\n=-=-=-=-=\n');
61 dump('oils_unsaved_data_P for ' + location.href + '\n');
62 if (!count) { count = 1; }
63 dump('decrementing window.oils_lock by ' + count + '\n');
64 window.oils_lock -= count;
65 if (window.oils_lock < 0) { window.oils_lock = 0; }
66 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
67 data.stash_retrieve();
68 if (typeof data.unsaved_data == 'undefined') { data.unsaved_data = 0; }
69 dump('decrementing data.unsaved_data by ' + count + '\n');
70 data.unsaved_data -= count;
71 if (data.unsaved_data < 0) { data.unsaved_data = 0; }
72 data.stash('unsaved_data');
73 dump('\twindow.oils_lock == ' + window.oils_lock + '\n');
74 dump('\tdata.unsaved_data == ' + data.unsaved_data + '\n');
77 function oils_lock_page(params) {
78 dump('\n=-=-=-=-=\n');
79 dump('oils_lock_page for ' + location.href + '\n');
80 if (!params) { params = {}; }
81 if (window.oils_lock > 0) {
82 if (!params.allow_multiple_locks) {
83 return window.oils_lock;
86 if (typeof xulG != 'undefined') {
87 if (typeof xulG.unlock_tab == 'function') {
88 dump('\twith xulG.lock_tab\n');
90 window.oils_lock++; // different window scope than the chrome of xulG.lock_tab
92 dump('\twithout xulG.lock_tab\n');
93 oils_unsaved_data_V();
96 dump('\twithout xulG.lock_tab\n');
97 oils_unsaved_data_V();
99 return window.oils_lock;
102 function oils_unlock_page(params) {
103 dump('\n=-=-=-=-=\n');
104 dump('oils_unlock_page for ' + location.href + '\n');
105 if (typeof xulG != 'undefined') {
106 if (typeof xulG.unlock_tab == 'function') {
107 dump('\twith xulG.unlock_tab\n');
109 window.oils_lock--; // different window scope than the chrome of xulG.unlock_tab
110 if (window.oils_lock < 0) { window.oils_lock = 0; }
112 dump('\twithout xulG.unlock_tab\n');
113 oils_unsaved_data_P();
116 dump('\twithout xulG.unlock_tab\n');
117 oils_unsaved_data_P();
119 return window.oils_lock;
122 window.oils_lock = 0;
123 dump('\n=-=-=-=-=\n');
124 dump('init window.oils_lock == ' + window.oils_lock + ' for ' + location.href + '\n');
125 window.addEventListener(
129 dump('\n=-=-=-=-=\n');
130 dump('oils_lock_page/oils_unlock_page onclose handler for ' + location.href + '\n');
131 if (window.oils_lock > 0) {
132 var confirmation = window.confirm($('offlineStrings').getString('menu.close_window.unsaved_data_warning'));
139 if (typeof xulG != 'undefined') {
140 if (typeof xulG.unlock_tab == 'function') {
143 oils_unsaved_data_P( window.oils_lock );
146 oils_unsaved_data_P( window.oils_lock );
148 window.oils_lock = 0;
149 dump('forcing window.oils_lock == ' + window.oils_lock + '\n');
151 // Dispatching the window close event doesn't always close the window, even though the event does happen
157 dump('Error inside global_util.js, onclose handler, setTimeout window.close KLUDGE: ' + E + '\n');
164 dump('Error inside global_util.js, onclose handler: ' + E + '\n');
171 function ses(a,params) {
173 if (!params) params = {};
176 data = params.data; data.stash_retrieve();
178 // This has been breaking in certain contexts, with an internal instantiation of util.error failing because of util.error being an object instead of the constructor function it should be
179 JSAN.use('OpenILS.data'); data = new OpenILS.data(); data.stash_retrieve();
183 case 'staff' : return data.list.au[0]; break;
184 case 'staff_id' : return data.list.au[0].id(); break;
185 case 'staff_usrname' : return data.list.au[0].usrname(); break;
190 return data.list.au[0].wsid();
193 return data.list.au[0].ws_ou();
195 case 'ws_ou_shortname' :
196 return data.hash.aou[ data.list.au[0].ws_ou() ].shortname();
199 return data.session.authtime;
203 return data.session.key;
207 alert(location.href + '\nError in global_utils.js, ses(): ' + E);
212 function font_helper() {
214 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
215 removeCSSClass(document.documentElement,'ALL_FONTS_LARGER');
216 removeCSSClass(document.documentElement,'ALL_FONTS_SMALLER');
217 removeCSSClass(document.documentElement,'ALL_FONTS_XX_SMALL');
218 removeCSSClass(document.documentElement,'ALL_FONTS_X_SMALL');
219 removeCSSClass(document.documentElement,'ALL_FONTS_SMALL');
220 removeCSSClass(document.documentElement,'ALL_FONTS_MEDIUM');
221 removeCSSClass(document.documentElement,'ALL_FONTS_LARGE');
222 removeCSSClass(document.documentElement,'ALL_FONTS_X_LARGE');
223 removeCSSClass(document.documentElement,'ALL_FONTS_XX_LARGE');
224 addCSSClass(document.documentElement,data.global_font_adjust);
226 var Strings = $('offlineStrings') || $('commonStrings');
227 alert(Strings.getFormattedString('openils.global_util.font_size.error', [E]));
231 function oils_persist(e,cancelable) {
234 if (typeof cancelable == 'undefined') { cancelable = false; }
235 var evt = document.createEvent("Events");
236 evt.initEvent( 'oils_persist', false, cancelable ); // event name, bubbles, cancelable
237 e.dispatchEvent(evt);
239 alert('Error with oils_persist():' + E);
243 function oils_persist_hostname() {
244 if(location.protocol == 'oils:') {
245 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
246 return data.server_unadorned;
248 return location.hostname;
252 function persist_helper(base_key_suffix) {
254 if (base_key_suffix) {
255 base_key_suffix = base_key_suffix.replace(/[^A-Za-z]/g,'_') + '_';
257 base_key_suffix = '';
259 window.persist_helper_event_listeners = new EventListenerList();
261 function gen_event_handler(etype,node) {
262 return function(ev) {
264 oils_persist(ev.target);
266 alert('Error in persist_helper, firing virtual event oils_persist after ' + etype + ' event on ' + node.nodeName + '.id = ' + node.id + ': ' + E);
271 function gen_oils_persist_handler(bk,node) {
272 return function(ev) {
275 if (ev.target.nodeName == 'command') {
277 if (ev.explicitOriginalTarget != node) return;
280 if (target == window) {
281 target = window.document.documentElement;
284 var filename = location.pathname.split('/')[ location.pathname.split('/').length - 1 ];
285 var base_key = 'oils_persist_' + String(oils_persist_hostname() + '_' + filename + '_' + target.getAttribute('id')).replace('/','_','g') + '_' + base_key_suffix;
286 var attribute_list = target.getAttribute('oils_persist').split(' ');
287 dump('on_oils_persist: <<< ' + target.nodeName + '.id = ' + target.id + '\t' + bk + '\n');
288 for (var j = 0; j < attribute_list.length; j++) {
289 var key = base_key + attribute_list[j];
292 value = encodeURI(target.getAttribute( attribute_list[j] ));
294 dump('Error in persist_helper with encodeURI: ' + E + '\n');
295 value = target.getAttribute( attribute_list[j] );
297 if ( attribute_list[j] == 'checked' && ['checkbox','toolbarbutton'].indexOf( target.nodeName ) > -1 ) {
298 value = target.checked;
299 dump('\t' + value + ' <== .' + attribute_list[j] + '\n');
300 } else if ( attribute_list[j] == 'value' && ['menulist'].indexOf( target.nodeName ) > -1 ) {
301 value = target.value;
302 dump('\t' + value + ' <== .' + attribute_list[j] + '\n');
303 } else if ( attribute_list[j] == 'value' && ['textbox'].indexOf( target.nodeName ) > -1 ) {
304 value = target.value;
305 dump('\t' + value + ' <== .' + attribute_list[j] + '\n');
306 } else if ( attribute_list[j] == 'sizemode' && ['window'].indexOf( target.nodeName ) > -1 ) {
307 value = window.windowState;
308 dump('\t' + value + ' <== window.windowState, @' + attribute_list[j] + '\n');
309 } else if ( attribute_list[j] == 'height' && ['window'].indexOf( target.nodeName ) > -1 ) {
310 value = window.outerHeight;
311 dump('\t' + value + ' <== window.outerHeight, @' + attribute_list[j] + '\n');
312 } else if ( attribute_list[j] == 'width' && ['window'].indexOf( target.nodeName ) > -1 ) {
313 value = window.outerWidth;
314 dump('\t' + value + ' <== window.outerWidth, @' + attribute_list[j] + '\n');
316 dump('\t' + value + ' <== @' + attribute_list[j] + '\n');
318 prefs.setCharPref( key, value );
319 // TODO: Need to add logic for splitter repositioning, grippy state, etc.
320 // NOTE: oils_persist_peers and oils_persist="width" on those peers can help with the elements adjacent to a splitter
322 if (target.hasAttribute('oils_persist_peers') && ! ev.cancelable) { // We abuse the .cancelable field on the oils_persist event to prevent looping
323 var peer_list = target.getAttribute('oils_persist_peers').split(' ');
324 for (var j = 0; j < peer_list.length; j++) {
325 dump('on_oils_persist: dispatching oils_persist to peer ' + peer_list[j] + '\n');
326 oils_persist( document.getElementById( peer_list[j] ), true );
330 alert('Error in persist_helper() event listener for ' + bk + ': ' + E);
335 var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
336 var nodes = document.getElementsByAttribute('oils_persist','*');
337 for (var i = 0; i < nodes.length; i++) {
338 var filename = location.pathname.split('/')[ location.pathname.split('/').length - 1 ];
339 var base_key = 'oils_persist_' + String(oils_persist_hostname() + '_' + filename + '_' + nodes[i].getAttribute('id')).replace('/','_','g') + '_' + base_key_suffix;
340 var attribute_list = nodes[i].getAttribute('oils_persist').split(' ');
341 dump('persist_helper: >>> ' + nodes[i].nodeName + '.id = ' + nodes[i].id + '\t' + base_key + '\n');
342 for (var j = 0; j < attribute_list.length; j++) {
343 var key = base_key + attribute_list[j];
344 var has_key = prefs.prefHasUserValue(key);
347 value = has_key ? decodeURI(prefs.getCharPref(key)) : null;
349 dump('Error in persist_helper with decodeURI: ' + E + '\n');
350 value = has_key ? prefs.getCharPref(key) : null;
352 if (value == 'true') { value = true; }
353 if (value == 'false') { value = false; }
355 if ( attribute_list[j] == 'checked' && ['checkbox','toolbarbutton'].indexOf( nodes[i].nodeName ) > -1 ) {
356 nodes[i].checked = value;
357 dump('\t' + value + ' ==> .' + attribute_list[j] + '\n');
359 nodes[i].removeAttribute('checked');
360 dump('\tremoving @checked\n');
362 } else if ( attribute_list[j] == 'value' && ['textbox'].indexOf( nodes[i].nodeName ) > -1 ) {
363 nodes[i].value = value;
364 dump('\t' + value + ' ==> .' + attribute_list[j] + '\n');
365 } else if ( attribute_list[j] == 'value' && ['menulist'].indexOf( nodes[i].nodeName ) > -1 ) {
366 nodes[i].value = value;
367 dump('\t' + value + ' ==> .' + attribute_list[j] + '\n');
368 } else if ( attribute_list[j] == 'sizemode' && ['window'].indexOf( nodes[i].nodeName ) > -1 ) {
370 case window.STATE_MAXIMIZED:
373 case window.STATE_MINIMIZED:
377 dump('\t' + value + ' ==> window.windowState, @' + attribute_list[j] + '\n');
378 } else if ( attribute_list[j] == 'height' && ['window'].indexOf( nodes[i].nodeName ) > -1 ) {
379 window.outerHeight = value;
380 dump('\t' + value + ' ==> window.outerHeight, @' + attribute_list[j] + '\n');
381 } else if ( attribute_list[j] == 'width' && ['window'].indexOf( nodes[i].nodeName ) > -1 ) {
382 window.outerWidth = value;
383 dump('\t' + value + ' ==> window.outerWidth, @' + attribute_list[j] + '\n');
385 nodes[i].setAttribute( attribute_list[j], value);
386 dump('\t' + value + ' ==> @' + attribute_list[j] + '\n');
390 var cmd = nodes[i].getAttribute('command');
391 var cmd_el = document.getElementById(cmd);
392 if (nodes[i].disabled == false && nodes[i].hidden == false) {
393 var no_poke = nodes[i].getAttribute('oils_persist_no_poke');
394 if (no_poke && no_poke == 'true') {
395 // Timing issue for some checkboxes; don't poke them with an event
396 dump('\tnot poking\n');
399 dump('\tpoking @command\n');
400 var evt = document.createEvent("Events");
401 evt.initEvent( 'command', true, true );
402 cmd_el.dispatchEvent(evt);
405 var evt = document.createEvent("Events");
406 evt.initEvent( 'command', true, true );
407 nodes[i].dispatchEvent(evt);
412 window.persist_helper_event_listeners.add(cmd_el,
414 gen_event_handler('command',cmd_el),
417 window.persist_helper_event_listeners.add(cmd_el,
419 gen_oils_persist_handler( base_key, nodes[i] ),
424 var event_types = [];
425 if (node.hasAttribute('oils_persist_events')) {
426 var event_type_list = node.getAttribute('oils_persist_events').split(' ');
427 for (var j = 0; j < event_type_list.length; j++) {
428 event_types.push( event_type_list[j] );
431 if (node.nodeName == 'textbox') {
432 event_types.push('change');
433 } else if (node.nodeName == 'menulist') {
434 event_types.push('select');
435 } else if (node.nodeName == 'window') {
436 event_types.push('resize');
437 node = window; // xul window is an element of window.document
439 event_types.push('command');
442 for (var j = 0; j < event_types.length; j++) {
443 window.persist_helper_event_listeners.add(node,
445 gen_event_handler(event_types[j],node),
449 window.persist_helper_event_listeners.add(node,
451 gen_oils_persist_handler( base_key, node ),
457 alert('Error in persist_helper(): ' + E);
461 function persist_helper_cleanup() {
463 window.persist_helper_event_listeners.removeAll();
465 alert('Error in persist_helper_cleanup(): ' + E);
469 function getKeys(o) {
471 for (var k in o) keys.push(k);
475 function get_contentWindow(frame) {
477 if (frame && frame.contentWindow) {
479 if (typeof frame.contentWindow.wrappedJSObject != 'undefined') {
480 return frame.contentWindow.wrappedJSObject;
483 var Strings = $('offlineStrings') || $('commonStrings');
484 alert(Strings.getFormattedString('openils.global_util.content_window_jsobject.error', [frame, E]));
486 return frame.contentWindow;
491 var Strings = $('offlineStrings') || $('commonStrings');
492 alert(Strings.getFormattedString('openils.global_util.content_window.error', [frame, E]));
496 function xul_param(param_name,_params) {
497 /* By default, this function looks for a CGI-style query param identified by param_name. If one isn't found, it then looks in xulG. If one still isn't found, and _params.stash_name is true, it looks in the global xpcom stash for the field identified by stash_name. If _params.concat is true, then it looks in all these places and concatenates the results. There are also options for converting JSON to javascript objects, and clearing the xpcom stash_name field after retrieval. Also added, ability to search a specific spot in the xpcom stash that implements a stack to hold xulG's for modal windows */
499 //dump('xul_param('+param_name+','+js2JSON(_params)+')\n');
500 var value = undefined; if (!_params) _params = {};
501 if (typeof _params.no_cgi == 'undefined') {
503 if (cgi.param(param_name)) {
504 var x = cgi.param(param_name);
505 //dump('\tfound via location.href = ' + x + '\n');
506 if (typeof _params.JSON2js_if_cgi != 'undefined') {
508 //dump('\tJSON2js = ' + x + '\n');
510 if (typeof _params.concat == 'undefined') {
511 //alert(param_name + ' x = ' + x);
515 if (value.constructor != Array) value = [ value ];
516 value = value.concat(x);
523 if (typeof _params.no_xulG == 'undefined') {
524 if (typeof xulG == 'object' && typeof xulG[ param_name ] != 'undefined') {
525 var x = xulG[ param_name ];
526 //dump('\tfound via xulG = ' + x + '\n');
527 if (typeof _params.JSON2js_if_xulG != 'undefined') {
529 //dump('\tJSON2js = ' + x + '\n');
531 if (typeof _params.concat == 'undefined') {
532 //alert(param_name + ' x = ' + x);
536 if (value.constructor != Array) value = [ value ];
537 value = value.concat(x);
544 if (typeof _params.no_xpcom == 'undefined') {
545 /* the field names used for temp variables in the global stash tend to be more unique than xuLG or CGI param names, to avoid collisions */
546 if (typeof _params.stash_name != 'undefined') {
547 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
548 if (typeof data[ _params.stash_name ] != 'undefined') {
549 var x = data[ _params.stash_name ];
550 //dump('\tfound via xpcom = ' + x + '\n');
551 if (typeof _params.JSON2js_if_xpcom != 'undefined') {
553 //dump('\tJSON2js = ' + x + '\n');
555 if (_params.clear_xpcom) {
556 data[ _params.stash_name ] = undefined; data.stash( _params.stash_name );
558 if (typeof _params.concat == 'undefined') {
559 //alert(param_name + ' x = ' + x);
563 if (value.constructor != Array) value = [ value ];
564 value = value.concat(x);
572 //alert(param_name + ' value = ' + value);
575 dump('xul_param error: ' + E + '\n');
579 function get_bool(a) {
580 // Normal javascript interpretation except 'f' == false, per postgres, and 'F' == false, and '0' == false (newer JSON is returning '0' instead of 0 in cases)
581 // So false includes 'f', '', '0', 0, null, and undefined
582 if (a == 'f') return false;
583 if (a == 'F') return false;
584 if (a == '0') return false;
585 if (a) return true; else return false;
588 function get_localized_bool(a) {
589 var Strings = $('offlineStrings') || $('commonStrings');
590 return get_bool(a) ? Strings.getString('common.yes') : Strings.getString('common.no');
593 function get_db_true() {
597 function get_db_false() {
601 function copy_to_clipboard(ev) {
604 if (typeof ev == 'object') {
605 if (typeof ev.target != 'undefined') {
606 if (typeof ev.target.textContent != 'undefined') if (ev.target.textContent) text = ev.target.textContent;
607 if (typeof ev.target.value != 'undefined') if (ev.target.value) text = ev.target.value;
609 } else if (typeof ev == 'string') {
612 const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"]
613 .getService(Components.interfaces.nsIClipboardHelper);
614 gClipboardHelper.copyString(text);
615 var Strings = $('offlineStrings') || $('commonStrings');
616 // alert(Strings.getFormattedString('openils.global_util.clipboard', [text]));
618 var Strings = $('offlineStrings') || $('commonStrings');
619 alert(Strings.getFormattedString('openils.global_util.clipboard.error', [E]));
623 function clear_the_cache() {
625 var cacheClass = Components.classes["@mozilla.org/network/cache-service;1"];
626 var cacheService = cacheClass.getService(Components.interfaces.nsICacheService);
627 cacheService.evictEntries(Components.interfaces.nsICache.STORE_ON_DISK);
628 cacheService.evictEntries(Components.interfaces.nsICache.STORE_IN_MEMORY);
630 var Strings = $('offlineStrings') || $('commonStrings');
631 alert(Strings.getFormattedString('openils.global_util.clear_cache.error', [E]));
635 function toOpenWindowByType(inType, uri) {
636 var winopts = "chrome,extrachrome,menubar,resizable,scrollbars,status,toolbar";
637 window.open(uri, "_blank", winopts);
640 function url_prefix(url) {
641 var base_url = url.match(/^[^?/|]+/);
643 base_url = base_url[0];
645 url = url.replace(/^[^?/|]+\|?/, urls[base_url]);
647 if (url.match(/^\//)) url = urls.remote + url;
648 if (! url.match(/^(http|https|chrome|oils):\/\//) && ! url.match(/^data:/) ) url = 'http://' + url;
649 dump('url_prefix = ' + url + '\n');
653 function widget_prompt(node,args) {
654 // args is an object that may contain the following keys: title, desc, ok_label, ok_accesskey, cancel_label, cancel_accesskey, access, method
655 // access may contain 'property' or 'attribute' or 'method' for retrieving the value from the node
656 // if 'method', then the method key will reference a function that returns the value
658 if (!node) { return false; }
659 if (!args) { args = {}; }
660 args[ 'widget' ] = node;
662 var url = location.protocol == 'chrome'
663 ? 'chrome://open_ils_staff_client/content/util/widget_prompt.xul'
664 : '/xul/server/util/widget_prompt.xul';
666 JSAN.use('util.window'); var win = new util.window();
667 var my_xulG = win.open(
669 args.title || 'widget_prompt',
674 if (my_xulG.status == 'incomplete') {
677 return my_xulG.value;
680 alert('Error in global_utils.js, widget_prompt(): ' + E);