]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/OpenILS/global_util.js
merge from my offline repo. Toward a billing interface replacement. Other things...
[working/Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / OpenILS / global_util.js
1     function $(id) { return document.getElementById(id); }
2
3     function ses(a,params) {
4         try {
5             if (!params) params = {};
6             var data;
7             if (params.data) {
8                 data = params.data; data.stash_retrieve();
9             } else {
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();
12             }
13
14             switch(a) {
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;
18                 case 'ws_ou' :
19                     return data.list.au[0].ws_ou();
20                 break;
21                 case 'authtime' :
22                     return data.session.authtime;
23                 break;
24                 case 'key':
25                 default:
26                     return data.session.key;
27                 break;
28             }
29         } catch(E) {
30             alert(location.href + '\nError in global_utils.js, ses(): ' + E);
31             throw(E);
32         }
33     }
34
35     function font_helper() {
36         try {
37             JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
38             removeCSSClass(document.documentElement,'ALL_FONTS_LARGER');
39             removeCSSClass(document.documentElement,'ALL_FONTS_SMALLER');
40             removeCSSClass(document.documentElement,'ALL_FONTS_XX_SMALL');
41             removeCSSClass(document.documentElement,'ALL_FONTS_X_SMALL');
42             removeCSSClass(document.documentElement,'ALL_FONTS_SMALL');
43             removeCSSClass(document.documentElement,'ALL_FONTS_MEDIUM');
44             removeCSSClass(document.documentElement,'ALL_FONTS_LARGE');
45             removeCSSClass(document.documentElement,'ALL_FONTS_X_LARGE');
46             removeCSSClass(document.documentElement,'ALL_FONTS_XX_LARGE');
47             addCSSClass(document.documentElement,data.global_font_adjust);
48         } catch(E) {
49             var Strings = $('offlineStrings') || $('commonStrings');
50             alert(Strings.getFormattedString('openils.global_util.font_size.error', [E]));
51         }
52     }
53
54     function oils_persist(e,cancelable) {
55         try {
56             if (!e) { return; }
57             if (typeof cancelable == 'undefined') { cancelable = false; } 
58             var evt = document.createEvent("Events");
59             evt.initEvent( 'oils_persist', false, cancelable ); // event name, bubbles, cancelable
60             e.dispatchEvent(evt);
61         } catch(E) {
62             alert('Error with oils_persist():' + E);
63         }
64     }
65
66     function persist_helper() {
67         try {
68             function gen_event_handler(etype,node) {
69                 return function(ev) {
70                     try {
71                         oils_persist(ev.target);
72                     } catch(E) {
73                         alert('Error in persist_helper, firing virtual event oils_persist after ' + etype + ' event on ' + node.nodeName + '.id = ' + node.id + ': ' + E);
74                     }
75                 };
76             };
77
78             function gen_oils_persist_handler(bk,node) {
79                 return function(ev) {
80                     try {
81                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
82                         var target;
83                         if (ev.target.nodeName == 'command') {
84                             target = node;
85                             if (ev.explicitOriginalTarget != node) return;
86                         } else {
87                             target = ev.target;
88                         }
89                         var filename = location.pathname.split('/')[ location.pathname.split('/').length - 1 ];
90                         var base_key = 'oils_persist_' + String(location.hostname + '_' + filename + '_' + target.getAttribute('id')).replace('/','_','g') + '_';
91                         var attribute_list = target.getAttribute('oils_persist').split(' ');
92                         dump('on_oils_persist: <<< ' + target.nodeName + '.id = ' + target.id + '\t' + bk + '\n');
93                         for (var j = 0; j < attribute_list.length; j++) {
94                             var key = base_key + attribute_list[j];
95                             var value = target.getAttribute( attribute_list[j] );
96                             if ( attribute_list[j] == 'checked' && ['checkbox','toolbarbutton'].indexOf( target.nodeName ) > -1 ) {
97                                 value = target.checked;
98                                 dump('\t' + value + ' <== .' + attribute_list[j] + '\n');
99                             } else if ( attribute_list[j] == 'value' && ['textbox'].indexOf( target.nodeName ) > -1 ) {
100                                 value = target.value;
101                                 dump('\t' + value + ' <== .' + attribute_list[j] + '\n');
102                             } else {
103                                 dump('\t' + value + ' <== @' + attribute_list[j] + '\n');
104                             }
105                             prefs.setCharPref( key, value );
106                             // TODO: Need to add logic for window resizing, splitter repositioning, grippy state, etc.
107                         }
108                         if (target.hasAttribute('oils_persist_peers') && ! ev.cancelable) { // We abuse the .cancelable field on the oils_persist event to prevent looping
109                             var peer_list = target.getAttribute('oils_persist_peers').split(' ');
110                             for (var j = 0; j < peer_list.length; j++) {
111                                 dump('on_oils_persist: dispatching oils_persist to peer ' + peer_list[j] + '\n');
112                                 oils_persist( document.getElementById( peer_list[j] ), true );
113                             } 
114                         }
115                     } catch(E) {
116                         alert('Error in persist_helper() event listener for ' + bk + ': ' + E);
117                     }
118                 };
119             }
120
121             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
122             var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
123             var nodes = document.getElementsByAttribute('oils_persist','*');
124             for (var i = 0; i < nodes.length; i++) {
125                 var filename = location.pathname.split('/')[ location.pathname.split('/').length - 1 ];
126                 var base_key = 'oils_persist_' + String(location.hostname + '_' + filename + '_' + nodes[i].getAttribute('id')).replace('/','_','g') + '_';
127                 var attribute_list = nodes[i].getAttribute('oils_persist').split(' ');
128                 dump('persist_helper: >>> ' + nodes[i].nodeName + '.id = ' + nodes[i].id + '\t' + base_key + '\n');
129                 for (var j = 0; j < attribute_list.length; j++) {
130                     var key = base_key + attribute_list[j];
131                     var has_key = prefs.prefHasUserValue(key);
132                     var value = has_key ? prefs.getCharPref(key) : null;
133                     if (value == 'true') { value = true; }
134                     if (value == 'false') { value = false; }
135                     if (has_key) {
136                         if ( attribute_list[j] == 'checked' && ['checkbox','toolbarbutton'].indexOf( nodes[i].nodeName ) > -1 ) {
137                             nodes[i].checked = value; 
138                             dump('\t' + value + ' ==> .' + attribute_list[j] + '\n');
139                             if (!value) {
140                                 nodes[i].removeAttribute('checked');
141                                 dump('\tremoving @checked\n');
142                             }
143                         } else if ( attribute_list[j] == 'value' && ['textbox'].indexOf( nodes[i].nodeName ) > -1 ) {
144                             nodes[i].value = value;
145                             dump('\t' + value + ' ==> .' + attribute_list[j] + '\n');
146                         } else {
147                             nodes[i].setAttribute( attribute_list[j], value);
148                             dump('\t' + value + ' ==> @' + attribute_list[j] + '\n');
149                         }
150                     }
151                 }
152                 var cmd = nodes[i].getAttribute('command');
153                 var cmd_el = document.getElementById(cmd);
154                 if (nodes[i].disabled == false && nodes[i].hidden == false) {
155                     var no_poke = nodes[i].getAttribute('oils_persist_no_poke');
156                     if (no_poke && no_poke == 'true') {
157                         // Timing issue for some checkboxes; don't poke them with an event
158                         dump('\tnot poking\n');
159                     } else {
160                         if (cmd_el) {
161                             dump('\tpoking @command\n');
162                             var evt = document.createEvent("Events");
163                             evt.initEvent( 'command', true, true );
164                             cmd_el.dispatchEvent(evt);
165                         } else {
166                             dump('\tpoking\n');
167                             var evt = document.createEvent("Events");
168                             evt.initEvent( 'command', true, true );
169                             nodes[i].dispatchEvent(evt);
170                         }
171                     }
172                 }
173                 if (cmd_el) {
174                     cmd_el.addEventListener(
175                         'command',
176                         gen_event_handler('command',cmd_el),
177                         false
178                     );
179                     cmd_el.addEventListener(
180                         'oils_persist',
181                         gen_oils_persist_handler( base_key, nodes[i] ),
182                         false
183                     );
184                 } else {
185                     var event_types = [];
186                     if (nodes[i].hasAttribute('oils_persist_events')) {
187                         var event_type_list = nodes[i].getAttribute('oils_persist_events').split(' ');
188                         for (var j = 0; j < event_type_list.length; j++) {
189                             event_types.push( event_type_list[j] );
190                         }
191                     } else {
192                         if (nodes[i].nodeName == 'textbox') { 
193                             event_types.push('change'); 
194                         } else {
195                             event_types.push('command'); 
196                         }
197                     }
198                     for (var j = 0; j < event_types.length; j++) {
199                         nodes[i].addEventListener(
200                             event_types[j],
201                             gen_event_handler(event_types[j],nodes[i]),
202                             false
203                         );
204                     }
205                     nodes[i].addEventListener(
206                         'oils_persist',
207                         gen_oils_persist_handler( base_key, nodes[i] ),
208                         false
209                     );
210                 }
211             }
212         } catch(E) {
213             alert('Error in persist_helper(): ' + E);
214         }
215     }
216
217     function getKeys(o) {
218         var keys = [];
219         for (var k in o) keys.push(k);
220         return keys;
221     }
222
223     function get_contentWindow(frame) {
224         try {
225             netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
226             if (frame && frame.contentWindow) {
227                 try {
228                     if (typeof frame.contentWindow.wrappedJSObject != 'undefined') {
229                                      return frame.contentWindow.wrappedJSObject;
230                           }
231                 } catch(E) {
232                     var Strings = $('offlineStrings') || $('commonStrings');
233                     alert(Strings.getFormattedString('openils.global_util.content_window_jsobject.error', [frame, E]));
234                 }
235                 return frame.contentWindow;
236             } else {
237                 return null;
238             }
239         } catch(E) {
240             var Strings = $('offlineStrings') || $('commonStrings');
241             alert(Strings.getFormattedString('openils.global_util.content_window.error', [frame, E]));
242         }
243     }
244
245     function update_modal_xulG(v) {
246         try {
247             JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
248             var key = location.pathname + location.search + location.hash;
249             if (typeof data.modal_xulG_stack != 'undefined' && typeof data.modal_xulG_stack[key] != 'undefined') {
250                 data.modal_xulG_stack[key][ data.modal_xulG_stack[key].length - 1 ] = v;
251                 data.stash('modal_xulG_stack');
252             }
253         } catch(E) {
254             alert('FIXME: update_modal_xulG => ' + E);
255         }
256     }
257
258     function xul_param(param_name,_params) {
259         /* 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 */
260         try {
261             //dump('xul_param('+param_name+','+js2JSON(_params)+')\n');
262             var value = undefined; if (!_params) _params = {};
263             if (typeof _params.no_cgi == 'undefined') {
264                 var cgi = new CGI();
265                 if (cgi.param(param_name)) {
266                     var x = cgi.param(param_name);
267                     //dump('\tfound via location.href = ' + x + '\n');
268                     if (typeof _params.JSON2js_if_cgi != 'undefined') {
269                         x = JSON2js( x );
270                         //dump('\tJSON2js = ' + x + '\n');
271                     }
272                     if (typeof _params.concat == 'undefined') {
273                         //alert(param_name + ' x = ' + x);
274                         return x; // value
275                     } else {
276                         if (value) {
277                             if (value.constructor != Array) value = [ value ];
278                             value = value.concat(x);
279                         } else {
280                             value = x;
281                         }
282                     }
283                 }
284             }
285             if (typeof _params.no_xulG == 'undefined') {
286                 if (typeof _params.modal_xulG != 'undefined') {
287                     JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
288                     var key = location.pathname + location.search + location.hash;
289                     //dump('xul_param, considering modal key = ' + key + '\n');
290                     if (typeof data.modal_xulG_stack != 'undefined' && typeof data.modal_xulG_stack[key] != 'undefined') {
291                         xulG = data.modal_xulG_stack[key][ data.modal_xulG_stack[key].length - 1 ];
292                     }
293                 }
294                 if (typeof xulG == 'object' && typeof xulG[ param_name ] != 'undefined') {
295                     var x = xulG[ param_name ];
296                     //dump('\tfound via xulG = ' + x + '\n');
297                     if (typeof _params.JSON2js_if_xulG != 'undefined') {
298                         x = JSON2js( x );
299                         //dump('\tJSON2js = ' + x + '\n');
300                     }
301                     if (typeof _params.concat == 'undefined') {
302                         //alert(param_name + ' x = ' + x);
303                         return x; // value
304                     } else {
305                         if (value) {
306                             if (value.constructor != Array) value = [ value ];
307                             value = value.concat(x);
308                         } else {
309                             value = x;
310                         }
311                     }
312                 }
313             }
314             if (typeof _params.no_xpcom == 'undefined') {
315                 /* 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 */
316                 if (typeof _params.stash_name != 'undefined') { 
317                     JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
318                     if (typeof data[ _params.stash_name ] != 'undefined') {
319                         var x = data[ _params.stash_name ];
320                         //dump('\tfound via xpcom = ' + x + '\n');
321                         if (typeof _params.JSON2js_if_xpcom != 'undefined') {
322                             x = JSON2js( x );
323                             //dump('\tJSON2js = ' + x + '\n');
324                         }
325                         if (_params.clear_xpcom) { 
326                             data[ _params.stash_name ] = undefined; data.stash( _params.stash_name ); 
327                         }
328                         if (typeof _params.concat == 'undefined') {
329                             //alert(param_name + ' x = ' + x);
330                             return x; // value
331                         } else {
332                             if (value) {
333                                 if (value.constructor != Array) value = [ value ];
334                                 value = value.concat(x);
335                             } else {
336                                 value = x;
337                             }
338                         }
339                     }
340                 }
341             }
342             //alert(param_name + ' value = ' + value);
343             return value;
344         } catch(E) {
345             dump('xul_param error: ' + E + '\n');
346         }
347     }
348
349     function get_bool(a) {
350         // Normal javascript interpretation except 'f' == false, per postgres, and 'F' == false, and '0' == false (newer JSON is returning '0' instead of 0 in cases)
351         // So false includes 'f', '', '0', 0, null, and undefined
352         if (a == 'f') return false;
353         if (a == 'F') return false;
354         if (a == '0') return false;
355         if (a) return true; else return false;
356     }
357
358     function get_db_true() {
359         return 't';
360     }
361
362     function get_db_false() {
363         return 'f';
364     }
365
366     function copy_to_clipboard(ev) {
367         try {
368             netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
369             var text;
370             if (typeof ev == 'object') {
371                 if (typeof ev.target != 'undefined') {
372                     if (typeof ev.target.textContent != 'undefined') if (ev.target.textContent) text = ev.target.textContent;
373                     if (typeof ev.target.value != 'undefined') if (ev.target.value) text = ev.target.value;
374                 }
375             } else if (typeof ev == 'string') {
376                 text = ev;
377             }
378             const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"]
379                 .getService(Components.interfaces.nsIClipboardHelper);
380             gClipboardHelper.copyString(text);
381             var Strings = $('offlineStrings') || $('commonStrings');
382             alert(Strings.getFormattedString('openils.global_util.clipboard', [text]));
383         } catch(E) {
384             var Strings = $('offlineStrings') || $('commonStrings');
385             alert(Strings.getFormattedString('openils.global_util.clipboard.error', [E]));    
386         }
387     }
388
389     function clear_the_cache() {
390         try {
391             netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
392             var cacheClass         = Components.classes["@mozilla.org/network/cache-service;1"];
393             var cacheService    = cacheClass.getService(Components.interfaces.nsICacheService);
394             cacheService.evictEntries(Components.interfaces.nsICache.STORE_ON_DISK);
395             cacheService.evictEntries(Components.interfaces.nsICache.STORE_IN_MEMORY);
396         } catch(E) {
397             var Strings = $('offlineStrings') || $('commonStrings');
398             alert(Strings.getFormattedString('openils.global_util.clear_cache.error', [E]));
399         }
400     }
401
402     function toOpenWindowByType(inType, uri) {
403         var winopts = "chrome,extrachrome,menubar,resizable,scrollbars,status,toolbar";
404         window.open(uri, "_blank", winopts);
405     }
406
407     function url_prefix(url) {
408         if (url.match(/^\//)) url = urls.remote + url;
409         if (! url.match(/^(http|chrome):\/\//) && ! url.match(/^data:/) ) url = 'http://' + url;
410         dump('url_prefix = ' + url + '\n');
411         return url;
412     }
413