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