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