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