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