some other tweaks, in the same vein of passing in only as little information as neces...
[Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / evergreen / auth / controller.js
1 dump('entering auth/controller.js\n');
2
3 if (typeof auth == 'undefined') auth = {};
4 auth.controller = function (w) {
5         JSAN.use('util.error'); this.error = new util.error();
6         this.w = w;
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                 // This talks to our ILS
19                 JSAN.use('auth.session');
20                 obj.session = new auth.session(obj.view);
21
22                 // Attach this object to the XUL through event listeners
23
24                 var cmd_login = w.document.getElementById('cmd_login');
25                         if (cmd_login) 
26                                 cmd_login.addEventListener('command',function () { obj.login(); },false);
27
28                 var cmd_logoff = w.document.getElementById('cmd_logoff');
29                         if (cmd_logoff) 
30                                 cmd_logoff.addEventListener('command',function () { obj.logoff(); },false);
31
32                 var cmd_close_window = w.document.getElementById('cmd_close_window');
33                         if (cmd_close_window) 
34                                 cmd_close_window.addEventListener('command',function () { obj.close(); },false);
35
36                 obj.view.name_prompt = w.document.getElementById('name_prompt');
37                         if (obj.view.name_prompt) {
38                                 obj.view.name_prompt.addEventListener('keypress',handle_keypress,false);
39                                 obj.view.name_prompt.focus();
40                         }
41
42                 obj.view.password_prompt = w.document.getElementById('password_prompt');
43                         if (obj.view.password_prompt)
44                                 obj.view.password_prompt.addEventListener('keypress',handle_keypress,false);
45         
46                 obj.view.submit_button = w.document.getElementById('submit_button');
47         
48                 obj.view.progress_bar = w.document.getElementById('auth_meter');
49
50                 function handle_keypress(ev) {
51                         if (ev.keyCode && ev.keyCode == 13) {
52                                 switch(this) {
53                                         case obj.name_prompt:
54                                                 ev.preventDefault();
55                                                 obj.view.password_prompt.focus(); obj.view.password_prompt.select();
56                                         break;
57                                         case obj.view.password_prompt:
58                                                 ev.preventDefault();
59                                                 obj.view.submit_button.focus(); 
60                                                 obj.login();
61                                         break;
62                                         default: break;
63                                 }
64                         }
65                 }
66
67                 if (typeof this.on_init == 'function') {
68                         this.error.sdump('D_AUTH','auth.controller.on_init()\n');
69                         this.on_init();
70                 }
71         },
72
73         'view' : {},
74
75         'login' : function() { 
76
77                 this.error.sdump('D_AUTH','login with ' + this.view.name_prompt.value + ' and ' + this.view.password_prompt.value + '\n'); 
78                 this.view.name_prompt.disabled = true;
79                 this.view.password_prompt.disabled = true;
80                 this.view.submit_button.disabled = true;
81
82                 try {
83
84                         if (typeof this.on_login == 'function') {
85                                 this.error.sdump('D_AUTH','auth.controller.session.on_init = ' +
86                                         'auth.controller.on_login\n');
87                                 this.session.on_init = this.on_login;
88                                 this.session.on_error = this.on_logoff;
89                         }
90                         
91                         this.session.init();
92
93                 } catch(E) {
94                         var error = '!! ' + E + '\n';
95                         this.error.sdump('D_ERROR',error); 
96                         alert(error);
97                         this.logoff();
98
99                         if (typeof this.on_login_error == 'function') {
100                                 this.error.sdump('D_AUTH','auth.controller.on_login_error()\n');
101                                 this.on_login_error(E);
102                         }
103                 }
104
105         },
106         'logoff' : function() { 
107         
108                 this.error.sdump('D_AUTH','logoff' + this.w + '\n'); 
109                 this.view.progress_bar.value = 0; this.view.progress_bar.setAttribute('real','0.0');
110                 this.view.submit_button.disabled = false;
111                 this.view.password_prompt.disabled = false;
112                 this.view.password_prompt.value = '';
113                 this.view.name_prompt.disabled = false;
114                 this.view.name_prompt.focus(); this.view.name_prompt.select();
115
116                 this.session.close();
117
118                 if (typeof this.on_logoff == 'function') {
119                         this.error.sdump('D_AUTH','auth.controller.on_logoff()\n');
120                         this.on_logoff();
121                 }
122                 
123         },
124         'close' : function() { 
125         
126                 this.error.sdump('D_AUTH','close' + this.w + '\n');
127                 this.logoff();
128                 for (var w in this.G.window.appshell_list) {
129                         this.G.window.appshell_list[w].close();
130                 }
131                 this.w.close(); /* Probably won't go any further */
132
133                 if (typeof this.on_close == 'function') {
134                         this.error.sdump('D_AUTH','auth.controller.on_close()\n');
135                         this.on_close();
136                 }
137                 
138         }
139 }
140
141 dump('exiting auth/controller.js\n');