1 if(!dojo._hasResource["openils.XUL"]) {
3 dojo.provide("openils.XUL");
4 dojo.declare('openils.XUL', null, {});
6 openils.XUL.Component_copy;
7 if (!window.IAMBROWSER) {
8 // looks like Firefox also exposes 'Components', so its
9 // existence is not sufficient check of XUL-ness
11 if (Components.classes)
12 openils.XUL.Component_copy = Components;
14 openils.XUL.Component_copy = null;
18 openils.XUL.isXUL = function() {
19 if(location.protocol == 'chrome:' || location.protocol == 'oils:') return true;
20 return Boolean(window.IAMXUL);
24 openils.XUL.buildId = function() {
25 return window.XUL_BUILD_ID || '';
28 openils.XUL.getStash = function() {
29 if(openils.XUL.Component_copy) {
31 var CacheClass = openils.XUL.Component_copy.classes["@open-ils.org/openils_data_cache;1"].getService();
32 return CacheClass.wrappedJSObject.data;
34 console.log("Error loading XUL stash: " + e);
35 return { 'error' : e };
39 return { 'error' : 'openils.XUL.isXUL() == false' };
42 openils.XUL.newTab = function(path, tabInfo, options) {
44 throw new Error('xulG is not defined. Cannot open tab');
45 xulG.new_tab(path, tabInfo, options);
48 openils.XUL.newTabEasy = function(
49 url, tab_name, extra_content_params, wrap_in_browser
51 var content_params = {
52 "session": openils.User.authtoken,
53 "authtime": openils.User.authtime
56 ["url_prefix", "new_tab", "set_tab", "close_tab", "new_patron_tab",
57 "set_patron_tab", "volume_item_creator", "get_new_session",
58 "holdings_maintenance_tab", "set_tab_name", "open_chrome_window",
59 "url_prefix", "network_meter", "page_meter", "set_statusbar",
61 ].forEach(function(k) { content_params[k] = xulG[k]; });
63 if (extra_content_params)
64 dojo.mixin(content_params, extra_content_params);
66 var loc = xulG.url_prefix(url);
68 if (wrap_in_browser) {
69 var urls = xulG.urls || window.urls;
70 loc = urls.XUL_BROWSER + "?url=" + window.encodeURIComponent(loc);
71 content_params = dojo.mixin(
73 "no_xulG": false, "show_print_button": true,
74 "show_nav_buttons": true,
75 "passthru_content_params": extra_content_params
80 xulG.new_tab(loc, {"tab_name": tab_name}, content_params);
84 * @return bool True if a new session was successfully created, false otherwise.
86 openils.XUL.getNewSession = function(callback) {
87 return xulG.get_new_session({callback : callback});
90 /* This class cuts down on the obscenely long incantations needed to
91 * use XPCOM components. */
92 openils.XUL.SimpleXPCOM = function() {};
94 openils.XUL.SimpleXPCOM.prototype = {
96 "iface": openils.XUL.Component_copy.interfaces.nsIFilePicker,
97 "cls": "@mozilla.org/filepicker;1"
100 "iface": openils.XUL.Component_copy.interfaces.nsIFileInputStream,
101 "cls": "@mozilla.org/network/file-input-stream;1"
104 "iface": openils.XUL.Component_copy.interfaces.nsIScriptableInputStream,
105 "cls": "@mozilla.org/scriptableinputstream;1"
108 "iface": openils.XUL.Component_copy.interfaces.nsIFileOutputStream,
109 "cls": "@mozilla.org/network/file-output-stream;1"
112 "iface": openils.XUL.Component_copy.interfaces.nsIConverterOutputStream,
113 "cls": "@mozilla.org/intl/converter-output-stream;1"
115 "create": function(key) {
116 return openils.XUL.Component_copy.classes[this[key].cls].
117 createInstance(this[key].iface);
120 } catch (e) { /* not XUL */ };
122 openils.XUL.contentFromFileOpenDialog = function(windowTitle, sizeLimit) {
123 if (!openils.XUL.Component_copy) return null;
125 var api = new openils.XUL.SimpleXPCOM();
127 var picker = api.create("FP");
129 window, windowTitle || "Upload File", api.FP.iface.modeOpen
131 if (picker.show() == api.FP.iface.returnOK && picker.file) {
132 var fis = api.create("FIS");
133 var sis = api.create("SIS");
135 fis.init(picker.file, 1 /* MODE_RDONLY */, 0, 0);
138 return sis.read(sizeLimit || -1);
144 openils.XUL.contentToFileSaveDialog = function(content, windowTitle, dispositionArgs) {
145 if (!openils.XUL.Component_copy) return null;
147 var api = new openils.XUL.SimpleXPCOM();
149 var picker = api.create("FP");
151 window, windowTitle || "Save File", api.FP.iface.modeSave
154 if (dispositionArgs) {
156 * https://developer.mozilla.org/En/NsIFilePicker
158 * { defaultString : 'MyExport.csv',
159 defaultExtension : '.csv',
161 filterExtension : '*.csv',
162 filterAll : true } */
164 picker.defaultString = dispositionArgs.defaultString;
165 picker.defaultExtension = dispositionArgs.defaultExtension;
166 if (dispositionArgs.filterName) {
168 dispositionArgs.filterName,
169 dispositionArgs.filterExtension
172 if (dispositionArgs.filterAll)
173 picker.appendFilters(picker.filterAll)
176 var result = picker.show();
178 (result == api.FP.iface.returnOK ||
179 result == api.FP.iface.returnReplace)) {
180 if (!picker.file.exists())
181 picker.file.create(0, 0644); /* XXX hardcoded = bad */
183 var fos = api.create("FOS");
184 fos.init(picker.file, 42 /* WRONLY | CREAT | TRUNCATE */, 0644, 0);
186 var cos = api.create("COS");
187 cos.init(fos, "UTF-8", 0, 0); /* It's the 21st century. You don't
189 cos.writeString(content);
196 // returns a ref to a XUL localStorage interface
197 // localStorage is not directly accessible within oils://
198 // http://fartersoft.com/blog/2011/03/07/using-localstorage-in-firefox-extensions-for-persistent-data-storage/
199 openils.XUL.localStorage = function() {
201 // in browser mode, use the standard localStorage interface
202 if (!openils.XUL.Component_copy)
203 return window.localStorage;
205 var url = location.protocol + '//' + location.hostname;
206 var ios = openils.XUL.Component_copy.classes["@mozilla.org/network/io-service;1"]
207 .getService(openils.XUL.Component_copy.interfaces.nsIIOService);
208 var ssm = openils.XUL.Component_copy.classes["@mozilla.org/scriptsecuritymanager;1"]
209 .getService(openils.XUL.Component_copy.interfaces.nsIScriptSecurityManager);
210 var dsm = openils.XUL.Component_copy.classes["@mozilla.org/dom/storagemanager;1"]
211 .getService(openils.XUL.Component_copy.interfaces.nsIDOMStorageManager);
212 var uri = ios.newURI(url, "", null);
213 var principal = ssm.getCodebasePrincipal(uri);
214 return dsm.getLocalStorageForPrincipal(principal, "");
217 }catch (e) { console.log('Failed to load openils.XUL: ' + e) }