cleaned fines display some. persisting font size via cookie and user pref
[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                 // MVC
19                 JSAN.use('util.controller'); obj.controller = new util.controller();
20                 obj.controller.init(
21                         {
22                                 'control_map' : {
23                                         'cmd_login' : [
24                                                 ['command'],
25                                                 function() {
26                                                         obj.login();
27                                                 }
28                                         ],
29                                         'cmd_override' : [
30                                                 ['command'],
31                                                 function() {
32                                                         obj.override();
33                                                 }
34                                         ],
35                                         'cmd_logoff' : [
36                                                 ['command'],
37                                                 function() {
38                                                         obj.logoff()
39                                                 }
40                                         ],
41                                         'cmd_close_window' : [
42                                                 ['command'],
43                                                 function() {
44                                                         obj.close()
45                                                 }
46                                         ],
47
48                                         'server_prompt' : [
49                                                 ['keypress'],
50                                                 handle_keypress
51                                         ],
52                                         'name_prompt' : [
53                                                 ['keypress'],
54                                                 handle_keypress
55                                         ],
56                                         'password_prompt' : [
57                                                 ['keypress'],
58                                                 handle_keypress
59                                         ],
60                                         'submit_button' : [
61                                                 ['render'],
62                                                 function(e) { return function() {} }
63                                         ],
64                                         'progress_bar' : [
65                                                 ['render'],
66                                                 function(e) { return function() {} }
67                                         ],
68                                         'status' : [
69                                                 ['render'],
70                                                 function(e) { return function() {
71                                                 } }
72                                         ],
73                                         'ws_deck' : [
74                                                 ['render'],
75                                                 function(e) { return function() {
76                                                 } }
77                                         ],
78                                         'menu_spot' : [
79                                                 ['render'],
80                                                 function(e) { return function() {
81                                                 } }
82                                         ],
83
84                                 }
85                         }
86                 );
87                 obj.controller.view.name_prompt.focus();
88
89                 function handle_keypress(ev) {
90                         try {
91                                 if (ev.keyCode && ev.keyCode == 13) {
92                                         switch(this) {
93                                                 case obj.controller.view.server_prompt:
94                                                         ev.preventDefault();
95                                                         obj.controller.view.name_prompt.focus(); obj.controller.view.name_prompt.select();
96                                                 break;
97                                                 case obj.controller.view.name_prompt:
98                                                         ev.preventDefault();
99                                                         obj.controller.view.password_prompt.focus(); obj.controller.view.password_prompt.select();
100                                                 break;
101                                                 case obj.controller.view.password_prompt:
102                                                         ev.preventDefault();
103                                                         obj.controller.view.submit_button.focus(); 
104                                                         obj.login();
105                                                 break;
106                                                 default: break;
107                                         }
108                                 }
109                         } catch(E) {
110                                 alert(E);
111                         }
112                 }
113
114                 // This talks to our ILS
115                 JSAN.use('auth.session');
116                 obj.session = new auth.session(obj.controller.view);
117
118                 obj.controller.render();
119
120                 if (typeof this.on_init == 'function') {
121                         this.error.sdump('D_AUTH','auth.controller.on_init()\n');
122                         this.on_init();
123                 }
124         },
125
126         'login' : function() { 
127
128                 var obj = this;
129
130                 this.error.sdump('D_AUTH','login with ' 
131                         + this.controller.view.name_prompt.value + ' and ' 
132                         + this.controller.view.password_prompt.value + ' at ' 
133                         + this.controller.view.server_prompt.value + '\n'
134                 ); 
135                 this.controller.view.server_prompt.disabled = true;
136                 this.controller.view.name_prompt.disabled = true;
137                 this.controller.view.password_prompt.disabled = true;
138                 this.controller.view.submit_button.disabled = true;
139                 XML_HTTP_SERVER = this.controller.view.server_prompt.value;
140
141                 try {
142
143                         if (typeof this.on_login == 'function') {
144                                 this.error.sdump('D_AUTH','auth.controller.session.on_init = ' +
145                                         'auth.controller.on_login\n');
146                                 this.session.on_init = this.on_login;
147                                 this.session.on_error = function() { obj.logoff(); };
148                         }
149                         
150                         this.session.init();
151
152                 } catch(E) {
153                         var error = '!! ' + E + '\n';
154                         this.error.sdump('D_ERROR',error); 
155                         alert(error);
156                         this.logoff();
157                         if (E == 'open-ils.auth.authenticate.init returned false\n') {
158                                 this.controller.view.server_prompt.focus();
159                                 this.controller.view.server_prompt.select();
160                         }
161
162                         if (typeof this.on_login_error == 'function') {
163                                 this.error.sdump('D_AUTH','auth.controller.on_login_error()\n');
164                                 this.on_login_error(E);
165                         }
166                 }
167
168         },
169
170         'logoff' : function() { 
171         
172                 this.error.sdump('D_AUTH','logoff' + this.w + '\n'); 
173                 this.controller.view.progress_bar.value = 0; 
174                 this.controller.view.progress_bar.setAttribute('real','0.0');
175                 this.controller.view.submit_button.disabled = false;
176                 this.controller.view.password_prompt.disabled = false;
177                 this.controller.view.password_prompt.value = '';
178                 this.controller.view.name_prompt.disabled = false;
179                 this.controller.view.name_prompt.focus(); 
180                 this.controller.view.name_prompt.select();
181                 this.controller.view.server_prompt.disabled = false;
182
183                 var windowManager = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService();
184                 var windowManagerInterface = windowManager.QueryInterface(Components.interfaces.nsIWindowMediator);
185                 var enumerator = windowManagerInterface.getEnumerator(null);
186
187                 var w; // close all other windows
188                 while ( w = enumerator.getNext() ) {
189                         if (w != window) w.close();
190                 }
191
192                 this.session.close();
193
194                 if (typeof this.on_logoff == 'function') {
195                         this.error.sdump('D_AUTH','auth.controller.on_logoff()\n');
196                         this.on_logoff();
197                 }
198                 
199         },
200         'close' : function() { 
201         
202                 this.error.sdump('D_AUTH','close' + this.w + '\n');
203                 this.logoff();
204                 //Basically, we want to close all the windows for this application (and in case we're running this as
205                 //a firefox extension, we don't want to merely shutdown mozilla).  I'll probably create an XPCOM for
206                 //tracking the windows.
207                 //for (var w in this.G.window.appshell_list) {
208                 //      this.G.window.appshell_list[w].close();
209                 //}
210                 this.w.close(); /* Probably won't go any further */
211
212                 if (typeof this.on_close == 'function') {
213                         this.error.sdump('D_AUTH','auth.controller.on_close()\n');
214                         this.on_close();
215                 }
216                 
217         }
218 }
219
220 dump('exiting auth/controller.js\n');