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