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