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