a little polish
[Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / auth / controller.js
1 dump('entering auth/controller.js\n');
2
3 if (typeof auth == 'undefined') auth = {};
4 auth.controller = function (params) {
5         JSAN.use('util.error'); this.error = new util.error();
6         this.w = params.window;
7
8         return this;
9 };
10
11 auth.controller.prototype = {
12
13         'init' : function () {
14
15                 var obj = this;  // so the 'this' in event handlers don't confuse us
16                 var w = obj.w;
17
18                 JSAN.use('OpenILS.data');
19                 obj.data = new OpenILS.data(); obj.data.init({'via':'stash'});
20
21                 // MVC
22                 JSAN.use('util.controller'); obj.controller = new util.controller();
23                 obj.controller.init(
24                         {
25                                 'control_map' : {
26                                         'cmd_login' : [
27                                                 ['command'],
28                                                 function() {
29                                                         obj.login();
30                                                 }
31                                         ],
32                                         'cmd_override' : [
33                                                 ['command'],
34                                                 function() {
35                                                         obj.override();
36                                                 }
37                                         ],
38                                         'cmd_logoff' : [
39                                                 ['command'],
40                                                 function() {
41                                                         obj.logoff()
42                                                 }
43                                         ],
44                                         'cmd_close_window' : [
45                                                 ['command'],
46                                                 function() {
47                                                         obj.close()
48                                                 }
49                                         ],
50
51                                         'server_prompt' : [
52                                                 ['keypress'],
53                                                 handle_keypress
54                                         ],
55                                         'name_prompt' : [
56                                                 ['keypress'],
57                                                 handle_keypress
58                                         ],
59                                         'password_prompt' : [
60                                                 ['keypress'],
61                                                 handle_keypress
62                                         ],
63                                         'submit_button' : [
64                                                 ['render'],
65                                                 function(e) { return function() {} }
66                                         ],
67                                         'progress_bar' : [
68                                                 ['render'],
69                                                 function(e) { return function() {} }
70                                         ],
71                                         'status' : [
72                                                 ['render'],
73                                                 function(e) { return function() {
74                                                 } }
75                                         ],
76                                         'ws_deck' : [
77                                                 ['render'],
78                                                 function(e) { return function() {
79                                                         try {
80                                                         JSAN.use('util.widgets'); util.widgets.remove_children(e);
81                                                         if (obj.data.ws_info && obj.data.ws_info[ obj.controller.view.server_prompt.value ]) {
82                                                                 var ws = obj.data.ws_info[ obj.controller.view.server_prompt.value ];
83                                                                 var x = document.createElement('description');
84                                                                 e.appendChild(x);
85                                                                 x.appendChild(
86                                                                         document.createTextNode(
87                                                                                 ws.name + ' @  ' + ws.lib_shortname
88                                                                         )
89                                                                 );
90                                                         }
91                                                         } catch(E) {
92                                                                 alert(E);
93                                                         }
94                                                 } }
95                                         ],
96                                         'menu_spot' : [
97                                                 ['render'],
98                                                 function(e) { return function() {
99                                                 } }
100                                         ],
101
102                                 }
103                         }
104                 );
105                 obj.controller.view.name_prompt.focus();
106
107                 function handle_keypress(ev) {
108                         try {
109                                 if (ev.keyCode && ev.keyCode == 13) {
110                                         switch(this) {
111                                                 case obj.controller.view.server_prompt:
112                                                         ev.preventDefault();
113                                                         obj.controller.view.name_prompt.focus(); obj.controller.view.name_prompt.select();
114                                                 break;
115                                                 case obj.controller.view.name_prompt:
116                                                         ev.preventDefault();
117                                                         obj.controller.view.password_prompt.focus(); obj.controller.view.password_prompt.select();
118                                                 break;
119                                                 case obj.controller.view.password_prompt:
120                                                         ev.preventDefault();
121                                                         obj.controller.view.submit_button.focus(); 
122                                                         obj.login();
123                                                 break;
124                                                 default: break;
125                                         }
126                                 }
127                         } catch(E) {
128                                 alert(E);
129                         }
130                 }
131
132                 obj.controller.view.server_prompt.addEventListener(
133                         'change',
134                         function () { obj.controller.render('ws_deck'); },
135                         false
136                 );
137
138                 // This talks to our ILS
139                 JSAN.use('auth.session');
140                 obj.session = new auth.session(obj.controller.view);
141
142                 obj.controller.render();
143
144                 if (typeof this.on_init == 'function') {
145                         this.error.sdump('D_AUTH','auth.controller.on_init()\n');
146                         this.on_init();
147                 }
148         },
149
150         'login' : function() { 
151
152                 var obj = this;
153
154                 this.error.sdump('D_AUTH','login with ' 
155                         + this.controller.view.name_prompt.value + ' and ' 
156                         + this.controller.view.password_prompt.value + ' at ' 
157                         + this.controller.view.server_prompt.value + '\n'
158                 ); 
159                 this.controller.view.server_prompt.disabled = true;
160                 this.controller.view.name_prompt.disabled = true;
161                 this.controller.view.password_prompt.disabled = true;
162                 this.controller.view.submit_button.disabled = true;
163                 XML_HTTP_SERVER = this.controller.view.server_prompt.value;
164
165                 try {
166
167                         if (typeof this.on_login == 'function') {
168                                 this.error.sdump('D_AUTH','auth.controller.session.on_init = ' +
169                                         'auth.controller.on_login\n');
170                                 this.session.on_init = this.on_login;
171                                 this.session.on_error = function() { obj.logoff(); };
172                         }
173                         
174                         this.session.init();
175
176                 } catch(E) {
177                         var error = '!! ' + E + '\n';
178                         this.error.sdump('D_ERROR',error); 
179                         alert(error);
180                         this.logoff();
181                         if (E == 'open-ils.auth.authenticate.init returned false\n') {
182                                 this.controller.view.server_prompt.focus();
183                                 this.controller.view.server_prompt.select();
184                         }
185
186                         if (typeof this.on_login_error == 'function') {
187                                 this.error.sdump('D_AUTH','auth.controller.on_login_error()\n');
188                                 this.on_login_error(E);
189                         }
190                 }
191
192         },
193
194         'logoff' : function() { 
195         
196                 this.error.sdump('D_AUTH','logoff' + this.w + '\n'); 
197                 this.controller.view.progress_bar.value = 0; 
198                 this.controller.view.progress_bar.setAttribute('real','0.0');
199                 this.controller.view.submit_button.disabled = false;
200                 this.controller.view.password_prompt.disabled = false;
201                 this.controller.view.password_prompt.value = '';
202                 this.controller.view.name_prompt.disabled = false;
203                 this.controller.view.name_prompt.focus(); 
204                 this.controller.view.name_prompt.select();
205                 this.controller.view.server_prompt.disabled = false;
206
207                 var windowManager = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService();
208                 var windowManagerInterface = windowManager.QueryInterface(Components.interfaces.nsIWindowMediator);
209                 var enumerator = windowManagerInterface.getEnumerator(null);
210
211                 var w; // close all other windows
212                 while ( w = enumerator.getNext() ) {
213                         if (w != window) w.close();
214                 }
215
216                 this.controller.render('ws_deck');
217
218                 this.session.close();
219
220                 if (typeof this.on_logoff == 'function') {
221                         this.error.sdump('D_AUTH','auth.controller.on_logoff()\n');
222                         this.on_logoff();
223                 }
224                 
225         },
226         'close' : function() { 
227         
228                 this.error.sdump('D_AUTH','close' + this.w + '\n');
229                 this.logoff();
230                 //Basically, we want to close all the windows for this application (and in case we're running this as
231                 //a firefox extension, we don't want to merely shutdown mozilla).  I'll probably create an XPCOM for
232                 //tracking the windows.
233                 //for (var w in this.G.window.appshell_list) {
234                 //      this.G.window.appshell_list[w].close();
235                 //}
236                 this.w.close(); /* Probably won't go any further */
237
238                 if (typeof this.on_close == 'function') {
239                         this.error.sdump('D_AUTH','auth.controller.on_close()\n');
240                         this.on_close();
241                 }
242                 
243         }
244 }
245
246 dump('exiting auth/controller.js\n');