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