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