]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/components/clh.js
patch from tsbere to improve tab behavior and to give command-line options for contro...
[Evergreen.git] / Open-ILS / xul / staff_client / components / clh.js
1 const nsISupports           = Components.interfaces.nsISupports;\r
2 const nsICategoryManager    = Components.interfaces.nsICategoryManager;\r
3 const nsIComponentRegistrar = Components.interfaces.nsIComponentRegistrar;\r
4 const nsICommandLine        = Components.interfaces.nsICommandLine;\r
5 const nsICommandLineHandler = Components.interfaces.nsICommandLineHandler;\r
6 const nsIFactory            = Components.interfaces.nsIFactory;\r
7 const nsIModule             = Components.interfaces.nsIModule;\r
8 const nsIWindowWatcher      = Components.interfaces.nsIWindowWatcher;\r
9 \r
10 \r
11 const XUL_STANDALONE = "chrome://open_ils_staff_client/content/circ/offline.xul";\r
12 const XUL_MAIN = "chrome://open_ils_staff_client/content/main/main.xul";\r
13 const WINDOW_STANDALONE = "eg_offline"\r
14 const WINDOW_MAIN = "eg_main"\r
15 \r
16 const clh_contractID = "@mozilla.org/commandlinehandler/general-startup;1?type=egcli";\r
17 const clh_CID = Components.ID("{7e608198-7355-483a-a85a-20322e4ef91a}");\r
18 // category names are sorted alphabetically. Typical command-line handlers use a\r
19 // category that begins with the letter "m".\r
20 const clh_category = "m-egcli";\r
21 \r
22 /**\r
23  * Utility functions\r
24  */\r
25 \r
26 /**\r
27  * Opens a chrome window.\r
28  * @param aChromeURISpec a string specifying the URI of the window to open.\r
29  * @param aArgument an argument to pass to the window (may be null)\r
30  */\r
31 function findOrOpenWindow(aWindowType, aChromeURISpec, aName, aArgument)\r
32 {\r
33   var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].\r
34     getService(Components.interfaces.nsIWindowMediator);\r
35   var targetWindow = wm.getMostRecentWindow(aWindowType);\r
36   if (targetWindow != null) {\r
37       if(typeof targetWindow.new_tabs == 'function' && aArgument != null)\r
38       {\r
39           targetWindow.new_tabs(aArgument);\r
40       }\r
41       else {\r
42           targetwindow.focus;\r
43       }\r
44   }\r
45   else {\r
46     var params = null;\r
47     if (aArgument != null && aArgument.length != 0)\r
48     {\r
49         params = { "openTabs" : aArgument };\r
50         params.wrappedJSObject = params;\r
51     }\r
52     var ww = Components.classes["@mozilla.org/embedcomp/window-watcher;1"].\r
53         getService(Components.interfaces.nsIWindowWatcher);\r
54     ww.openWindow(null, aChromeURISpec, aName,\r
55         "chrome,resizable,dialog=no", params);\r
56   }\r
57 }\r
58  \r
59 /**\r
60  * The XPCOM component that implements nsICommandLineHandler.\r
61  * It also implements nsIFactory to serve as its own singleton factory.\r
62  */\r
63 const myAppHandler = {\r
64   /* nsISupports */\r
65   QueryInterface : function clh_QI(iid)\r
66   {\r
67     if (iid.equals(nsICommandLineHandler) ||\r
68         iid.equals(nsIFactory) ||\r
69         iid.equals(nsISupports))\r
70       return this;\r
71 \r
72     throw Components.results.NS_ERROR_NO_INTERFACE;\r
73   },\r
74 \r
75   /* nsICommandLineHandler */\r
76 \r
77   handle : function clh_handle(cmdLine)\r
78   {\r
79     // Each of these options is used for opening a new tab, either on login or remote send.\r
80     // XULRunner does some sanitize to turn /ilsblah into -ilsblah, for example.\r
81     // In addition to the ones here, -ilslogin, -ilsoffline, and -ilsstandalone\r
82     // are defined below.\r
83 \r
84     // With the exception of 'new', 'tab', and 'init', the value is checked for in urls in main.js.\r
85 \r
86     // NOTE: The option itself should be all lowercase (we .toLowerCase below)\r
87     var options = {\r
88     '-ilscheckin' : 'XUL_CHECKIN',\r
89     '-ilscheckout' : 'XUL_PATRON_BARCODE_ENTRY',\r
90     '-ilsnew' : 'new', // 'new' is a special keyword for opening a new window\r
91     '-ilstab' : 'tab', // 'tab' is a special keyword for opening a new tab with the default content\r
92     '-ilsnew_default' : 'init', // 'init' is a special keyword for opening a new window with an initial default tab\r
93     };\r
94 \r
95     var inParams = new Array();\r
96         var position = 0;\r
97         while (position < cmdLine.length) {\r
98                 var arg = cmdLine.getArgument(position).toLowerCase();\r
99                 if (options[arg] != undefined) {\r
100                         inParams.push(options[arg]);\r
101                         cmdLine.removeArguments(position,position);\r
102                         continue;\r
103                 }\r
104         if (arg == '-ilsurl' && cmdLine.length > position) {\r
105                   inParams.push(cmdLine.getArgument(position + 1));\r
106                   cmdLine.removeArguments(position, position + 1);\r
107                   continue;\r
108                 }\r
109                 position=position + 1;\r
110         }\r
111 \r
112         if (cmdLine.handleFlag("ILSlogin", false) || inParams.length > 0) {\r
113           findOrOpenWindow(WINDOW_MAIN, XUL_MAIN, '_blank', inParams);\r
114           cmdLine.preventDefault = true;\r
115         }\r
116 \r
117     if (cmdLine.handleFlag("ILSoffline", false) || cmdLine.handleFlag("ILSstandalone", false)) {\r
118           findOrOpenWindow(WINDOW_STANDALONE, XUL_STANDALONE, 'Offline', null);\r
119       cmdLine.preventDefault = true;\r
120         }\r
121   },\r
122 \r
123   // CHANGEME: change the help info as appropriate, but\r
124   // follow the guidelines in nsICommandLineHandler.idl\r
125   // specifically, flag descriptions should start at\r
126   // character 24, and lines should be wrapped at\r
127   // 72 characters with embedded newlines,\r
128   // and finally, the string should end with a newline\r
129   helpInfo : "  -ILScheckin          Open an Evergreen checkin tab\n" +\r
130              "  -ILScheckout         Open an Evergreen checkout tab\n" +\r
131              "  -ILSnew              Open a new Evergreen 'menu' window\n" +\r
132              "  -ILSnew_default      Open a new Evergreen 'menu' window,\n" +\r
133              "                       with a 'default' tab\n" +\r
134              "  -ILStab              Open a 'default' tab alone\n" +\r
135              "  -ILSurl <url>        Open the specified url in an Evergreen tab\n" +\r
136              "  The above six imply -ILSlogin\n" +\r
137              "  -ILSlogin            Open the Evergreen Login window\n" +\r
138              "  -ILSstandalone       Open the Evergreen Standalone interface\n" +\r
139              "  -ILSoffline          Alias for -ILSstandalone\n",\r
140 \r
141   /* nsIFactory */\r
142 \r
143   createInstance : function clh_CI(outer, iid)\r
144   {\r
145     if (outer != null)\r
146       throw Components.results.NS_ERROR_NO_AGGREGATION;\r
147 \r
148     return this.QueryInterface(iid);\r
149   },\r
150 \r
151   lockFactory : function clh_lock(lock)\r
152   {\r
153     /* no-op */\r
154   }\r
155 };\r
156 \r
157 /**\r
158  * The XPCOM glue that implements nsIModule\r
159  */\r
160 const myAppHandlerModule = {\r
161   /* nsISupports */\r
162   QueryInterface : function mod_QI(iid)\r
163   {\r
164     if (iid.equals(nsIModule) ||\r
165         iid.equals(nsISupports))\r
166       return this;\r
167 \r
168     throw Components.results.NS_ERROR_NO_INTERFACE;\r
169   },\r
170 \r
171   /* nsIModule */\r
172   getClassObject : function mod_gch(compMgr, cid, iid)\r
173   {\r
174     if (cid.equals(clh_CID))\r
175       return myAppHandler.QueryInterface(iid);\r
176 \r
177     throw Components.results.NS_ERROR_NOT_REGISTERED;\r
178   },\r
179 \r
180   registerSelf : function mod_regself(compMgr, fileSpec, location, type)\r
181   {\r
182     compMgr.QueryInterface(nsIComponentRegistrar);\r
183 \r
184     compMgr.registerFactoryLocation(clh_CID,\r
185                                     "myAppHandler",\r
186                                     clh_contractID,\r
187                                     fileSpec,\r
188                                     location,\r
189                                     type);\r
190 \r
191     var catMan = Components.classes["@mozilla.org/categorymanager;1"].\r
192       getService(nsICategoryManager);\r
193     catMan.addCategoryEntry("command-line-handler",\r
194                             clh_category,\r
195                             clh_contractID, true, true);\r
196   },\r
197 \r
198   unregisterSelf : function mod_unreg(compMgr, location, type)\r
199   {\r
200     compMgr.QueryInterface(nsIComponentRegistrar);\r
201     compMgr.unregisterFactoryLocation(clh_CID, location);\r
202 \r
203     var catMan = Components.classes["@mozilla.org/categorymanager;1"].\r
204       getService(nsICategoryManager);\r
205     catMan.deleteCategoryEntry("command-line-handler", clh_category);\r
206   },\r
207 \r
208   canUnload : function (compMgr)\r
209   {\r
210     return true;\r
211   }\r
212 };\r
213 \r
214 /* The NSGetModule function is the magic entry point that XPCOM uses to find what XPCOM objects\r
215  * this component provides\r
216  */\r
217 function NSGetModule(comMgr, fileSpec)\r
218 {\r
219   return myAppHandlerModule;\r
220 }\r
221 \r