]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/evergreen/auth/controller.js
prompt for server at startup
[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 (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_logoff' : [
30                                                 ['command'],
31                                                 function() {
32                                                         obj.logoff()
33                                                 }
34                                         ],
35                                         'cmd_close_window' : [
36                                                 ['command'],
37                                                 function() {
38                                                         obj.close()
39                                                 }
40                                         ],
41                                         'server_prompt' : [
42                                                 ['keypress'],
43                                                 handle_keypress
44                                         ],
45                                         'name_prompt' : [
46                                                 ['keypress'],
47                                                 handle_keypress
48                                         ],
49                                         'password_prompt' : [
50                                                 ['keypress'],
51                                                 handle_keypress
52                                         ],
53                                         'submit_button' : [
54                                                 ['render'],
55                                                 function(e) { return function() {} }
56                                         ],
57                                         'progress_bar' : [
58                                                 ['render'],
59                                                 function(e) { return function() {} }
60                                         ]
61                                 }
62                         }
63                 );
64                 obj.controller.view.name_prompt.focus();
65
66                 function handle_keypress(ev) {
67                         if (ev.keyCode && ev.keyCode == 13) {
68                                 switch(this) {
69                                         case obj.controller.view.server_prompt:
70                                                 ev.preventDefault();
71                                                 obj.controller.view.name_prompt.focus(); obj.controller.view.name_prompt.select();
72                                         break;
73                                         case obj.controller.view.name_prompt:
74                                                 ev.preventDefault();
75                                                 obj.controller.view.password_prompt.focus(); obj.controller.view.password_prompt.select();
76                                         break;
77                                         case obj.controller.view.password_prompt:
78                                                 ev.preventDefault();
79                                                 obj.controller.view.submit_button.focus(); 
80                                                 obj.login();
81                                         break;
82                                         default: break;
83                                 }
84                         }
85                 }
86
87                 // This talks to our ILS
88                 JSAN.use('auth.session');
89                 obj.session = new auth.session(obj.controller.view);
90
91                 if (typeof this.on_init == 'function') {
92                         this.error.sdump('D_AUTH','auth.controller.on_init()\n');
93                         this.on_init();
94                 }
95         },
96
97         'login' : function() { 
98
99                 var obj = this;
100
101                 this.error.sdump('D_AUTH','login with ' 
102                         + this.controller.view.name_prompt.value + ' and ' 
103                         + this.controller.view.password_prompt.value + ' at ' 
104                         + this.controller.view.server_prompt.value + '\n'
105                 ); 
106                 this.controller.view.server_prompt.disabled = true;
107                 this.controller.view.name_prompt.disabled = true;
108                 this.controller.view.password_prompt.disabled = true;
109                 this.controller.view.submit_button.disabled = true;
110                 XML_HTTP_SERVER = this.controller.view.server_prompt.value;
111
112                 try {
113
114                         if (typeof this.on_login == 'function') {
115                                 this.error.sdump('D_AUTH','auth.controller.session.on_init = ' +
116                                         'auth.controller.on_login\n');
117                                 this.session.on_init = this.on_login;
118                                 this.session.on_error = function() { obj.logoff(); };
119                         }
120                         
121                         this.session.init();
122
123                 } catch(E) {
124                         var error = '!! ' + E + '\n';
125                         this.error.sdump('D_ERROR',error); 
126                         alert(error);
127                         this.logoff();
128                         if (E == 'open-ils.auth.authenticate.init returned false\n') {
129                                 this.controller.view.server_prompt.focus();
130                                 this.controller.view.server_prompt.select();
131                         }
132
133                         if (typeof this.on_login_error == 'function') {
134                                 this.error.sdump('D_AUTH','auth.controller.on_login_error()\n');
135                                 this.on_login_error(E);
136                         }
137                 }
138
139         },
140         'logoff' : function() { 
141         
142                 this.error.sdump('D_AUTH','logoff' + this.w + '\n'); 
143                 this.controller.view.progress_bar.value = 0; 
144                 this.controller.view.progress_bar.setAttribute('real','0.0');
145                 this.controller.view.submit_button.disabled = false;
146                 this.controller.view.password_prompt.disabled = false;
147                 this.controller.view.password_prompt.value = '';
148                 this.controller.view.name_prompt.disabled = false;
149                 this.controller.view.name_prompt.focus(); 
150                 this.controller.view.name_prompt.select();
151                 this.controller.view.server_prompt.disabled = false;
152
153                 this.session.close();
154
155                 if (typeof this.on_logoff == 'function') {
156                         this.error.sdump('D_AUTH','auth.controller.on_logoff()\n');
157                         this.on_logoff();
158                 }
159                 
160         },
161         'close' : function() { 
162         
163                 this.error.sdump('D_AUTH','close' + this.w + '\n');
164                 this.logoff();
165                 //Basically, we want to close all the windows for this application (and in case we're running this as
166                 //a firefox extension, we don't want to merely shutdown mozilla).  I'll probably create an XPCOM for
167                 //tracking the windows.
168                 //for (var w in this.G.window.appshell_list) {
169                 //      this.G.window.appshell_list[w].close();
170                 //}
171                 this.w.close(); /* Probably won't go any further */
172
173                 if (typeof this.on_close == 'function') {
174                         this.error.sdump('D_AUTH','auth.controller.on_close()\n');
175                         this.on_close();
176                 }
177                 
178         }
179 }
180
181 dump('exiting auth/controller.js\n');