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 = '';
260 function gen_event_handler(etype,node) {
261 return function(ev) {
263 oils_persist(ev.target);
265 alert('Error in persist_helper, firing virtual event oils_persist after ' + etype + ' event on ' + node.nodeName + '.id = ' + node.id + ': ' + E);
270 function gen_oils_persist_handler(bk,node) {
271 return function(ev) {
274 if (ev.target.nodeName == 'command') {
276 if (ev.explicitOriginalTarget != node) return;
279 if (target == window) {
280 target = window.document.documentElement;
283 var filename = location.pathname.split('/')[ location.pathname.split('/').length - 1 ];
284 var base_key = 'oils_persist_' + String(oils_persist_hostname() + '_' + filename + '_' + target.getAttribute('id')).replace('/','_','g') + '_' + base_key_suffix;
285 var attribute_list = target.getAttribute('oils_persist').split(' ');
286 dump('on_oils_persist: <<< ' + target.nodeName + '.id = ' + target.id + '\t' + bk + '\n');
287 for (var j = 0; j < attribute_list.length; j++) {
288 var key = base_key + attribute_list[j];
291 value = encodeURI(target.getAttribute( attribute_list[j] ));
293 dump('Error in persist_helper with encodeURI: ' + E + '\n');
294 value = target.getAttribute( attribute_list[j] );
296 if ( attribute_list[j] == 'checked' && ['checkbox','toolbarbutton'].indexOf( target.nodeName ) > -1 ) {
297 value = target.checked;
298 dump('\t' + value + ' <== .' + attribute_list[j] + '\n');
299 } else if ( attribute_list[j] == 'value' && ['menulist'].indexOf( target.nodeName ) > -1 ) {
300 value = target.value;
301 dump('\t' + value + ' <== .' + attribute_list[j] + '\n');
302 } else if ( attribute_list[j] == 'value' && ['textbox'].indexOf( target.nodeName ) > -1 ) {
303 value = target.value;
304 dump('\t' + value + ' <== .' + attribute_list[j] + '\n');
305 } else if ( attribute_list[j] == 'sizemode' && ['window'].indexOf( target.nodeName ) > -1 ) {
306 value = window.windowState;
307 dump('\t' + value + ' <== window.windowState, @' + attribute_list[j] + '\n');
308 } else if ( attribute_list[j] == 'height' && ['window'].indexOf( target.nodeName ) > -1 ) {
309 value = window.outerHeight;
310 dump('\t' + value + ' <== window.outerHeight, @' + attribute_list[j] + '\n');
311 } else if ( attribute_list[j] == 'width' && ['window'].indexOf( target.nodeName ) > -1 ) {
312 value = window.outerWidth;
313 dump('\t' + value + ' <== window.outerWidth, @' + attribute_list[j] + '\n');
315 dump('\t' + value + ' <== @' + attribute_list[j] + '\n');
317 prefs.setCharPref( key, value );
318 // TODO: Need to add logic for splitter repositioning, grippy state, etc.
319 // NOTE: oils_persist_peers and oils_persist="width" on those peers can help with the elements adjacent to a splitter
321 if (target.hasAttribute('oils_persist_peers') && ! ev.cancelable) { // We abuse the .cancelable field on the oils_persist event to prevent looping
322 var peer_list = target.getAttribute('oils_persist_peers').split(' ');
323 for (var j = 0; j < peer_list.length; j++) {
324 dump('on_oils_persist: dispatching oils_persist to peer ' + peer_list[j] + '\n');
325 oils_persist( document.getElementById( peer_list[j] ), true );
329 alert('Error in persist_helper() event listener for ' + bk + ': ' + E);
334 var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
335 var nodes = document.getElementsByAttribute('oils_persist','*');
336 for (var i = 0; i < nodes.length; i++) {
337 var filename = location.pathname.split('/')[ location.pathname.split('/').length - 1 ];
338 var base_key = 'oils_persist_' + String(oils_persist_hostname() + '_' + filename + '_' + nodes[i].getAttribute('id')).replace('/','_','g') + '_' + base_key_suffix;
339 var attribute_list = nodes[i].getAttribute('oils_persist').split(' ');
340 dump('persist_helper: >>> ' + nodes[i].nodeName + '.id = ' + nodes[i].id + '\t' + base_key + '\n');
341 for (var j = 0; j < attribute_list.length; j++) {
342 var key = base_key + attribute_list[j];
343 var has_key = prefs.prefHasUserValue(key);
346 value = has_key ? decodeURI(prefs.getCharPref(key)) : null;
348 dump('Error in persist_helper with decodeURI: ' + E + '\n');
349 value = has_key ? prefs.getCharPref(key) : null;
351 if (value == 'true') { value = true; }
352 if (value == 'false') { value = false; }
354 if ( attribute_list[j] == 'checked' && ['checkbox','toolbarbutton'].indexOf( nodes[i].nodeName ) > -1 ) {
355 nodes[i].checked = value;
356 dump('\t' + value + ' ==> .' + attribute_list[j] + '\n');
358 nodes[i].removeAttribute('checked');
359 dump('\tremoving @checked\n');
361 } else if ( attribute_list[j] == 'value' && ['textbox'].indexOf( nodes[i].nodeName ) > -1 ) {
362 nodes[i].value = value;
363 dump('\t' + value + ' ==> .' + attribute_list[j] + '\n');
364 } else if ( attribute_list[j] == 'value' && ['menulist'].indexOf( nodes[i].nodeName ) > -1 ) {
365 nodes[i].value = value;
366 dump('\t' + value + ' ==> .' + attribute_list[j] + '\n');
367 } else if ( attribute_list[j] == 'sizemode' && ['window'].indexOf( nodes[i].nodeName ) > -1 ) {
369 case window.STATE_MAXIMIZED:
372 case window.STATE_MINIMIZED:
376 dump('\t' + value + ' ==> window.windowState, @' + attribute_list[j] + '\n');
377 } else if ( attribute_list[j] == 'height' && ['window'].indexOf( nodes[i].nodeName ) > -1 ) {
378 window.outerHeight = value;
379 dump('\t' + value + ' ==> window.outerHeight, @' + attribute_list[j] + '\n');
380 } else if ( attribute_list[j] == 'width' && ['window'].indexOf( nodes[i].nodeName ) > -1 ) {
381 window.outerWidth = value;
382 dump('\t' + value + ' ==> window.outerWidth, @' + attribute_list[j] + '\n');
384 nodes[i].setAttribute( attribute_list[j], value);
385 dump('\t' + value + ' ==> @' + attribute_list[j] + '\n');
389 var cmd = nodes[i].getAttribute('command');
390 var cmd_el = document.getElementById(cmd);
391 if (nodes[i].disabled == false && nodes[i].hidden == false) {
392 var no_poke = nodes[i].getAttribute('oils_persist_no_poke');
393 if (no_poke && no_poke == 'true') {
394 // Timing issue for some checkboxes; don't poke them with an event
395 dump('\tnot poking\n');
398 dump('\tpoking @command\n');
399 var evt = document.createEvent("Events");
400 evt.initEvent( 'command', true, true );
401 cmd_el.dispatchEvent(evt);
404 var evt = document.createEvent("Events");
405 evt.initEvent( 'command', true, true );
406 nodes[i].dispatchEvent(evt);
411 cmd_el.addEventListener(
413 gen_event_handler('command',cmd_el),
416 cmd_el.addEventListener(
418 gen_oils_persist_handler( base_key, nodes[i] ),
423 var event_types = [];
424 if (node.hasAttribute('oils_persist_events')) {
425 var event_type_list = node.getAttribute('oils_persist_events').split(' ');
426 for (var j = 0; j < event_type_list.length; j++) {
427 event_types.push( event_type_list[j] );
430 if (node.nodeName == 'textbox') {
431 event_types.push('change');
432 } else if (node.nodeName == 'menulist') {
433 event_types.push('select');
434 } else if (node.nodeName == 'window') {
435 event_types.push('resize');
436 node = window; // xul window is an element of window.document
438 event_types.push('command');
441 for (var j = 0; j < event_types.length; j++) {
442 node.addEventListener(
444 gen_event_handler(event_types[j],node),
448 node.addEventListener(
450 gen_oils_persist_handler( base_key, node ),
456 alert('Error in persist_helper(): ' + E);
460 function getKeys(o) {
462 for (var k in o) keys.push(k);
466 function get_contentWindow(frame) {
468 if (frame && frame.contentWindow) {
470 if (typeof frame.contentWindow.wrappedJSObject != 'undefined') {
471 return frame.contentWindow.wrappedJSObject;
474 var Strings = $('offlineStrings') || $('commonStrings');
475 alert(Strings.getFormattedString('openils.global_util.content_window_jsobject.error', [frame, E]));
477 return frame.contentWindow;
482 var Strings = $('offlineStrings') || $('commonStrings');
483 alert(Strings.getFormattedString('openils.global_util.content_window.error', [frame, E]));
487 function xul_param(param_name,_params) {
488 /* By default, this function looks for a CGI-style query param identified by param_name. If one isn't found, it then looks in xulG. If one still isn't found, and _params.stash_name is true, it looks in the global xpcom stash for the field identified by stash_name. If _params.concat is true, then it looks in all these places and concatenates the results. There are also options for converting JSON to javascript objects, and clearing the xpcom stash_name field after retrieval. Also added, ability to search a specific spot in the xpcom stash that implements a stack to hold xulG's for modal windows */
490 //dump('xul_param('+param_name+','+js2JSON(_params)+')\n');
491 var value = undefined; if (!_params) _params = {};
492 if (typeof _params.no_cgi == 'undefined') {
494 if (cgi.param(param_name)) {
495 var x = cgi.param(param_name);
496 //dump('\tfound via location.href = ' + x + '\n');
497 if (typeof _params.JSON2js_if_cgi != 'undefined') {
499 //dump('\tJSON2js = ' + x + '\n');
501 if (typeof _params.concat == 'undefined') {
502 //alert(param_name + ' x = ' + x);
506 if (value.constructor != Array) value = [ value ];
507 value = value.concat(x);
514 if (typeof _params.no_xulG == 'undefined') {
515 if (typeof xulG == 'object' && typeof xulG[ param_name ] != 'undefined') {
516 var x = xulG[ param_name ];
517 //dump('\tfound via xulG = ' + x + '\n');
518 if (typeof _params.JSON2js_if_xulG != 'undefined') {
520 //dump('\tJSON2js = ' + x + '\n');
522 if (typeof _params.concat == 'undefined') {
523 //alert(param_name + ' x = ' + x);
527 if (value.constructor != Array) value = [ value ];
528 value = value.concat(x);
535 if (typeof _params.no_xpcom == 'undefined') {
536 /* the field names used for temp variables in the global stash tend to be more unique than xuLG or CGI param names, to avoid collisions */
537 if (typeof _params.stash_name != 'undefined') {
538 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
539 if (typeof data[ _params.stash_name ] != 'undefined') {
540 var x = data[ _params.stash_name ];
541 //dump('\tfound via xpcom = ' + x + '\n');
542 if (typeof _params.JSON2js_if_xpcom != 'undefined') {
544 //dump('\tJSON2js = ' + x + '\n');
546 if (_params.clear_xpcom) {
547 data[ _params.stash_name ] = undefined; data.stash( _params.stash_name );
549 if (typeof _params.concat == 'undefined') {
550 //alert(param_name + ' x = ' + x);
554 if (value.constructor != Array) value = [ value ];
555 value = value.concat(x);
563 //alert(param_name + ' value = ' + value);
566 dump('xul_param error: ' + E + '\n');
570 function get_bool(a) {
571 // Normal javascript interpretation except 'f' == false, per postgres, and 'F' == false, and '0' == false (newer JSON is returning '0' instead of 0 in cases)
572 // So false includes 'f', '', '0', 0, null, and undefined
573 if (a == 'f') return false;
574 if (a == 'F') return false;
575 if (a == '0') return false;
576 if (a) return true; else return false;
579 function get_localized_bool(a) {
580 var Strings = $('offlineStrings') || $('commonStrings');
581 return get_bool(a) ? Strings.getString('common.yes') : Strings.getString('common.no');
584 function get_db_true() {
588 function get_db_false() {
592 function copy_to_clipboard(ev) {
595 if (typeof ev == 'object') {
596 if (typeof ev.target != 'undefined') {
597 if (typeof ev.target.textContent != 'undefined') if (ev.target.textContent) text = ev.target.textContent;
598 if (typeof ev.target.value != 'undefined') if (ev.target.value) text = ev.target.value;
600 } else if (typeof ev == 'string') {
603 const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"]
604 .getService(Components.interfaces.nsIClipboardHelper);
605 gClipboardHelper.copyString(text);
606 var Strings = $('offlineStrings') || $('commonStrings');
607 // alert(Strings.getFormattedString('openils.global_util.clipboard', [text]));
609 var Strings = $('offlineStrings') || $('commonStrings');
610 alert(Strings.getFormattedString('openils.global_util.clipboard.error', [E]));
614 function clear_the_cache() {
616 var cacheClass = Components.classes["@mozilla.org/network/cache-service;1"];
617 var cacheService = cacheClass.getService(Components.interfaces.nsICacheService);
618 cacheService.evictEntries(Components.interfaces.nsICache.STORE_ON_DISK);
619 cacheService.evictEntries(Components.interfaces.nsICache.STORE_IN_MEMORY);
621 var Strings = $('offlineStrings') || $('commonStrings');
622 alert(Strings.getFormattedString('openils.global_util.clear_cache.error', [E]));
626 function toOpenWindowByType(inType, uri) {
627 var winopts = "chrome,extrachrome,menubar,resizable,scrollbars,status,toolbar";
628 window.open(uri, "_blank", winopts);
631 function url_prefix(url) {
632 var base_url = url.match(/^[^?/|]+/);
634 base_url = base_url[0];
636 url = url.replace(/^[^?/|]+\|?/, urls[base_url]);
638 if (url.match(/^\//)) url = urls.remote + url;
639 if (! url.match(/^(http|https|chrome|oils):\/\//) && ! url.match(/^data:/) ) url = 'http://' + url;
640 dump('url_prefix = ' + url + '\n');
644 function widget_prompt(node,args) {
645 // args is an object that may contain the following keys: title, desc, ok_label, ok_accesskey, cancel_label, cancel_accesskey, access, method
646 // access may contain 'property' or 'attribute' or 'method' for retrieving the value from the node
647 // if 'method', then the method key will reference a function that returns the value
649 if (!node) { return false; }
650 if (!args) { args = {}; }
651 args[ 'widget' ] = node;
653 var url = location.protocol == 'chrome'
654 ? 'chrome://open_ils_staff_client/content/util/widget_prompt.xul'
655 : '/xul/server/util/widget_prompt.xul';
657 JSAN.use('util.window'); var win = new util.window();
658 var my_xulG = win.open(
660 args.title || 'widget_prompt',
665 if (my_xulG.status == 'incomplete') {
668 return my_xulG.value;
671 alert('Error in global_utils.js, widget_prompt(): ' + E);