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('incrementing window.oils_lock\n');
11 dump(location.href + ' window.oils_lock == ' + window.oils_lock + '\n');
12 dump('incrementing data.unsaved_data\n');
13 dump('data.unsaved_data == ' + data.unsaved_data + '\n');
16 function oils_unsaved_data_P(count) {
17 if (!count) { count = 1; }
18 dump('decrementing window.oils_lock by ' + count + '\n');
19 window.oils_lock -= count;
20 if (window.oils_lock < 0) { window.oils_lock = 0; }
21 dump(location.href + ' window.oils_lock == ' + window.oils_lock + '\n');
22 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
23 data.stash_retrieve();
24 if (typeof data.unsaved_data == 'undefined') { data.unsaved_data = 0; }
25 dump('decrementing data.unsaved_data by ' + count + '\n');
26 data.unsaved_data -= count;
27 if (data.unsaved_data < 0) { data.unsaved_data = 0; }
28 data.stash('unsaved_data');
29 dump('data.unsaved_data == ' + data.unsaved_data + '\n');
32 function oils_lock_page(params) {
33 dump('oils_lock_page\n');
34 if (!params) { params = {}; }
35 if (window.oils_lock > 0) {
36 if (!params.allow_multiple_locks) {
37 return window.oils_lock;
40 if (typeof xulG != 'undefined') {
41 if (typeof xulG.unlock_tab == 'function') {
42 dump('\twith xulG.lock_tab\n');
45 dump('\twithout xulG.lock_tab\n');
46 oils_unsaved_data_V();
49 dump('\twithout xulG.lock_tab\n');
50 oils_unsaved_data_V();
52 return window.oils_lock;
55 function oils_unlock_page(params) {
56 dump('oils_unlock_page\n');
57 if (typeof xulG != 'undefined') {
58 if (typeof xulG.unlock_tab == 'function') {
59 dump('\twith xulG.unlock_tab\n');
62 dump('\twithout xulG.unlock_tab\n');
63 oils_unsaved_data_P();
66 dump('\twithout xulG.unlock_tab\n');
67 oils_unsaved_data_P();
69 return window.oils_lock;
73 dump(location.href + ' init window.oils_lock == ' + window.oils_lock + '\n');
74 window.addEventListener(
78 dump('oils_lock_page/oils_unlock_page onclose handler\n');
79 if (window.oils_lock > 0) {
80 var confirmation = window.confirm($('offlineStrings').getString('menu.close_window.unsaved_data_warning'));
87 if (typeof xulG != 'undefined') {
88 if (typeof xulG.unlock_tab == 'function') {
91 oils_unsaved_data_P( window.oils_lock );
94 oils_unsaved_data_P( window.oils_lock );
97 dump(location.href + ' forcing window.oils_lock == ' + window.oils_lock + '\n');
99 // Dispatching the window close event doesn't always close the window, even though the event does happen
105 dump('Error inside global_util.js, onclose handler, setTimeout window.close KLUDGE: ' + E + '\n');
112 dump('Error inside global_util.js, onclose handler: ' + E + '\n');
119 function ses(a,params) {
121 if (!params) params = {};
124 data = params.data; data.stash_retrieve();
126 // 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
127 JSAN.use('OpenILS.data'); data = new OpenILS.data(); data.stash_retrieve();
131 case 'staff' : return data.list.au[0]; break;
132 case 'staff_id' : return data.list.au[0].id(); break;
133 case 'staff_usrname' : return data.list.au[0].usrname(); break;
135 return data.list.au[0].ws_ou();
137 case 'ws_ou_shortname' :
138 return data.hash.aou[ data.list.au[0].ws_ou() ].shortname();
141 return data.session.authtime;
145 return data.session.key;
149 alert(location.href + '\nError in global_utils.js, ses(): ' + E);
154 function font_helper() {
156 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
157 removeCSSClass(document.documentElement,'ALL_FONTS_LARGER');
158 removeCSSClass(document.documentElement,'ALL_FONTS_SMALLER');
159 removeCSSClass(document.documentElement,'ALL_FONTS_XX_SMALL');
160 removeCSSClass(document.documentElement,'ALL_FONTS_X_SMALL');
161 removeCSSClass(document.documentElement,'ALL_FONTS_SMALL');
162 removeCSSClass(document.documentElement,'ALL_FONTS_MEDIUM');
163 removeCSSClass(document.documentElement,'ALL_FONTS_LARGE');
164 removeCSSClass(document.documentElement,'ALL_FONTS_X_LARGE');
165 removeCSSClass(document.documentElement,'ALL_FONTS_XX_LARGE');
166 addCSSClass(document.documentElement,data.global_font_adjust);
168 var Strings = $('offlineStrings') || $('commonStrings');
169 alert(Strings.getFormattedString('openils.global_util.font_size.error', [E]));
173 function oils_persist(e,cancelable) {
176 if (typeof cancelable == 'undefined') { cancelable = false; }
177 var evt = document.createEvent("Events");
178 evt.initEvent( 'oils_persist', false, cancelable ); // event name, bubbles, cancelable
179 e.dispatchEvent(evt);
181 alert('Error with oils_persist():' + E);
185 function persist_helper(base_key_suffix) {
187 if (base_key_suffix) {
188 base_key_suffix = base_key_suffix.replace(/[^A-Za-z]/g,'_') + '_';
190 base_key_suffix = '';
193 function gen_event_handler(etype,node) {
194 return function(ev) {
196 oils_persist(ev.target);
198 alert('Error in persist_helper, firing virtual event oils_persist after ' + etype + ' event on ' + node.nodeName + '.id = ' + node.id + ': ' + E);
203 function gen_oils_persist_handler(bk,node) {
204 return function(ev) {
206 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
208 if (ev.target.nodeName == 'command') {
210 if (ev.explicitOriginalTarget != node) return;
213 if (target == window) {
214 target = window.document.documentElement;
217 var filename = location.pathname.split('/')[ location.pathname.split('/').length - 1 ];
218 var base_key = 'oils_persist_' + String(location.hostname + '_' + filename + '_' + target.getAttribute('id')).replace('/','_','g') + '_' + base_key_suffix;
219 var attribute_list = target.getAttribute('oils_persist').split(' ');
220 dump('on_oils_persist: <<< ' + target.nodeName + '.id = ' + target.id + '\t' + bk + '\n');
221 for (var j = 0; j < attribute_list.length; j++) {
222 var key = base_key + attribute_list[j];
223 var value = target.getAttribute( attribute_list[j] );
224 if ( attribute_list[j] == 'checked' && ['checkbox','toolbarbutton'].indexOf( target.nodeName ) > -1 ) {
225 value = target.checked;
226 dump('\t' + value + ' <== .' + attribute_list[j] + '\n');
227 } else if ( attribute_list[j] == 'value' && ['textbox'].indexOf( target.nodeName ) > -1 ) {
228 value = target.value;
229 dump('\t' + value + ' <== .' + attribute_list[j] + '\n');
230 } else if ( attribute_list[j] == 'sizemode' && ['window'].indexOf( target.nodeName ) > -1 ) {
231 value = window.windowState;
232 dump('\t' + value + ' <== window.windowState, @' + attribute_list[j] + '\n');
233 } else if ( attribute_list[j] == 'height' && ['window'].indexOf( target.nodeName ) > -1 ) {
234 value = window.outerHeight;
235 dump('\t' + value + ' <== window.outerHeight, @' + attribute_list[j] + '\n');
236 } else if ( attribute_list[j] == 'width' && ['window'].indexOf( target.nodeName ) > -1 ) {
237 value = window.outerWidth;
238 dump('\t' + value + ' <== window.outerWidth, @' + attribute_list[j] + '\n');
240 dump('\t' + value + ' <== @' + attribute_list[j] + '\n');
242 prefs.setCharPref( key, value );
243 // TODO: Need to add logic for splitter repositioning, grippy state, etc.
244 // NOTE: oils_persist_peers and oils_persist="width" on those peers can help with the elements adjacent to a splitter
246 if (target.hasAttribute('oils_persist_peers') && ! ev.cancelable) { // We abuse the .cancelable field on the oils_persist event to prevent looping
247 var peer_list = target.getAttribute('oils_persist_peers').split(' ');
248 for (var j = 0; j < peer_list.length; j++) {
249 dump('on_oils_persist: dispatching oils_persist to peer ' + peer_list[j] + '\n');
250 oils_persist( document.getElementById( peer_list[j] ), true );
254 alert('Error in persist_helper() event listener for ' + bk + ': ' + E);
259 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
260 var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
261 var nodes = document.getElementsByAttribute('oils_persist','*');
262 for (var i = 0; i < nodes.length; i++) {
263 var filename = location.pathname.split('/')[ location.pathname.split('/').length - 1 ];
264 var base_key = 'oils_persist_' + String(location.hostname + '_' + filename + '_' + nodes[i].getAttribute('id')).replace('/','_','g') + '_' + base_key_suffix;
265 var attribute_list = nodes[i].getAttribute('oils_persist').split(' ');
266 dump('persist_helper: >>> ' + nodes[i].nodeName + '.id = ' + nodes[i].id + '\t' + base_key + '\n');
267 for (var j = 0; j < attribute_list.length; j++) {
268 var key = base_key + attribute_list[j];
269 var has_key = prefs.prefHasUserValue(key);
270 var value = has_key ? prefs.getCharPref(key) : null;
271 if (value == 'true') { value = true; }
272 if (value == 'false') { value = false; }
274 if ( attribute_list[j] == 'checked' && ['checkbox','toolbarbutton'].indexOf( nodes[i].nodeName ) > -1 ) {
275 nodes[i].checked = value;
276 dump('\t' + value + ' ==> .' + attribute_list[j] + '\n');
278 nodes[i].removeAttribute('checked');
279 dump('\tremoving @checked\n');
281 } else if ( attribute_list[j] == 'value' && ['textbox'].indexOf( nodes[i].nodeName ) > -1 ) {
282 nodes[i].value = value;
283 dump('\t' + value + ' ==> .' + attribute_list[j] + '\n');
284 } else if ( attribute_list[j] == 'sizemode' && ['window'].indexOf( nodes[i].nodeName ) > -1 ) {
286 case window.STATE_MAXIMIZED:
289 case window.STATE_MINIMIZED:
293 dump('\t' + value + ' ==> window.windowState, @' + attribute_list[j] + '\n');
294 } else if ( attribute_list[j] == 'height' && ['window'].indexOf( nodes[i].nodeName ) > -1 ) {
295 window.outerHeight = value;
296 dump('\t' + value + ' ==> window.outerHeight, @' + attribute_list[j] + '\n');
297 } else if ( attribute_list[j] == 'width' && ['window'].indexOf( nodes[i].nodeName ) > -1 ) {
298 window.outerWidth = value;
299 dump('\t' + value + ' ==> window.outerWidth, @' + attribute_list[j] + '\n');
301 nodes[i].setAttribute( attribute_list[j], value);
302 dump('\t' + value + ' ==> @' + attribute_list[j] + '\n');
306 var cmd = nodes[i].getAttribute('command');
307 var cmd_el = document.getElementById(cmd);
308 if (nodes[i].disabled == false && nodes[i].hidden == false) {
309 var no_poke = nodes[i].getAttribute('oils_persist_no_poke');
310 if (no_poke && no_poke == 'true') {
311 // Timing issue for some checkboxes; don't poke them with an event
312 dump('\tnot poking\n');
315 dump('\tpoking @command\n');
316 var evt = document.createEvent("Events");
317 evt.initEvent( 'command', true, true );
318 cmd_el.dispatchEvent(evt);
321 var evt = document.createEvent("Events");
322 evt.initEvent( 'command', true, true );
323 nodes[i].dispatchEvent(evt);
328 cmd_el.addEventListener(
330 gen_event_handler('command',cmd_el),
333 cmd_el.addEventListener(
335 gen_oils_persist_handler( base_key, nodes[i] ),
340 var event_types = [];
341 if (node.hasAttribute('oils_persist_events')) {
342 var event_type_list = node.getAttribute('oils_persist_events').split(' ');
343 for (var j = 0; j < event_type_list.length; j++) {
344 event_types.push( event_type_list[j] );
347 if (node.nodeName == 'textbox') {
348 event_types.push('change');
349 } else if (node.nodeName == 'window') {
350 event_types.push('resize');
351 node = window; // xul window is an element of window.document
353 event_types.push('command');
356 for (var j = 0; j < event_types.length; j++) {
357 node.addEventListener(
359 gen_event_handler(event_types[j],node),
363 node.addEventListener(
365 gen_oils_persist_handler( base_key, node ),
371 alert('Error in persist_helper(): ' + E);
375 function getKeys(o) {
377 for (var k in o) keys.push(k);
381 function get_contentWindow(frame) {
383 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
384 if (frame && frame.contentWindow) {
386 if (typeof frame.contentWindow.wrappedJSObject != 'undefined') {
387 return frame.contentWindow.wrappedJSObject;
390 var Strings = $('offlineStrings') || $('commonStrings');
391 alert(Strings.getFormattedString('openils.global_util.content_window_jsobject.error', [frame, E]));
393 return frame.contentWindow;
398 var Strings = $('offlineStrings') || $('commonStrings');
399 alert(Strings.getFormattedString('openils.global_util.content_window.error', [frame, E]));
403 function update_modal_xulG(v) {
405 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
406 var key = location.pathname + location.search + location.hash;
407 if (typeof data.modal_xulG_stack != 'undefined' && typeof data.modal_xulG_stack[key] != 'undefined') {
408 data.modal_xulG_stack[key][ data.modal_xulG_stack[key].length - 1 ] = v;
409 data.stash('modal_xulG_stack');
412 alert('FIXME: update_modal_xulG => ' + E);
416 function xul_param(param_name,_params) {
417 /* 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 */
419 //dump('xul_param('+param_name+','+js2JSON(_params)+')\n');
420 var value = undefined; if (!_params) _params = {};
421 if (typeof _params.no_cgi == 'undefined') {
423 if (cgi.param(param_name)) {
424 var x = cgi.param(param_name);
425 //dump('\tfound via location.href = ' + x + '\n');
426 if (typeof _params.JSON2js_if_cgi != 'undefined') {
428 //dump('\tJSON2js = ' + x + '\n');
430 if (typeof _params.concat == 'undefined') {
431 //alert(param_name + ' x = ' + x);
435 if (value.constructor != Array) value = [ value ];
436 value = value.concat(x);
443 if (typeof _params.no_xulG == 'undefined') {
444 if (typeof _params.modal_xulG != 'undefined') {
445 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
446 var key = location.pathname + location.search + location.hash;
447 //dump('xul_param, considering modal key = ' + key + '\n');
448 if (typeof data.modal_xulG_stack != 'undefined' && typeof data.modal_xulG_stack[key] != 'undefined') {
449 xulG = data.modal_xulG_stack[key][ data.modal_xulG_stack[key].length - 1 ];
452 if (typeof xulG == 'object' && typeof xulG[ param_name ] != 'undefined') {
453 var x = xulG[ param_name ];
454 //dump('\tfound via xulG = ' + x + '\n');
455 if (typeof _params.JSON2js_if_xulG != 'undefined') {
457 //dump('\tJSON2js = ' + x + '\n');
459 if (typeof _params.concat == 'undefined') {
460 //alert(param_name + ' x = ' + x);
464 if (value.constructor != Array) value = [ value ];
465 value = value.concat(x);
472 if (typeof _params.no_xpcom == 'undefined') {
473 /* 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 */
474 if (typeof _params.stash_name != 'undefined') {
475 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
476 if (typeof data[ _params.stash_name ] != 'undefined') {
477 var x = data[ _params.stash_name ];
478 //dump('\tfound via xpcom = ' + x + '\n');
479 if (typeof _params.JSON2js_if_xpcom != 'undefined') {
481 //dump('\tJSON2js = ' + x + '\n');
483 if (_params.clear_xpcom) {
484 data[ _params.stash_name ] = undefined; data.stash( _params.stash_name );
486 if (typeof _params.concat == 'undefined') {
487 //alert(param_name + ' x = ' + x);
491 if (value.constructor != Array) value = [ value ];
492 value = value.concat(x);
500 //alert(param_name + ' value = ' + value);
503 dump('xul_param error: ' + E + '\n');
507 function get_bool(a) {
508 // Normal javascript interpretation except 'f' == false, per postgres, and 'F' == false, and '0' == false (newer JSON is returning '0' instead of 0 in cases)
509 // So false includes 'f', '', '0', 0, null, and undefined
510 if (a == 'f') return false;
511 if (a == 'F') return false;
512 if (a == '0') return false;
513 if (a) return true; else return false;
516 function get_localized_bool(a) {
517 var Strings = $('offlineStrings') || $('commonStrings');
518 return get_bool(a) ? Strings.getString('common.yes') : Strings.getString('common.no');
521 function get_db_true() {
525 function get_db_false() {
529 function copy_to_clipboard(ev) {
531 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
533 if (typeof ev == 'object') {
534 if (typeof ev.target != 'undefined') {
535 if (typeof ev.target.textContent != 'undefined') if (ev.target.textContent) text = ev.target.textContent;
536 if (typeof ev.target.value != 'undefined') if (ev.target.value) text = ev.target.value;
538 } else if (typeof ev == 'string') {
541 const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"]
542 .getService(Components.interfaces.nsIClipboardHelper);
543 gClipboardHelper.copyString(text);
544 var Strings = $('offlineStrings') || $('commonStrings');
545 alert(Strings.getFormattedString('openils.global_util.clipboard', [text]));
547 var Strings = $('offlineStrings') || $('commonStrings');
548 alert(Strings.getFormattedString('openils.global_util.clipboard.error', [E]));
552 function clear_the_cache() {
554 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
555 var cacheClass = Components.classes["@mozilla.org/network/cache-service;1"];
556 var cacheService = cacheClass.getService(Components.interfaces.nsICacheService);
557 cacheService.evictEntries(Components.interfaces.nsICache.STORE_ON_DISK);
558 cacheService.evictEntries(Components.interfaces.nsICache.STORE_IN_MEMORY);
560 var Strings = $('offlineStrings') || $('commonStrings');
561 alert(Strings.getFormattedString('openils.global_util.clear_cache.error', [E]));
565 function toOpenWindowByType(inType, uri) {
566 var winopts = "chrome,extrachrome,menubar,resizable,scrollbars,status,toolbar";
567 window.open(uri, "_blank", winopts);
570 function url_prefix(url) {
571 if (url.match(/^\//)) url = urls.remote + url;
572 if (! url.match(/^(http|chrome):\/\//) && ! url.match(/^data:/) ) url = 'http://' + url;
573 dump('url_prefix = ' + url + '\n');