]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/dojo/openils/XUL.js
Protect XUL-ish code when not running under XULRunner
[Evergreen.git] / Open-ILS / web / js / dojo / openils / XUL.js
1 if(!dojo._hasResource["openils.XUL"]) {
2
3     dojo.provide("openils.XUL");
4     dojo.declare('openils.XUL', null, {});
5
6     openils.XUL.Component_copy;
7     try {
8         openils.XUL.Component_copy = Components;
9     } catch (e) {
10         openils.XUL.Component_copy = null;
11     };
12
13     openils.XUL.isXUL = function() {
14         if(location.protocol == 'chrome:' || location.protocol == 'oils:') return true;
15         return Boolean(window.IAMXUL);
16     }
17
18  try {
19     openils.XUL.buildId = function() {
20         return window.XUL_BUILD_ID || '';
21     }
22     
23     openils.XUL.getStash = function() {
24         if(openils.XUL.Component_copy) {
25             try {
26                 var CacheClass = openils.XUL.Component_copy.classes["@open-ils.org/openils_data_cache;1"].getService();
27                 return CacheClass.wrappedJSObject.data;
28             } catch(e) {
29                 console.log("Error loading XUL stash: " + e);
30                 return { 'error' : e };
31             }
32         }
33
34         return { 'error' : 'openils.XUL.isXUL() == false' };
35     }
36
37     openils.XUL.newTab = function(path, tabInfo, options) {
38         if(xulG == undefined) 
39             throw new Error('xulG is not defined.  Cannot open tab');
40         xulG.new_tab(path, tabInfo, options);
41     }
42
43     openils.XUL.newTabEasy = function(
44         url, tab_name, extra_content_params, wrap_in_browser
45     ) {
46         var content_params = {
47             "session": openils.User.authtoken,
48             "authtime": openils.User.authtime
49         };
50
51         ["url_prefix", "new_tab", "set_tab", "close_tab", "new_patron_tab",
52             "set_patron_tab", "volume_item_creator", "get_new_session",
53             "holdings_maintenance_tab", "set_tab_name", "open_chrome_window",
54             "url_prefix", "network_meter", "page_meter", "set_statusbar",
55             "set_help_context"
56         ].forEach(function(k) { content_params[k] = xulG[k]; });
57
58         if (extra_content_params)
59             dojo.mixin(content_params, extra_content_params);
60
61         var loc = xulG.url_prefix(url);
62
63         if (wrap_in_browser) {
64             var urls = xulG.urls || window.urls;
65             loc = urls.XUL_BROWSER + "?url=" + window.encodeURIComponent(loc);
66             content_params = dojo.mixin(
67                 {
68                     "no_xulG": false, "show_print_button": true,
69                     "show_nav_buttons": true,
70                     "passthru_content_params": extra_content_params
71                 }, content_params
72             );
73         }
74
75         xulG.new_tab(loc, {"tab_name": tab_name}, content_params);
76     };
77
78     /**
79      * @return bool True if a new session was successfully created, false otherwise.
80      */
81     openils.XUL.getNewSession = function(callback) {
82         return xulG.get_new_session({callback : callback});
83     }
84
85     /* This class cuts down on the obscenely long incantations needed to
86      * use XPCOM components. */
87     openils.XUL.SimpleXPCOM = function() {};
88     try {
89         openils.XUL.SimpleXPCOM.prototype = {
90             "FP": {
91                 "iface": openils.XUL.Component_copy.interfaces.nsIFilePicker,
92                 "cls": "@mozilla.org/filepicker;1"
93             },
94             "FIS": {
95                 "iface": openils.XUL.Component_copy.interfaces.nsIFileInputStream,
96                 "cls": "@mozilla.org/network/file-input-stream;1"
97             },
98             "SIS": {
99                 "iface": openils.XUL.Component_copy.interfaces.nsIScriptableInputStream,
100                 "cls": "@mozilla.org/scriptableinputstream;1"
101             },
102             "FOS": {
103                 "iface": openils.XUL.Component_copy.interfaces.nsIFileOutputStream,
104                 "cls": "@mozilla.org/network/file-output-stream;1"
105             },
106             "COS": {
107                 "iface": openils.XUL.Component_copy.interfaces.nsIConverterOutputStream,
108                 "cls": "@mozilla.org/intl/converter-output-stream;1"
109             },
110             "create": function(key) {
111                 return openils.XUL.Component_copy.classes[this[key].cls].
112                     createInstance(this[key].iface);
113             }
114         };
115     } catch (e) { /* not XUL */ };
116
117     openils.XUL.contentFromFileOpenDialog = function(windowTitle, sizeLimit) {
118         if (!openils.XUL.Component_copy) return null;
119
120         var api = new openils.XUL.SimpleXPCOM();
121
122         var picker = api.create("FP");
123         picker.init(
124             window, windowTitle || "Upload File", api.FP.iface.modeOpen
125         );
126         if (picker.show() == api.FP.iface.returnOK && picker.file) {
127             var fis = api.create("FIS");
128             var sis = api.create("SIS");
129
130             fis.init(picker.file, 1 /* MODE_RDONLY */, 0, 0);
131             sis.init(fis);
132
133             return sis.read(sizeLimit || -1);
134         } else {
135             return null;
136         }
137     };
138
139     openils.XUL.contentToFileSaveDialog = function(content, windowTitle, dispositionArgs) {
140         if (!openils.XUL.Component_copy) return null;
141
142         var api = new openils.XUL.SimpleXPCOM();
143
144         var picker = api.create("FP");
145         picker.init(
146             window, windowTitle || "Save File", api.FP.iface.modeSave
147         );
148
149         if (dispositionArgs) {
150             /**
151              * https://developer.mozilla.org/En/NsIFilePicker
152              * Example: 
153              * { defaultString : 'MyExport.csv',
154                  defaultExtension : '.csv',
155                  filterName : 'CSV',
156                  filterExtension : '*.csv',
157                  filterAll : true } */
158
159             picker.defaultString = dispositionArgs.defaultString;
160             picker.defaultExtension = dispositionArgs.defaultExtension;
161             if (dispositionArgs.filterName) {
162                 picker.appendFilter(
163                     dispositionArgs.filterName,
164                     dispositionArgs.filterExtension
165                 );
166             }
167             if (dispositionArgs.filterAll) 
168                 picker.appendFilters(picker.filterAll)
169         }
170
171         var result = picker.show();
172         if (picker.file &&
173                 (result == api.FP.iface.returnOK ||
174                     result == api.FP.iface.returnReplace)) {
175             if (!picker.file.exists())
176                 picker.file.create(0, 0644); /* XXX hardcoded = bad */
177
178             var fos = api.create("FOS");
179             fos.init(picker.file, 42 /* WRONLY | CREAT | TRUNCATE */, 0644, 0);
180
181             var cos = api.create("COS");
182             cos.init(fos, "UTF-8", 0, 0);   /* It's the 21st century. You don't
183                                                 use ISO-8859-*. */
184             cos.writeString(content);
185             return cos.close();
186         } else {
187             return 0;
188         }
189     };
190
191     // returns a ref to a XUL localStorage interface
192     // localStorage is not directly accessible within oils://
193     // http://fartersoft.com/blog/2011/03/07/using-localstorage-in-firefox-extensions-for-persistent-data-storage/
194     openils.XUL.localStorage = function() {
195
196         // in browser mode, use the standard localStorage interface
197         if (!openils.XUL.Component_copy) 
198             return window.localStorage;
199
200         var url = location.protocol + '//' + location.hostname;
201         var ios = openils.XUL.Component_copy.classes["@mozilla.org/network/io-service;1"]
202                   .getService(openils.XUL.Component_copy.interfaces.nsIIOService);
203         var ssm = openils.XUL.Component_copy.classes["@mozilla.org/scriptsecuritymanager;1"]
204                   .getService(openils.XUL.Component_copy.interfaces.nsIScriptSecurityManager);
205         var dsm = openils.XUL.Component_copy.classes["@mozilla.org/dom/storagemanager;1"]
206                   .getService(openils.XUL.Component_copy.interfaces.nsIDOMStorageManager);
207         var uri = ios.newURI(url, "", null);
208         var principal = ssm.getCodebasePrincipal(uri);
209         return dsm.getLocalStorageForPrincipal(principal, "");
210     };
211
212  }catch (e) { console.log('Failed to load openils.XUL: ' + e) }
213 }