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