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 = '';
223 function gen_event_handler(etype,node) {
224 return function(ev) {
226 oils_persist(ev.target);
228 alert('Error in persist_helper, firing virtual event oils_persist after ' + etype + ' event on ' + node.nodeName + '.id = ' + node.id + ': ' + E);
233 function gen_oils_persist_handler(bk,node) {
234 return function(ev) {
237 if (ev.target.nodeName == 'command') {
239 if (ev.explicitOriginalTarget != node) return;
242 if (target == window) {
243 target = window.document.documentElement;
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];
254 value = encodeURI(target.getAttribute( attribute_list[j] ));
256 dump('Error in persist_helper with encodeURI: ' + E + '\n');
257 value = target.getAttribute( attribute_list[j] );
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');
278 dump('\t' + value + ' <== @' + attribute_list[j] + '\n');
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
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 );
292 alert('Error in persist_helper() event listener for ' + bk + ': ' + E);
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);
309 value = has_key ? decodeURI(prefs.getCharPref(key)) : null;
311 dump('Error in persist_helper with decodeURI: ' + E + '\n');
312 value = has_key ? prefs.getCharPref(key) : null;
314 if (value == 'true') { value = true; }
315 if (value == 'false') { value = false; }
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');
321 nodes[i].removeAttribute('checked');
322 dump('\tremoving @checked\n');
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 ) {
332 case window.STATE_MAXIMIZED:
335 case window.STATE_MINIMIZED:
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');
347 nodes[i].setAttribute( attribute_list[j], value);
348 dump('\t' + value + ' ==> @' + attribute_list[j] + '\n');
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');
361 dump('\tpoking @command\n');
362 var evt = document.createEvent("Events");
363 evt.initEvent( 'command', true, true );
364 cmd_el.dispatchEvent(evt);
367 var evt = document.createEvent("Events");
368 evt.initEvent( 'command', true, true );
369 nodes[i].dispatchEvent(evt);
374 cmd_el.addEventListener(
376 gen_event_handler('command',cmd_el),
379 cmd_el.addEventListener(
381 gen_oils_persist_handler( base_key, 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] );
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
401 event_types.push('command');
404 for (var j = 0; j < event_types.length; j++) {
405 node.addEventListener(
407 gen_event_handler(event_types[j],node),
411 node.addEventListener(
413 gen_oils_persist_handler( base_key, node ),
419 alert('Error in persist_helper(): ' + E);
423 function getKeys(o) {
425 for (var k in o) keys.push(k);
429 function get_contentWindow(frame) {
431 if (frame && frame.contentWindow) {
433 if (typeof frame.contentWindow.wrappedJSObject != 'undefined') {
434 return frame.contentWindow.wrappedJSObject;
437 var Strings = $('offlineStrings') || $('commonStrings');
438 alert(Strings.getFormattedString('openils.global_util.content_window_jsobject.error', [frame, E]));
440 return frame.contentWindow;
445 var Strings = $('offlineStrings') || $('commonStrings');
446 alert(Strings.getFormattedString('openils.global_util.content_window.error', [frame, E]));
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 */
453 //dump('xul_param('+param_name+','+js2JSON(_params)+')\n');
454 var value = undefined; if (!_params) _params = {};
455 if (typeof _params.no_cgi == 'undefined') {
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') {
462 //dump('\tJSON2js = ' + x + '\n');
464 if (typeof _params.concat == 'undefined') {
465 //alert(param_name + ' x = ' + x);
469 if (value.constructor != Array) value = [ value ];
470 value = value.concat(x);
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') {
483 //dump('\tJSON2js = ' + x + '\n');
485 if (typeof _params.concat == 'undefined') {
486 //alert(param_name + ' x = ' + x);
490 if (value.constructor != Array) value = [ value ];
491 value = value.concat(x);
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') {
507 //dump('\tJSON2js = ' + x + '\n');
509 if (_params.clear_xpcom) {
510 data[ _params.stash_name ] = undefined; data.stash( _params.stash_name );
512 if (typeof _params.concat == 'undefined') {
513 //alert(param_name + ' x = ' + x);
517 if (value.constructor != Array) value = [ value ];
518 value = value.concat(x);
526 //alert(param_name + ' value = ' + value);
529 dump('xul_param error: ' + E + '\n');
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;
542 function get_localized_bool(a) {
543 var Strings = $('offlineStrings') || $('commonStrings');
544 return get_bool(a) ? Strings.getString('common.yes') : Strings.getString('common.no');
547 function get_db_true() {
551 function get_db_false() {
555 function copy_to_clipboard(ev) {
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;
563 } else if (typeof ev == 'string') {
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]));
572 var Strings = $('offlineStrings') || $('commonStrings');
573 alert(Strings.getFormattedString('openils.global_util.clipboard.error', [E]));
577 function clear_the_cache() {
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);
584 var Strings = $('offlineStrings') || $('commonStrings');
585 alert(Strings.getFormattedString('openils.global_util.clear_cache.error', [E]));
589 function toOpenWindowByType(inType, uri) {
590 var winopts = "chrome,extrachrome,menubar,resizable,scrollbars,status,toolbar";
591 window.open(uri, "_blank", winopts);
594 function url_prefix(url) {
595 var base_url = url.match(/^[^?/|]+/);
597 base_url = base_url[0];
599 url = url.replace(/^[^?/|]+\|?/, urls[base_url]);
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');
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
612 if (!node) { return false; }
613 if (!args) { args = {}; }
614 args[ 'widget' ] = node;
616 var url = location.protocol == 'chrome'
617 ? 'chrome://open_ils_staff_client/content/util/widget_prompt.xul'
618 : '/xul/server/util/widget_prompt.xul';
620 JSAN.use('util.window'); var win = new util.window();
621 var my_xulG = win.open(
623 args.title || 'widget_prompt',
628 if (my_xulG.status == 'incomplete') {
631 return my_xulG.value;
634 alert('Error in global_utils.js, widget_prompt(): ' + E);