]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/OpenILS/global_util.js
fix OPAC visible cosmetic bug for stat cats in staff client. Bitten again by JSON...
[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 'ws_ou' :
7                 return data.list.au[0].ws_ou();
8             break;
9                         case 'authtime' :
10                                 return data.session.authtime;
11                         break;
12                         case 'key':
13                         default:
14                                 return data.session.key;
15                         break;
16                 }
17         }
18
19         function font_helper() {
20                 try {
21                         JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
22                         removeCSSClass(document.documentElement,'ALL_FONTS_LARGER');
23                         removeCSSClass(document.documentElement,'ALL_FONTS_SMALLER');
24                         removeCSSClass(document.documentElement,'ALL_FONTS_XX_SMALL');
25                         removeCSSClass(document.documentElement,'ALL_FONTS_X_SMALL');
26                         removeCSSClass(document.documentElement,'ALL_FONTS_SMALL');
27                         removeCSSClass(document.documentElement,'ALL_FONTS_MEDIUM');
28                         removeCSSClass(document.documentElement,'ALL_FONTS_LARGE');
29                         removeCSSClass(document.documentElement,'ALL_FONTS_X_LARGE');
30                         removeCSSClass(document.documentElement,'ALL_FONTS_XX_LARGE');
31                         addCSSClass(document.documentElement,data.global_font_adjust);
32                 } catch(E) {
33             var Strings = $('offlineStrings') || $('commonStrings');
34                         alert(Strings.getFormattedString('openils.global_util.font_size.error', [E]));
35                 }
36         }
37
38         function getKeys(o) {
39                 var keys = [];
40                 for (var k in o) keys.push(k);
41                 return keys;
42         }
43
44         function get_contentWindow(frame) {
45         try {
46             netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
47             if (frame && frame.contentWindow) {
48                 try {
49                     if (typeof frame.contentWindow.wrappedJSObject != 'undefined') {
50                                                                          return frame.contentWindow.wrappedJSObject;
51                                                   }
52                 } catch(E) {
53                     var Strings = $('offlineStrings') || $('commonStrings');
54                     alert(Strings.getFormattedString('openils.global_util.content_window_jsobject.error', [frame, E]));
55                 }
56                 return frame.contentWindow;
57             } else {
58                 return null;
59             }
60         } catch(E) {
61             var Strings = $('offlineStrings') || $('commonStrings');
62             alert(Strings.getFormattedString('openils.global_util.content_window.error', [frame, E]));
63         }
64         }
65
66         function update_modal_xulG(v) {
67                 try {
68                         JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
69                         var key = location.pathname + location.search + location.hash;
70                         if (typeof data.modal_xulG_stack != 'undefined' && typeof data.modal_xulG_stack[key] != 'undefined') {
71                                 data.modal_xulG_stack[key][ data.modal_xulG_stack[key].length - 1 ] = v;
72                                 data.stash('modal_xulG_stack');
73                         }
74                 } catch(E) {
75                         alert('FIXME: update_modal_xulG => ' + E);
76                 }
77         }
78
79         function xul_param(param_name,_params) {
80                 /* 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 */
81                 try {
82                         //dump('xul_param('+param_name+','+js2JSON(_params)+')\n');
83                         var value = undefined; if (!_params) _params = {};
84                         if (typeof _params.no_cgi == 'undefined') {
85                                 var cgi = new CGI();
86                                 if (cgi.param(param_name)) {
87                                         var x = cgi.param(param_name);
88                                         //dump('\tfound via location.href = ' + x + '\n');
89                                         if (typeof _params.JSON2js_if_cgi != 'undefined') {
90                                                 x = JSON2js( x );
91                                                 //dump('\tJSON2js = ' + x + '\n');
92                                         }
93                                         if (typeof _params.concat == 'undefined') {
94                                                 //alert(param_name + ' x = ' + x);
95                                                 return x; // value
96                                         } else {
97                                                 if (value) {
98                                                         if (value.constructor != Array) value = [ value ];
99                                                         value = value.concat(x);
100                                                 } else {
101                                                         value = x;
102                                                 }
103                                         }
104                                 }
105                         }
106                         if (typeof _params.no_xulG == 'undefined') {
107                                 if (typeof _params.modal_xulG != 'undefined') {
108                                         JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
109                                         var key = location.pathname + location.search + location.hash;
110                                         //dump('xul_param, considering modal key = ' + key + '\n');
111                                         if (typeof data.modal_xulG_stack != 'undefined' && typeof data.modal_xulG_stack[key] != 'undefined') {
112                                                 xulG = data.modal_xulG_stack[key][ data.modal_xulG_stack[key].length - 1 ];
113                                         }
114                                 }
115                                 if (typeof xulG == 'object' && typeof xulG[ param_name ] != 'undefined') {
116                                         var x = xulG[ param_name ];
117                                         //dump('\tfound via xulG = ' + x + '\n');
118                                         if (typeof _params.JSON2js_if_xulG != 'undefined') {
119                                                 x = JSON2js( x );
120                                                 //dump('\tJSON2js = ' + x + '\n');
121                                         }
122                                         if (typeof _params.concat == 'undefined') {
123                                                 //alert(param_name + ' x = ' + x);
124                                                 return x; // value
125                                         } else {
126                                                 if (value) {
127                                                         if (value.constructor != Array) value = [ value ];
128                                                         value = value.concat(x);
129                                                 } else {
130                                                         value = x;
131                                                 }
132                                         }
133                                 }
134                         }
135                         if (typeof _params.no_xpcom == 'undefined') {
136                                 /* 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 */
137                                 if (typeof _params.stash_name != 'undefined') { 
138                                         JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
139                                         if (typeof data[ _params.stash_name ] != 'undefined') {
140                                                 var x = data[ _params.stash_name ];
141                                                 //dump('\tfound via xpcom = ' + x + '\n');
142                                                 if (typeof _params.JSON2js_if_xpcom != 'undefined') {
143                                                         x = JSON2js( x );
144                                                         //dump('\tJSON2js = ' + x + '\n');
145                                                 }
146                                                 if (_params.clear_xpcom) { 
147                                                         data[ _params.stash_name ] = undefined; data.stash( _params.stash_name ); 
148                                                 }
149                                                 if (typeof _params.concat == 'undefined') {
150                                                         //alert(param_name + ' x = ' + x);
151                                                         return x; // value
152                                                 } else {
153                                                         if (value) {
154                                                                 if (value.constructor != Array) value = [ value ];
155                                                                 value = value.concat(x);
156                                                         } else {
157                                                                 value = x;
158                                                         }
159                                                 }
160                                         }
161                                 }
162                         }
163                         //alert(param_name + ' value = ' + value);
164                         return value;
165                 } catch(E) {
166                         dump('xul_param error: ' + E + '\n');
167                 }
168         }
169
170         function get_bool(a) {
171                 // Normal javascript interpretation except 'f' == false, per postgres, and 'F' == false, and '0' == false (newer JSON is returning '0' instead of 0 in cases)
172                 // So false includes 'f', '', '0', 0, null, and undefined
173                 if (a == 'f') return false;
174                 if (a == 'F') return false;
175                 if (a == '0') return false;
176                 if (a) return true; else return false;
177         }
178
179         function get_db_true() {
180                 return 't';
181         }
182
183         function get_db_false() {
184                 return 'f';
185         }
186
187         function copy_to_clipboard(ev) {
188                 try {
189                         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
190                         var text;
191                         if (typeof ev == 'object') {
192                                 if (typeof ev.target != 'undefined') {
193                                         if (typeof ev.target.textContent != 'undefined') if (ev.target.textContent) text = ev.target.textContent;
194                                         if (typeof ev.target.value != 'undefined') if (ev.target.value) text = ev.target.value;
195                                 }
196                         } else if (typeof ev == 'string') {
197                                 text = ev;
198                         }
199                         const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"]
200                                 .getService(Components.interfaces.nsIClipboardHelper);
201                         gClipboardHelper.copyString(text);
202             var Strings = $('offlineStrings') || $('commonStrings');
203                         alert(Strings.getFormattedString('openils.global_util.clipboard', [text]));
204                 } catch(E) {
205             var Strings = $('offlineStrings') || $('commonStrings');
206                         alert(Strings.getFormattedString('openils.global_util.clipboard.error', [E]));  
207                 }
208         }
209
210         function clear_the_cache() {
211                 try {
212                         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
213                         var cacheClass          = Components.classes["@mozilla.org/network/cache-service;1"];
214                         var cacheService        = cacheClass.getService(Components.interfaces.nsICacheService);
215                         cacheService.evictEntries(Components.interfaces.nsICache.STORE_ON_DISK);
216                         cacheService.evictEntries(Components.interfaces.nsICache.STORE_IN_MEMORY);
217                 } catch(E) {
218             var Strings = $('offlineStrings') || $('commonStrings');
219                         alert(Strings.getFormattedString('openils.global_util.clear_cache.error', [E]));
220                 }
221         }
222
223         function toOpenWindowByType(inType, uri) {
224                 var winopts = "chrome,extrachrome,menubar,resizable,scrollbars,status,toolbar";
225                 window.open(uri, "_blank", winopts);
226         }
227
228         function url_prefix(url) {
229                 if (url.match(/^\//)) url = urls.remote + url;
230                 if (! url.match(/^(http|chrome):\/\//) && ! url.match(/^data:/) ) url = 'http://' + url;
231                 dump('url_prefix = ' + url + '\n');
232                 return url;
233         }
234