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