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;
146 return data.list.au[0].ws_ou();
148 case 'ws_ou_shortname' :
149 return data.hash.aou[ data.list.au[0].ws_ou() ].shortname();
152 return data.session.authtime;
156 return data.session.key;
160 alert(location.href + '\nError in global_utils.js, ses(): ' + E);
165 function font_helper() {
167 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
168 removeCSSClass(document.documentElement,'ALL_FONTS_LARGER');
169 removeCSSClass(document.documentElement,'ALL_FONTS_SMALLER');
170 removeCSSClass(document.documentElement,'ALL_FONTS_XX_SMALL');
171 removeCSSClass(document.documentElement,'ALL_FONTS_X_SMALL');
172 removeCSSClass(document.documentElement,'ALL_FONTS_SMALL');
173 removeCSSClass(document.documentElement,'ALL_FONTS_MEDIUM');
174 removeCSSClass(document.documentElement,'ALL_FONTS_LARGE');
175 removeCSSClass(document.documentElement,'ALL_FONTS_X_LARGE');
176 removeCSSClass(document.documentElement,'ALL_FONTS_XX_LARGE');
177 addCSSClass(document.documentElement,data.global_font_adjust);
179 var Strings = $('offlineStrings') || $('commonStrings');
180 alert(Strings.getFormattedString('openils.global_util.font_size.error', [E]));
184 function oils_persist(e,cancelable) {
187 if (typeof cancelable == 'undefined') { cancelable = false; }
188 var evt = document.createEvent("Events");
189 evt.initEvent( 'oils_persist', false, cancelable ); // event name, bubbles, cancelable
190 e.dispatchEvent(evt);
192 alert('Error with oils_persist():' + E);
196 function persist_helper(base_key_suffix) {
198 if (base_key_suffix) {
199 base_key_suffix = base_key_suffix.replace(/[^A-Za-z]/g,'_') + '_';
201 base_key_suffix = '';
204 function gen_event_handler(etype,node) {
205 return function(ev) {
207 oils_persist(ev.target);
209 alert('Error in persist_helper, firing virtual event oils_persist after ' + etype + ' event on ' + node.nodeName + '.id = ' + node.id + ': ' + E);
214 function gen_oils_persist_handler(bk,node) {
215 return function(ev) {
217 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
219 if (ev.target.nodeName == 'command') {
221 if (ev.explicitOriginalTarget != node) return;
224 if (target == window) {
225 target = window.document.documentElement;
228 var filename = location.pathname.split('/')[ location.pathname.split('/').length - 1 ];
229 var base_key = 'oils_persist_' + String(location.hostname + '_' + filename + '_' + target.getAttribute('id')).replace('/','_','g') + '_' + base_key_suffix;
230 var attribute_list = target.getAttribute('oils_persist').split(' ');
231 dump('on_oils_persist: <<< ' + target.nodeName + '.id = ' + target.id + '\t' + bk + '\n');
232 for (var j = 0; j < attribute_list.length; j++) {
233 var key = base_key + attribute_list[j];
234 var value = encodeURI(target.getAttribute( attribute_list[j] ));
235 if ( attribute_list[j] == 'checked' && ['checkbox','toolbarbutton'].indexOf( target.nodeName ) > -1 ) {
236 value = target.checked;
237 dump('\t' + value + ' <== .' + attribute_list[j] + '\n');
238 } else if ( attribute_list[j] == 'value' && ['menulist'].indexOf( target.nodeName ) > -1 ) {
239 value = target.value;
240 dump('\t' + value + ' <== .' + attribute_list[j] + '\n');
241 } else if ( attribute_list[j] == 'value' && ['textbox'].indexOf( target.nodeName ) > -1 ) {
242 value = target.value;
243 dump('\t' + value + ' <== .' + attribute_list[j] + '\n');
244 } else if ( attribute_list[j] == 'sizemode' && ['window'].indexOf( target.nodeName ) > -1 ) {
245 value = window.windowState;
246 dump('\t' + value + ' <== window.windowState, @' + attribute_list[j] + '\n');
247 } else if ( attribute_list[j] == 'height' && ['window'].indexOf( target.nodeName ) > -1 ) {
248 value = window.outerHeight;
249 dump('\t' + value + ' <== window.outerHeight, @' + attribute_list[j] + '\n');
250 } else if ( attribute_list[j] == 'width' && ['window'].indexOf( target.nodeName ) > -1 ) {
251 value = window.outerWidth;
252 dump('\t' + value + ' <== window.outerWidth, @' + attribute_list[j] + '\n');
254 dump('\t' + value + ' <== @' + attribute_list[j] + '\n');
256 prefs.setCharPref( key, value );
257 // TODO: Need to add logic for splitter repositioning, grippy state, etc.
258 // NOTE: oils_persist_peers and oils_persist="width" on those peers can help with the elements adjacent to a splitter
260 if (target.hasAttribute('oils_persist_peers') && ! ev.cancelable) { // We abuse the .cancelable field on the oils_persist event to prevent looping
261 var peer_list = target.getAttribute('oils_persist_peers').split(' ');
262 for (var j = 0; j < peer_list.length; j++) {
263 dump('on_oils_persist: dispatching oils_persist to peer ' + peer_list[j] + '\n');
264 oils_persist( document.getElementById( peer_list[j] ), true );
268 alert('Error in persist_helper() event listener for ' + bk + ': ' + E);
273 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
274 var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
275 var nodes = document.getElementsByAttribute('oils_persist','*');
276 for (var i = 0; i < nodes.length; i++) {
277 var filename = location.pathname.split('/')[ location.pathname.split('/').length - 1 ];
278 var base_key = 'oils_persist_' + String(location.hostname + '_' + filename + '_' + nodes[i].getAttribute('id')).replace('/','_','g') + '_' + base_key_suffix;
279 var attribute_list = nodes[i].getAttribute('oils_persist').split(' ');
280 dump('persist_helper: >>> ' + nodes[i].nodeName + '.id = ' + nodes[i].id + '\t' + base_key + '\n');
281 for (var j = 0; j < attribute_list.length; j++) {
282 var key = base_key + attribute_list[j];
283 var has_key = prefs.prefHasUserValue(key);
284 var value = has_key ? decodeURI(prefs.getCharPref(key)) : null;
285 if (value == 'true') { value = true; }
286 if (value == 'false') { value = false; }
288 if ( attribute_list[j] == 'checked' && ['checkbox','toolbarbutton'].indexOf( nodes[i].nodeName ) > -1 ) {
289 nodes[i].checked = value;
290 dump('\t' + value + ' ==> .' + attribute_list[j] + '\n');
292 nodes[i].removeAttribute('checked');
293 dump('\tremoving @checked\n');
295 } else if ( attribute_list[j] == 'value' && ['textbox'].indexOf( nodes[i].nodeName ) > -1 ) {
296 nodes[i].value = value;
297 dump('\t' + value + ' ==> .' + attribute_list[j] + '\n');
298 } else if ( attribute_list[j] == 'value' && ['menulist'].indexOf( nodes[i].nodeName ) > -1 ) {
299 nodes[i].value = value;
300 dump('\t' + value + ' ==> .' + attribute_list[j] + '\n');
301 } else if ( attribute_list[j] == 'sizemode' && ['window'].indexOf( nodes[i].nodeName ) > -1 ) {
303 case window.STATE_MAXIMIZED:
306 case window.STATE_MINIMIZED:
310 dump('\t' + value + ' ==> window.windowState, @' + attribute_list[j] + '\n');
311 } else if ( attribute_list[j] == 'height' && ['window'].indexOf( nodes[i].nodeName ) > -1 ) {
312 window.outerHeight = value;
313 dump('\t' + value + ' ==> window.outerHeight, @' + attribute_list[j] + '\n');
314 } else if ( attribute_list[j] == 'width' && ['window'].indexOf( nodes[i].nodeName ) > -1 ) {
315 window.outerWidth = value;
316 dump('\t' + value + ' ==> window.outerWidth, @' + attribute_list[j] + '\n');
318 nodes[i].setAttribute( attribute_list[j], value);
319 dump('\t' + value + ' ==> @' + attribute_list[j] + '\n');
323 var cmd = nodes[i].getAttribute('command');
324 var cmd_el = document.getElementById(cmd);
325 if (nodes[i].disabled == false && nodes[i].hidden == false) {
326 var no_poke = nodes[i].getAttribute('oils_persist_no_poke');
327 if (no_poke && no_poke == 'true') {
328 // Timing issue for some checkboxes; don't poke them with an event
329 dump('\tnot poking\n');
332 dump('\tpoking @command\n');
333 var evt = document.createEvent("Events");
334 evt.initEvent( 'command', true, true );
335 cmd_el.dispatchEvent(evt);
338 var evt = document.createEvent("Events");
339 evt.initEvent( 'command', true, true );
340 nodes[i].dispatchEvent(evt);
345 cmd_el.addEventListener(
347 gen_event_handler('command',cmd_el),
350 cmd_el.addEventListener(
352 gen_oils_persist_handler( base_key, nodes[i] ),
357 var event_types = [];
358 if (node.hasAttribute('oils_persist_events')) {
359 var event_type_list = node.getAttribute('oils_persist_events').split(' ');
360 for (var j = 0; j < event_type_list.length; j++) {
361 event_types.push( event_type_list[j] );
364 if (node.nodeName == 'textbox') {
365 event_types.push('change');
366 } else if (node.nodeName == 'menulist') {
367 event_types.push('select');
368 } else if (node.nodeName == 'window') {
369 event_types.push('resize');
370 node = window; // xul window is an element of window.document
372 event_types.push('command');
375 for (var j = 0; j < event_types.length; j++) {
376 node.addEventListener(
378 gen_event_handler(event_types[j],node),
382 node.addEventListener(
384 gen_oils_persist_handler( base_key, node ),
390 alert('Error in persist_helper(): ' + E);
394 function getKeys(o) {
396 for (var k in o) keys.push(k);
400 function get_contentWindow(frame) {
402 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
403 if (frame && frame.contentWindow) {
405 if (typeof frame.contentWindow.wrappedJSObject != 'undefined') {
406 return frame.contentWindow.wrappedJSObject;
409 var Strings = $('offlineStrings') || $('commonStrings');
410 alert(Strings.getFormattedString('openils.global_util.content_window_jsobject.error', [frame, E]));
412 return frame.contentWindow;
417 var Strings = $('offlineStrings') || $('commonStrings');
418 alert(Strings.getFormattedString('openils.global_util.content_window.error', [frame, E]));
422 function update_modal_xulG(v) {
424 if (typeof xulG != "undefined" && xulG.not_modal) {
426 xulG.not_modal = true;
430 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
431 var key = location.pathname + location.search + location.hash;
432 if (typeof data.modal_xulG_stack != 'undefined' && typeof data.modal_xulG_stack[key] != 'undefined') {
433 data.modal_xulG_stack[key][ data.modal_xulG_stack[key].length - 1 ] = v;
434 data.stash('modal_xulG_stack');
437 alert('FIXME: update_modal_xulG => ' + E);
441 function xul_param(param_name,_params) {
442 /* 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 */
444 //dump('xul_param('+param_name+','+js2JSON(_params)+')\n');
445 var value = undefined; if (!_params) _params = {};
446 if (typeof _params.no_cgi == 'undefined') {
448 if (cgi.param(param_name)) {
449 var x = cgi.param(param_name);
450 //dump('\tfound via location.href = ' + x + '\n');
451 if (typeof _params.JSON2js_if_cgi != 'undefined') {
453 //dump('\tJSON2js = ' + x + '\n');
455 if (typeof _params.concat == 'undefined') {
456 //alert(param_name + ' x = ' + x);
460 if (value.constructor != Array) value = [ value ];
461 value = value.concat(x);
468 if (typeof _params.no_xulG == 'undefined') {
469 if (typeof _params.modal_xulG != 'undefined') {
470 if (typeof xulG != 'undefined' && xulG.not_modal) {
471 // for interfaces that used to be modal but aren't now, do nothing
473 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
474 var key = location.pathname + location.search + location.hash;
475 //dump('xul_param, considering modal key = ' + key + '\n');
476 if (typeof data.modal_xulG_stack != 'undefined' && typeof data.modal_xulG_stack[key] != 'undefined') {
477 xulG = data.modal_xulG_stack[key][ data.modal_xulG_stack[key].length - 1 ];
481 if (typeof xulG == 'object' && typeof xulG[ param_name ] != 'undefined') {
482 var x = xulG[ param_name ];
483 //dump('\tfound via xulG = ' + x + '\n');
484 if (typeof _params.JSON2js_if_xulG != 'undefined') {
486 //dump('\tJSON2js = ' + x + '\n');
488 if (typeof _params.concat == 'undefined') {
489 //alert(param_name + ' x = ' + x);
493 if (value.constructor != Array) value = [ value ];
494 value = value.concat(x);
501 if (typeof _params.no_xpcom == 'undefined') {
502 /* 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 */
503 if (typeof _params.stash_name != 'undefined') {
504 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
505 if (typeof data[ _params.stash_name ] != 'undefined') {
506 var x = data[ _params.stash_name ];
507 //dump('\tfound via xpcom = ' + x + '\n');
508 if (typeof _params.JSON2js_if_xpcom != 'undefined') {
510 //dump('\tJSON2js = ' + x + '\n');
512 if (_params.clear_xpcom) {
513 data[ _params.stash_name ] = undefined; data.stash( _params.stash_name );
515 if (typeof _params.concat == 'undefined') {
516 //alert(param_name + ' x = ' + x);
520 if (value.constructor != Array) value = [ value ];
521 value = value.concat(x);
529 //alert(param_name + ' value = ' + value);
532 dump('xul_param error: ' + E + '\n');
536 function get_bool(a) {
537 // Normal javascript interpretation except 'f' == false, per postgres, and 'F' == false, and '0' == false (newer JSON is returning '0' instead of 0 in cases)
538 // So false includes 'f', '', '0', 0, null, and undefined
539 if (a == 'f') return false;
540 if (a == 'F') return false;
541 if (a == '0') return false;
542 if (a) return true; else return false;
545 function get_localized_bool(a) {
546 var Strings = $('offlineStrings') || $('commonStrings');
547 return get_bool(a) ? Strings.getString('common.yes') : Strings.getString('common.no');
550 function get_db_true() {
554 function get_db_false() {
558 function copy_to_clipboard(ev) {
560 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
562 if (typeof ev == 'object') {
563 if (typeof ev.target != 'undefined') {
564 if (typeof ev.target.textContent != 'undefined') if (ev.target.textContent) text = ev.target.textContent;
565 if (typeof ev.target.value != 'undefined') if (ev.target.value) text = ev.target.value;
567 } else if (typeof ev == 'string') {
570 const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"]
571 .getService(Components.interfaces.nsIClipboardHelper);
572 gClipboardHelper.copyString(text);
573 var Strings = $('offlineStrings') || $('commonStrings');
574 alert(Strings.getFormattedString('openils.global_util.clipboard', [text]));
576 var Strings = $('offlineStrings') || $('commonStrings');
577 alert(Strings.getFormattedString('openils.global_util.clipboard.error', [E]));
581 function clear_the_cache() {
583 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
584 var cacheClass = Components.classes["@mozilla.org/network/cache-service;1"];
585 var cacheService = cacheClass.getService(Components.interfaces.nsICacheService);
586 cacheService.evictEntries(Components.interfaces.nsICache.STORE_ON_DISK);
587 cacheService.evictEntries(Components.interfaces.nsICache.STORE_IN_MEMORY);
589 var Strings = $('offlineStrings') || $('commonStrings');
590 alert(Strings.getFormattedString('openils.global_util.clear_cache.error', [E]));
594 function toOpenWindowByType(inType, uri) {
595 var winopts = "chrome,extrachrome,menubar,resizable,scrollbars,status,toolbar";
596 window.open(uri, "_blank", winopts);
599 function url_prefix(url) {
600 if (url.match(/^\//)) url = urls.remote + url;
601 if (! url.match(/^(http|chrome):\/\//) && ! url.match(/^data:/) ) url = 'http://' + url;
602 dump('url_prefix = ' + url + '\n');
606 function widget_prompt(node,args) {
607 // args is an object that may contain the following keys: title, desc, ok_label, ok_accesskey, cancel_label, cancel_accesskey, access, method
608 // access may contain 'property' or 'attribute' or 'method' for retrieving the value from the node
609 // if 'method', then the method key will reference a function that returns the value
611 if (!node) { return false; }
612 if (!args) { args = {}; }
613 args[ 'widget' ] = node;
615 var url = location.protocol == 'chrome'
616 ? 'chrome://open_ils_staff_client/content/util/widget_prompt.xul'
617 : '/xul/server/util/widget_prompt.xul';
619 JSAN.use('util.window'); var win = new util.window();
620 var my_xulG = win.open(
622 args.title || 'widget_prompt',
627 if (my_xulG.status == 'incomplete') {
630 return my_xulG.value;
633 alert('Error in global_utils.js, widget_prompt(): ' + E);