]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/auth/controller.js
Improve Firefox/XULRunner Support
[working/Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / auth / controller.js
1 dump('entering auth/controller.js\n');
2 // vim:sw=4:ts=4:noet:
3
4 if (typeof auth == 'undefined') auth = {};
5 auth.controller = function (params) {
6     JSAN.use('util.error'); this.error = new util.error();
7     this.w = params.window;
8
9     return this;
10 };
11
12 auth.controller.prototype = {
13
14     'init' : function () {
15
16         var obj = this;  // so the 'this' in event handlers don't confuse us
17         var w = obj.w;
18
19         JSAN.use('OpenILS.data');
20         obj.data = new OpenILS.data(); obj.data.init({'via':'stash'});
21
22         // MVC
23         JSAN.use('util.controller'); obj.controller = new util.controller();
24         obj.controller.init(
25             {
26                 'control_map' : {
27                     'cmd_login' : [
28                         ['command'],
29                         function() {
30                             obj.login();
31                         }
32                     ],
33                     'cmd_standalone' : [
34                         ['command'],
35                         function() {
36                             obj.standalone();
37                         }
38                     ],
39                     'cmd_standalone_import' : [
40                         ['command'],
41                         function() {
42                             obj.standalone_import();
43                         }
44                     ],
45                     'cmd_standalone_export' : [
46                         ['command'],
47                         function() {
48                             obj.standalone_export();
49                         }
50                     ],
51                     'cmd_clear_cache' : [
52                         ['command'],
53                         function() {
54                             obj.debug('clear_cache');
55                         }
56                     ],
57                     'cmd_js_console' : [
58                         ['command'],
59                         function() {
60                             obj.debug('js_console');
61                         }
62                     ],
63                     'cmd_debugger' : [
64                         ['command'],
65                         function() {
66                             start_debugger();
67                         }
68                     ],
69                     'cmd_inspector' : [
70                         ['command'],
71                         function() {
72                             start_inspector();
73                         }
74                     ],
75                     'cmd_chrome_list' : [
76                         ['command'],
77                         function() {
78                             start_chrome_list();
79                         }
80                     ],
81                     'cmd_js_shell' : [
82                         ['command'],
83                         function() {
84                             start_js_shell();
85                         }
86                     ],
87                     'cmd_override' : [
88                         ['command'],
89                         function() {
90                             obj.override();
91                         }
92                     ],
93                     'cmd_logoff' : [
94                         ['command'],
95                         function() {
96                             obj.logoff()
97                         }
98                     ],
99                     'cmd_close_window' : [
100                         ['command'],
101                         function() {
102                             obj.close()
103                         }
104                     ],
105                     'cmd_test_server' : [
106                         ['command'],
107                         function() {
108                             obj.test_server( obj.controller.view.server_prompt.value );
109                         }
110                     ],
111                     'ssl_exception' : [
112                         ['render'],
113                         function(e) {
114                             return function() {
115                                 try {
116                                     obj.controller.view.cmd_ssl_exception.setAttribute('hidden','true');
117                                     var x = new XMLHttpRequest();
118                                     x.open("GET",'chrome://pippki/content/exceptionDialog.xul',false);
119                                     x.send(null);
120                                     obj.controller.view.cmd_ssl_exception.setAttribute('hidden','false');
121                                 } catch(E) {
122                                     obj.controller.view.cmd_ssl_exception.setAttribute('hidden','true');
123                                 }
124                             };
125                         }
126                     ],
127                     'cmd_ssl_exception' : [
128                         ['command'],
129                         function() {
130                             window.openDialog(
131                                 'chrome://pippki/content/exceptionDialog.xul',
132                                 '', 
133                                 'chrome,centerscreen,modal', 
134                                 { 
135                                     'location' : 'https://' + obj.controller.view.server_prompt.value.match(/^[^\/]*/), 
136                                     'prefetchCert' : true 
137                                 } 
138                             );
139                             obj.test_server( obj.controller.view.server_prompt.value );
140                         }
141                     ],
142                     'server_prompt' : [
143                         ['keypress'],
144                         handle_keypress
145                     ],
146                     'server_menu' : [
147                         ['render'],
148                         function(e) {
149                             return function() {
150                                 for (var s in obj.data.ws_info) {
151                                     var mi = document.createElement('menuitem');
152                                     mi.setAttribute('label',s);
153                                     mi.setAttribute('value',s);
154                                     e.appendChild(mi);
155                                 }
156                             };
157                         }
158                     ],
159                     'name_prompt' : [
160                         ['keypress'],
161                         handle_keypress
162                     ],
163                     'password_prompt' : [
164                         ['keypress'],
165                         handle_keypress
166                     ],
167                     'submit_button' : [
168                         ['render'],
169                         function(e) { return function() {} }
170                     ],
171                     'apply_locale_btn' : [
172                         ['render'],
173                         function(e) { return function() {} }
174                     ],
175                     'progress_bar' : [
176                         ['render'],
177                         function(e) { return function() {} }
178                     ],
179                     'status' : [
180                         ['render'],
181                         function(e) { return function() {
182                         } }
183                     ],
184                     'ws_deck' : [
185                         ['render'],
186                         function(e) { return function() {
187                             try {
188                                 JSAN.use('util.widgets'); util.widgets.remove_children(e);
189                                 var x = document.createElement('description');
190                                 e.appendChild(x);
191                                 if (obj.data.ws_info 
192                                     && obj.data.ws_info[ obj.controller.view.server_prompt.value ]) {
193                                     var ws = obj.data.ws_info[ obj.controller.view.server_prompt.value ];
194                                     x.appendChild(
195                                         document.createTextNode(
196                                             ws.name /*+ ' @  ' + ws.lib_shortname*/
197                                         )
198                                     );
199                                     JSAN.use('util.file'); var file = new util.file('last_ws_server');
200                                     file.set_object(obj.controller.view.server_prompt.value);
201                                     file.close();
202                                 } else {
203                                     x.appendChild(
204                                         document.createTextNode(
205                                             document.getElementById('authStrings').getString('staff.auth.controller.not_configured')
206                                         )
207                                     );
208                                 }
209                             } catch(E) {
210                                 alert(E);
211                             }
212                         } }
213                     ],
214                     'menu_spot' : [
215                         ['render'],
216                         function(e) { return function() {
217                         } }
218                     ],
219
220                 }
221             }
222         );
223         obj.controller.view.name_prompt.focus();
224
225         function handle_keypress(ev) {
226             try {
227                 if (ev.keyCode && ev.keyCode == 13) {
228                     switch(this) {
229                         case obj.controller.view.server_prompt:
230                             ev.preventDefault();
231                             obj.controller.view.name_prompt.focus(); obj.controller.view.name_prompt.select();
232                         break;
233                         case obj.controller.view.name_prompt:
234                             ev.preventDefault();
235                             obj.controller.view.password_prompt.focus(); obj.controller.view.password_prompt.select();
236                         break;
237                         case obj.controller.view.password_prompt:
238                             ev.preventDefault();
239                             obj.controller.view.submit_button.focus(); 
240                             obj.login();
241                         break;
242                         default: break;
243                     }
244                 }
245             } catch(E) {
246                 alert(E);
247             }
248         }
249
250         obj.controller.view.server_prompt.addEventListener(
251             'change',
252             function (ev) { 
253                 obj.test_server(ev.target.value);
254                 obj.controller.render('ws_deck'); 
255             },
256             false
257         );
258         obj.controller.view.server_prompt.addEventListener(
259             'command',
260             function (ev) {
261                 obj.controller.view.name_prompt.focus();
262                 obj.controller.view.name_prompt.select();
263                 obj.test_server(ev.target.value);
264                 obj.controller.render('ws_deck'); 
265             },
266             false
267         );
268
269         // This talks to our ILS
270         JSAN.use('auth.session');
271         obj.session = new auth.session(obj.controller.view);
272
273         obj.controller.render();
274         obj.test_server( obj.controller.view.server_prompt.value );
275         obj.controller.render('ws_deck'); 
276
277         if (typeof this.on_init == 'function') {
278             this.error.sdump('D_AUTH','auth.controller.on_init()\n');
279             this.on_init();
280         }
281     },
282
283     'test_server' : function(url) {
284         var obj = this;
285         if (!url) {
286             JSAN.use('util.file'); var file = new util.file('last_ws_server');
287             if (file._file.exists()) {
288                 url = file.get_object(); file.close();
289                 obj.controller.view.server_prompt.value = url;
290             }
291         }
292         url = url.match(/^[^\/]*/).toString(); // Only test the pre-slash URL
293         obj.controller.view.submit_button.disabled = true;
294         obj.controller.view.server_prompt.disabled = true;
295         var s = document.getElementById('status');
296         s.setAttribute('value', document.getElementById('authStrings').getString('staff.auth.controller.testing_hostname'));
297         s.setAttribute('style','color: orange;');
298         document.getElementById('version').value = '';
299         if (!url) {
300             s.setAttribute('value', document.getElementById('authStrings').getString('staff.auth.controller.prompt_hostname'));
301             s.setAttribute('style','color: red;');
302             obj.controller.view.server_prompt.disabled = false;
303             obj.controller.view.server_prompt.focus();
304             return;
305         }
306         try {
307             if ( ! url.match(/^https:\/\//) ) url = 'https://' + url;
308             var x = new XMLHttpRequest();
309             dump('server url = ' + url + '\n');
310             x.open("GET",url,true);
311             x.onreadystatechange = function() {
312                 try {
313                     if (x.readyState != 4) return;
314                     s.setAttribute('value', document.getElementById('authStrings').getFormattedString('staff.auth.controller.status', [x.status, x.statusText]));
315                     if (x.status == 200) {
316                         s.setAttribute('style','color: green;');
317                     } else {
318                         s.setAttribute('style','color: red;');
319                     }
320                     obj.test_version(url);
321                 } catch(E) {
322                     obj.controller.view.server_prompt.disabled = false;
323                     obj.controller.view.server_prompt.focus();
324                     s.setAttribute('value', document.getElementById('authStrings').getString('staff.auth.controller.error_hostname'));
325                     s.setAttribute('style','color: red;');
326                     obj.error.sdump('D_ERROR',E);
327                 }
328             }
329             x.send(null);
330         } catch(E) {
331             s.setAttribute('value', document.getElementById('authStrings').getString('staff.auth.controller.error_hostname'));
332             s.setAttribute('style','color: brown;');
333             obj.error.sdump('D_ERROR',E);
334             obj.controller.view.server_prompt.disabled = false;
335             obj.controller.view.server_prompt.focus();
336         }
337     },
338
339     'test_version' : function(url) {
340         var obj = this;
341         var s = document.getElementById('version');
342         s.setAttribute('value', document.getElementById('authStrings').getString('staff.auth.controller.testing_version'));
343         s.setAttribute('style','color: orange;');
344         try {
345             var x = new XMLHttpRequest();
346             var url2 = url + '/xul/server/';
347             dump('version url = ' + url2 + '\n');
348             x.open("GET",url2,true);
349             x.onreadystatechange = function() {
350                 try {
351                     if (x.readyState != 4) return;
352                     s.setAttribute('value', document.getElementById('authStrings').getFormattedString('staff.auth.controller.status', [x.status, x.statusText]));
353                     if (x.status == 200) {
354                         s.setAttribute('style','color: green;');
355                         obj.controller.view.submit_button.disabled = false;
356                     } else {
357                         s.setAttribute('style','color: red;');
358                         obj.test_upgrade_instructions(url);
359                     }
360                     obj.controller.view.server_prompt.disabled = false;
361                 } catch(E) {
362                     s.setAttribute('value', document.getElementById('authStrings').getString('staff.auth.controller.error_version'));
363                     s.setAttribute('style','color: red;');
364                     obj.error.sdump('D_ERROR',E);
365                     obj.controller.view.server_prompt.disabled = false;
366                 }
367             }
368             x.send(null);
369         } catch(E) {
370             s.setAttribute('value', document.getElementById('authStrings').getString('staff.auth.controller.error_version'));
371             s.setAttribute('style','color: brown;');
372             obj.error.sdump('D_ERROR',E);
373             obj.controller.view.server_prompt.disabled = false;
374         }
375     },
376
377     'test_upgrade_instructions' : function(url) {
378         var obj = this;
379         try {
380             var x = new XMLHttpRequest();
381             var url2 = url + '/xul/versions.html';
382             dump('upgrade url = ' + url2 + '\n');
383             x.open("GET",url2,true);
384             x.onreadystatechange = function() {
385                 try {
386                     if (x.readyState != 4) return;
387                     if (x.status == 200) {
388                         window.open('data:text/html,'+window.escape(x.responseText),'upgrade','chrome,resizable,modal,centered');
389                     } else {
390                         if(typeof(G.upgradeCheck) == "function")
391                         {
392                             if (confirm("This server does not support your version of the staff client, an upgrade may be required. If you wish to check for an upgrade please press Ok. Otherwise please press cancel."))
393                                 G.upgradeCheck();
394                         } else {
395                             alert(document.getElementById('authStrings').getString('staff.auth.controller.version_mismatch'));
396                         }
397                     }
398                     obj.controller.view.server_prompt.disabled = false;
399                 } catch(E) {
400                     obj.error.sdump('D_ERROR',E);
401                     obj.controller.view.server_prompt.disabled = false;
402                 }
403             }
404             x.send(null);
405         } catch(E) {
406             obj.error.sdump('D_ERROR',E);
407             obj.controller.view.server_prompt.disabled = false;
408         }
409     },
410
411     'login' : function() { 
412
413         var obj = this;
414
415         this.error.sdump('D_AUTH',
416             document.getElementById('authStrings').getFormattedString(
417                 'staff.auth.controller.error_login', [
418                     this.controller.view.name_prompt.value,
419                     this.controller.view.password_prompt.value,
420                     this.controller.view.server_prompt.value
421                 ]
422             )
423         ); 
424         this.controller.view.server_prompt.disabled = true;
425         this.controller.view.name_prompt.disabled = true;
426         this.controller.view.password_prompt.disabled = true;
427         this.controller.view.submit_button.disabled = true;
428         this.controller.view.apply_locale_btn.disabled = true;
429         XML_HTTP_SERVER = this.controller.view.server_prompt.value.match(/^[^\/]*/).toString();
430
431         try {
432
433             if (typeof this.on_login == 'function') {
434                 this.error.sdump('D_AUTH','auth.controller.session.on_init = ' +
435                     'auth.controller.on_login\n');
436                 this.session.on_init = this.on_login;
437                 this.session.on_error = function() { obj.logoff(); };
438             }
439             
440             this.session.init();
441
442         } catch(E) {
443             var error = '!! ' + E + '\n';
444             this.error.sdump('D_ERROR',error); 
445             alert(error);
446             this.logoff();
447             if (E == 'open-ils.auth.authenticate.init returned false\n') {
448                 this.controller.view.server_prompt.focus();
449                 this.controller.view.server_prompt.select();
450             }
451
452             if (typeof this.on_login_error == 'function') {
453                 this.error.sdump('D_AUTH','auth.controller.on_login_error()\n');
454                 this.on_login_error(E);
455             }
456         }
457         // Once we are done with it, clear the password
458         this.controller.view.password_prompt.value = '';
459
460     },
461
462     'standalone' : function() {
463         var obj = this;
464         try {
465             if (typeof this.on_standalone == 'function') {
466                 obj.on_standalone();
467             }
468         } catch(E) {
469             var error = '!! ' + E + '\n';
470             obj.error.sdump('D_ERROR',error); 
471             alert(error);
472         }
473     },
474
475     'standalone_import' : function() {
476         var obj = this;
477         try {
478             if (typeof this.on_standalone_import == 'function') {
479                 obj.on_standalone_import();
480             }
481         } catch(E) {
482             var error = '!! ' + E + '\n';
483             obj.error.sdump('D_ERROR',error); 
484             alert(error);
485         }
486     },
487
488     'standalone_export' : function() {
489         var obj = this;
490         try {
491             if (typeof this.on_standalone_export == 'function') {
492                 obj.on_standalone_export();
493             }
494         } catch(E) {
495             var error = '!! ' + E + '\n';
496             obj.error.sdump('D_ERROR',error); 
497             alert(error);
498         }
499     },
500
501     'debug' : function(action) {
502         var obj = this;
503         try {
504             if (typeof this.on_debug == 'function') {
505                 obj.on_debug(action);
506             }
507         } catch(E) {
508             var error = '!! ' + E + '\n';
509             obj.error.sdump('D_ERROR',error);
510             alert(error);
511         }
512     },
513
514     'logoff' : function() { 
515
516         this.data.stash_retrieve();
517         if (typeof this.data.unsaved_data != 'undefined') {
518             if (this.data.unsaved_data > 0) {
519                 var confirmation = window.confirm( document.getElementById('offlineStrings').getString('menu.logoff.unsaved_data_warning') );
520                 if (!confirmation) { return; }
521                 this.data.unsaved_data = 0;
522                 this.data.stash('unsaved_data');
523             }
524         }
525     
526         this.error.sdump('D_AUTH','logoff' + this.w + '\n'); 
527         this.controller.view.progress_bar.value = 0; 
528         this.controller.view.progress_bar.setAttribute('real','0.0');
529         this.controller.view.submit_button.disabled = false;
530         this.controller.view.apply_locale_btn.disabled = false;
531         this.controller.view.password_prompt.disabled = false;
532         this.controller.view.password_prompt.value = '';
533         this.controller.view.name_prompt.disabled = false;
534         this.controller.view.name_prompt.focus(); 
535         this.controller.view.name_prompt.select();
536         this.controller.view.server_prompt.disabled = false;
537
538         var windowManager = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService();
539         var windowManagerInterface = windowManager.QueryInterface(Components.interfaces.nsIWindowMediator);
540         var enumerator = windowManagerInterface.getEnumerator(null);
541
542         var w; // close all other windows
543         while ( w = enumerator.getNext() ) {
544             if (w != window) {
545                 if (w.xulG) { w.close(); } // FIXME: kludge so we don't close Firefox windows as an extension.  We should define a @windowtype for all the staff client windows and have the enumerator just pull those
546             }
547         }
548
549         this.controller.render('ws_deck');
550
551         this.session.close();
552         this.data.menu_perms = false;
553         this.data.current_hotkeyset = undefined;
554         this.data.enable_debug = this.data.debug_client;
555         this.data.session = undefined;
556         this.data.stash('menu_perms');
557         this.data.stash('current_hotkeyset');
558         this.data.stash('enable_debug');
559         this.data.stash('session');
560
561         /* FIXME - need some locking or object destruction for the async tests */
562         /* this.test_server( this.controller.view.server_prompt.value ); */
563
564         if (typeof this.on_logoff == 'function') {
565             this.error.sdump('D_AUTH','auth.controller.on_logoff()\n');
566             this.on_logoff();
567         }
568         
569     },
570     'close' : function() { 
571     
572         this.error.sdump('D_AUTH','close' + this.w + '\n');
573
574         var confirm_string = document.getElementById('authStrings').getString('staff.auth.controller.confirm_close');
575
576         this.data.stash_retrieve();
577         if (typeof this.data.unsaved_data != 'undefined') {
578             if (this.data.unsaved_data > 0) {
579                 confirm_string = document.getElementById('offlineStrings').getString('menu.shutdown.unsaved_data_warning');
580             }
581         }
582  
583         if (window.confirm(confirm_string)) {
584             this.data.unsaved_data = 0;
585             this.data.stash('unsaved_data');
586             this.logoff();
587             this.w.close(); /* Probably won't go any further */
588
589             if (typeof this.on_close == 'function') {
590                 this.error.sdump('D_AUTH','auth.controller.on_close()\n');
591                 this.on_close();
592             }
593         }
594         
595     }
596 }
597
598 dump('exiting auth/controller.js\n');