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