dev tools
[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_filter_console' : [
97                                 ['command','keypress'],
98                                 function() {
99                                         obj.set_tab(urls.remote_debug_filter_console,{},{});
100                                 }
101                         ],
102                         'cmd_fieldmapper' : [
103                                 ['command','keypress'],
104                                 function() {
105                                         obj.set_tab(urls.remote_debug_fieldmapper,{},{});
106                                 }
107                         ],
108
109                 };
110
111                 for (var i in cmd_map) {
112                         var cmd = this.w.document.getElementById(i);
113                         if (cmd) {
114                                 for (var j in cmd_map[i][0]) {
115                                         cmd.addEventListener(cmd_map[i][0][j],cmd_map[i][1],false);
116                                 }
117                         }
118                         obj.view[i] = cmd;
119                 }
120
121                 obj.new_tab(true);
122         },
123
124         'close_tab' : function () {
125                 var idx = this.view.tabs.selectedIndex;
126                 if (idx == 0) {
127                         try {
128                                 this.view.tabs.advanceSelectedTab(+1);
129                         } catch(E) {
130                                 this.error.sdump('D_TAB','failed tabs.advanceSelectedTab(+1):'+js2JSON(E) + '\n');
131                                 try {
132                                         this.view.tabs.advanceSelectedTab(-1);
133                                 } catch(E) {
134                                         this.error.sdump('D_TAB','failed again tabs.advanceSelectedTab(-1):'+
135                                                 js2JSON(E) + '\n');
136                                 }
137                         }
138                 } else {
139                         try {
140                                 this.view.tabs.advanceSelectedTab(-1);
141                         } catch(E) {
142                                 this.error.sdump('D_TAB','failed tabs.advanceSelectedTab(-1):'+js2JSON(E) + '\n');
143                                 try {
144                                         this.view.tabs.advanceSelectedTab(+1);
145                                 } catch(E) {
146                                         this.error.sdump('D_TAB','failed again tabs.advanceSelectedTab(+1):'+
147                                                 js2JSON(E) + '\n');
148                                 }
149                         }
150
151                 }
152                 
153                 this.error.sdump('D_TAB','\tnew tabbox.selectedIndex = ' + this.view.tabbox.selectedIndex + '\n');
154
155                 this.view.tabs.childNodes[ idx ].hidden = true;
156                 this.error.sdump('D_TAB','tabs.childNodes[ ' + idx + ' ].hidden = true;\n');
157
158                 // Make sure we keep at least one tab open.
159                 var tab_flag = true;
160                 for (var i = 0; i < this.view.tabs.childNodes.length; i++) {
161                         var tab = this.view.tabs.childNodes[i];
162                         if (!tab.hidden)
163                                 tab_flag = false;
164                 }
165                 if (tab_flag) this.new_tab();
166         },
167
168         'find_free_tab' : function() {
169                 var last_not_hidden = -1;
170                 for (var i = 0; i<this.view.tabs.childNodes.length; i++) {
171                         var tab = this.view.tabs.childNodes[i];
172                         if (!tab.hidden)
173                                 last_not_hidden = i;
174                 }
175                 if (last_not_hidden == this.view.tabs.childNodes.length - 1)
176                         last_not_hidden = -1;
177                 // If the one next to last_not_hidden is hidden, we want it.
178                 // Basically, we fill in tabs after existing tabs for as 
179                 // long as possible.
180                 var idx = last_not_hidden + 1;
181                 var candidate = this.view.tabs.childNodes[ idx ];
182                 if (candidate.hidden)
183                         return idx;
184                 // Alright, find the first hidden then
185                 for (var i = 0; i<this.view.tabs.childNodes.length; i++) {
186                         var tab = this.view.tabs.childNodes[i];
187                         if (tab.hidden)
188                                 return i;
189                 }
190                 return -1;
191         },
192
193         'new_tab' : function(focus) {
194                 var tc = this.find_free_tab();
195                 if (tc == -1) { return null; } // 9 tabs max
196                 var tab = this.view.tabs.childNodes[ tc ];
197                 //tab.setAttribute('label','Tab ' + (tc + 1) );
198                 tab.hidden = false;
199                 try {
200                         if (focus) this.view.tabs.selectedIndex = tc;
201                         this.set_tab('data:text/html,<h1>Hello World</h1>',{ 'index' : tc });
202                 } catch(E) {
203                         this.error.sdump('D_ERROR',E);
204                 }
205         },
206
207         'set_tab' : function(url,params,content_params) {
208                 var idx = this.view.tabs.selectedIndex;
209                 if (params && typeof params.index != 'undefined') idx = params.index;
210                 var tab = this.view.tabs.childNodes[ idx ];
211                 var panel = this.view.panels.childNodes[ idx ];
212                 while ( panel.lastChild ) panel.removeChild( panel.lastChild );
213                 var frame = this.w.document.createElement('iframe');
214                 frame.setAttribute('flex','1');
215                 frame.setAttribute('src',url);
216                 panel.appendChild(frame);
217                 if (content_params) {
218                         try {
219                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
220                                 //this.error.sdump('D_MENU', 'frame.contentWindow = ' + frame.contentWindow + '\n');
221                                 frame.contentWindow.IAMXUL = true;
222                                 frame.contentWindow.xulG = content_params;
223                                 //this.error.sdump('D_MENU','content_params ' + js2JSON(content_params) +
224                                 //'\nframe.contentWindow.xulG = ' + js2JSON(frame.contentWindow.xulG) );
225                         } catch(E) {
226                                 this.error.sdump('D_ERROR', 'main.menu: ' + E);
227                         }
228                 }
229                 return frame;
230         }
231
232 }
233
234 dump('exiting main/menu.js\n');