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