fedac660d7f197ab26b68ec4525c3059b0f0b7e2
[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 (mw,G) {
5
6         this.mw = mw; this.G = G;
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 mw = obj.mw;
17                 var G = obj.G;
18
19                 // This talks to our ILS
20                 JSAN.use('auth.session');
21                 obj.session = new auth.session(obj,mw,G);
22
23                 // Attach this object to the XUL through event listeners
24
25                 var cmd_login = mw.document.getElementById('cmd_login');
26                         if (cmd_login) 
27                                 cmd_login.addEventListener('command',function () { obj.login(); },false);
28
29                 var cmd_logoff = mw.document.getElementById('cmd_logoff');
30                         if (cmd_logoff) 
31                                 cmd_logoff.addEventListener('command',function () { obj.logoff(); },false);
32
33                 var cmd_close_window = mw.document.getElementById('cmd_close_window');
34                         if (cmd_close_window) 
35                                 cmd_close_window.addEventListener('command',function () { obj.close(); },false);
36
37                 obj.view.name_prompt = mw.document.getElementById('name_prompt');
38                         if (obj.view.name_prompt) {
39                                 obj.view.name_prompt.addEventListener('keypress',handle_keypress,false);
40                                 obj.view.name_prompt.focus();
41                         }
42
43                 obj.view.password_prompt = mw.document.getElementById('password_prompt');
44                         if (obj.view.password_prompt)
45                                 obj.view.password_prompt.addEventListener('keypress',handle_keypress,false);
46         
47                 obj.view.submit_button = mw.document.getElementById('submit_button');
48         
49                 obj.view.progress_bar = mw.document.getElementById('auth_meter');
50
51                 function handle_keypress(ev) {
52                         if (ev.keyCode && ev.keyCode == 13) {
53                                 switch(this) {
54                                         case obj.name_prompt:
55                                                 ev.preventDefault();
56                                                 obj.view.password_prompt.focus(); obj.view.password_prompt.select();
57                                         break;
58                                         case obj.view.password_prompt:
59                                                 ev.preventDefault();
60                                                 obj.view.submit_button.focus(); 
61                                                 obj.login();
62                                         break;
63                                         default: break;
64                                 }
65                         }
66                 }
67
68                 if (typeof this.on_init == 'function') {
69                         this.G.error.sdump('D_AUTH','auth.controller.on_init()\n');
70                         this.on_init();
71                 }
72         },
73
74         'view' : {},
75
76         'login' : function() { 
77
78                 this.G.error.sdump('D_AUTH','login with ' + this.view.name_prompt.value + ' and ' + this.view.password_prompt.value + '\n'); 
79                 this.view.name_prompt.disabled = true;
80                 this.view.password_prompt.disabled = true;
81                 this.view.submit_button.disabled = true;
82
83                 try {
84
85                         if (typeof this.on_login == 'function') {
86                                 this.G.error.sdump('D_AUTH','auth.controller.session.on_init = ' +
87                                         'auth.controller.on_login\n');
88                                 this.session.on_init = this.on_login;
89                         }
90                         
91                         this.session.init();
92
93                 } catch(E) {
94                         var error = '!! ' + E + '\n';
95                         this.G.error.sdump('D_ERROR',error); 
96                         alert(error);
97                         this.logoff();
98
99                         if (typeof this.on_login_error == 'function') {
100                                 this.G.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.G.error.sdump('D_AUTH','logoff' + this.mw + '\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.G.error.sdump('D_AUTH','auth.controller.on_logoff()\n');
120                         this.on_logoff();
121                 }
122                 
123         },
124         'close' : function() { 
125         
126                 this.G.error.sdump('D_AUTH','close' + this.mw + '\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.mw.close(); /* Probably won't go any further */
132
133                 if (typeof this.on_close == 'function') {
134                         this.G.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');