]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/auth/controller.js
cosmetic
[working/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_standalone' : [
33                                                 ['command'],
34                                                 function() {
35                                                         obj.standalone();
36                                                 }
37                                         ],
38                                         'cmd_standalone_import' : [
39                                                 ['command'],
40                                                 function() {
41                                                         obj.standalone_import();
42                                                 }
43                                         ],
44                                         'cmd_standalone_export' : [
45                                                 ['command'],
46                                                 function() {
47                                                         obj.standalone_export();
48                                                 }
49                                         ],
50                                         'cmd_clear_cache' : [
51                                                 ['command'],
52                                                 function() {
53                                                         obj.debug('clear_cache');
54                                                 }
55                                         ],
56                                         'cmd_js_console' : [
57                                                 ['command'],
58                                                 function() {
59                                                         obj.debug('js_console');
60                                                 }
61                                         ],
62                                         'cmd_override' : [
63                                                 ['command'],
64                                                 function() {
65                                                         obj.override();
66                                                 }
67                                         ],
68                                         'cmd_logoff' : [
69                                                 ['command'],
70                                                 function() {
71                                                         obj.logoff()
72                                                 }
73                                         ],
74                                         'cmd_close_window' : [
75                                                 ['command'],
76                                                 function() {
77                                                         obj.close()
78                                                 }
79                                         ],
80                                         'test_server' : [
81                                                 ['command'],
82                                                 function() {
83                                                         obj.test_server( obj.controller.view.server_prompt.value );
84                                                 }
85                                         ],
86                                         'server_prompt' : [
87                                                 ['keypress'],
88                                                 handle_keypress
89                                         ],
90                                         'name_prompt' : [
91                                                 ['keypress'],
92                                                 handle_keypress
93                                         ],
94                                         'password_prompt' : [
95                                                 ['keypress'],
96                                                 handle_keypress
97                                         ],
98                                         'submit_button' : [
99                                                 ['render'],
100                                                 function(e) { return function() {} }
101                                         ],
102                                         'progress_bar' : [
103                                                 ['render'],
104                                                 function(e) { return function() {} }
105                                         ],
106                                         'status' : [
107                                                 ['render'],
108                                                 function(e) { return function() {
109                                                 } }
110                                         ],
111                                         'ws_deck' : [
112                                                 ['render'],
113                                                 function(e) { return function() {
114                                                         try {
115                                                                 JSAN.use('util.widgets'); util.widgets.remove_children(e);
116                                                                 var x = document.createElement('description');
117                                                                 e.appendChild(x);
118                                                                 if (obj.data.ws_info 
119                                                                         && obj.data.ws_info[ obj.controller.view.server_prompt.value ]) {
120                                                                         var ws = obj.data.ws_info[ obj.controller.view.server_prompt.value ];
121                                                                         x.appendChild(
122                                                                                 document.createTextNode(
123                                                                                         ws.name + ' @  ' + ws.lib_shortname
124                                                                                 )
125                                                                         );
126                                                                 } else {
127                                                                         x.appendChild(
128                                                                                 document.createTextNode(
129                                                                                         'Not yet configured for the specified server.'
130                                                                                 )
131                                                                         );
132                                                                 }
133                                                         } catch(E) {
134                                                                 alert(E);
135                                                         }
136                                                 } }
137                                         ],
138                                         'menu_spot' : [
139                                                 ['render'],
140                                                 function(e) { return function() {
141                                                 } }
142                                         ],
143
144                                 }
145                         }
146                 );
147                 obj.controller.view.name_prompt.focus();
148
149                 function handle_keypress(ev) {
150                         try {
151                                 if (ev.keyCode && ev.keyCode == 13) {
152                                         switch(this) {
153                                                 case obj.controller.view.server_prompt:
154                                                         ev.preventDefault();
155                                                         obj.controller.view.name_prompt.focus(); obj.controller.view.name_prompt.select();
156                                                 break;
157                                                 case obj.controller.view.name_prompt:
158                                                         ev.preventDefault();
159                                                         obj.controller.view.password_prompt.focus(); obj.controller.view.password_prompt.select();
160                                                 break;
161                                                 case obj.controller.view.password_prompt:
162                                                         ev.preventDefault();
163                                                         obj.controller.view.submit_button.focus(); 
164                                                         obj.login();
165                                                 break;
166                                                 default: break;
167                                         }
168                                 }
169                         } catch(E) {
170                                 alert(E);
171                         }
172                 }
173
174                 obj.controller.view.server_prompt.addEventListener(
175                         'change',
176                         function (ev) { 
177                                 obj.controller.render('ws_deck'); 
178                                 obj.test_server(ev.target.value);
179                         },
180                         false
181                 );
182
183                 // This talks to our ILS
184                 JSAN.use('auth.session');
185                 obj.session = new auth.session(obj.controller.view);
186
187                 obj.controller.render();
188                 obj.test_server( obj.controller.view.server_prompt.value );
189
190                 if (typeof this.on_init == 'function') {
191                         this.error.sdump('D_AUTH','auth.controller.on_init()\n');
192                         this.on_init();
193                 }
194         },
195
196         'test_server' : function(url) {
197                 var obj = this;
198                 obj.controller.view.submit_button.disabled = true;
199                 obj.controller.view.server_prompt.disabled = true;
200                 var s = document.getElementById('status');
201                 s.setAttribute('value','Testing hostname...');
202                 s.setAttribute('style','color: orange;');
203                 document.getElementById('version').value = '';
204                 if (!url) {
205                         s.setAttribute('value','Please enter a server hostname.');
206                         s.setAttribute('style','color: red;');
207                         obj.controller.view.server_prompt.disabled = false;
208                         return;
209                 }
210                 try {
211                         if ( ! url.match(/^http:\/\//) ) url = 'http://' + url;
212                         var x = new XMLHttpRequest();
213                         dump('server url = ' + url + '\n');
214                         x.open("GET",url,true);
215                         x.onreadystatechange = function() {
216                                 try {
217                                         if (x.readyState != 4) return;
218                                         s.setAttribute('value',x.status + ' : ' + x.statusText);
219                                         if (x.status == 200) {
220                                                 s.setAttribute('style','color: green;');
221                                         } else {
222                                                 s.setAttribute('style','color: red;');
223                                         }
224                                         obj.test_version(url);
225                                 } catch(E) {
226                                         obj.controller.view.server_prompt.disabled = false;
227                                         s.setAttribute('value','There was an error testing this hostname.');
228                                         s.setAttribute('style','color: red;');
229                                         obj.error.sdump('D_ERROR',E);
230                                 }
231                         }
232                         x.send(null);
233                 } catch(E) {
234                         s.setAttribute('value','There was an error testing this hostname.');
235                         s.setAttribute('style','color: brown;');
236                         obj.error.sdump('D_ERROR',E);
237                         obj.controller.view.server_prompt.disabled = false;
238                 }
239         },
240
241         'test_version' : function(url) {
242                 var obj = this;
243                 var s = document.getElementById('version');
244                 s.setAttribute('value','Testing version...');
245                 s.setAttribute('style','color: orange;');
246                 try {
247                         var x = new XMLHttpRequest();
248                         var url2 = url + '/xul/server/';
249                         dump('version url = ' + url2 + '\n');
250                         x.open("GET",url2,true);
251                         x.onreadystatechange = function() {
252                                 try {
253                                         if (x.readyState != 4) return;
254                                         s.setAttribute('value',x.status + ' : ' + x.statusText);
255                                         if (x.status == 200) {
256                                                 s.setAttribute('style','color: green;');
257                                                 obj.controller.view.submit_button.disabled = false;
258                                         } else {
259                                                 s.setAttribute('style','color: red;');
260                                                 obj.test_upgrade_instructions(url);
261                                         }
262                                         obj.controller.view.server_prompt.disabled = false;
263                                 } catch(E) {
264                                         s.setAttribute('value','There was an error checking version support.');
265                                         s.setAttribute('style','color: red;');
266                                         obj.error.sdump('D_ERROR',E);
267                                         obj.controller.view.server_prompt.disabled = false;
268                                 }
269                         }
270                         x.send(null);
271                 } catch(E) {
272                         s.setAttribute('value','There was an error checking version support.');
273                         s.setAttribute('style','color: brown;');
274                         obj.error.sdump('D_ERROR',E);
275                         obj.controller.view.server_prompt.disabled = false;
276                 }
277         },
278
279         'test_upgrade_instructions' : function(url) {
280                 var obj = this;
281                 try {
282                         var x = new XMLHttpRequest();
283                         var url2 = url + '/xul/versions.html';
284                         dump('upgrade url = ' + url2 + '\n');
285                         x.open("GET",url2,true);
286                         x.onreadystatechange = function() {
287                                 try {
288                                         if (x.readyState != 4) return;
289                                         if (x.status == 200) {
290                                                 window.open('data:text/html,'+window.escape(x.responseText),'upgrade','chrome,resizable,modal,centered');
291                                         } else {
292                                                 alert('This server does not support your version of the staff client.  Please check with your system administrator.');
293                                         }
294                                         obj.controller.view.server_prompt.disabled = false;
295                                 } catch(E) {
296                                         obj.error.sdump('D_ERROR',E);
297                                         obj.controller.view.server_prompt.disabled = false;
298                                 }
299                         }
300                         x.send(null);
301                 } catch(E) {
302                         obj.error.sdump('D_ERROR',E);
303                         obj.controller.view.server_prompt.disabled = false;
304                 }
305         },
306
307         'login' : function() { 
308
309                 var obj = this;
310
311                 this.error.sdump('D_AUTH','login with ' 
312                         + this.controller.view.name_prompt.value + ' and ' 
313                         + this.controller.view.password_prompt.value + ' at ' 
314                         + this.controller.view.server_prompt.value + '\n'
315                 ); 
316                 this.controller.view.server_prompt.disabled = true;
317                 this.controller.view.name_prompt.disabled = true;
318                 this.controller.view.password_prompt.disabled = true;
319                 this.controller.view.submit_button.disabled = true;
320                 XML_HTTP_SERVER = this.controller.view.server_prompt.value;
321
322                 try {
323
324                         if (typeof this.on_login == 'function') {
325                                 this.error.sdump('D_AUTH','auth.controller.session.on_init = ' +
326                                         'auth.controller.on_login\n');
327                                 this.session.on_init = this.on_login;
328                                 this.session.on_error = function() { obj.logoff(); };
329                         }
330                         
331                         this.session.init();
332
333                 } catch(E) {
334                         var error = '!! ' + E + '\n';
335                         this.error.sdump('D_ERROR',error); 
336                         alert(error);
337                         this.logoff();
338                         if (E == 'open-ils.auth.authenticate.init returned false\n') {
339                                 this.controller.view.server_prompt.focus();
340                                 this.controller.view.server_prompt.select();
341                         }
342
343                         if (typeof this.on_login_error == 'function') {
344                                 this.error.sdump('D_AUTH','auth.controller.on_login_error()\n');
345                                 this.on_login_error(E);
346                         }
347                 }
348
349         },
350
351         'standalone' : function() {
352                 var obj = this;
353                 try {
354                         if (typeof this.on_standalone == 'function') {
355                                 obj.on_standalone();
356                         }
357                 } catch(E) {
358                         var error = '!! ' + E + '\n';
359                         obj.error.sdump('D_ERROR',error); 
360                         alert(error);
361                 }
362         },
363
364         'standalone_import' : function() {
365                 var obj = this;
366                 try {
367                         if (typeof this.on_standalone_import == 'function') {
368                                 obj.on_standalone_import();
369                         }
370                 } catch(E) {
371                         var error = '!! ' + E + '\n';
372                         obj.error.sdump('D_ERROR',error); 
373                         alert(error);
374                 }
375         },
376
377         'standalone_export' : function() {
378                 var obj = this;
379                 try {
380                         if (typeof this.on_standalone_export == 'function') {
381                                 obj.on_standalone_export();
382                         }
383                 } catch(E) {
384                         var error = '!! ' + E + '\n';
385                         obj.error.sdump('D_ERROR',error); 
386                         alert(error);
387                 }
388         },
389
390         'debug' : function(action) {
391                 var obj = this;
392                 try {
393                         if (typeof this.on_debug == 'function') {
394                                 obj.on_debug(action);
395                         }
396                 } catch(E) {
397                         var error = '!! ' + E + '\n';
398                         obj.error.sdump('D_ERROR',error);
399                         alert(error);
400                 }
401         },
402
403         'logoff' : function() { 
404         
405                 this.error.sdump('D_AUTH','logoff' + this.w + '\n'); 
406                 this.controller.view.progress_bar.value = 0; 
407                 this.controller.view.progress_bar.setAttribute('real','0.0');
408                 this.controller.view.submit_button.disabled = false;
409                 this.controller.view.password_prompt.disabled = false;
410                 this.controller.view.password_prompt.value = '';
411                 this.controller.view.name_prompt.disabled = false;
412                 this.controller.view.name_prompt.focus(); 
413                 this.controller.view.name_prompt.select();
414                 this.controller.view.server_prompt.disabled = false;
415
416                 var windowManager = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService();
417                 var windowManagerInterface = windowManager.QueryInterface(Components.interfaces.nsIWindowMediator);
418                 var enumerator = windowManagerInterface.getEnumerator(null);
419
420                 var w; // close all other windows
421                 while ( w = enumerator.getNext() ) {
422                         if (w != window) w.close();
423                 }
424
425                 this.controller.render('ws_deck');
426
427                 this.session.close();
428
429                 /* FIXME - need some locking or object destruction for the async tests */
430                 /* this.test_server( this.controller.view.server_prompt.value ); */
431
432                 if (typeof this.on_logoff == 'function') {
433                         this.error.sdump('D_AUTH','auth.controller.on_logoff()\n');
434                         this.on_logoff();
435                 }
436                 
437         },
438         'close' : function() { 
439         
440                 this.error.sdump('D_AUTH','close' + this.w + '\n');
441                 this.logoff();
442                 //Basically, we want to close all the windows for this application (and in case we're running this as
443                 //a firefox extension, we don't want to merely shutdown mozilla).  I'll probably create an XPCOM for
444                 //tracking the windows.
445                 //for (var w in this.G.window.appshell_list) {
446                 //      this.G.window.appshell_list[w].close();
447                 //}
448                 this.w.close(); /* Probably won't go any further */
449
450                 if (typeof this.on_close == 'function') {
451                         this.error.sdump('D_AUTH','auth.controller.on_close()\n');
452                         this.on_close();
453                 }
454                 
455         }
456 }
457
458 dump('exiting auth/controller.js\n');