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