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