1 function $(id) { return document.getElementById(id); }
3 function ses(a,params) {
5 if (!params) params = {};
8 data = params.data; data.stash_retrieve();
10 // 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
11 JSAN.use('OpenILS.data'); data = new OpenILS.data(); data.stash_retrieve();
15 case 'staff' : return data.list.au[0]; break;
16 case 'staff_id' : return data.list.au[0].id(); break;
17 case 'staff_usrname' : return data.list.au[0].usrname(); break;
19 return data.list.au[0].ws_ou();
21 case 'ws_ou_shortname' :
22 return data.hash.aou[ data.list.au[0].ws_ou() ].shortname();
25 return data.session.authtime;
29 return data.session.key;
33 alert(location.href + '\nError in global_utils.js, ses(): ' + E);
38 function font_helper() {
40 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
41 removeCSSClass(document.documentElement,'ALL_FONTS_LARGER');
42 removeCSSClass(document.documentElement,'ALL_FONTS_SMALLER');
43 removeCSSClass(document.documentElement,'ALL_FONTS_XX_SMALL');
44 removeCSSClass(document.documentElement,'ALL_FONTS_X_SMALL');
45 removeCSSClass(document.documentElement,'ALL_FONTS_SMALL');
46 removeCSSClass(document.documentElement,'ALL_FONTS_MEDIUM');
47 removeCSSClass(document.documentElement,'ALL_FONTS_LARGE');
48 removeCSSClass(document.documentElement,'ALL_FONTS_X_LARGE');
49 removeCSSClass(document.documentElement,'ALL_FONTS_XX_LARGE');
50 addCSSClass(document.documentElement,data.global_font_adjust);
52 var Strings = $('offlineStrings') || $('commonStrings');
53 alert(Strings.getFormattedString('openils.global_util.font_size.error', [E]));
57 function oils_persist(e,cancelable) {
60 if (typeof cancelable == 'undefined') { cancelable = false; }
61 var evt = document.createEvent("Events");
62 evt.initEvent( 'oils_persist', false, cancelable ); // event name, bubbles, cancelable
65 alert('Error with oils_persist():' + E);
69 function persist_helper(base_key_suffix) {
71 if (base_key_suffix) {
72 base_key_suffix = base_key_suffix.replace(/[^A-Za-z]/g,'_') + '_';
77 function gen_event_handler(etype,node) {
80 oils_persist(ev.target);
82 alert('Error in persist_helper, firing virtual event oils_persist after ' + etype + ' event on ' + node.nodeName + '.id = ' + node.id + ': ' + E);
87 function gen_oils_persist_handler(bk,node) {
90 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
92 if (ev.target.nodeName == 'command') {
94 if (ev.explicitOriginalTarget != node) return;
98 var filename = location.pathname.split('/')[ location.pathname.split('/').length - 1 ];
99 var base_key = 'oils_persist_' + String(location.hostname + '_' + filename + '_' + target.getAttribute('id')).replace('/','_','g') + '_' + base_key_suffix;
100 var attribute_list = target.getAttribute('oils_persist').split(' ');
101 dump('on_oils_persist: <<< ' + target.nodeName + '.id = ' + target.id + '\t' + bk + '\n');
102 for (var j = 0; j < attribute_list.length; j++) {
103 var key = base_key + attribute_list[j];
104 var value = target.getAttribute( attribute_list[j] );
105 if ( attribute_list[j] == 'checked' && ['checkbox','toolbarbutton'].indexOf( target.nodeName ) > -1 ) {
106 value = target.checked;
107 dump('\t' + value + ' <== .' + attribute_list[j] + '\n');
108 } else if ( attribute_list[j] == 'value' && ['textbox'].indexOf( target.nodeName ) > -1 ) {
109 value = target.value;
110 dump('\t' + value + ' <== .' + attribute_list[j] + '\n');
112 dump('\t' + value + ' <== @' + attribute_list[j] + '\n');
114 prefs.setCharPref( key, value );
115 // TODO: Need to add logic for window resizing, splitter repositioning, grippy state, etc.
116 // NOTE: oils_persist_peers and oils_persist="width" on those peers can help with the elements adjacent to a splitter
118 if (target.hasAttribute('oils_persist_peers') && ! ev.cancelable) { // We abuse the .cancelable field on the oils_persist event to prevent looping
119 var peer_list = target.getAttribute('oils_persist_peers').split(' ');
120 for (var j = 0; j < peer_list.length; j++) {
121 dump('on_oils_persist: dispatching oils_persist to peer ' + peer_list[j] + '\n');
122 oils_persist( document.getElementById( peer_list[j] ), true );
126 alert('Error in persist_helper() event listener for ' + bk + ': ' + E);
131 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
132 var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
133 var nodes = document.getElementsByAttribute('oils_persist','*');
134 for (var i = 0; i < nodes.length; i++) {
135 var filename = location.pathname.split('/')[ location.pathname.split('/').length - 1 ];
136 var base_key = 'oils_persist_' + String(location.hostname + '_' + filename + '_' + nodes[i].getAttribute('id')).replace('/','_','g') + '_' + base_key_suffix;
137 var attribute_list = nodes[i].getAttribute('oils_persist').split(' ');
138 dump('persist_helper: >>> ' + nodes[i].nodeName + '.id = ' + nodes[i].id + '\t' + base_key + '\n');
139 for (var j = 0; j < attribute_list.length; j++) {
140 var key = base_key + attribute_list[j];
141 var has_key = prefs.prefHasUserValue(key);
142 var value = has_key ? prefs.getCharPref(key) : null;
143 if (value == 'true') { value = true; }
144 if (value == 'false') { value = false; }
146 if ( attribute_list[j] == 'checked' && ['checkbox','toolbarbutton'].indexOf( nodes[i].nodeName ) > -1 ) {
147 nodes[i].checked = value;
148 dump('\t' + value + ' ==> .' + attribute_list[j] + '\n');
150 nodes[i].removeAttribute('checked');
151 dump('\tremoving @checked\n');
153 } else if ( attribute_list[j] == 'value' && ['textbox'].indexOf( nodes[i].nodeName ) > -1 ) {
154 nodes[i].value = value;
155 dump('\t' + value + ' ==> .' + attribute_list[j] + '\n');
157 nodes[i].setAttribute( attribute_list[j], value);
158 dump('\t' + value + ' ==> @' + attribute_list[j] + '\n');
162 var cmd = nodes[i].getAttribute('command');
163 var cmd_el = document.getElementById(cmd);
164 if (nodes[i].disabled == false && nodes[i].hidden == false) {
165 var no_poke = nodes[i].getAttribute('oils_persist_no_poke');
166 if (no_poke && no_poke == 'true') {
167 // Timing issue for some checkboxes; don't poke them with an event
168 dump('\tnot poking\n');
171 dump('\tpoking @command\n');
172 var evt = document.createEvent("Events");
173 evt.initEvent( 'command', true, true );
174 cmd_el.dispatchEvent(evt);
177 var evt = document.createEvent("Events");
178 evt.initEvent( 'command', true, true );
179 nodes[i].dispatchEvent(evt);
184 cmd_el.addEventListener(
186 gen_event_handler('command',cmd_el),
189 cmd_el.addEventListener(
191 gen_oils_persist_handler( base_key, nodes[i] ),
195 var event_types = [];
196 if (nodes[i].hasAttribute('oils_persist_events')) {
197 var event_type_list = nodes[i].getAttribute('oils_persist_events').split(' ');
198 for (var j = 0; j < event_type_list.length; j++) {
199 event_types.push( event_type_list[j] );
202 if (nodes[i].nodeName == 'textbox') {
203 event_types.push('change');
205 event_types.push('command');
208 for (var j = 0; j < event_types.length; j++) {
209 nodes[i].addEventListener(
211 gen_event_handler(event_types[j],nodes[i]),
215 nodes[i].addEventListener(
217 gen_oils_persist_handler( base_key, nodes[i] ),
223 alert('Error in persist_helper(): ' + E);
227 function getKeys(o) {
229 for (var k in o) keys.push(k);
233 function get_contentWindow(frame) {
235 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
236 if (frame && frame.contentWindow) {
238 if (typeof frame.contentWindow.wrappedJSObject != 'undefined') {
239 return frame.contentWindow.wrappedJSObject;
242 var Strings = $('offlineStrings') || $('commonStrings');
243 alert(Strings.getFormattedString('openils.global_util.content_window_jsobject.error', [frame, E]));
245 return frame.contentWindow;
250 var Strings = $('offlineStrings') || $('commonStrings');
251 alert(Strings.getFormattedString('openils.global_util.content_window.error', [frame, E]));
255 function update_modal_xulG(v) {
257 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
258 var key = location.pathname + location.search + location.hash;
259 if (typeof data.modal_xulG_stack != 'undefined' && typeof data.modal_xulG_stack[key] != 'undefined') {
260 data.modal_xulG_stack[key][ data.modal_xulG_stack[key].length - 1 ] = v;
261 data.stash('modal_xulG_stack');
264 alert('FIXME: update_modal_xulG => ' + E);
268 function xul_param(param_name,_params) {
269 /* 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 */
271 //dump('xul_param('+param_name+','+js2JSON(_params)+')\n');
272 var value = undefined; if (!_params) _params = {};
273 if (typeof _params.no_cgi == 'undefined') {
275 if (cgi.param(param_name)) {
276 var x = cgi.param(param_name);
277 //dump('\tfound via location.href = ' + x + '\n');
278 if (typeof _params.JSON2js_if_cgi != 'undefined') {
280 //dump('\tJSON2js = ' + x + '\n');
282 if (typeof _params.concat == 'undefined') {
283 //alert(param_name + ' x = ' + x);
287 if (value.constructor != Array) value = [ value ];
288 value = value.concat(x);
295 if (typeof _params.no_xulG == 'undefined') {
296 if (typeof _params.modal_xulG != 'undefined') {
297 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
298 var key = location.pathname + location.search + location.hash;
299 //dump('xul_param, considering modal key = ' + key + '\n');
300 if (typeof data.modal_xulG_stack != 'undefined' && typeof data.modal_xulG_stack[key] != 'undefined') {
301 xulG = data.modal_xulG_stack[key][ data.modal_xulG_stack[key].length - 1 ];
304 if (typeof xulG == 'object' && typeof xulG[ param_name ] != 'undefined') {
305 var x = xulG[ param_name ];
306 //dump('\tfound via xulG = ' + x + '\n');
307 if (typeof _params.JSON2js_if_xulG != 'undefined') {
309 //dump('\tJSON2js = ' + x + '\n');
311 if (typeof _params.concat == 'undefined') {
312 //alert(param_name + ' x = ' + x);
316 if (value.constructor != Array) value = [ value ];
317 value = value.concat(x);
324 if (typeof _params.no_xpcom == 'undefined') {
325 /* 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 */
326 if (typeof _params.stash_name != 'undefined') {
327 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
328 if (typeof data[ _params.stash_name ] != 'undefined') {
329 var x = data[ _params.stash_name ];
330 //dump('\tfound via xpcom = ' + x + '\n');
331 if (typeof _params.JSON2js_if_xpcom != 'undefined') {
333 //dump('\tJSON2js = ' + x + '\n');
335 if (_params.clear_xpcom) {
336 data[ _params.stash_name ] = undefined; data.stash( _params.stash_name );
338 if (typeof _params.concat == 'undefined') {
339 //alert(param_name + ' x = ' + x);
343 if (value.constructor != Array) value = [ value ];
344 value = value.concat(x);
352 //alert(param_name + ' value = ' + value);
355 dump('xul_param error: ' + E + '\n');
359 function get_bool(a) {
360 // Normal javascript interpretation except 'f' == false, per postgres, and 'F' == false, and '0' == false (newer JSON is returning '0' instead of 0 in cases)
361 // So false includes 'f', '', '0', 0, null, and undefined
362 if (a == 'f') return false;
363 if (a == 'F') return false;
364 if (a == '0') return false;
365 if (a) return true; else return false;
368 function get_localized_bool(a) {
369 var Strings = $('offlineStrings') || $('commonStrings');
370 return get_bool(a) ? Strings.getString('common.yes') : Strings.getString('common.no');
373 function get_db_true() {
377 function get_db_false() {
381 function copy_to_clipboard(ev) {
383 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
385 if (typeof ev == 'object') {
386 if (typeof ev.target != 'undefined') {
387 if (typeof ev.target.textContent != 'undefined') if (ev.target.textContent) text = ev.target.textContent;
388 if (typeof ev.target.value != 'undefined') if (ev.target.value) text = ev.target.value;
390 } else if (typeof ev == 'string') {
393 const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"]
394 .getService(Components.interfaces.nsIClipboardHelper);
395 gClipboardHelper.copyString(text);
396 var Strings = $('offlineStrings') || $('commonStrings');
397 alert(Strings.getFormattedString('openils.global_util.clipboard', [text]));
399 var Strings = $('offlineStrings') || $('commonStrings');
400 alert(Strings.getFormattedString('openils.global_util.clipboard.error', [E]));
404 function clear_the_cache() {
406 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
407 var cacheClass = Components.classes["@mozilla.org/network/cache-service;1"];
408 var cacheService = cacheClass.getService(Components.interfaces.nsICacheService);
409 cacheService.evictEntries(Components.interfaces.nsICache.STORE_ON_DISK);
410 cacheService.evictEntries(Components.interfaces.nsICache.STORE_IN_MEMORY);
412 var Strings = $('offlineStrings') || $('commonStrings');
413 alert(Strings.getFormattedString('openils.global_util.clear_cache.error', [E]));
417 function toOpenWindowByType(inType, uri) {
418 var winopts = "chrome,extrachrome,menubar,resizable,scrollbars,status,toolbar";
419 window.open(uri, "_blank", winopts);
422 function url_prefix(url) {
423 if (url.match(/^\//)) url = urls.remote + url;
424 if (! url.match(/^(http|chrome):\/\//) && ! url.match(/^data:/) ) url = 'http://' + url;
425 dump('url_prefix = ' + url + '\n');