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