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