]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/auth/controller.js
standalone and debugging options
[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                 JSAN.use('OpenILS.data');
19                 obj.data = new OpenILS.data(); obj.data.init({'via':'stash'});
20
21                 // MVC
22                 JSAN.use('util.controller'); obj.controller = new util.controller();
23                 obj.controller.init(
24                         {
25                                 'control_map' : {
26                                         'cmd_login' : [
27                                                 ['command'],
28                                                 function() {
29                                                         obj.login();
30                                                 }
31                                         ],
32                                         'cmd_standalone' : [
33                                                 ['command'],
34                                                 function() {
35                                                         obj.standalone();
36                                                 }
37                                         ],
38                                         'cmd_clear_cache' : [
39                                                 ['command'],
40                                                 function() {
41                                                         obj.debug('clear_cache');
42                                                 }
43                                         ],
44                                         'cmd_js_console' : [
45                                                 ['command'],
46                                                 function() {
47                                                         obj.debug('js_console');
48                                                 }
49                                         ],
50                                         'cmd_override' : [
51                                                 ['command'],
52                                                 function() {
53                                                         obj.override();
54                                                 }
55                                         ],
56                                         'cmd_logoff' : [
57                                                 ['command'],
58                                                 function() {
59                                                         obj.logoff()
60                                                 }
61                                         ],
62                                         'cmd_close_window' : [
63                                                 ['command'],
64                                                 function() {
65                                                         obj.close()
66                                                 }
67                                         ],
68
69                                         'server_prompt' : [
70                                                 ['keypress'],
71                                                 handle_keypress
72                                         ],
73                                         'name_prompt' : [
74                                                 ['keypress'],
75                                                 handle_keypress
76                                         ],
77                                         'password_prompt' : [
78                                                 ['keypress'],
79                                                 handle_keypress
80                                         ],
81                                         'submit_button' : [
82                                                 ['render'],
83                                                 function(e) { return function() {} }
84                                         ],
85                                         'progress_bar' : [
86                                                 ['render'],
87                                                 function(e) { return function() {} }
88                                         ],
89                                         'status' : [
90                                                 ['render'],
91                                                 function(e) { return function() {
92                                                 } }
93                                         ],
94                                         'ws_deck' : [
95                                                 ['render'],
96                                                 function(e) { return function() {
97                                                         try {
98                                                                 JSAN.use('util.widgets'); util.widgets.remove_children(e);
99                                                                 var x = document.createElement('description');
100                                                                 e.appendChild(x);
101                                                                 if (obj.data.ws_info 
102                                                                         && obj.data.ws_info[ obj.controller.view.server_prompt.value ]) {
103                                                                         var ws = obj.data.ws_info[ obj.controller.view.server_prompt.value ];
104                                                                         x.appendChild(
105                                                                                 document.createTextNode(
106                                                                                         ws.name + ' @  ' + ws.lib_shortname
107                                                                                 )
108                                                                         );
109                                                                 } else {
110                                                                         x.appendChild(
111                                                                                 document.createTextNode(
112                                                                                         'Not yet configured for the specified server.'
113                                                                                 )
114                                                                         );
115                                                                 }
116                                                         } catch(E) {
117                                                                 alert(E);
118                                                         }
119                                                 } }
120                                         ],
121                                         'menu_spot' : [
122                                                 ['render'],
123                                                 function(e) { return function() {
124                                                 } }
125                                         ],
126
127                                 }
128                         }
129                 );
130                 obj.controller.view.name_prompt.focus();
131
132                 function handle_keypress(ev) {
133                         try {
134                                 if (ev.keyCode && ev.keyCode == 13) {
135                                         switch(this) {
136                                                 case obj.controller.view.server_prompt:
137                                                         ev.preventDefault();
138                                                         obj.controller.view.name_prompt.focus(); obj.controller.view.name_prompt.select();
139                                                 break;
140                                                 case obj.controller.view.name_prompt:
141                                                         ev.preventDefault();
142                                                         obj.controller.view.password_prompt.focus(); obj.controller.view.password_prompt.select();
143                                                 break;
144                                                 case obj.controller.view.password_prompt:
145                                                         ev.preventDefault();
146                                                         obj.controller.view.submit_button.focus(); 
147                                                         obj.login();
148                                                 break;
149                                                 default: break;
150                                         }
151                                 }
152                         } catch(E) {
153                                 alert(E);
154                         }
155                 }
156
157                 obj.controller.view.server_prompt.addEventListener(
158                         'change',
159                         function (ev) { 
160                                 obj.controller.render('ws_deck'); 
161                                 obj.test_server(ev.target.value);
162                         },
163                         false
164                 );
165
166                 // This talks to our ILS
167                 JSAN.use('auth.session');
168                 obj.session = new auth.session(obj.controller.view);
169
170                 obj.controller.render();
171                 obj.test_server( obj.controller.view.server_prompt.value );
172
173                 if (typeof this.on_init == 'function') {
174                         this.error.sdump('D_AUTH','auth.controller.on_init()\n');
175                         this.on_init();
176                 }
177         },
178
179         'test_server' : function(url) {
180                 var obj = this;
181                 obj.controller.view.submit_button.disabled = true;
182                 obj.controller.view.server_prompt.disabled = true;
183                 var s = document.getElementById('status');
184                 s.setAttribute('value','Testing hostname...');
185                 s.setAttribute('style','color: orange;');
186                 document.getElementById('version').value = '';
187                 if (!url) {
188                         s.setAttribute('value','Please enter a server hostname.');
189                         s.setAttribute('style','color: red;');
190                         obj.controller.view.server_prompt.disabled = false;
191                         return;
192                 }
193                 try {
194                         if ( ! url.match(/^http:\/\//) ) url = 'http://' + url;
195                         var x = new XMLHttpRequest();
196                         dump('server url = ' + url + '\n');
197                         x.open("GET",url,true);
198                         x.onreadystatechange = function() {
199                                 try {
200                                         if (x.readyState != 4) return;
201                                         s.setAttribute('value',x.status + ' : ' + x.statusText);
202                                         if (x.status == 200) {
203                                                 s.setAttribute('style','color: green;');
204                                         } else {
205                                                 s.setAttribute('style','color: red;');
206                                         }
207                                         obj.test_version(url);
208                                 } catch(E) {
209                                         obj.controller.view.server_prompt.disabled = false;
210                                         s.setAttribute('value','There was an error testing this hostname.');
211                                         s.setAttribute('style','color: red;');
212                                         obj.error.sdump('D_ERROR',E);
213                                 }
214                         }
215                         x.send(null);
216                 } catch(E) {
217                         s.setAttribute('value','There was an error testing this hostname.');
218                         s.setAttribute('style','color: brown;');
219                         obj.error.sdump('D_ERROR',E);
220                         obj.controller.view.server_prompt.disabled = false;
221                 }
222         },
223
224         'test_version' : function(url) {
225                 var obj = this;
226                 var s = document.getElementById('version');
227                 s.setAttribute('value','Testing version...');
228                 s.setAttribute('style','color: orange;');
229                 try {
230                         var x = new XMLHttpRequest();
231                         var url2 = url + '/xul/server/';
232                         dump('version url = ' + url2 + '\n');
233                         x.open("GET",url2,true);
234                         x.onreadystatechange = function() {
235                                 try {
236                                         if (x.readyState != 4) return;
237                                         s.setAttribute('value',x.status + ' : ' + x.statusText);
238                                         if (x.status == 200) {
239                                                 s.setAttribute('style','color: green;');
240                                                 obj.controller.view.submit_button.disabled = false;
241                                         } else {
242                                                 s.setAttribute('style','color: red;');
243                                                 obj.test_upgrade_instructions(url);
244                                         }
245                                         obj.controller.view.server_prompt.disabled = false;
246                                 } catch(E) {
247                                         s.setAttribute('value','There was an error checking version support.');
248                                         s.setAttribute('style','color: red;');
249                                         obj.error.sdump('D_ERROR',E);
250                                         obj.controller.view.server_prompt.disabled = false;
251                                 }
252                         }
253                         x.send(null);
254                 } catch(E) {
255                         s.setAttribute('value','There was an error checking version support.');
256                         s.setAttribute('style','color: brown;');
257                         obj.error.sdump('D_ERROR',E);
258                         obj.controller.view.server_prompt.disabled = false;
259                 }
260         },
261
262         'test_upgrade_instructions' : function(url) {
263                 var obj = this;
264                 try {
265                         var x = new XMLHttpRequest();
266                         var url2 = url + '/xul/versions.html';
267                         dump('upgrade url = ' + url2 + '\n');
268                         x.open("GET",url2,true);
269                         x.onreadystatechange = function() {
270                                 try {
271                                         if (x.readyState != 4) return;
272                                         if (x.status == 200) {
273                                                 window.open('data:text/html,'+window.escape(x.responseText),'upgrade','chrome,resizable,modal,centered');
274                                         } else {
275                                                 alert('This server does not support your version of the staff client.  Please check with your system administrator.');
276                                         }
277                                         obj.controller.view.server_prompt.disabled = false;
278                                 } catch(E) {
279                                         obj.error.sdump('D_ERROR',E);
280                                         obj.controller.view.server_prompt.disabled = false;
281                                 }
282                         }
283                         x.send(null);
284                 } catch(E) {
285                         obj.error.sdump('D_ERROR',E);
286                         obj.controller.view.server_prompt.disabled = false;
287                 }
288         },
289
290         'login' : function() { 
291
292                 var obj = this;
293
294                 this.error.sdump('D_AUTH','login with ' 
295                         + this.controller.view.name_prompt.value + ' and ' 
296                         + this.controller.view.password_prompt.value + ' at ' 
297                         + this.controller.view.server_prompt.value + '\n'
298                 ); 
299                 this.controller.view.server_prompt.disabled = true;
300                 this.controller.view.name_prompt.disabled = true;
301                 this.controller.view.password_prompt.disabled = true;
302                 this.controller.view.submit_button.disabled = true;
303                 XML_HTTP_SERVER = this.controller.view.server_prompt.value;
304
305                 try {
306
307                         if (typeof this.on_login == 'function') {
308                                 this.error.sdump('D_AUTH','auth.controller.session.on_init = ' +
309                                         'auth.controller.on_login\n');
310                                 this.session.on_init = this.on_login;
311                                 this.session.on_error = function() { obj.logoff(); };
312                         }
313                         
314                         this.session.init();
315
316                 } catch(E) {
317                         var error = '!! ' + E + '\n';
318                         this.error.sdump('D_ERROR',error); 
319                         alert(error);
320                         this.logoff();
321                         if (E == 'open-ils.auth.authenticate.init returned false\n') {
322                                 this.controller.view.server_prompt.focus();
323                                 this.controller.view.server_prompt.select();
324                         }
325
326                         if (typeof this.on_login_error == 'function') {
327                                 this.error.sdump('D_AUTH','auth.controller.on_login_error()\n');
328                                 this.on_login_error(E);
329                         }
330                 }
331
332         },
333
334         'standalone' : function() {
335                 var obj = this;
336                 try {
337                         if (typeof this.on_standalone == 'function') {
338                                 obj.on_standalone();
339                         }
340                 } catch(E) {
341                         var error = '!! ' + E + '\n';
342                         obj.error.sdump('D_ERROR',error); 
343                         alert(error);
344                 }
345         },
346
347         'debug' : function(action) {
348                 var obj = this;
349                 try {
350                         if (typeof this.on_debug == 'function') {
351                                 obj.on_debug(action);
352                         }
353                 } catch(E) {
354                         var error = '!! ' + E + '\n';
355                         obj.error.sdump('D_ERROR',error);
356                         alert(error);
357                 }
358         },
359
360         'logoff' : function() { 
361         
362                 this.error.sdump('D_AUTH','logoff' + this.w + '\n'); 
363                 this.controller.view.progress_bar.value = 0; 
364                 this.controller.view.progress_bar.setAttribute('real','0.0');
365                 this.controller.view.submit_button.disabled = false;
366                 this.controller.view.password_prompt.disabled = false;
367                 this.controller.view.password_prompt.value = '';
368                 this.controller.view.name_prompt.disabled = false;
369                 this.controller.view.name_prompt.focus(); 
370                 this.controller.view.name_prompt.select();
371                 this.controller.view.server_prompt.disabled = false;
372
373                 var windowManager = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService();
374                 var windowManagerInterface = windowManager.QueryInterface(Components.interfaces.nsIWindowMediator);
375                 var enumerator = windowManagerInterface.getEnumerator(null);
376
377                 var w; // close all other windows
378                 while ( w = enumerator.getNext() ) {
379                         if (w != window) w.close();
380                 }
381
382                 this.controller.render('ws_deck');
383
384                 this.session.close();
385
386                 /* FIXME - need some locking or object destruction for the async tests */
387                 /* this.test_server( this.controller.view.server_prompt.value ); */
388
389                 if (typeof this.on_logoff == 'function') {
390                         this.error.sdump('D_AUTH','auth.controller.on_logoff()\n');
391                         this.on_logoff();
392                 }
393                 
394         },
395         'close' : function() { 
396         
397                 this.error.sdump('D_AUTH','close' + this.w + '\n');
398                 this.logoff();
399                 //Basically, we want to close all the windows for this application (and in case we're running this as
400                 //a firefox extension, we don't want to merely shutdown mozilla).  I'll probably create an XPCOM for
401                 //tracking the windows.
402                 //for (var w in this.G.window.appshell_list) {
403                 //      this.G.window.appshell_list[w].close();
404                 //}
405                 this.w.close(); /* Probably won't go any further */
406
407                 if (typeof this.on_close == 'function') {
408                         this.error.sdump('D_AUTH','auth.controller.on_close()\n');
409                         this.on_close();
410                 }
411                 
412         }
413 }
414
415 dump('exiting auth/controller.js\n');