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