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