]> git.evergreen-ils.org Git - Evergreen.git/blob - Evergreen/staff_client/chrome/content/evergreen/auth/auth.js
most of what we need for the first screen
[Evergreen.git] / Evergreen / staff_client / chrome / content / evergreen / auth / auth.js
1 var G = {}; // the master Global variable
2 G['main_window'] = self;
3 G['win_list'] = new Array();
4 G['window_name_increment'] = 0;
5 G['auth_ses'] = '';
6 G['user_ou'] = '';
7 G['main_test_variable'] = 'Hello World';
8 G['org_tree'] = '';
9 G['my_orgs'] = [];
10 G['my_orgs_hash'] = {};
11 G['fieldmap'] = '';
12 G['patrons'] = {};
13
14 G['ap_list'] = []; // actor::profile
15 G['ap_hash'] = {};
16 G['cit_list'] = []; // config::identification_type
17 G['cit_hash'] = {};
18 G['cst_list'] = []; // config::standing
19 G['cst_hash'] = {};
20 G['acpl_list'] = []; // asset::copy_location
21 G['acpl_hash'] = {}; G['acpl_my_orgs'] = []; G['acpl_my_orgs_hash'] = {};
22 G['aout_list'] = []; // actor::org_unit_type
23 G['aout_hash'] = {};
24 G['ccs_list'] = []; // config::copy_status
25 G['ccs_hash'] = {};
26 G['asc_list'] = []; // asset::stat_cat
27 G['actsc_list'] = []; // actor::stat_cat
28 G['actsc_hash']; // actor::stat_cat
29
30 var mw = G['main_window'];
31 var auth_meter_per = 10;
32
33 function OpenILS_init() {
34         my_init();
35 }
36
37 function my_init() {
38         dump('TESTING: auth.js: ' + mw.G['main_test_variable'] + '\n');
39         var np = document.getElementById('name_prompt');
40         np.addEventListener("keypress",handle_keypress,false);
41         np.focus();
42         var pp = document.getElementById('password_prompt');
43         pp.addEventListener("keypress",handle_keypress,false);
44         self.addEventListener("unload",nice_shutdown,false);
45         G['sound'] = xp_sound_init(); 
46         //G.sound.beep();
47         snd_logon();
48 }
49
50 function handle_keypress(ev) {
51         if (ev.keyCode && ev.keyCode == 13) {
52                 switch(this) {
53                         case document.getElementById('name_prompt') :
54                                 ev.preventDefault();
55                                 var pp = document.getElementById('password_prompt');
56                                 pp.focus(); pp.select();
57                         break;
58                         case document.getElementById('password_prompt') :
59                                 ev.preventDefault();
60                                 var sb = document.getElementById('submit_button');
61                                 sb.focus();
62                                 authenticate();
63                         break;
64                         default:
65                         break;
66                 }
67         }
68 }
69
70 function disable_login_prompts() {
71         disable_widgets('password_prompt','name_prompt','submit_button');
72         G.sound.beep();
73 }
74
75 function enable_login_prompts() {
76         enable_widgets('password_prompt','name_prompt','submit_button');
77         document.getElementById('password_prompt').value = '';
78         var np = document.getElementById('name_prompt');
79         np.focus(); np.select();
80         document.getElementById('auth_meter').value = 0;
81         G.sound.beep();
82 }
83
84 function authenticate() {
85         timer_init('cat');
86         var name = document.getElementById('name_prompt').value;
87         if (name.length == 0) { enable_login_prompts(); return; }
88         // Talk to the system and authenticate the user.
89         user_async_request(
90                 'open-ils.auth',
91                 'open-ils.auth.authenticate.init',
92                 [ name ],
93                 auth_init_callback
94         );
95 }
96
97 function auth_init_callback(request) {
98         var auth_init;
99         try {
100                 auth_init = request.getResultObject();
101                 if (!auth_init) { throw('null result'); }
102         } catch(E) {
103                 alert('Login failed on auth_init: ' + js2JSON(E)); enable_login_prompts(); return;
104         }
105
106         dump( 'AUTH_INIT: ' + typeof(auth_init) + ' : ' + auth_init + '\n');
107         var name = document.getElementById('name_prompt').value;
108         var pw = document.getElementById('password_prompt').value;
109
110         user_async_request(
111                 'open-ils.auth',
112                 'open-ils.auth.authenticate.complete',
113                 [ name, hex_md5(auth_init + hex_md5(pw)) ],
114                 auth_ses_callback
115         );
116         document.getElementById('auth_meter').value += auth_meter_per;
117 }
118
119 function auth_ses_callback(request) {
120         var auth_ses;
121         try {
122                 auth_ses = request.getResultObject();
123                 if (!auth_ses) { throw('null result'); }
124                 if (auth_ses == 0) { throw('0 result'); }
125         } catch(E) {
126                 alert('Login failed on auth_ses: ' + js2JSON(E)); enable_login_prompts(); return;
127         }
128         mw.G.auth_ses = [ auth_ses ];
129         dump( 'AUTH_SES: ' + typeof(mw.G['auth_ses'][0]) + ' : ' + mw.G['auth_ses'][0] + '\n');
130
131         user_async_request(
132                 'open-ils.actor',
133                 'open-ils.actor.user.profiles.retrieve',
134                 [],
135                 ap_list_callback
136         );
137         document.getElementById('auth_meter').value += auth_meter_per;
138 }
139
140 function ap_list_callback(request) {
141         var ap_list;
142         try {
143                 ap_list = request.getResultObject();
144                 if (!ap_list) { throw('null result'); }
145                 if (ap_list.length == 0) { throw('zero length result'); }
146         } catch(E) {
147                 alert('Login failed on ap_list: ' + js2JSON(E)); enable_login_prompts(); return;
148         }
149         mw.G.ap_list = ap_list;
150         mw.G.ap_hash = convert_object_list_to_hash( ap_list );
151
152         user_async_request(
153                 'open-ils.actor',
154                 'open-ils.actor.user.ident_types.retrieve',
155                 [],
156                 cit_list_callback
157         );
158         document.getElementById('auth_meter').value += auth_meter_per;
159 }
160
161 function cit_list_callback(request) {
162         var cit_list;
163         try {
164                 cit_list = request.getResultObject();
165                 if (!cit_list) { throw('null result'); }
166                 if (cit_list.length == 0) { throw('zero length result'); }
167         } catch(E) {
168                 alert('Login failed on cit_list: ' + js2JSON(E)); enable_login_prompts(); return;
169         }
170         mw.G.cit_list = cit_list;
171         mw.G.cit_hash = convert_object_list_to_hash( cit_list );
172         
173         user_async_request(
174                 'open-ils.actor',
175                 'open-ils.actor.standings.retrieve',
176                 [],
177                 cst_list_callback
178         );
179
180         document.getElementById('auth_meter').value += auth_meter_per;
181 }
182
183 function cst_list_callback(request) {
184         var cst_list;
185         try {
186                 cst_list = request.getResultObject();
187                 if (!cst_list) { throw('null result'); }
188                 if (cst_list.length == 0) { throw('zero length result'); }
189         } catch(E) {
190                 alert('Login failed on cst_list: ' + js2JSON(E)); enable_login_prompts(); return;
191         }
192         mw.G.cst_list = cst_list;
193         mw.G.cst_hash = convert_object_list_to_hash( cst_list );
194         dump('cst_list = ' + js2JSON(cst_list) + '\n');
195
196         user_async_request(
197                 'open-ils.search',
198                 'open-ils.search.config.copy_location.retrieve.all',
199                 [],
200                 acpl_list_callback
201         );
202         document.getElementById('auth_meter').value += auth_meter_per;
203
204 }
205
206 function acpl_list_callback(request) {
207         var acpl_list;
208         try {
209                 acpl_list = request.getResultObject();
210                 if (!acpl_list) { throw('null result'); }
211                 if (acpl_list.length == 0) { throw('zero length result'); }
212         } catch(E) {
213                 alert('Login failed on acpl_list: ' + js2JSON(E)); enable_login_prompts(); return;
214         }
215         mw.G.acpl_list = acpl_list;
216         mw.G.acpl_hash = convert_object_list_to_hash( acpl_list );
217         dump('acpl_list = ' + js2JSON(acpl_list) + '\n');
218
219         user_async_request(
220                 'open-ils.search',
221                 'open-ils.search.config.copy_status.retrieve.all',
222                 [],
223                 ccs_list_callback
224         );
225         document.getElementById('auth_meter').value += auth_meter_per;
226 }
227
228 function ccs_list_callback(request) {
229         var ccs_list;
230         try {
231                 ccs_list = request.getResultObject();
232                 if (!ccs_list) { throw('null result'); }
233                 if (ccs_list.length == 0) { throw('zero length result'); }
234         } catch(E) {
235                 alert('Login failed on ccs_list: ' + js2JSON(E)); enable_login_prompts(); return;
236         }
237         mw.G.ccs_list = ccs_list;
238         mw.G.ccs_hash = convert_object_list_to_hash( ccs_list );
239         dump('ccs_list = ' + js2JSON(ccs_list) + '\n');
240
241         user_async_request(
242                 'open-ils.search',
243                 'open-ils.search.actor.user.session',
244                 [ mw.G['auth_ses'][0] ],
245                 user_callback
246         );
247         document.getElementById('auth_meter').value += auth_meter_per;
248 }
249
250 function user_callback(request) {
251         var user;
252         var user_ou;
253         try {
254                 user = request.getResultObject();
255                 if (!user) { throw('null result'); }
256                 if (typeof(user) != 'object') { throw('result not an object' + user); }
257         } catch(E) {
258                 alert('Login failed on user: ' + js2JSON(E)); enable_login_prompts(); return;
259         }
260         mw.G.user = user;
261         mw.G.user_ou = user.home_ou();
262         dump("user: " + js2JSON(mw.G['user']) + '\n');
263         dump("user_ou: " + js2JSON(mw.G['user_ou']) + '\n');
264         /*user_async_request(
265                 'open-ils.search',
266                 'open-ils.search.actor.org_tree.retrieve',
267                 [],
268                 org_tree_callback
269         );*/
270         /*user_async_request(
271                 'open-ils.actor',
272                 'open-ils.actor.org_types.retrieve',
273                 [ mw.G.auth_ses[0] ],
274                 org_type_callback
275         );*/
276         org_type_callback();
277         document.getElementById('auth_meter').value += auth_meter_per;
278 }
279
280 function org_type_callback(request) {
281         var aout_list = globalOrgTypes;
282         /*try {
283                 aout_list = request.getResultObject();
284                 if (!aout_list) { throw('null result'); }
285                 if (typeof(aout_list) != 'object') { throw('result not an object' + aout_list); }
286                 if (aout_list.length == 0) { throw('empty aout_list'); }
287         } catch(E) {
288                 alert('Login failed on aout_list: ' + js2JSON(E)); enable_login_prompts(); return;
289         }*/
290         mw.G.aout_list = aout_list;
291         mw.G.aout_hash = convert_object_list_to_hash( aout_list );
292         mw.G.org_tree = globalOrgTree;
293         mw.G.org_tree_hash = convert_object_list_to_hash( flatten_ou_branch( globalOrgTree ) );
294         mw.G.user_ou = find_ou( mw.G.org_tree, mw.G.user_ou );
295
296         user_async_request(
297                 'open-ils.actor',
298                 'open-ils.actor.org_unit.full_path.retrieve',
299                 [ mw.G.auth_ses[0] ],
300                 my_orgs_callback
301         );
302         document.getElementById('auth_meter').value += auth_meter_per;
303
304 }
305
306 function my_orgs_callback(request) {
307         var my_orgs;
308         try {
309                 my_orgs = request.getResultObject();
310                 if (!my_orgs) { throw('null result'); }
311                 if (typeof(my_orgs) != 'object') { throw('result not an object' + my_orgs); }
312                 if (my_orgs.length == 0) { throw('empty my_orgs'); }
313         } catch(E) {
314                 alert('Login failed on my_orgs: ' + js2JSON(E)); enable_login_prompts(); return;
315         }
316
317         mw.G.my_orgs = my_orgs;
318         mw.G.my_orgs_hash = convert_object_list_to_hash( my_orgs );
319         dump('my_orgs = ' + js2JSON(my_orgs) + '\n');
320         mw.G.acpl_my_orgs = filter_list( 
321                 mw.G.acpl_list, 
322                 function (obj) {
323                         if ( typeof obj != 'object' ) return null;
324                         if ( mw.G.my_orgs_hash[ obj.owning_lib() ] ) return obj;
325                 }
326         );
327         mw.G.acpl_my_orgs_hash = convert_object_list_to_hash( mw.G.acpl_my_orgs );
328         //dump('my_orgs.length = ' + mw.G.my_orgs.length + '   other_orgs.length = ' + mw.G.other_orgs.length + '\n');
329
330         user_async_request(
331                 'open-ils.circ',
332                 'open-ils.circ.stat_cat.actor.retrieve.all',
333                 [ mw.G.auth_ses[0], mw.G.user_ou.id() ],
334                 my_actsc_list_callback
335         );
336
337         document.getElementById('auth_meter').value += auth_meter_per;
338 }
339
340 function my_actsc_list_callback(request) {
341         var actsc_list;
342         try {
343                 actsc_list = request.getResultObject();
344                 if (!actsc_list) { throw('null result'); }
345                 if (actsc_list.length == 0) { throw('zero length result'); }
346         } catch(E) {
347                 alert('Login failed on asc_list: ' + js2JSON(E)); enable_login_prompts(); return;
348         }
349         mw.G.actsc_list = actsc_list;
350         mw.G.actsc_hash = convert_object_list_to_hash( actsc_list );
351         dump('actsc_list = ' + js2JSON(actsc_list) + '\n');
352
353         document.getElementById('auth_meter').value += auth_meter_per;
354
355         spawn_main();
356
357 }
358
359
360 function spawn_main() {
361         try {
362                 var w = new_window('chrome://evergreen/content/evergreen/main.xul');
363                 if (!w) { throw('window ref == null'); }
364                 try {
365                         w.document.title = mw.G.user.usrname() + '@' + mw.G.user_ou.name();
366                 } catch(E) {
367                         alert('Hrmm. ' + pretty_print( js2JSON(E) ) );
368                 }
369         } catch(E) {
370                 alert('Login failed on new_window: ' + js2JSON(E)); enable_login_prompts(); return;
371         }
372         document.getElementById('auth_meter').value += auth_meter_per;
373 }
374
375 function logoff() {
376         mw.G['auth_ses'] = '';
377         close_all_windows();
378         enable_login_prompts();
379         snd_logoff();
380 }
381
382 function nice_shutdown() {
383         if (ses) { logoff(); ses.disconnect(); }
384         snd_exit;
385         close_all_windows();
386         window.close();
387 }
388
389 function test_mvc () {
390         var w = new_window('chrome://evergreen/content/blank.xul');
391         w.title = 'testing';
392         w.addEventListener('load',test_mvc2,false);
393 }
394
395 function test_mvc2(ev) {
396
397         // Model
398
399         var cm = user_request(
400                 'open-ils.search',
401                 'open-ils.search.asset.copy.batch.retrieve',
402                 [ [2] ]
403         )[0][0];
404         dump('cm = ' + js2JSON(cm) + '\n');
405         
406         // View
407
408         var cv = new CopyDisplayXULView();
409
410         var doc = ev.target;
411         var cnv = doc.getElementById('canvas');
412         cv.canvas( cnv );
413
414         // Controller
415
416         var cc = new CopyController();
417         cc.add_view(cv);
418         cc.add_model(cm);
419
420         cc.render_active_view();
421 }