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