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