another entry point for bucket management
[Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / main / menu.js
1 dump('entering main/menu.js\n');
2
3 if (typeof main == 'undefined') main = {};
4 main.menu = function () {
5
6         JSAN.use('util.error'); this.error = new util.error();
7         JSAN.use('util.window'); this.window = new util.window();
8
9         this.w = window;
10 }
11
12 main.menu.prototype = {
13
14         'url_prefix' : function(url) {
15                 if (url.match(/^\//)) url = urls.remote + url;
16                 if (! url.match(/^(http|chrome):\/\//) && ! url.match(/^data:/) ) url = 'http://' + url;
17                 return url;
18         },
19
20         'init' : function( params ) {
21
22                 var session = params['session'];
23                 var authtime = params['authtime'];
24                 urls.remote = params['server'];
25
26                 var obj = this;
27
28                 var cmd_map = {
29                         'cmd_broken' : [
30                                 ['oncommand'],
31                                 function() { alert('Not Yet Implemented'); }
32                         ],
33
34                         /* File Menu */
35                         'cmd_close_window' : [ 
36                                 ['oncommand'], 
37                                 function() { window.close(); } 
38                         ],
39                         'cmd_new_window' : [
40                                 ['oncommand'],
41                                 function() {
42                                         obj.window.open(
43                                                 obj.url_prefix(urls.XUL_MENU_FRAME)
44                                                 + '?session='+window.escape(session) 
45                                                 + '&authtime='+window.escape(authtime) 
46                                                 + '&server='+window.escape(urls.remote),
47                                                 'main' + obj.window.window_name_increment(),
48                                                 'chrome,resizable'); 
49                                 }
50                         ],
51                         'cmd_new_tab' : [
52                                 ['oncommand'],
53                                 function() { obj.new_tab(null,{'focus':true},null); }
54                         ],
55                         'cmd_close_tab' : [
56                                 ['oncommand'],
57                                 function() { obj.close_tab(); }
58                         ],
59
60                         /* Edit Menu */
61                         'cmd_edit_copy_buckets' : [
62                                 ['oncommand'],
63                                 function() {
64                                         obj.set_tab(obj.url_prefix(urls.XUL_COPY_BUCKETS)
65                                                 + '?session='+window.escape(session),{'tab_name':'Copy Buckets'},{});
66                                 }
67                         ],
68
69                         /* Search Menu */
70                         'cmd_patron_search' : [
71                                 ['oncommand'],
72                                 function() {
73                                         obj.set_tab(obj.url_prefix(urls.XUL_PATRON_DISPLAY)
74                                                 + '?session='+window.escape(session),{},{});
75                                 }
76                         ],
77                         'cmd_search_opac' : [
78                                 ['oncommand'],
79                                 function() {
80                                         var content_params = { 'session' : session, 'authtime' : authtime };
81                                         obj.set_tab(obj.url_prefix(urls.XUL_OPAC_WRAPPER), {'tab_name':'Catalog'}, content_params);
82                                 }
83                         ],
84                         'cmd_search_tcn' : [
85                                 ['oncommand'],
86                                 function() {
87                                         var tcn = prompt('What is the TCN or accession ID for the record?','','TCN Lookup');
88
89                                         if (tcn) {
90                                                 JSAN.use('util.network');
91                                                 var network = new util.network();
92                                                 var robj = network.simple_request('FM_BRE_ID_SEARCH_VIA_TCN',[tcn]);
93                                                 if (robj.count != robj.ids.length) throw('FIXME -- FM_BRE_ID_SEARCH_VIA_TCN = ' + js2JSON(robj));
94                                                 if (robj.count == 0) {
95                                                         alert('TCN not found');
96                                                 } else {
97                                                         for (var i = 0; i < robj.count; i++) {
98                                                                 var id = robj.ids[i];
99                                                                 var opac_url = obj.url_prefix( urls.opac_rdetail ) + '?r=' + id;
100                                                                 var content_params = { 
101                                                                         'session' : session, 
102                                                                         'authtime' : authtime,
103                                                                         'opac_url' : opac_url,
104                                                                 };
105                                                                 if (i == 0) {
106                                                                         obj.set_tab(
107                                                                                 obj.url_prefix(urls.XUL_OPAC_WRAPPER), 
108                                                                                 {'tab_name':tcn}, 
109                                                                                 content_params
110                                                                         );
111                                                                 } else {
112                                                                         obj.new_tab(
113                                                                                 obj.url_prefix(urls.XUL_OPAC_WRAPPER), 
114                                                                                 {'tab_name':tcn}, 
115                                                                                 content_params
116                                                                         );
117                                                                 }
118                                                         }
119                                                 }
120                                         }
121                                 }
122                         ],
123                         'cmd_copy_status' : [
124                                 ['oncommand'],
125                                 function() {
126                                         obj.set_tab(obj.url_prefix(urls.XUL_COPY_STATUS)
127                                                 + '?session='+window.escape(session),{},{});
128                                 }
129                         ],
130
131                         /* Circulation Menu */
132                         'cmd_patron_register' : [
133                                 ['oncommand'],
134                                 function() {
135                                         var loc = obj.url_prefix( urls.XUL_REMOTE_BROWSER ) 
136                                                 + '?url=' + window.escape( urls.XUL_PATRON_EDIT + '?ses=' + window.escape( session ) );
137                                         obj.set_tab(loc, {}, { 'show_print_button' : true , 'tab_name' : 'Register Patron' });
138                                 }
139                         ],
140                         'cmd_circ_checkin' : [
141                                 ['oncommand'],
142                                 function() { 
143                                         obj.set_tab(obj.url_prefix(urls.XUL_CHECKIN) + '?session='+window.escape(session),{},{});
144                                 }
145                         ],
146                         'cmd_circ_checkout' : [
147                                 ['oncommand'],
148                                 function() { 
149                                         obj.set_tab(obj.url_prefix(urls.XUL_PATRON_BARCODE_ENTRY) + '?session='+window.escape(session),{},{});
150                                 }
151                         ],
152                         'cmd_circ_hold_capture' : [
153                                 ['oncommand'],
154                                 function() { 
155                                         obj.set_tab(obj.url_prefix(urls.XUL_HOLD_CAPTURE) + '?session='+window.escape(session),{},{});
156                                 }
157                         ],
158                         'cmd_in_house_use' : [
159                                 ['oncommand'],
160                                 function() { 
161                                         obj.set_tab(obj.url_prefix(urls.XUL_IN_HOUSE_USE) + '?session='+window.escape(session),{},{});
162                                 }
163                         ],
164
165                         /* Cataloging Menu */
166                         'cmd_z39_50_import' : [
167                                 ['oncommand'],
168                                 function() {
169                                         obj.set_tab(obj.url_prefix(urls.XUL_Z3950_IMPORT) + '?session='+window.escape(session),{},{});
170                                 }
171                         ],
172
173                         /* Admin menu */
174                         'cmd_adv_user_edit' : [
175                                 ['oncommand'],
176                                 function() {
177                                         obj.set_tab(obj.url_prefix(urls.XUL_ADV_USER_BARCODE_ENTRY) + '?session=' + window.escape(session), {}, {});
178                                 }
179                         ],
180                         'cmd_print_list_template_edit' : [
181                                 ['oncommand'],
182                                 function() {
183                                         obj.set_tab(obj.url_prefix(urls.XUL_PRINT_LIST_TEMPLATE_EDITOR) + '?session=' + window.escape(session), {}, {});
184                                 }
185                         ],
186                         'cmd_stat_cat_edit' : [
187                                 ['oncommand'],
188                                 function() {
189                                         obj.set_tab(obj.url_prefix(urls.XUL_STAT_CAT_EDIT) + '?ses='+window.escape(session),{'tab_name':'Stat Cat Editor'},{});
190                                 }
191                         ],
192                         'cmd_non_cat_type_edit' : [
193                                 ['oncommand'],
194                                 function() {
195                                         obj.set_tab(obj.url_prefix(urls.XUL_NON_CAT_LABEL_EDIT) + '?ses='+window.escape(session),{'tab_name':'Non-Cataloged Type Editor'},{});
196                                 }
197                         ],
198                         'cmd_copy_location_edit' : [
199                                 ['oncommand'],
200                                 function() {
201                                         obj.set_tab(obj.url_prefix(urls.XUL_COPY_LOCATION_EDIT) + '?ses='+window.escape(session),{'tab_name':'Copy Location Editor'},{});
202                                 }
203                         ],
204                         'cmd_test' : [
205                                 ['oncommand'],
206                                 function() {
207                                         var content_params = { 'session' : session, 'authtime' : authtime };
208                                         obj.set_tab(obj.url_prefix(urls.XUL_OPAC_WRAPPER), {}, content_params);
209                                 }
210                         ],
211                         'cmd_test_html' : [
212                                 ['oncommand'],
213                                 function() {
214                                         obj.set_tab(obj.url_prefix(urls.TEST_HTML) + '?session='+window.escape(session),{},{});
215                                 }
216                         ],
217                         'cmd_test_xul' : [
218                                 ['oncommand'],
219                                 function() {
220                                         obj.set_tab(obj.url_prefix(urls.TEST_XUL) + '?session='+window.escape(session),{},{});
221                                 }
222                         ],
223                         'cmd_console' : [
224                                 ['oncommand'],
225                                 function() {
226                                         obj.set_tab(obj.url_prefix(urls.XUL_DEBUG_CONSOLE),{'tab_name':'Console'},{});
227                                 }
228                         ],
229                         'cmd_shell' : [
230                                 ['oncommand'],
231                                 function() {
232                                         obj.set_tab(obj.url_prefix(urls.XUL_DEBUG_SHELL),{'tab_name':'JS Shell'},{});
233                                 }
234                         ],
235                         'cmd_xuleditor' : [
236                                 ['oncommand'],
237                                 function() {
238                                         obj.set_tab(obj.url_prefix(urls.XUL_DEBUG_XULEDITOR),{'tab_name':'XUL Editor'},{});
239                                 }
240                         ],
241                         'cmd_fieldmapper' : [
242                                 ['oncommand'],
243                                 function() {
244                                         obj.set_tab(obj.url_prefix(urls.XUL_DEBUG_FIELDMAPPER),{'tab_name':'Fieldmapper'},{});
245                                 }
246                         ],
247                         'cmd_survey_wizard' : [
248                                 ['oncommand'],
249                                 function() {
250                                         obj.window.open(obj.url_prefix(urls.XUL_SURVEY_WIZARD)+ '?session='+window.escape(session),'survey_wizard','chrome'); 
251                                 }
252                         ],
253
254                 };
255
256                 JSAN.use('util.controller');
257                 var cmd;
258                 obj.controller = new util.controller();
259                 obj.controller.init( { 'window_knows_me_by' : 'g.menu.controller', 'control_map' : cmd_map } );
260
261                 obj.controller.view.tabbox = window.document.getElementById('main_tabbox');
262                 obj.controller.view.tabs = obj.controller.view.tabbox.firstChild;
263                 obj.controller.view.panels = obj.controller.view.tabbox.lastChild;
264
265                 obj.new_tab(null,{'focus':true},null);
266         },
267
268         'close_tab' : function () {
269                 var idx = this.controller.view.tabs.selectedIndex;
270                 if (idx == 0) {
271                         try {
272                                 this.controller.view.tabs.advanceSelectedTab(+1);
273                         } catch(E) {
274                                 this.error.sdump('D_TAB','failed tabs.advanceSelectedTab(+1):'+js2JSON(E) + '\n');
275                                 try {
276                                         this.controller.view.tabs.advanceSelectedTab(-1);
277                                 } catch(E) {
278                                         this.error.sdump('D_TAB','failed again tabs.advanceSelectedTab(-1):'+
279                                                 js2JSON(E) + '\n');
280                                 }
281                         }
282                 } else {
283                         try {
284                                 this.controller.view.tabs.advanceSelectedTab(-1);
285                         } catch(E) {
286                                 this.error.sdump('D_TAB','failed tabs.advanceSelectedTab(-1):'+js2JSON(E) + '\n');
287                                 try {
288                                         this.controller.view.tabs.advanceSelectedTab(+1);
289                                 } catch(E) {
290                                         this.error.sdump('D_TAB','failed again tabs.advanceSelectedTab(+1):'+
291                                                 js2JSON(E) + '\n');
292                                 }
293                         }
294
295                 }
296                 
297                 this.error.sdump('D_TAB','\tnew tabbox.selectedIndex = ' + this.controller.view.tabbox.selectedIndex + '\n');
298
299                 this.controller.view.tabs.childNodes[ idx ].hidden = true;
300                 this.error.sdump('D_TAB','tabs.childNodes[ ' + idx + ' ].hidden = true;\n');
301
302                 // Make sure we keep at least one tab open.
303                 var tab_flag = true;
304                 for (var i = 0; i < this.controller.view.tabs.childNodes.length; i++) {
305                         var tab = this.controller.view.tabs.childNodes[i];
306                         if (!tab.hidden)
307                                 tab_flag = false;
308                 }
309                 if (tab_flag) this.new_tab();
310         },
311
312         'find_free_tab' : function() {
313                 var last_not_hidden = -1;
314                 for (var i = 0; i<this.controller.view.tabs.childNodes.length; i++) {
315                         var tab = this.controller.view.tabs.childNodes[i];
316                         if (!tab.hidden)
317                                 last_not_hidden = i;
318                 }
319                 if (last_not_hidden == this.controller.view.tabs.childNodes.length - 1)
320                         last_not_hidden = -1;
321                 // If the one next to last_not_hidden is hidden, we want it.
322                 // Basically, we fill in tabs after existing tabs for as 
323                 // long as possible.
324                 var idx = last_not_hidden + 1;
325                 var candidate = this.controller.view.tabs.childNodes[ idx ];
326                 if (candidate.hidden)
327                         return idx;
328                 // Alright, find the first hidden then
329                 for (var i = 0; i<this.controller.view.tabs.childNodes.length; i++) {
330                         var tab = this.controller.view.tabs.childNodes[i];
331                         if (tab.hidden)
332                                 return i;
333                 }
334                 return -1;
335         },
336
337         'new_tab' : function(url,params,content_params) {
338                 if (!params) params = {};
339                 if (!content_params) content_params = {};
340                 var tc = this.find_free_tab();
341                 if (tc == -1) { return null; } // 9 tabs max
342                 var tab = this.controller.view.tabs.childNodes[ tc ];
343                 tab.hidden = false;
344                 try {
345                         if (params.focus) this.controller.view.tabs.selectedIndex = tc;
346                         params.index = tc;
347                         this.set_tab(url,params,content_params);
348                 } catch(E) {
349                         this.error.sdump('D_ERROR',E);
350                 }
351         },
352
353         'set_tab' : function(url,params,content_params) {
354                 var obj = this;
355                 if (!url) url = '/xul/server/';
356                 if (!url.match(/:\/\//) && !url.match(/^data:/)) url = urls.remote + url;
357                 if (!params) params = {};
358                 if (!content_params) content_params = {};
359                 var idx = this.controller.view.tabs.selectedIndex;
360                 if (params && typeof params.index != 'undefined') idx = params.index;
361                 var tab = this.controller.view.tabs.childNodes[ idx ];
362                 var panel = this.controller.view.panels.childNodes[ idx ];
363                 while ( panel.lastChild ) panel.removeChild( panel.lastChild );
364
365                 content_params.new_tab = function(a,b,c) { return obj.new_tab(a,b,c); };
366                 content_params.set_tab = function(a,b,c) { return obj.set_tab(a,b,c); };
367                 content_params.set_tab_name = function(name) { tab.setAttribute('label',(idx + 1) + ' ' + name); };
368                 content_params.open_chrome_window = function(a,b,c) { return obj.window.open(a,b,c); };
369                 content_params.url_prefix = function(url) { return obj.url_prefix(url); };
370                 if (params && params.tab_name) content_params.set_tab_name( params.tab_name );
371                 
372                 var frame = this.w.document.createElement('iframe');
373                 frame.setAttribute('flex','1');
374                 frame.setAttribute('src',url);
375                 panel.appendChild(frame);
376
377                 try {
378                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
379                         frame.contentWindow.IAMXUL = true;
380                         frame.contentWindow.xulG = content_params;
381                 } catch(E) {
382                         this.error.sdump('D_ERROR', 'main.menu: ' + E);
383                 }
384                 return frame;
385         }
386
387 }
388
389 dump('exiting main/menu.js\n');