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