]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/XUL.js
Acq: Several improvements to the LI search interface
[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.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     /** 
37      * This can be used by privileged Firefox in addition to XUL.
38      * To use use in Firefox directly, set signed.applets.codebase_principal_support to true in about:config
39      */ 
40     openils.XUL.enableXPConnect = function() {
41         try {
42             netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
43         } catch (E) {
44             if(dojo.isFF) {
45                 console.error("Unable to enable UniversalXPConnect privileges.  " +
46                     "Try setting 'signed.applets.codebase_principal_support' to true in about:config");
47             }
48             return false;
49         }
50         return true;
51     }
52
53     /* This class cuts down on the obscenely long incantations needed to
54      * use XPCOM components. */
55     openils.XUL.SimpleXPCOM = function() {};
56     openils.XUL.SimpleXPCOM.prototype = {
57         "FP": {
58             "iface": Components.interfaces.nsIFilePicker,
59             "cls": "@mozilla.org/filepicker;1"
60         },
61         "FIS": {
62             "iface": Components.interfaces.nsIFileInputStream,
63             "cls": "@mozilla.org/network/file-input-stream;1"
64         },
65         "SIS": {
66             "iface": Components.interfaces.nsIScriptableInputStream,
67             "cls": "@mozilla.org/scriptableinputstream;1"
68         },
69         "FOS": {
70             "iface": Components.interfaces.nsIFileOutputStream,
71             "cls": "@mozilla.org/network/file-output-stream;1"
72         },
73         "create": function(key) {
74             return Components.classes[this[key].cls].
75                 createInstance(this[key].iface);
76         },
77         "getPrivilegeManager": function() {
78             return netscape.security.PrivilegeManager;
79         }
80     };
81
82     openils.XUL.contentFromFileOpenDialog = function(windowTitle, sizeLimit) {
83         var api = new openils.XUL.SimpleXPCOM();
84
85         /* The following enablePrivilege() call must happen at this exact
86          * level of scope -- not wrapped in another function -- otherwise
87          * it doesn't work. */
88         api.getPrivilegeManager().enablePrivilege("UniversalXPConnect");
89
90         var picker = api.create("FP");
91         picker.init(
92             window, windowTitle || "Upload File", api.FP.iface.modeOpen
93         );
94         if (picker.show() == api.FP.iface.returnOK && picker.file) {
95             var fis = api.create("FIS");
96             var sis = api.create("SIS");
97
98             fis.init(picker.file, 1 /* MODE_RDONLY */, 0, 0);
99             sis.init(fis);
100
101             return sis.read(sizeLimit || -1);
102         } else {
103             return null;
104         }
105     };
106
107     openils.XUL.contentToFileSaveDialog = function(content, windowTitle) {
108         var api = new openils.XUL.SimpleXPCOM();
109         api.getPrivilegeManager().enablePrivilege("UniversalXPConnect");
110
111         var picker = api.create("FP");
112         picker.init(
113             window, windowTitle || "Save File", api.FP.iface.modeSave
114         );
115         var result = picker.show();
116         if (picker.file &&
117                 (result == api.FP.iface.returnOK ||
118                     result == api.FP.iface.returnReplace)) {
119             if (!picker.file.exists())
120                 picker.file.create(0, 0644); /* XXX hardcoded = bad */
121             var fos = api.create("FOS");
122             fos.init(picker.file, 42 /* WRONLY | CREAT | TRUNCATE */, 0644, 0);
123             return fos.write(content, content.length);
124         } else {
125             return 0;
126         }
127     };
128 }