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