1 if(window.arguments && typeof window.arguments[0] == 'object' && typeof xulG == 'undefined') {
2 xulG = window.arguments[0];
5 function $(id) { return document.getElementById(id); }
7 function oils_unsaved_data_V() {
8 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
10 if (typeof data.unsaved_data == 'undefined') { data.unsaved_data = 0; }
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');
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');
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;
49 if (typeof xulG != 'undefined') {
50 if (typeof xulG.unlock_tab == 'function') {
51 dump('\twith xulG.lock_tab\n');
53 window.oils_lock++; // different window scope than the chrome of xulG.lock_tab
55 dump('\twithout xulG.lock_tab\n');
56 oils_unsaved_data_V();
59 dump('\twithout xulG.lock_tab\n');
60 oils_unsaved_data_V();
62 return window.oils_lock;
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');
72 window.oils_lock--; // different window scope than the chrome of xulG.unlock_tab
73 if (window.oils_lock < 0) { window.oils_lock = 0; }
75 dump('\twithout xulG.unlock_tab\n');
76 oils_unsaved_data_P();
79 dump('\twithout xulG.unlock_tab\n');
80 oils_unsaved_data_P();
82 return window.oils_lock;
86 dump('\n=-=-=-=-=\n');
87 dump('init window.oils_lock == ' + window.oils_lock + ' for ' + location.href + '\n');
88 window.addEventListener(
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'));
102 if (typeof xulG != 'undefined') {
103 if (typeof xulG.unlock_tab == 'function') {
106 oils_unsaved_data_P( window.oils_lock );
109 oils_unsaved_data_P( window.oils_lock );
111 window.oils_lock = 0;
112 dump('forcing window.oils_lock == ' + window.oils_lock + '\n');
114 // Dispatching the window close event doesn't always close the window, even though the event does happen
120 dump('Error inside global_util.js, onclose handler, setTimeout window.close KLUDGE: ' + E + '\n');
127 dump('Error inside global_util.js, onclose handler: ' + E + '\n');
134 function ses(a,params) {
136 if (!params) params = {};
139 data = params.data; data.stash_retrieve();
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();
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;
153 return data.list.au[0].wsid();
156 return data.list.au[0].ws_ou();
158 case 'ws_ou_shortname' :
159 return data.hash.aou[ data.list.au[0].ws_ou() ].shortname();
162 return data.session.authtime;
166 return data.session.key;
170 alert(location.href + '\nError in global_utils.js, ses(): ' + E);
175 function font_helper() {
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);
189 var Strings = $('offlineStrings') || $('commonStrings');
190 alert(Strings.getFormattedString('openils.global_util.font_size.error', [E]));
194 function oils_persist(e,cancelable) {
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);
202 alert('Error with oils_persist():' + E);
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;
211 return location.hostname;
215 function persist_helper(base_key_suffix) {
217 if (base_key_suffix) {
218 base_key_suffix = base_key_suffix.replace(/[^A-Za-z]/g,'_') + '_';
220 base_key_suffix = '';
222 window.persist_helper_event_listeners = new EventListenerList();
224 function gen_event_handler(etype,node) {
225 return function(ev) {
227 oils_persist(ev.target);
229 alert('Error in persist_helper, firing virtual event oils_persist after ' + etype + ' event on ' + node.nodeName + '.id = ' + node.id + ': ' + E);
234 function gen_oils_persist_handler(bk,node) {
235 return function(ev) {
238 if (ev.target.nodeName == 'command') {
240 if (ev.explicitOriginalTarget != node) return;
243 if (target == window) {
244 target = window.document.documentElement;
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];
255 value = encodeURI(target.getAttribute( attribute_list[j] ));
257 dump('Error in persist_helper with encodeURI: ' + E + '\n');
258 value = target.getAttribute( attribute_list[j] );
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');
279 dump('\t' + value + ' <== @' + attribute_list[j] + '\n');
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
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 );
293 alert('Error in persist_helper() event listener for ' + bk + ': ' + E);
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);
310 value = has_key ? decodeURI(prefs.getCharPref(key)) : null;
312 dump('Error in persist_helper with decodeURI: ' + E + '\n');
313 value = has_key ? prefs.getCharPref(key) : null;
315 if (value == 'true') { value = true; }
316 if (value == 'false') { value = false; }
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');
322 nodes[i].removeAttribute('checked');
323 dump('\tremoving @checked\n');
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 ) {
333 case window.STATE_MAXIMIZED:
336 case window.STATE_MINIMIZED:
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');
348 nodes[i].setAttribute( attribute_list[j], value);
349 dump('\t' + value + ' ==> @' + attribute_list[j] + '\n');
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');
362 dump('\tpoking @command\n');
363 var evt = document.createEvent("Events");
364 evt.initEvent( 'command', true, true );
365 cmd_el.dispatchEvent(evt);
368 var evt = document.createEvent("Events");
369 evt.initEvent( 'command', true, true );
370 nodes[i].dispatchEvent(evt);
375 window.persist_helper_event_listeners.add(cmd_el,
377 gen_event_handler('command',cmd_el),
380 window.persist_helper_event_listeners.add(cmd_el,
382 gen_oils_persist_handler( base_key, 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] );
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
402 event_types.push('command');
405 for (var j = 0; j < event_types.length; j++) {
406 window.persist_helper_event_listeners.add(node,
408 gen_event_handler(event_types[j],node),
412 window.persist_helper_event_listeners.add(node,
414 gen_oils_persist_handler( base_key, node ),
420 alert('Error in persist_helper(): ' + E);
424 function persist_helper_cleanup() {
426 window.persist_helper_event_listeners.removeAll();
428 alert('Error in persist_helper_cleanup(): ' + E);
432 function getKeys(o) {
434 for (var k in o) keys.push(k);
438 function get_contentWindow(frame) {
440 if (frame && frame.contentWindow) {
442 if (typeof frame.contentWindow.wrappedJSObject != 'undefined') {
443 return frame.contentWindow.wrappedJSObject;
446 var Strings = $('offlineStrings') || $('commonStrings');
447 alert(Strings.getFormattedString('openils.global_util.content_window_jsobject.error', [frame, E]));
449 return frame.contentWindow;
454 var Strings = $('offlineStrings') || $('commonStrings');
455 alert(Strings.getFormattedString('openils.global_util.content_window.error', [frame, E]));
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 */
462 //dump('xul_param('+param_name+','+js2JSON(_params)+')\n');
463 var value = undefined; if (!_params) _params = {};
464 if (typeof _params.no_cgi == 'undefined') {
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') {
471 //dump('\tJSON2js = ' + x + '\n');
473 if (typeof _params.concat == 'undefined') {
474 //alert(param_name + ' x = ' + x);
478 if (value.constructor != Array) value = [ value ];
479 value = value.concat(x);
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') {
492 //dump('\tJSON2js = ' + x + '\n');
494 if (typeof _params.concat == 'undefined') {
495 //alert(param_name + ' x = ' + x);
499 if (value.constructor != Array) value = [ value ];
500 value = value.concat(x);
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') {
516 //dump('\tJSON2js = ' + x + '\n');
518 if (_params.clear_xpcom) {
519 data[ _params.stash_name ] = undefined; data.stash( _params.stash_name );
521 if (typeof _params.concat == 'undefined') {
522 //alert(param_name + ' x = ' + x);
526 if (value.constructor != Array) value = [ value ];
527 value = value.concat(x);
535 //alert(param_name + ' value = ' + value);
538 dump('xul_param error: ' + E + '\n');
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;
551 function get_localized_bool(a) {
552 var Strings = $('offlineStrings') || $('commonStrings');
553 return get_bool(a) ? Strings.getString('common.yes') : Strings.getString('common.no');
556 function get_db_true() {
560 function get_db_false() {
564 function copy_to_clipboard(ev) {
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;
572 } else if (typeof ev == 'string') {
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]));
581 var Strings = $('offlineStrings') || $('commonStrings');
582 alert(Strings.getFormattedString('openils.global_util.clipboard.error', [E]));
586 function clear_the_cache() {
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);
593 var Strings = $('offlineStrings') || $('commonStrings');
594 alert(Strings.getFormattedString('openils.global_util.clear_cache.error', [E]));
598 function toOpenWindowByType(inType, uri) {
599 var winopts = "chrome,extrachrome,menubar,resizable,scrollbars,status,toolbar";
600 window.open(uri, "_blank", winopts);
603 function url_prefix(url) {
604 var base_url = url.match(/^[^?/|]+/);
606 base_url = base_url[0];
608 url = url.replace(/^[^?/|]+\|?/, urls[base_url]);
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');
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
621 if (!node) { return false; }
622 if (!args) { args = {}; }
623 args[ 'widget' ] = node;
625 var url = location.protocol == 'chrome'
626 ? 'chrome://open_ils_staff_client/content/util/widget_prompt.xul'
627 : '/xul/server/util/widget_prompt.xul';
629 JSAN.use('util.window'); var win = new util.window();
630 var my_xulG = win.open(
632 args.title || 'widget_prompt',
637 if (my_xulG.status == 'incomplete') {
640 return my_xulG.value;
643 alert('Error in global_utils.js, widget_prompt(): ' + E);
648 window.addEventListener(
652 if (window.oils_autoloaded) { return; }
653 JSAN.use('addon.autoloader');
654 window.oils_autoloaded = new addon.autoloader();
656 dump('Error in global_util.js with addon.autoloader: ' + E + '\n');