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