1 function $(id) { return document.getElementById(id); }
3 function oils_unsaved_data_V() {
4 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
6 if (typeof data.unsaved_data == 'undefined') { data.unsaved_data = 0; }
9 data.stash('unsaved_data');
10 dump('\n=-=-=-=-=\n');
11 dump('oils_unsaved_data_V for ' + location.href + '\n');
12 dump('incrementing window.oils_lock\n');
13 dump('incrementing data.unsaved_data\n');
14 dump('\twindow.oils_lock == ' + window.oils_lock + '\n');
15 dump('\tdata.unsaved_data == ' + data.unsaved_data + '\n');
18 function oils_unsaved_data_P(count) {
19 dump('\n=-=-=-=-=\n');
20 dump('oils_unsaved_data_P for ' + location.href + '\n');
21 if (!count) { count = 1; }
22 dump('decrementing window.oils_lock by ' + count + '\n');
23 window.oils_lock -= count;
24 if (window.oils_lock < 0) { window.oils_lock = 0; }
25 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
26 data.stash_retrieve();
27 if (typeof data.unsaved_data == 'undefined') { data.unsaved_data = 0; }
28 dump('decrementing data.unsaved_data by ' + count + '\n');
29 data.unsaved_data -= count;
30 if (data.unsaved_data < 0) { data.unsaved_data = 0; }
31 data.stash('unsaved_data');
32 dump('\twindow.oils_lock == ' + window.oils_lock + '\n');
33 dump('\tdata.unsaved_data == ' + data.unsaved_data + '\n');
36 function oils_lock_page(params) {
37 dump('\n=-=-=-=-=\n');
38 dump('oils_lock_page for ' + location.href + '\n');
39 if (!params) { params = {}; }
40 if (window.oils_lock > 0) {
41 if (!params.allow_multiple_locks) {
42 return window.oils_lock;
45 if (typeof xulG != 'undefined') {
46 if (typeof xulG.unlock_tab == 'function') {
47 dump('\twith xulG.lock_tab\n');
49 window.oils_lock++; // different window scope than the chrome of xulG.lock_tab
51 dump('\twithout xulG.lock_tab\n');
52 oils_unsaved_data_V();
55 dump('\twithout xulG.lock_tab\n');
56 oils_unsaved_data_V();
58 return window.oils_lock;
61 function oils_unlock_page(params) {
62 dump('\n=-=-=-=-=\n');
63 dump('oils_unlock_page for ' + location.href + '\n');
64 if (typeof xulG != 'undefined') {
65 if (typeof xulG.unlock_tab == 'function') {
66 dump('\twith xulG.unlock_tab\n');
68 window.oils_lock--; // different window scope than the chrome of xulG.unlock_tab
69 if (window.oils_lock < 0) { window.oils_lock = 0; }
71 dump('\twithout xulG.unlock_tab\n');
72 oils_unsaved_data_P();
75 dump('\twithout xulG.unlock_tab\n');
76 oils_unsaved_data_P();
78 return window.oils_lock;
82 dump('\n=-=-=-=-=\n');
83 dump('init window.oils_lock == ' + window.oils_lock + ' for ' + location.href + '\n');
84 window.addEventListener(
88 dump('\n=-=-=-=-=\n');
89 dump('oils_lock_page/oils_unlock_page onclose handler for ' + location.href + '\n');
90 if (window.oils_lock > 0) {
91 var confirmation = window.confirm($('offlineStrings').getString('menu.close_window.unsaved_data_warning'));
98 if (typeof xulG != 'undefined') {
99 if (typeof xulG.unlock_tab == 'function') {
102 oils_unsaved_data_P( window.oils_lock );
105 oils_unsaved_data_P( window.oils_lock );
107 window.oils_lock = 0;
108 dump('forcing window.oils_lock == ' + window.oils_lock + '\n');
110 // Dispatching the window close event doesn't always close the window, even though the event does happen
116 dump('Error inside global_util.js, onclose handler, setTimeout window.close KLUDGE: ' + E + '\n');
123 dump('Error inside global_util.js, onclose handler: ' + E + '\n');
130 function ses(a,params) {
132 if (!params) params = {};
135 data = params.data; data.stash_retrieve();
137 // 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
138 JSAN.use('OpenILS.data'); data = new OpenILS.data(); data.stash_retrieve();
142 case 'staff' : return data.list.au[0]; break;
143 case 'staff_id' : return data.list.au[0].id(); break;
144 case 'staff_usrname' : return data.list.au[0].usrname(); break;
149 return data.list.au[0].wsid();
152 return data.list.au[0].ws_ou();
154 case 'ws_ou_shortname' :
155 return data.hash.aou[ data.list.au[0].ws_ou() ].shortname();
158 return data.session.authtime;
162 return data.session.key;
166 alert(location.href + '\nError in global_utils.js, ses(): ' + E);
171 function font_helper() {
173 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
174 removeCSSClass(document.documentElement,'ALL_FONTS_LARGER');
175 removeCSSClass(document.documentElement,'ALL_FONTS_SMALLER');
176 removeCSSClass(document.documentElement,'ALL_FONTS_XX_SMALL');
177 removeCSSClass(document.documentElement,'ALL_FONTS_X_SMALL');
178 removeCSSClass(document.documentElement,'ALL_FONTS_SMALL');
179 removeCSSClass(document.documentElement,'ALL_FONTS_MEDIUM');
180 removeCSSClass(document.documentElement,'ALL_FONTS_LARGE');
181 removeCSSClass(document.documentElement,'ALL_FONTS_X_LARGE');
182 removeCSSClass(document.documentElement,'ALL_FONTS_XX_LARGE');
183 addCSSClass(document.documentElement,data.global_font_adjust);
185 var Strings = $('offlineStrings') || $('commonStrings');
186 alert(Strings.getFormattedString('openils.global_util.font_size.error', [E]));
190 function oils_persist(e,cancelable) {
193 if (typeof cancelable == 'undefined') { cancelable = false; }
194 var evt = document.createEvent("Events");
195 evt.initEvent( 'oils_persist', false, cancelable ); // event name, bubbles, cancelable
196 e.dispatchEvent(evt);
198 alert('Error with oils_persist():' + E);
202 function oils_persist_hostname() {
203 if(location.protocol == 'oils:') {
204 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
205 return data.server_unadorned;
207 return location.hostname;
211 function persist_helper(base_key_suffix) {
213 if (base_key_suffix) {
214 base_key_suffix = base_key_suffix.replace(/[^A-Za-z]/g,'_') + '_';
216 base_key_suffix = '';
219 function gen_event_handler(etype,node) {
220 return function(ev) {
222 oils_persist(ev.target);
224 alert('Error in persist_helper, firing virtual event oils_persist after ' + etype + ' event on ' + node.nodeName + '.id = ' + node.id + ': ' + E);
229 function gen_oils_persist_handler(bk,node) {
230 return function(ev) {
233 if (ev.target.nodeName == 'command') {
235 if (ev.explicitOriginalTarget != node) return;
238 if (target == window) {
239 target = window.document.documentElement;
242 var filename = location.pathname.split('/')[ location.pathname.split('/').length - 1 ];
243 var base_key = 'oils_persist_' + String(oils_persist_hostname() + '_' + filename + '_' + target.getAttribute('id')).replace('/','_','g') + '_' + base_key_suffix;
244 var attribute_list = target.getAttribute('oils_persist').split(' ');
245 dump('on_oils_persist: <<< ' + target.nodeName + '.id = ' + target.id + '\t' + bk + '\n');
246 for (var j = 0; j < attribute_list.length; j++) {
247 var key = base_key + attribute_list[j];
250 value = encodeURI(target.getAttribute( attribute_list[j] ));
252 dump('Error in persist_helper with encodeURI: ' + E + '\n');
253 value = target.getAttribute( attribute_list[j] );
255 if ( attribute_list[j] == 'checked' && ['checkbox','toolbarbutton'].indexOf( target.nodeName ) > -1 ) {
256 value = target.checked;
257 dump('\t' + value + ' <== .' + attribute_list[j] + '\n');
258 } else if ( attribute_list[j] == 'value' && ['menulist'].indexOf( target.nodeName ) > -1 ) {
259 value = target.value;
260 dump('\t' + value + ' <== .' + attribute_list[j] + '\n');
261 } else if ( attribute_list[j] == 'value' && ['textbox'].indexOf( target.nodeName ) > -1 ) {
262 value = target.value;
263 dump('\t' + value + ' <== .' + attribute_list[j] + '\n');
264 } else if ( attribute_list[j] == 'sizemode' && ['window'].indexOf( target.nodeName ) > -1 ) {
265 value = window.windowState;
266 dump('\t' + value + ' <== window.windowState, @' + attribute_list[j] + '\n');
267 } else if ( attribute_list[j] == 'height' && ['window'].indexOf( target.nodeName ) > -1 ) {
268 value = window.outerHeight;
269 dump('\t' + value + ' <== window.outerHeight, @' + attribute_list[j] + '\n');
270 } else if ( attribute_list[j] == 'width' && ['window'].indexOf( target.nodeName ) > -1 ) {
271 value = window.outerWidth;
272 dump('\t' + value + ' <== window.outerWidth, @' + attribute_list[j] + '\n');
274 dump('\t' + value + ' <== @' + attribute_list[j] + '\n');
276 prefs.setCharPref( key, value );
277 // TODO: Need to add logic for splitter repositioning, grippy state, etc.
278 // NOTE: oils_persist_peers and oils_persist="width" on those peers can help with the elements adjacent to a splitter
280 if (target.hasAttribute('oils_persist_peers') && ! ev.cancelable) { // We abuse the .cancelable field on the oils_persist event to prevent looping
281 var peer_list = target.getAttribute('oils_persist_peers').split(' ');
282 for (var j = 0; j < peer_list.length; j++) {
283 dump('on_oils_persist: dispatching oils_persist to peer ' + peer_list[j] + '\n');
284 oils_persist( document.getElementById( peer_list[j] ), true );
288 alert('Error in persist_helper() event listener for ' + bk + ': ' + E);
293 var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
294 var nodes = document.getElementsByAttribute('oils_persist','*');
295 for (var i = 0; i < nodes.length; i++) {
296 var filename = location.pathname.split('/')[ location.pathname.split('/').length - 1 ];
297 var base_key = 'oils_persist_' + String(oils_persist_hostname() + '_' + filename + '_' + nodes[i].getAttribute('id')).replace('/','_','g') + '_' + base_key_suffix;
298 var attribute_list = nodes[i].getAttribute('oils_persist').split(' ');
299 dump('persist_helper: >>> ' + nodes[i].nodeName + '.id = ' + nodes[i].id + '\t' + base_key + '\n');
300 for (var j = 0; j < attribute_list.length; j++) {
301 var key = base_key + attribute_list[j];
302 var has_key = prefs.prefHasUserValue(key);
305 value = has_key ? decodeURI(prefs.getCharPref(key)) : null;
307 dump('Error in persist_helper with decodeURI: ' + E + '\n');
308 value = has_key ? prefs.getCharPref(key) : null;
310 if (value == 'true') { value = true; }
311 if (value == 'false') { value = false; }
313 if ( attribute_list[j] == 'checked' && ['checkbox','toolbarbutton'].indexOf( nodes[i].nodeName ) > -1 ) {
314 nodes[i].checked = value;
315 dump('\t' + value + ' ==> .' + attribute_list[j] + '\n');
317 nodes[i].removeAttribute('checked');
318 dump('\tremoving @checked\n');
320 } else if ( attribute_list[j] == 'value' && ['textbox'].indexOf( nodes[i].nodeName ) > -1 ) {
321 nodes[i].value = value;
322 dump('\t' + value + ' ==> .' + attribute_list[j] + '\n');
323 } else if ( attribute_list[j] == 'value' && ['menulist'].indexOf( nodes[i].nodeName ) > -1 ) {
324 nodes[i].value = value;
325 dump('\t' + value + ' ==> .' + attribute_list[j] + '\n');
326 } else if ( attribute_list[j] == 'sizemode' && ['window'].indexOf( nodes[i].nodeName ) > -1 ) {
328 case window.STATE_MAXIMIZED:
331 case window.STATE_MINIMIZED:
335 dump('\t' + value + ' ==> window.windowState, @' + attribute_list[j] + '\n');
336 } else if ( attribute_list[j] == 'height' && ['window'].indexOf( nodes[i].nodeName ) > -1 ) {
337 window.outerHeight = value;
338 dump('\t' + value + ' ==> window.outerHeight, @' + attribute_list[j] + '\n');
339 } else if ( attribute_list[j] == 'width' && ['window'].indexOf( nodes[i].nodeName ) > -1 ) {
340 window.outerWidth = value;
341 dump('\t' + value + ' ==> window.outerWidth, @' + attribute_list[j] + '\n');
343 nodes[i].setAttribute( attribute_list[j], value);
344 dump('\t' + value + ' ==> @' + attribute_list[j] + '\n');
348 var cmd = nodes[i].getAttribute('command');
349 var cmd_el = document.getElementById(cmd);
350 if (nodes[i].disabled == false && nodes[i].hidden == false) {
351 var no_poke = nodes[i].getAttribute('oils_persist_no_poke');
352 if (no_poke && no_poke == 'true') {
353 // Timing issue for some checkboxes; don't poke them with an event
354 dump('\tnot poking\n');
357 dump('\tpoking @command\n');
358 var evt = document.createEvent("Events");
359 evt.initEvent( 'command', true, true );
360 cmd_el.dispatchEvent(evt);
363 var evt = document.createEvent("Events");
364 evt.initEvent( 'command', true, true );
365 nodes[i].dispatchEvent(evt);
370 cmd_el.addEventListener(
372 gen_event_handler('command',cmd_el),
375 cmd_el.addEventListener(
377 gen_oils_persist_handler( base_key, nodes[i] ),
382 var event_types = [];
383 if (node.hasAttribute('oils_persist_events')) {
384 var event_type_list = node.getAttribute('oils_persist_events').split(' ');
385 for (var j = 0; j < event_type_list.length; j++) {
386 event_types.push( event_type_list[j] );
389 if (node.nodeName == 'textbox') {
390 event_types.push('change');
391 } else if (node.nodeName == 'menulist') {
392 event_types.push('select');
393 } else if (node.nodeName == 'window') {
394 event_types.push('resize');
395 node = window; // xul window is an element of window.document
397 event_types.push('command');
400 for (var j = 0; j < event_types.length; j++) {
401 node.addEventListener(
403 gen_event_handler(event_types[j],node),
407 node.addEventListener(
409 gen_oils_persist_handler( base_key, node ),
415 alert('Error in persist_helper(): ' + E);
419 function getKeys(o) {
421 for (var k in o) keys.push(k);
425 function get_contentWindow(frame) {
427 if (frame && frame.contentWindow) {
429 if (typeof frame.contentWindow.wrappedJSObject != 'undefined') {
430 return frame.contentWindow.wrappedJSObject;
433 var Strings = $('offlineStrings') || $('commonStrings');
434 alert(Strings.getFormattedString('openils.global_util.content_window_jsobject.error', [frame, E]));
436 return frame.contentWindow;
441 var Strings = $('offlineStrings') || $('commonStrings');
442 alert(Strings.getFormattedString('openils.global_util.content_window.error', [frame, E]));
446 function update_modal_xulG(v) {
448 if (typeof xulG != "undefined" && xulG.not_modal) {
450 xulG.not_modal = true;
454 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
455 var key = location.pathname + location.search + location.hash;
456 if (typeof data.modal_xulG_stack != 'undefined' && typeof data.modal_xulG_stack[key] != 'undefined') {
457 data.modal_xulG_stack[key][ data.modal_xulG_stack[key].length - 1 ] = v;
458 data.stash('modal_xulG_stack');
461 alert('FIXME: update_modal_xulG => ' + E);
465 function xul_param(param_name,_params) {
466 /* 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 */
468 //dump('xul_param('+param_name+','+js2JSON(_params)+')\n');
469 var value = undefined; if (!_params) _params = {};
470 if (typeof _params.no_cgi == 'undefined') {
472 if (cgi.param(param_name)) {
473 var x = cgi.param(param_name);
474 //dump('\tfound via location.href = ' + x + '\n');
475 if (typeof _params.JSON2js_if_cgi != 'undefined') {
477 //dump('\tJSON2js = ' + x + '\n');
479 if (typeof _params.concat == 'undefined') {
480 //alert(param_name + ' x = ' + x);
484 if (value.constructor != Array) value = [ value ];
485 value = value.concat(x);
492 if (typeof _params.no_xulG == 'undefined') {
493 if (typeof _params.modal_xulG != 'undefined') {
494 if (typeof xulG != 'undefined' && xulG.not_modal) {
495 // for interfaces that used to be modal but aren't now, do nothing
497 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
498 var key = location.pathname + location.search + location.hash;
499 //dump('xul_param, considering modal key = ' + key + '\n');
500 if (typeof data.modal_xulG_stack != 'undefined' && typeof data.modal_xulG_stack[key] != 'undefined') {
501 xulG = data.modal_xulG_stack[key][ data.modal_xulG_stack[key].length - 1 ];
505 if (typeof xulG == 'object' && typeof xulG[ param_name ] != 'undefined') {
506 var x = xulG[ param_name ];
507 //dump('\tfound via xulG = ' + x + '\n');
508 if (typeof _params.JSON2js_if_xulG != 'undefined') {
510 //dump('\tJSON2js = ' + x + '\n');
512 if (typeof _params.concat == 'undefined') {
513 //alert(param_name + ' x = ' + x);
517 if (value.constructor != Array) value = [ value ];
518 value = value.concat(x);
525 if (typeof _params.no_xpcom == 'undefined') {
526 /* 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 */
527 if (typeof _params.stash_name != 'undefined') {
528 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
529 if (typeof data[ _params.stash_name ] != 'undefined') {
530 var x = data[ _params.stash_name ];
531 //dump('\tfound via xpcom = ' + x + '\n');
532 if (typeof _params.JSON2js_if_xpcom != 'undefined') {
534 //dump('\tJSON2js = ' + x + '\n');
536 if (_params.clear_xpcom) {
537 data[ _params.stash_name ] = undefined; data.stash( _params.stash_name );
539 if (typeof _params.concat == 'undefined') {
540 //alert(param_name + ' x = ' + x);
544 if (value.constructor != Array) value = [ value ];
545 value = value.concat(x);
553 //alert(param_name + ' value = ' + value);
556 dump('xul_param error: ' + E + '\n');
560 function get_bool(a) {
561 // Normal javascript interpretation except 'f' == false, per postgres, and 'F' == false, and '0' == false (newer JSON is returning '0' instead of 0 in cases)
562 // So false includes 'f', '', '0', 0, null, and undefined
563 if (a == 'f') return false;
564 if (a == 'F') return false;
565 if (a == '0') return false;
566 if (a) return true; else return false;
569 function get_localized_bool(a) {
570 var Strings = $('offlineStrings') || $('commonStrings');
571 return get_bool(a) ? Strings.getString('common.yes') : Strings.getString('common.no');
574 function get_db_true() {
578 function get_db_false() {
582 function copy_to_clipboard(ev) {
585 if (typeof ev == 'object') {
586 if (typeof ev.target != 'undefined') {
587 if (typeof ev.target.textContent != 'undefined') if (ev.target.textContent) text = ev.target.textContent;
588 if (typeof ev.target.value != 'undefined') if (ev.target.value) text = ev.target.value;
590 } else if (typeof ev == 'string') {
593 const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"]
594 .getService(Components.interfaces.nsIClipboardHelper);
595 gClipboardHelper.copyString(text);
596 var Strings = $('offlineStrings') || $('commonStrings');
597 alert(Strings.getFormattedString('openils.global_util.clipboard', [text]));
599 var Strings = $('offlineStrings') || $('commonStrings');
600 alert(Strings.getFormattedString('openils.global_util.clipboard.error', [E]));
604 function clear_the_cache() {
606 var cacheClass = Components.classes["@mozilla.org/network/cache-service;1"];
607 var cacheService = cacheClass.getService(Components.interfaces.nsICacheService);
608 cacheService.evictEntries(Components.interfaces.nsICache.STORE_ON_DISK);
609 cacheService.evictEntries(Components.interfaces.nsICache.STORE_IN_MEMORY);
611 var Strings = $('offlineStrings') || $('commonStrings');
612 alert(Strings.getFormattedString('openils.global_util.clear_cache.error', [E]));
616 function toOpenWindowByType(inType, uri) {
617 var winopts = "chrome,extrachrome,menubar,resizable,scrollbars,status,toolbar";
618 window.open(uri, "_blank", winopts);
621 function url_prefix(url) {
622 var base_url = url.match(/^[^?/|]+/);
624 base_url = base_url[0];
626 url = url.replace(/^[^?/|]+\|/, urls[base_url]);
628 if (url.match(/^\//)) url = urls.remote + url;
629 if (! url.match(/^(http|https|chrome|oils):\/\//) && ! url.match(/^data:/) ) url = 'http://' + url;
630 dump('url_prefix = ' + url + '\n');
634 function widget_prompt(node,args) {
635 // args is an object that may contain the following keys: title, desc, ok_label, ok_accesskey, cancel_label, cancel_accesskey, access, method
636 // access may contain 'property' or 'attribute' or 'method' for retrieving the value from the node
637 // if 'method', then the method key will reference a function that returns the value
639 if (!node) { return false; }
640 if (!args) { args = {}; }
641 args[ 'widget' ] = node;
643 var url = location.protocol == 'chrome'
644 ? 'chrome://open_ils_staff_client/content/util/widget_prompt.xul'
645 : '/xul/server/util/widget_prompt.xul';
647 JSAN.use('util.window'); var win = new util.window();
648 var my_xulG = win.open(
650 args.title || 'widget_prompt',
655 if (my_xulG.status == 'incomplete') {
658 return my_xulG.value;
661 alert('Error in global_utils.js, widget_prompt(): ' + E);