]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/dojo/openils/XUL.js
Serials: dojo/autogrid-based scaffolding for building serials objects
[Evergreen.git] / Open-ILS / web / js / dojo / openils / XUL.js
1 if(!dojo._hasResource["openils.XUL"]) {
2
3     dojo.provide("openils.XUL");
4     dojo.require('dojo.cookie');
5     dojo.declare('openils.XUL', null, {});
6
7     openils.XUL.isXUL = function() {
8         return Boolean(dojo.cookie('xul')) || Boolean(window.IAMXUL);
9     }
10
11     openils.XUL.buildId = function() {
12         return window.XUL_BUILD_ID || '';
13     }
14     
15     openils.XUL.getStash = function() {
16         if(openils.XUL.isXUL()) {
17             try {
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;
21                 }
22             } catch(e) {
23                 console.log("Error loading XUL stash: " + e);
24             }
25         }
26
27         return {};
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(url, tab_name, extra_content_params) {
37         var content_params = {
38             "session": openils.User.authtoken,
39             "authtime": openils.User.authtime
40         };
41
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",
46             "set_help_context"
47         ].forEach(function(k) { content_params[k] = xulG[k]; });
48
49         if (extra_content_params)
50             dojo.mixin(content_params, extra_content_params);
51
52         xulG.new_tab(
53             xulG.url_prefix(url), {"tab_name": tab_name}, content_params
54         );
55     };
56
57     /**
58      * @return bool True if a new session was successfully created, false otherwise.
59      */
60     openils.XUL.getNewSession = function(callback) {
61         return xulG.get_new_session({callback : callback});
62     }
63
64     /** 
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
67      */ 
68     openils.XUL.enableXPConnect = function() {
69         try {
70             netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
71         } catch (E) {
72             if(dojo.isFF) {
73                 console.error("Unable to enable UniversalXPConnect privileges.  " +
74                     "Try setting 'signed.applets.codebase_principal_support' to true in about:config");
75             }
76             return false;
77         }
78         return true;
79     }
80
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 = {
85         "FP": {
86             "iface": Components.interfaces.nsIFilePicker,
87             "cls": "@mozilla.org/filepicker;1"
88         },
89         "FIS": {
90             "iface": Components.interfaces.nsIFileInputStream,
91             "cls": "@mozilla.org/network/file-input-stream;1"
92         },
93         "SIS": {
94             "iface": Components.interfaces.nsIScriptableInputStream,
95             "cls": "@mozilla.org/scriptableinputstream;1"
96         },
97         "FOS": {
98             "iface": Components.interfaces.nsIFileOutputStream,
99             "cls": "@mozilla.org/network/file-output-stream;1"
100         },
101         "create": function(key) {
102             return Components.classes[this[key].cls].
103                 createInstance(this[key].iface);
104         },
105         "getPrivilegeManager": function() {
106             return netscape.security.PrivilegeManager;
107         }
108     };
109
110     openils.XUL.contentFromFileOpenDialog = function(windowTitle, sizeLimit) {
111         var api = new openils.XUL.SimpleXPCOM();
112
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");
117
118         var picker = api.create("FP");
119         picker.init(
120             window, windowTitle || "Upload File", api.FP.iface.modeOpen
121         );
122         if (picker.show() == api.FP.iface.returnOK && picker.file) {
123             var fis = api.create("FIS");
124             var sis = api.create("SIS");
125
126             fis.init(picker.file, 1 /* MODE_RDONLY */, 0, 0);
127             sis.init(fis);
128
129             return sis.read(sizeLimit || -1);
130         } else {
131             return null;
132         }
133     };
134
135     openils.XUL.contentToFileSaveDialog = function(content, windowTitle) {
136         var api = new openils.XUL.SimpleXPCOM();
137         api.getPrivilegeManager().enablePrivilege("UniversalXPConnect");
138
139         var picker = api.create("FP");
140         picker.init(
141             window, windowTitle || "Save File", api.FP.iface.modeSave
142         );
143         var result = picker.show();
144         if (picker.file &&
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);
152         } else {
153             return 0;
154         }
155     };
156 }