d0561b00c8859a86c540bf099505d53ca7c1a4ac
[Evergreen.git] / Open-ILS / xul / staff_client / server / 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         'init' : function( params ) {
15
16                 var session = params['session'];
17                 var authtime = params['authtime'];
18
19                 var obj = this;
20
21                 var cmd_map = {
22                         'cmd_broken' : [
23                                 ['oncommand'],
24                                 function() { alert('Not Yet Implemented'); }
25                         ],
26
27                         /* File Menu */
28                         'cmd_close_window' : [ 
29                                 ['oncommand'], 
30                                 function() { obj.w.close(); } 
31                         ],
32                         'cmd_new_window' : [
33                                 ['oncommand'],
34                                 function() {
35                                         obj.window.open(urls.remote_menu_frame,'test' + 
36                                                 obj.window.appshell_name_increment++ ,'chrome'); 
37                                 }
38                         ],
39                         'cmd_new_tab' : [
40                                 ['oncommand'],
41                                 function() { obj.new_tab(null,{'focus':true},null); }
42                         ],
43                         'cmd_close_tab' : [
44                                 ['oncommand'],
45                                 function() { obj.close_tab(); }
46                         ],
47
48                         /* Search Menu */
49                         'cmd_patron_search' : [
50                                 ['oncommand'],
51                                 function() {
52                                         obj.set_tab(urls.remote_patron_display 
53                                                 + '?session='+obj.w.escape(session),{},{});
54                                 }
55                         ],
56                         'cmd_search_opac' : [
57                                 ['oncommand'],
58                                 function() {
59                                         var content_params = { 'authtoken' : session, 'authtime' : authtime };
60                                         obj.set_tab(urls.xul_opac_wrapper,{},content_params);
61                                 }
62                         ],
63
64
65                         /* Circulation Menu */
66                         'cmd_circ_checkin' : [
67                                 ['oncommand'],
68                                 function() { 
69                                         obj.set_tab(urls.remote_checkin + '?session='+obj.w.escape(session),{},{});
70                                 }
71                         ],
72                         'cmd_circ_checkout' : [
73                                 ['oncommand'],
74                                 function() { 
75                                         obj.set_tab(urls.remote_patron_barcode_entry + '?session='+obj.w.escape(session),{},{});
76                                 }
77                         ],
78                         'cmd_circ_hold_capture' : [
79                                 ['oncommand'],
80                                 function() { 
81                                         obj.set_tab(urls.remote_hold_capture + '?session='+obj.w.escape(session),{},{});
82                                 }
83                         ],
84
85
86                         /* Admin menu */
87                         'cmd_test_html' : [
88                                 ['oncommand'],
89                                 function() {
90                                         obj.set_tab(urls.test_html + '?session='+obj.w.escape(session),{},{});
91                                 }
92                         ],
93                         'cmd_test_xul' : [
94                                 ['oncommand'],
95                                 function() {
96                                         obj.set_tab(urls.test_xul + '?session='+obj.w.escape(session),{},{});
97                                 }
98                         ],
99                         'cmd_console' : [
100                                 ['oncommand'],
101                                 function() {
102                                         obj.set_tab(urls.remote_debug_console,{},{});
103                                 }
104                         ],
105                         'cmd_shell' : [
106                                 ['oncommand'],
107                                 function() {
108                                         obj.set_tab(urls.remote_debug_shell,{},{});
109                                 }
110                         ],
111                         'cmd_xuleditor' : [
112                                 ['oncommand'],
113                                 function() {
114                                         obj.set_tab(urls.remote_debug_xuleditor,{},{});
115                                 }
116                         ],
117                         'cmd_fieldmapper' : [
118                                 ['oncommand'],
119                                 function() {
120                                         obj.set_tab(urls.remote_debug_fieldmapper,{},{});
121                                 }
122                         ],
123
124                 };
125
126                 JSAN.use('util.controller');
127                 var cmd;
128                 obj.controller = new util.controller();
129                 obj.controller.init( { 'window_knows_me_by' : 'g.menu.controller', 'control_map' : cmd_map } );
130
131                 obj.controller.view.tabbox = obj.w.document.getElementById('main_tabbox');
132                 obj.controller.view.tabs = obj.controller.view.tabbox.firstChild;
133                 obj.controller.view.panels = obj.controller.view.tabbox.lastChild;
134
135                 obj.new_tab(null,{'focus':true},null);
136         },
137
138         'close_tab' : function () {
139                 var idx = this.controller.view.tabs.selectedIndex;
140                 if (idx == 0) {
141                         try {
142                                 this.controller.view.tabs.advanceSelectedTab(+1);
143                         } catch(E) {
144                                 this.error.sdump('D_TAB','failed tabs.advanceSelectedTab(+1):'+js2JSON(E) + '\n');
145                                 try {
146                                         this.controller.view.tabs.advanceSelectedTab(-1);
147                                 } catch(E) {
148                                         this.error.sdump('D_TAB','failed again tabs.advanceSelectedTab(-1):'+
149                                                 js2JSON(E) + '\n');
150                                 }
151                         }
152                 } else {
153                         try {
154                                 this.controller.view.tabs.advanceSelectedTab(-1);
155                         } catch(E) {
156                                 this.error.sdump('D_TAB','failed tabs.advanceSelectedTab(-1):'+js2JSON(E) + '\n');
157                                 try {
158                                         this.controller.view.tabs.advanceSelectedTab(+1);
159                                 } catch(E) {
160                                         this.error.sdump('D_TAB','failed again tabs.advanceSelectedTab(+1):'+
161                                                 js2JSON(E) + '\n');
162                                 }
163                         }
164
165                 }
166                 
167                 this.error.sdump('D_TAB','\tnew tabbox.selectedIndex = ' + this.controller.view.tabbox.selectedIndex + '\n');
168
169                 this.controller.view.tabs.childNodes[ idx ].hidden = true;
170                 this.error.sdump('D_TAB','tabs.childNodes[ ' + idx + ' ].hidden = true;\n');
171
172                 // Make sure we keep at least one tab open.
173                 var tab_flag = true;
174                 for (var i = 0; i < this.controller.view.tabs.childNodes.length; i++) {
175                         var tab = this.controller.view.tabs.childNodes[i];
176                         if (!tab.hidden)
177                                 tab_flag = false;
178                 }
179                 if (tab_flag) this.new_tab();
180         },
181
182         'find_free_tab' : function() {
183                 var last_not_hidden = -1;
184                 for (var i = 0; i<this.controller.view.tabs.childNodes.length; i++) {
185                         var tab = this.controller.view.tabs.childNodes[i];
186                         if (!tab.hidden)
187                                 last_not_hidden = i;
188                 }
189                 if (last_not_hidden == this.controller.view.tabs.childNodes.length - 1)
190                         last_not_hidden = -1;
191                 // If the one next to last_not_hidden is hidden, we want it.
192                 // Basically, we fill in tabs after existing tabs for as 
193                 // long as possible.
194                 var idx = last_not_hidden + 1;
195                 var candidate = this.controller.view.tabs.childNodes[ idx ];
196                 if (candidate.hidden)
197                         return idx;
198                 // Alright, find the first hidden then
199                 for (var i = 0; i<this.controller.view.tabs.childNodes.length; i++) {
200                         var tab = this.controller.view.tabs.childNodes[i];
201                         if (tab.hidden)
202                                 return i;
203                 }
204                 return -1;
205         },
206
207         'new_tab' : function(url,params,content_params) {
208                 if (!url) url = 'data:text/html,<h1>Hello World</h1>'
209                 if (!params) params = {};
210                 if (!content_params) content_params = {};
211                 var tc = this.find_free_tab();
212                 if (tc == -1) { return null; } // 9 tabs max
213                 var tab = this.controller.view.tabs.childNodes[ tc ];
214                 //tab.setAttribute('label','Tab ' + (tc + 1) );
215                 tab.hidden = false;
216                 try {
217                         if (params.focus) this.controller.view.tabs.selectedIndex = tc;
218                         params.index = tc;
219                         this.set_tab(url,params,content_params);
220                 } catch(E) {
221                         this.error.sdump('D_ERROR',E);
222                 }
223         },
224
225         'set_tab' : function(url,params,content_params) {
226                 if (!url) url = 'data:text/html,<h1>Hello World</h1>'
227                 if (!params) params = {};
228                 if (!content_params) content_params = {};
229                 var obj = this;
230                 var idx = this.controller.view.tabs.selectedIndex;
231                 if (params && typeof params.index != 'undefined') idx = params.index;
232                 var tab = this.controller.view.tabs.childNodes[ idx ];
233                 var panel = this.controller.view.panels.childNodes[ idx ];
234                 while ( panel.lastChild ) panel.removeChild( panel.lastChild );
235                 var frame = this.w.document.createElement('iframe');
236                 frame.setAttribute('flex','1');
237                 frame.setAttribute('src',url);
238                 panel.appendChild(frame);
239                 content_params.new_tab = function(a,b,c) { obj.new_tab(a,b,c); }
240                 try {
241                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
242                         frame.contentWindow.IAMXUL = true;
243                         frame.contentWindow.xulG = content_params;
244                 } catch(E) {
245                         this.error.sdump('D_ERROR', 'main.menu: ' + E);
246                 }
247                 return frame;
248         }
249
250 }
251
252 dump('exiting main/menu.js\n');