]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/auth/controller.js
shotgun fix
[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                                                                 var x = document.createElement('description');
82                                                                 e.appendChild(x);
83                                                                 if (obj.data.ws_info 
84                                                                         && obj.data.ws_info[ obj.controller.view.server_prompt.value ]) {
85                                                                         var ws = obj.data.ws_info[ obj.controller.view.server_prompt.value ];
86                                                                         x.appendChild(
87                                                                                 document.createTextNode(
88                                                                                         ws.name + ' @  ' + ws.lib_shortname
89                                                                                 )
90                                                                         );
91                                                                 } else {
92                                                                         x.appendChild(
93                                                                                 document.createTextNode(
94                                                                                         'Not yet configured for the specified server.'
95                                                                                 )
96                                                                         );
97                                                                 }
98                                                         } catch(E) {
99                                                                 alert(E);
100                                                         }
101                                                 } }
102                                         ],
103                                         'menu_spot' : [
104                                                 ['render'],
105                                                 function(e) { return function() {
106                                                 } }
107                                         ],
108
109                                 }
110                         }
111                 );
112                 obj.controller.view.name_prompt.focus();
113
114                 function handle_keypress(ev) {
115                         try {
116                                 if (ev.keyCode && ev.keyCode == 13) {
117                                         switch(this) {
118                                                 case obj.controller.view.server_prompt:
119                                                         ev.preventDefault();
120                                                         obj.controller.view.name_prompt.focus(); obj.controller.view.name_prompt.select();
121                                                 break;
122                                                 case obj.controller.view.name_prompt:
123                                                         ev.preventDefault();
124                                                         obj.controller.view.password_prompt.focus(); obj.controller.view.password_prompt.select();
125                                                 break;
126                                                 case obj.controller.view.password_prompt:
127                                                         ev.preventDefault();
128                                                         obj.controller.view.submit_button.focus(); 
129                                                         obj.login();
130                                                 break;
131                                                 default: break;
132                                         }
133                                 }
134                         } catch(E) {
135                                 alert(E);
136                         }
137                 }
138
139                 obj.controller.view.server_prompt.addEventListener(
140                         'change',
141                         function (ev) { 
142                                 obj.controller.render('ws_deck'); 
143                                 obj.test_server(ev.target.value);
144                         },
145                         false
146                 );
147
148                 // This talks to our ILS
149                 JSAN.use('auth.session');
150                 obj.session = new auth.session(obj.controller.view);
151
152                 obj.controller.render();
153                 obj.test_server( obj.controller.view.server_prompt.value );
154
155                 if (typeof this.on_init == 'function') {
156                         this.error.sdump('D_AUTH','auth.controller.on_init()\n');
157                         this.on_init();
158                 }
159         },
160
161         'test_server' : function(url) {
162                 var obj = this;
163                 obj.controller.view.submit_button.disabled = true;
164                 obj.controller.view.server_prompt.disabled = true;
165                 var s = document.getElementById('status');
166                 s.setAttribute('value','Testing hostname...');
167                 s.setAttribute('style','color: orange;');
168                 document.getElementById('version').value = '';
169                 if (!url) {
170                         s.setAttribute('value','Please enter a server hostname.');
171                         s.setAttribute('style','color: red;');
172                         obj.controller.view.server_prompt.disabled = false;
173                         return;
174                 }
175                 try {
176                         if ( ! url.match(/^http:\/\//) ) url = 'http://' + url;
177                         var x = new XMLHttpRequest();
178                         dump('server url = ' + url + '\n');
179                         x.open("GET",url,true);
180                         x.onreadystatechange = function() {
181                                 try {
182                                         if (x.readyState != 4) return;
183                                         s.setAttribute('value',x.status + ' : ' + x.statusText);
184                                         if (x.status == 200) {
185                                                 s.setAttribute('style','color: green;');
186                                         } else {
187                                                 s.setAttribute('style','color: red;');
188                                         }
189                                         obj.test_version(url);
190                                 } catch(E) {
191                                         obj.controller.view.server_prompt.disabled = false;
192                                         s.setAttribute('value','There was an error testing this hostname.');
193                                         s.setAttribute('style','color: red;');
194                                         obj.error.sdump('D_ERROR',E);
195                                 }
196                         }
197                         x.send(null);
198                 } catch(E) {
199                         s.setAttribute('value','There was an error testing this hostname.');
200                         s.setAttribute('style','color: brown;');
201                         obj.error.sdump('D_ERROR',E);
202                         obj.controller.view.server_prompt.disabled = false;
203                 }
204         },
205
206         'test_version' : function(url) {
207                 var obj = this;
208                 var s = document.getElementById('version');
209                 s.setAttribute('value','Testing version...');
210                 s.setAttribute('style','color: orange;');
211                 try {
212                         var x = new XMLHttpRequest();
213                         var url2 = url + '/xul/server/';
214                         dump('version url = ' + url2 + '\n');
215                         x.open("GET",url2,true);
216                         x.onreadystatechange = function() {
217                                 try {
218                                         if (x.readyState != 4) return;
219                                         s.setAttribute('value',x.status + ' : ' + x.statusText);
220                                         if (x.status == 200) {
221                                                 s.setAttribute('style','color: green;');
222                                                 obj.controller.view.submit_button.disabled = false;
223                                         } else {
224                                                 s.setAttribute('style','color: red;');
225                                                 obj.test_upgrade_instructions(url);
226                                         }
227                                         obj.controller.view.server_prompt.disabled = false;
228                                 } catch(E) {
229                                         s.setAttribute('value','There was an error checking version support.');
230                                         s.setAttribute('style','color: red;');
231                                         obj.error.sdump('D_ERROR',E);
232                                         obj.controller.view.server_prompt.disabled = false;
233                                 }
234                         }
235                         x.send(null);
236                 } catch(E) {
237                         s.setAttribute('value','There was an error checking version support.');
238                         s.setAttribute('style','color: brown;');
239                         obj.error.sdump('D_ERROR',E);
240                         obj.controller.view.server_prompt.disabled = false;
241                 }
242         },
243
244         'test_upgrade_instructions' : function(url) {
245                 var obj = this;
246                 try {
247                         var x = new XMLHttpRequest();
248                         var url2 = url + '/xul/versions.html';
249                         dump('upgrade url = ' + url2 + '\n');
250                         x.open("GET",url2,true);
251                         x.onreadystatechange = function() {
252                                 try {
253                                         if (x.readyState != 4) return;
254                                         if (x.status == 200) {
255                                                 window.open('data:text/html,'+window.escape(x.responseText),'upgrade','chrome,resizable,modal,centered');
256                                         } else {
257                                                 alert('This server does not support your version of the staff client.  Please check with your system administrator.');
258                                         }
259                                         obj.controller.view.server_prompt.disabled = false;
260                                 } catch(E) {
261                                         obj.error.sdump('D_ERROR',E);
262                                         obj.controller.view.server_prompt.disabled = false;
263                                 }
264                         }
265                         x.send(null);
266                 } catch(E) {
267                         obj.error.sdump('D_ERROR',E);
268                         obj.controller.view.server_prompt.disabled = false;
269                 }
270         },
271
272         'login' : function() { 
273
274                 var obj = this;
275
276                 this.error.sdump('D_AUTH','login with ' 
277                         + this.controller.view.name_prompt.value + ' and ' 
278                         + this.controller.view.password_prompt.value + ' at ' 
279                         + this.controller.view.server_prompt.value + '\n'
280                 ); 
281                 this.controller.view.server_prompt.disabled = true;
282                 this.controller.view.name_prompt.disabled = true;
283                 this.controller.view.password_prompt.disabled = true;
284                 this.controller.view.submit_button.disabled = true;
285                 XML_HTTP_SERVER = this.controller.view.server_prompt.value;
286
287                 try {
288
289                         if (typeof this.on_login == 'function') {
290                                 this.error.sdump('D_AUTH','auth.controller.session.on_init = ' +
291                                         'auth.controller.on_login\n');
292                                 this.session.on_init = this.on_login;
293                                 this.session.on_error = function() { obj.logoff(); };
294                         }
295                         
296                         this.session.init();
297
298                 } catch(E) {
299                         var error = '!! ' + E + '\n';
300                         this.error.sdump('D_ERROR',error); 
301                         alert(error);
302                         this.logoff();
303                         if (E == 'open-ils.auth.authenticate.init returned false\n') {
304                                 this.controller.view.server_prompt.focus();
305                                 this.controller.view.server_prompt.select();
306                         }
307
308                         if (typeof this.on_login_error == 'function') {
309                                 this.error.sdump('D_AUTH','auth.controller.on_login_error()\n');
310                                 this.on_login_error(E);
311                         }
312                 }
313
314         },
315
316         'logoff' : function() { 
317         
318                 this.error.sdump('D_AUTH','logoff' + this.w + '\n'); 
319                 this.controller.view.progress_bar.value = 0; 
320                 this.controller.view.progress_bar.setAttribute('real','0.0');
321                 this.controller.view.submit_button.disabled = false;
322                 this.controller.view.password_prompt.disabled = false;
323                 this.controller.view.password_prompt.value = '';
324                 this.controller.view.name_prompt.disabled = false;
325                 this.controller.view.name_prompt.focus(); 
326                 this.controller.view.name_prompt.select();
327                 this.controller.view.server_prompt.disabled = false;
328
329                 var windowManager = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService();
330                 var windowManagerInterface = windowManager.QueryInterface(Components.interfaces.nsIWindowMediator);
331                 var enumerator = windowManagerInterface.getEnumerator(null);
332
333                 var w; // close all other windows
334                 while ( w = enumerator.getNext() ) {
335                         if (w != window) w.close();
336                 }
337
338                 this.controller.render('ws_deck');
339
340                 this.session.close();
341
342                 /* FIXME - need some locking or object destruction for the async tests */
343                 /* this.test_server( this.controller.view.server_prompt.value ); */
344
345                 if (typeof this.on_logoff == 'function') {
346                         this.error.sdump('D_AUTH','auth.controller.on_logoff()\n');
347                         this.on_logoff();
348                 }
349                 
350         },
351         'close' : function() { 
352         
353                 this.error.sdump('D_AUTH','close' + this.w + '\n');
354                 this.logoff();
355                 //Basically, we want to close all the windows for this application (and in case we're running this as
356                 //a firefox extension, we don't want to merely shutdown mozilla).  I'll probably create an XPCOM for
357                 //tracking the windows.
358                 //for (var w in this.G.window.appshell_list) {
359                 //      this.G.window.appshell_list[w].close();
360                 //}
361                 this.w.close(); /* Probably won't go any further */
362
363                 if (typeof this.on_close == 'function') {
364                         this.error.sdump('D_AUTH','auth.controller.on_close()\n');
365                         this.on_close();
366                 }
367                 
368         }
369 }
370
371 dump('exiting auth/controller.js\n');