]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/auth/controller.js
Staff client initial hostname
[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                                 var list = [];
151                                 for (var s in obj.data.ws_info) {
152                                     list.push(s);
153                                 }
154                                 list.sort();
155                                 for (var i = 0; i < list.length; i++) {
156                                     var mi = document.createElement('menuitem');
157                                     mi.setAttribute('label',list[i]);
158                                     mi.setAttribute('value',list[i]);
159                                     e.appendChild(mi);
160                                 }
161                             };
162                         }
163                     ],
164                     'name_prompt' : [
165                         ['keypress'],
166                         handle_keypress
167                     ],
168                     'password_prompt' : [
169                         ['keypress'],
170                         handle_keypress
171                     ],
172                     'submit_button' : [
173                         ['render'],
174                         function(e) { return function() {} }
175                     ],
176                     'apply_locale_btn' : [
177                         ['render'],
178                         function(e) { return function() {} }
179                     ],
180                     'progress_bar' : [
181                         ['render'],
182                         function(e) { return function() {} }
183                     ],
184                     'status' : [
185                         ['render'],
186                         function(e) { return function() {
187                         } }
188                     ],
189                     'ws_deck' : [
190                         ['render'],
191                         function(e) { return function() {
192                             try {
193                                 JSAN.use('util.widgets'); util.widgets.remove_children(e);
194                                 var x = document.createElement('description');
195                                 e.appendChild(x);
196                                 if (obj.data.ws_info 
197                                     && obj.data.ws_info[ obj.controller.view.server_prompt.value ]) {
198                                     var ws = obj.data.ws_info[ obj.controller.view.server_prompt.value ];
199                                     x.appendChild(
200                                         document.createTextNode(
201                                             ws.name /*+ ' @  ' + ws.lib_shortname*/
202                                         )
203                                     );
204                                     JSAN.use('util.file'); var file = new util.file('last_ws_server');
205                                     file.set_object(obj.controller.view.server_prompt.value);
206                                     file.close();
207                                 } else {
208                                     x.appendChild(
209                                         document.createTextNode(
210                                             document.getElementById('authStrings').getString('staff.auth.controller.not_configured')
211                                         )
212                                     );
213                                 }
214                             } catch(E) {
215                                 alert(E);
216                             }
217                         } }
218                     ],
219                     'menu_spot' : [
220                         ['render'],
221                         function(e) { return function() {
222                         } }
223                     ],
224
225                 }
226             }
227         );
228         obj.controller.view.name_prompt.focus();
229
230         function handle_keypress(ev) {
231             try {
232                 if (ev.keyCode && ev.keyCode == 13) {
233                     switch(this) {
234                         case obj.controller.view.server_prompt:
235                             ev.preventDefault();
236                             obj.controller.view.name_prompt.focus(); obj.controller.view.name_prompt.select();
237                         break;
238                         case obj.controller.view.name_prompt:
239                             ev.preventDefault();
240                             obj.controller.view.password_prompt.focus(); obj.controller.view.password_prompt.select();
241                         break;
242                         case obj.controller.view.password_prompt:
243                             ev.preventDefault();
244                             obj.controller.view.submit_button.focus(); 
245                             obj.login();
246                         break;
247                         default: break;
248                     }
249                 }
250             } catch(E) {
251                 alert(E);
252             }
253         }
254
255         obj.controller.view.server_prompt.addEventListener(
256             'change',
257             function (ev) { 
258                 obj.test_server(ev.target.value);
259                 obj.controller.render('ws_deck'); 
260             },
261             false
262         );
263         obj.controller.view.server_prompt.addEventListener(
264             'command',
265             function (ev) {
266                 obj.controller.view.name_prompt.focus();
267                 obj.controller.view.name_prompt.select();
268                 obj.test_server(ev.target.value);
269                 obj.controller.render('ws_deck'); 
270             },
271             false
272         );
273
274         // This talks to our ILS
275         JSAN.use('auth.session');
276         obj.session = new auth.session(obj.controller.view);
277
278         obj.controller.render();
279         obj.controller.render('ws_deck'); 
280
281         if (typeof this.on_init == 'function') {
282             this.error.sdump('D_AUTH','auth.controller.on_init()\n');
283             this.on_init();
284         }
285     },
286
287     'test_server' : function(url) {
288         var obj = this;
289         if (!url) {
290             JSAN.use('util.file'); var file = new util.file('last_ws_server');
291             if (file._file.exists()) {
292                 url = file.get_object(); file.close();
293                 obj.controller.view.server_prompt.value = url;
294             }
295         }
296         url = url.match(/^[^\/]*/).toString(); // Only test the pre-slash URL
297         obj.controller.view.submit_button.disabled = true;
298         obj.controller.view.server_prompt.disabled = true;
299         var s = document.getElementById('status');
300         s.setAttribute('value', document.getElementById('authStrings').getString('staff.auth.controller.testing_hostname'));
301         s.setAttribute('style','color: orange;');
302         document.getElementById('version').value = '';
303         if (!url) {
304             s.setAttribute('value', document.getElementById('authStrings').getString('staff.auth.controller.prompt_hostname'));
305             s.setAttribute('style','color: red;');
306             obj.controller.view.server_prompt.disabled = false;
307             obj.controller.view.server_prompt.focus();
308             return;
309         }
310         try {
311             if ( ! url.match(/^https:\/\//) ) url = 'https://' + url;
312             var x = new XMLHttpRequest();
313             dump('server url = ' + url + '\n');
314             x.open("GET",url,true);
315             x.onreadystatechange = function() {
316                 try {
317                     if (x.readyState != 4) return;
318                     s.setAttribute('value', document.getElementById('authStrings').getFormattedString('staff.auth.controller.status', [x.status, x.statusText]));
319                     if (x.status == 200) {
320                         s.setAttribute('style','color: green;');
321                     } else {
322                         if(x.status == 0) {
323                             s.setAttribute('value', document.getElementById('authStrings').getString('staff.auth.controller.error_hostname'));
324                             obj.controller.view.server_prompt.disabled = false;
325                             obj.controller.view.server_prompt.focus();
326                         }
327                         s.setAttribute('style','color: red;');
328                     }
329                     if(x.status > 0)
330                         obj.test_version(url);
331                 } catch(E) {
332                     obj.controller.view.server_prompt.disabled = false;
333                     obj.controller.view.server_prompt.focus();
334                     s.setAttribute('value', document.getElementById('authStrings').getString('staff.auth.controller.error_hostname'));
335                     s.setAttribute('style','color: red;');
336                     obj.error.sdump('D_ERROR',E);
337                 }
338             }
339             x.send(null);
340         } catch(E) {
341             s.setAttribute('value', document.getElementById('authStrings').getString('staff.auth.controller.error_hostname'));
342             s.setAttribute('style','color: brown;');
343             obj.error.sdump('D_ERROR',E);
344             obj.controller.view.server_prompt.disabled = false;
345             obj.controller.view.server_prompt.focus();
346         }
347     },
348
349     'test_version' : function(url) {
350         var obj = this;
351         var s = document.getElementById('version');
352         s.setAttribute('value', document.getElementById('authStrings').getString('staff.auth.controller.testing_version'));
353         s.setAttribute('style','color: orange;');
354         try {
355             var x = new XMLHttpRequest();
356             var url2 = url + '/xul/server/';
357             dump('version url = ' + url2 + '\n');
358             x.open("GET",url2,true);
359             x.onreadystatechange = function() {
360                 try {
361                     if (x.readyState != 4) return;
362                     s.setAttribute('value', document.getElementById('authStrings').getFormattedString('staff.auth.controller.status', [x.status, x.statusText]));
363                     if (x.status == 200) {
364                         s.setAttribute('style','color: green;');
365                         obj.controller.view.submit_button.disabled = false;
366                     } else {
367                         s.setAttribute('style','color: red;');
368                         obj.test_upgrade_instructions(url);
369                     }
370                     obj.controller.view.server_prompt.disabled = false;
371                 } catch(E) {
372                     s.setAttribute('value', document.getElementById('authStrings').getString('staff.auth.controller.error_version'));
373                     s.setAttribute('style','color: red;');
374                     obj.error.sdump('D_ERROR',E);
375                     obj.controller.view.server_prompt.disabled = false;
376                 }
377             }
378             x.send(null);
379         } catch(E) {
380             s.setAttribute('value', document.getElementById('authStrings').getString('staff.auth.controller.error_version'));
381             s.setAttribute('style','color: brown;');
382             obj.error.sdump('D_ERROR',E);
383             obj.controller.view.server_prompt.disabled = false;
384         }
385     },
386
387     'test_upgrade_instructions' : function(url) {
388         var obj = this;
389         try {
390             var x = new XMLHttpRequest();
391             var url2 = url + '/xul/versions.html';
392             dump('upgrade url = ' + url2 + '\n');
393             x.open("GET",url2,true);
394             x.onreadystatechange = function() {
395                 try {
396                     if (x.readyState != 4) return;
397                     if (x.status == 200) {
398                         window.open('data:text/html,'+window.escape(x.responseText),'upgrade','chrome,resizable,modal,centered');
399                     } else {
400                         if(typeof(G.upgradeCheck) == "function")
401                         {
402                             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."))
403                                 G.upgradeCheck();
404                         } else {
405                             alert(document.getElementById('authStrings').getString('staff.auth.controller.version_mismatch'));
406                         }
407                     }
408                     obj.controller.view.server_prompt.disabled = false;
409                 } catch(E) {
410                     obj.error.sdump('D_ERROR',E);
411                     obj.controller.view.server_prompt.disabled = false;
412                 }
413             }
414             x.send(null);
415         } catch(E) {
416             obj.error.sdump('D_ERROR',E);
417             obj.controller.view.server_prompt.disabled = false;
418         }
419     },
420
421     'login' : function() { 
422
423         var obj = this;
424
425         this.error.sdump('D_AUTH',
426             document.getElementById('authStrings').getFormattedString(
427                 'staff.auth.controller.error_login', [
428                     this.controller.view.name_prompt.value,
429                     this.controller.view.password_prompt.value,
430                     this.controller.view.server_prompt.value
431                 ]
432             )
433         ); 
434         this.controller.view.server_prompt.disabled = true;
435         this.controller.view.name_prompt.disabled = true;
436         this.controller.view.password_prompt.disabled = true;
437         this.controller.view.submit_button.disabled = true;
438         this.controller.view.apply_locale_btn.disabled = true;
439         XML_HTTP_SERVER = this.controller.view.server_prompt.value.match(/^[^\/]*/).toString();
440
441         try {
442
443             if (typeof this.on_login == 'function') {
444                 this.error.sdump('D_AUTH','auth.controller.session.on_init = ' +
445                     'auth.controller.on_login\n');
446                 this.session.on_init = this.on_login;
447                 this.session.on_error = function() { obj.logoff(); };
448             }
449             
450             this.session.init();
451
452         } catch(E) {
453             var error = '!! ' + E + '\n';
454             this.error.sdump('D_ERROR',error); 
455             alert(error);
456             this.logoff();
457             if (E == 'open-ils.auth.authenticate.init returned false\n') {
458                 this.controller.view.server_prompt.focus();
459                 this.controller.view.server_prompt.select();
460             }
461
462             if (typeof this.on_login_error == 'function') {
463                 this.error.sdump('D_AUTH','auth.controller.on_login_error()\n');
464                 this.on_login_error(E);
465             }
466         }
467         // Once we are done with it, clear the password
468         this.controller.view.password_prompt.value = '';
469
470     },
471
472     'standalone' : function() {
473         var obj = this;
474         try {
475             if (typeof this.on_standalone == 'function') {
476                 obj.on_standalone();
477             }
478         } catch(E) {
479             var error = '!! ' + E + '\n';
480             obj.error.sdump('D_ERROR',error); 
481             alert(error);
482         }
483     },
484
485     'standalone_import' : function() {
486         var obj = this;
487         try {
488             if (typeof this.on_standalone_import == 'function') {
489                 obj.on_standalone_import();
490             }
491         } catch(E) {
492             var error = '!! ' + E + '\n';
493             obj.error.sdump('D_ERROR',error); 
494             alert(error);
495         }
496     },
497
498     'standalone_export' : function() {
499         var obj = this;
500         try {
501             if (typeof this.on_standalone_export == 'function') {
502                 obj.on_standalone_export();
503             }
504         } catch(E) {
505             var error = '!! ' + E + '\n';
506             obj.error.sdump('D_ERROR',error); 
507             alert(error);
508         }
509     },
510
511     'debug' : function(action) {
512         var obj = this;
513         try {
514             if (typeof this.on_debug == 'function') {
515                 obj.on_debug(action);
516             }
517         } catch(E) {
518             var error = '!! ' + E + '\n';
519             obj.error.sdump('D_ERROR',error);
520             alert(error);
521         }
522     },
523
524     'logoff' : function() { 
525
526         this.data.stash_retrieve();
527         if (typeof this.data.unsaved_data != 'undefined') {
528             if (this.data.unsaved_data > 0) {
529                 var confirmation = window.confirm( document.getElementById('offlineStrings').getString('menu.logoff.unsaved_data_warning') );
530                 if (!confirmation) { return; }
531                 this.data.unsaved_data = 0;
532                 this.data.stash('unsaved_data');
533             }
534         }
535     
536         this.error.sdump('D_AUTH','logoff' + this.w + '\n'); 
537         this.controller.view.progress_bar.value = 0; 
538         this.controller.view.progress_bar.setAttribute('real','0.0');
539         this.controller.view.submit_button.disabled = false;
540         this.controller.view.apply_locale_btn.disabled = false;
541         this.controller.view.password_prompt.disabled = false;
542         this.controller.view.password_prompt.value = '';
543         this.controller.view.name_prompt.disabled = false;
544         this.controller.view.name_prompt.focus(); 
545         this.controller.view.name_prompt.select();
546         this.controller.view.server_prompt.disabled = false;
547
548         var windowManager = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService();
549         var windowManagerInterface = windowManager.QueryInterface(Components.interfaces.nsIWindowMediator);
550         var enumerator = windowManagerInterface.getEnumerator(null);
551
552         var w; // close all other windows
553         while ( w = enumerator.getNext() ) {
554             if (w != window) {
555                 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
556             }
557         }
558
559         this.controller.render('ws_deck');
560
561         this.session.close();
562         this.data.menu_perms = false;
563         this.data.current_hotkeyset = undefined;
564         this.data.enable_debug = this.data.debug_client;
565         this.data.session = undefined;
566         this.data.stash('menu_perms');
567         this.data.stash('current_hotkeyset');
568         this.data.stash('enable_debug');
569         this.data.stash('session');
570
571         /* FIXME - need some locking or object destruction for the async tests */
572         /* this.test_server( this.controller.view.server_prompt.value ); */
573
574         if (typeof this.on_logoff == 'function') {
575             this.error.sdump('D_AUTH','auth.controller.on_logoff()\n');
576             this.on_logoff();
577         }
578         
579     },
580     'close' : function() { 
581     
582         this.error.sdump('D_AUTH','close' + this.w + '\n');
583
584         var confirm_string = document.getElementById('authStrings').getString('staff.auth.controller.confirm_close');
585
586         this.data.stash_retrieve();
587         if (typeof this.data.unsaved_data != 'undefined') {
588             if (this.data.unsaved_data > 0) {
589                 confirm_string = document.getElementById('offlineStrings').getString('menu.shutdown.unsaved_data_warning');
590             }
591         }
592  
593         if (window.confirm(confirm_string)) {
594             this.data.unsaved_data = 0;
595             this.data.stash('unsaved_data');
596             this.logoff();
597             this.w.close(); /* Probably won't go any further */
598
599             if (typeof this.on_close == 'function') {
600                 this.error.sdump('D_AUTH','auth.controller.on_close()\n');
601                 this.on_close();
602             }
603         }
604         
605     }
606 }
607
608 dump('exiting auth/controller.js\n');