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