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