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