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