distinguish between XUL and Firefox when excluding XUL code from webstaff
[working/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     if (!window.IAMBROWSER) {
8         // looks like Firefox also exposes 'Components', so its
9         // existence is not sufficient check of XUL-ness
10         try {
11             if (Components.classes)
12                 openils.XUL.Component_copy = Components;
13         } catch (e) {
14             openils.XUL.Component_copy = null;
15         };
16     }
17
18     openils.XUL.isXUL = function() {
19         if(location.protocol == 'chrome:' || location.protocol == 'oils:') return true;
20         return Boolean(window.IAMXUL);
21     }
22
23  try {
24     openils.XUL.buildId = function() {
25         return window.XUL_BUILD_ID || '';
26     }
27     
28     openils.XUL.getStash = function() {
29         if(openils.XUL.Component_copy) {
30             try {
31                 var CacheClass = openils.XUL.Component_copy.classes["@open-ils.org/openils_data_cache;1"].getService();
32                 return CacheClass.wrappedJSObject.data;
33             } catch(e) {
34                 console.log("Error loading XUL stash: " + e);
35                 return { 'error' : e };
36             }
37         }
38
39         return { 'error' : 'openils.XUL.isXUL() == false' };
40     }
41
42     openils.XUL.newTab = function(path, tabInfo, options) {
43         if(xulG == undefined) 
44             throw new Error('xulG is not defined.  Cannot open tab');
45         xulG.new_tab(path, tabInfo, options);
46     }
47
48     openils.XUL.newTabEasy = function(
49         url, tab_name, extra_content_params, wrap_in_browser
50     ) {
51         var content_params = {
52             "session": openils.User.authtoken,
53             "authtime": openils.User.authtime
54         };
55
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",
60             "set_help_context"
61         ].forEach(function(k) { content_params[k] = xulG[k]; });
62
63         if (extra_content_params)
64             dojo.mixin(content_params, extra_content_params);
65
66         var loc = xulG.url_prefix(url);
67
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(
72                 {
73                     "no_xulG": false, "show_print_button": true,
74                     "show_nav_buttons": true,
75                     "passthru_content_params": extra_content_params
76                 }, content_params
77             );
78         }
79
80         xulG.new_tab(loc, {"tab_name": tab_name}, content_params);
81     };
82
83     /**
84      * @return bool True if a new session was successfully created, false otherwise.
85      */
86     openils.XUL.getNewSession = function(callback) {
87         return xulG.get_new_session({callback : callback});
88     }
89
90     /* This class cuts down on the obscenely long incantations needed to
91      * use XPCOM components. */
92     openils.XUL.SimpleXPCOM = function() {};
93     try {
94         openils.XUL.SimpleXPCOM.prototype = {
95             "FP": {
96                 "iface": openils.XUL.Component_copy.interfaces.nsIFilePicker,
97                 "cls": "@mozilla.org/filepicker;1"
98             },
99             "FIS": {
100                 "iface": openils.XUL.Component_copy.interfaces.nsIFileInputStream,
101                 "cls": "@mozilla.org/network/file-input-stream;1"
102             },
103             "SIS": {
104                 "iface": openils.XUL.Component_copy.interfaces.nsIScriptableInputStream,
105                 "cls": "@mozilla.org/scriptableinputstream;1"
106             },
107             "FOS": {
108                 "iface": openils.XUL.Component_copy.interfaces.nsIFileOutputStream,
109                 "cls": "@mozilla.org/network/file-output-stream;1"
110             },
111             "COS": {
112                 "iface": openils.XUL.Component_copy.interfaces.nsIConverterOutputStream,
113                 "cls": "@mozilla.org/intl/converter-output-stream;1"
114             },
115             "create": function(key) {
116                 return openils.XUL.Component_copy.classes[this[key].cls].
117                     createInstance(this[key].iface);
118             }
119         };
120     } catch (e) { /* not XUL */ };
121
122     openils.XUL.contentFromFileOpenDialog = function(windowTitle, sizeLimit) {
123         if (!openils.XUL.Component_copy) return null;
124
125         var api = new openils.XUL.SimpleXPCOM();
126
127         var picker = api.create("FP");
128         picker.init(
129             window, windowTitle || "Upload File", api.FP.iface.modeOpen
130         );
131         if (picker.show() == api.FP.iface.returnOK && picker.file) {
132             var fis = api.create("FIS");
133             var sis = api.create("SIS");
134
135             fis.init(picker.file, 1 /* MODE_RDONLY */, 0, 0);
136             sis.init(fis);
137
138             return sis.read(sizeLimit || -1);
139         } else {
140             return null;
141         }
142     };
143
144     openils.XUL.contentToFileSaveDialog = function(content, windowTitle, dispositionArgs) {
145         if (!openils.XUL.Component_copy) return null;
146
147         var api = new openils.XUL.SimpleXPCOM();
148
149         var picker = api.create("FP");
150         picker.init(
151             window, windowTitle || "Save File", api.FP.iface.modeSave
152         );
153
154         if (dispositionArgs) {
155             /**
156              * https://developer.mozilla.org/En/NsIFilePicker
157              * Example: 
158              * { defaultString : 'MyExport.csv',
159                  defaultExtension : '.csv',
160                  filterName : 'CSV',
161                  filterExtension : '*.csv',
162                  filterAll : true } */
163
164             picker.defaultString = dispositionArgs.defaultString;
165             picker.defaultExtension = dispositionArgs.defaultExtension;
166             if (dispositionArgs.filterName) {
167                 picker.appendFilter(
168                     dispositionArgs.filterName,
169                     dispositionArgs.filterExtension
170                 );
171             }
172             if (dispositionArgs.filterAll) 
173                 picker.appendFilters(picker.filterAll)
174         }
175
176         var result = picker.show();
177         if (picker.file &&
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 */
182
183             var fos = api.create("FOS");
184             fos.init(picker.file, 42 /* WRONLY | CREAT | TRUNCATE */, 0644, 0);
185
186             var cos = api.create("COS");
187             cos.init(fos, "UTF-8", 0, 0);   /* It's the 21st century. You don't
188                                                 use ISO-8859-*. */
189             cos.writeString(content);
190             return cos.close();
191         } else {
192             return 0;
193         }
194     };
195
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() {
200
201         // in browser mode, use the standard localStorage interface
202         if (!openils.XUL.Component_copy) 
203             return window.localStorage;
204
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, "");
215     };
216
217  }catch (e) { console.log('Failed to load openils.XUL: ' + e) }
218 }