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