1 if(!dojo._hasResource["openils.XUL"]) {
3 dojo.provide("openils.XUL");
4 dojo.require('dojo.cookie');
5 dojo.declare('openils.XUL', null, {});
7 openils.XUL.isXUL = function() {
8 return Boolean(dojo.cookie('xul')) || Boolean(window.IAMXUL);
11 openils.XUL.buildId = function() {
12 return window.XUL_BUILD_ID || '';
15 openils.XUL.getStash = function() {
16 if(openils.XUL.isXUL()) {
18 if(openils.XUL.enableXPConnect()) {
19 var CacheClass = new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
20 return new CacheClass().wrappedJSObject.OpenILS.prototype.data;
23 console.log("Error loading XUL stash: " + e);
30 openils.XUL.newTab = function(path, tabInfo, options) {
32 throw new Error('xulG is not defined. Cannot open tab');
33 xulG.new_tab(path, tabInfo, options);
36 openils.XUL.newTabEasy = function(url, tab_name, extra_content_params) {
37 var content_params = {
38 "session": openils.User.authtoken,
39 "authtime": openils.User.authtime
42 ["url_prefix", "new_tab", "set_tab", "close_tab", "new_patron_tab",
43 "set_patron_tab", "volume_item_creator", "get_new_session",
44 "holdings_maintenance_tab", "set_tab_name", "open_chrome_window",
45 "url_prefix", "network_meter", "page_meter", "set_statusbar",
47 ].forEach(function(k) { content_params[k] = xulG[k]; });
49 if (extra_content_params)
50 dojo.mixin(content_params, extra_content_params);
53 xulG.url_prefix(url), {"tab_name": tab_name}, content_params
58 * @return bool True if a new session was successfully created, false otherwise.
60 openils.XUL.getNewSession = function(callback) {
61 return xulG.get_new_session({callback : callback});
65 * This can be used by privileged Firefox in addition to XUL.
66 * To use use in Firefox directly, set signed.applets.codebase_principal_support to true in about:config
68 openils.XUL.enableXPConnect = function() {
70 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
73 console.error("Unable to enable UniversalXPConnect privileges. " +
74 "Try setting 'signed.applets.codebase_principal_support' to true in about:config");
81 /* This class cuts down on the obscenely long incantations needed to
82 * use XPCOM components. */
83 openils.XUL.SimpleXPCOM = function() {};
84 openils.XUL.SimpleXPCOM.prototype = {
86 "iface": Components.interfaces.nsIFilePicker,
87 "cls": "@mozilla.org/filepicker;1"
90 "iface": Components.interfaces.nsIFileInputStream,
91 "cls": "@mozilla.org/network/file-input-stream;1"
94 "iface": Components.interfaces.nsIScriptableInputStream,
95 "cls": "@mozilla.org/scriptableinputstream;1"
98 "iface": Components.interfaces.nsIFileOutputStream,
99 "cls": "@mozilla.org/network/file-output-stream;1"
101 "create": function(key) {
102 return Components.classes[this[key].cls].
103 createInstance(this[key].iface);
105 "getPrivilegeManager": function() {
106 return netscape.security.PrivilegeManager;
110 openils.XUL.contentFromFileOpenDialog = function(windowTitle, sizeLimit) {
111 var api = new openils.XUL.SimpleXPCOM();
113 /* The following enablePrivilege() call must happen at this exact
114 * level of scope -- not wrapped in another function -- otherwise
115 * it doesn't work. */
116 api.getPrivilegeManager().enablePrivilege("UniversalXPConnect");
118 var picker = api.create("FP");
120 window, windowTitle || "Upload File", api.FP.iface.modeOpen
122 if (picker.show() == api.FP.iface.returnOK && picker.file) {
123 var fis = api.create("FIS");
124 var sis = api.create("SIS");
126 fis.init(picker.file, 1 /* MODE_RDONLY */, 0, 0);
129 return sis.read(sizeLimit || -1);
135 openils.XUL.contentToFileSaveDialog = function(content, windowTitle) {
136 var api = new openils.XUL.SimpleXPCOM();
137 api.getPrivilegeManager().enablePrivilege("UniversalXPConnect");
139 var picker = api.create("FP");
141 window, windowTitle || "Save File", api.FP.iface.modeSave
143 var result = picker.show();
145 (result == api.FP.iface.returnOK ||
146 result == api.FP.iface.returnReplace)) {
147 if (!picker.file.exists())
148 picker.file.create(0, 0644); /* XXX hardcoded = bad */
149 var fos = api.create("FOS");
150 fos.init(picker.file, 42 /* WRONLY | CREAT | TRUNCATE */, 0644, 0);
151 return fos.write(content, content.length);