cf8591258f4d64b67ece1dde4f52f9890579bc2e
[Evergreen.git] / Open-ILS / xul / staff_client / server / patron / display.js
1 dump('entering patron/display.js\n');
2
3 if (typeof patron == 'undefined') patron = {};
4 patron.display = function (params) {
5
6         JSAN.use('util.error'); this.error = new util.error();
7         JSAN.use('util.window'); this.window = new util.window();
8         JSAN.use('util.network'); this.network = new util.network();
9         this.w = window;
10 }
11
12 patron.display.prototype = {
13
14         'retrieve_ids' : [],
15
16         'init' : function( params ) {
17
18                 var obj = this;
19
20                 obj.session = params['session'];
21                 obj.barcode = params['barcode'];
22                 obj.id = params['id'];
23
24                 JSAN.use('OpenILS.data'); this.OpenILS = {}; 
25                 obj.OpenILS.data = new OpenILS.data(); obj.OpenILS.data.init({'via':'stash'});
26
27                 JSAN.use('util.deck'); 
28                 obj.right_deck = new util.deck('patron_right_deck');
29                 obj.left_deck = new util.deck('patron_left_deck');
30
31                 function spawn_checkout_interface() {
32                         obj.right_deck.set_iframe(
33                                 urls.remote_checkout
34                                 + '?session=' + window.escape( obj.session )
35                                 + '&patron_id=' + window.escape( obj.patron.id() ),
36                                 {},
37                                 { 
38                                         'on_checkout' : function(checkout) {
39                                                 var c = obj.summary_window.g.summary.patron.checkouts();
40                                                 c.push( checkout );
41                                                 obj.summary_window.g.summary.patron.checkouts( c );
42                                                 obj.summary_window.g.summary.controller.render('patron_checkouts');
43                                         }
44                                 }
45                         );
46                         dump('obj.right_deck.node.childNodes.length = ' + obj.right_deck.node.childNodes.length + '\n');
47                 }
48
49                 JSAN.use('util.controller'); obj.controller = new util.controller();
50                 obj.controller.init(
51                         {
52                                 control_map : {
53                                         'cmd_broken' : [
54                                                 ['command'],
55                                                 function() { alert('Not Yet Implemented'); }
56                                         ],
57                                         'cmd_patron_retrieve' : [
58                                                 ['command'],
59                                                 function(ev) {
60                                                         if (typeof window.xulG == 'object' && typeof window.xulG.new_tab == 'function') {
61                                                                 for (var i = 0; i < obj.retrieve_ids.length; i++) {     
62                                                                         try {
63                                                                                 var url = urls.remote_patron_display 
64                                                                                         + '?session=' + window.escape(obj.session) 
65                                                                                         + '&id=' + window.escape( obj.retrieve_ids[i] );
66                                                                                 window.xulG.new_tab(
67                                                                                         url
68                                                                                 );
69                                                                         } catch(E) {
70                                                                                 alert(E);
71                                                                         }
72                                                                 }
73                                                         }
74                                                 }
75                                         ],
76                                         'cmd_search_form' : [
77                                                 ['command'],
78                                                 function(ev) {
79                                                         obj.controller.view.cmd_search_form.setAttribute('disabled','true');
80                                                         obj.left_deck.node.selectedIndex = 0;
81                                                         obj.controller.view.patron_name.setAttribute('value','No Patron Selected');
82                                                 }
83                                         ],
84                                         'cmd_patron_refresh' : [
85                                                 ['command'],
86                                                 function(ev) {
87                                                         obj.controller.view.patron_name.setAttribute(
88                                                                 'value','Retrieving...'
89                                                         );
90                                                         try { obj.summary_window.refresh(); } catch(E) { dump(E + '\n'); }
91                                                         try { obj.refresh_deck(); } catch(E) { dump(E + '\n'); }
92                                                 }
93                                         ],
94                                         'cmd_patron_checkout' : [
95                                                 ['command'],
96                                                 spawn_checkout_interface
97                                         ],
98                                         'cmd_patron_items' : [
99                                                 ['command'],
100                                                 function(ev) {
101                                                         obj.right_deck.set_iframe(
102                                                                 urls.remote_patron_items
103                                                                 + '?session=' + window.escape( obj.session )
104                                                                 + '&patron_id=' + window.escape( obj.patron.id() ),
105                                                                 {},
106                                                                 {
107                                                                         'checkouts' : obj.patron.checkouts()
108                                                                 }
109                                                         );
110                                                         dump('obj.right_deck.node.childNodes.length = ' + obj.right_deck.node.childNodes.length + '\n');
111                                                 }
112                                         ],
113                                         'cmd_patron_holds' : [
114                                                 ['command'],
115                                                 function(ev) {
116                                                         obj.right_deck.set_iframe(
117                                                                 urls.remote_patron_holds        
118                                                                 + '?session=' + window.escape( obj.session )
119                                                                 + '&patron_id=' + window.escape( obj.patron.id() ),
120                                                                 {},
121                                                                 {
122                                                                         //FIXME//'holds' : obj.patron.holds()
123                                                                 }
124                                                         );
125                                                         dump('obj.right_deck.node.childNodes.length = ' + obj.right_deck.node.childNodes.length + '\n');
126                                                 }
127                                         ],
128                                         'cmd_patron_bills' : [
129                                                 ['command'],
130                                                 function(ev) {
131                                                         obj.right_deck.set_iframe(
132                                                                 urls.remote_patron_bills
133                                                                 + '?session=' + window.escape( obj.session )
134                                                                 + '&patron_id=' + window.escape( obj.patron.id() ),
135                                                                 {},
136                                                                 {
137                                                                         //FIXME//'bills' : obj.patron.bills()
138                                                                 }
139                                                         );
140                                                         dump('obj.right_deck.node.childNodes.length = ' + obj.right_deck.node.childNodes.length + '\n');
141                                                 }
142                                         ],
143                                         'cmd_patron_edit' : [
144                                                 ['command'],
145                                                 function(ev) {
146                                                         obj.right_deck.set_iframe(
147                                                                 urls.remote_patron_edit
148                                                                 + '?ses=' + window.escape( obj.session )
149                                                                 + '&usr=' + window.escape( obj.patron.id() ),
150                                                                 {}, {}
151                                                         );
152                                                         dump('obj.right_deck.node.childNodes.length = ' + obj.right_deck.node.childNodes.length + '\n');
153                                                 }
154                                         ],
155                                         'cmd_patron_info' : [
156                                                 ['command'],
157                                                 function(ev) {
158                                                         obj.right_deck.set_iframe(urls.remote_patron_info);
159                                                         dump('obj.right_deck.node.childNodes.length = ' + obj.right_deck.node.childNodes.length + '\n');
160                                                 }
161                                         ],
162                                         'patron_name' : [
163                                                 ['render'],
164                                                 function(e) {
165                                                         return function() { 
166                                                                 e.setAttribute('value',
167                                                                         obj.patron.family_name() + ', ' + obj.patron.first_given_name() + ' ' +
168                                                                         ( obj.patron.second_given_name() ? obj.patron.second_given_name() : '' )
169                                                                 );
170                                                                 e.setAttribute('style','background-color: lime');
171                                                                 if (obj.summary_window) {
172                                                                         //FIXME//bills should become a virtual field
173                                                                         if (obj.summary_window.g.summary.patron.bills.length > 0)
174                                                                                 e.setAttribute('style','background-color: yellow');
175                                                                         if (obj.summary_window.g.summary.patron.standing() == 2)
176                                                                                 e.setAttribute('style','background-color: lightred');
177                                                                 }
178
179                                                         };
180                                                 }
181                                         ],
182                                         'PatronNavBar' : [
183                                                 ['render'],
184                                                 function(e) {
185                                                         return function() {}
186                                                 }
187                                         ],
188                                 }
189                         }
190                 );
191
192                 if (obj.barcode || obj.id) {
193                         if (typeof window.xulG == 'object' && typeof window.xulG.set_tab_name == 'function') {
194                                 try { window.xulG.set_tab_name('Retrieving Patron...'); } catch(E) { alert(E); }
195                         }
196
197                         obj.controller.view.PatronNavBar.selectedIndex = 1;
198                         obj.controller.view.cmd_patron_refresh.setAttribute('disabled','true');
199                         obj.controller.view.cmd_patron_checkout.setAttribute('disabled','true');
200                         obj.controller.view.cmd_patron_items.setAttribute('disabled','true');
201                         obj.controller.view.cmd_patron_holds.setAttribute('disabled','true');
202                         obj.controller.view.cmd_patron_bills.setAttribute('disabled','true');
203                         obj.controller.view.cmd_patron_edit.setAttribute('disabled','true');
204                         obj.controller.view.cmd_patron_info.setAttribute('disabled','true');
205                         obj.controller.view.patron_name.setAttribute('value','Retrieving...');
206                         var frame = obj.left_deck.set_iframe(
207                                 urls.remote_patron_summary
208                                 +'?session=' + window.escape(obj.session)
209                                 +'&barcode=' + window.escape(obj.barcode) 
210                                 +'&id=' + window.escape(obj.id), 
211                                 {},
212                                 {
213                                         'on_finished' : function(patron) {
214
215                                                 obj.patron = patron; obj.controller.render();
216
217                                                 obj.controller.view.cmd_patron_refresh.setAttribute('disabled','false');
218                                                 obj.controller.view.cmd_patron_checkout.setAttribute('disabled','false');
219                                                 obj.controller.view.cmd_patron_items.setAttribute('disabled','false');
220                                                 obj.controller.view.cmd_patron_holds.setAttribute('disabled','false');
221                                                 obj.controller.view.cmd_patron_bills.setAttribute('disabled','false');
222                                                 obj.controller.view.cmd_patron_edit.setAttribute('disabled','false');
223                                                 obj.controller.view.cmd_patron_info.setAttribute('disabled','false');
224
225                                                 if (typeof window.xulG == 'object' && typeof window.xulG.set_tab_name == 'function') {
226                                                         try { 
227                                                                 window.xulG.set_tab_name(
228                                                                         'Patron: ' + patron.family_name() + ', ' + patron.first_given_name() + ' ' 
229                                                                                 + (patron.second_given_name() ? patron.second_given_name() : '' ) 
230                                                                 ); 
231                                                         } catch(E) { 
232                                                                 obj.error.sdump('D_ERROR',E);
233                                                         }
234                                                 }
235
236                                                 if (!obj._checkout_spawned) {
237                                                         spawn_checkout_interface();
238                                                         obj._checkout_spawned = true;
239                                                 }
240                                         }
241                                 }
242                         );
243                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
244                         obj.summary_window = frame.contentWindow;
245                 } else {
246                         if (typeof window.xulG == 'object' && typeof window.xulG.set_tab_name == 'function') {
247                                 try { window.xulG.set_tab_name('Patron Search'); } catch(E) { alert(E); }
248                         }
249
250                         obj.controller.view.PatronNavBar.selectedIndex = 0;
251                         obj.controller.view.cmd_patron_retrieve.setAttribute('disabled','true');
252                         obj.controller.view.cmd_search_form.setAttribute('disabled','true');
253                         var form_frame = obj.left_deck.set_iframe(
254                                 urls.remote_patron_search_form
255                                 +'?session=' + window.escape(obj.session),
256                                 {},
257                                 {
258                                         'on_submit' : function(query) {
259                                                 obj.controller.view.cmd_patron_retrieve.setAttribute('disabled','true');
260                                                 var list_frame = obj.right_deck.reset_iframe(
261                                                         urls.remote_patron_search_result
262                                                         +'?session=' + window.escape(obj.session) + '&' + query,
263                                                         {},
264                                                         {
265                                                                 'on_select' : function(list) {
266                                                                         obj.controller.view.cmd_patron_retrieve.setAttribute('disabled','false');
267                                                                         obj.controller.view.cmd_search_form.setAttribute('disabled','false');
268                                                                         obj.retrieve_ids = list;
269                                                                         obj.controller.view.patron_name.setAttribute('value','Retrieving...');
270                                                                         setTimeout(
271                                                                                 function() {
272                                                                                         var frame = obj.left_deck.set_iframe(
273                                                                                                 urls.remote_patron_summary
274                                                                                                         +'?session=' + window.escape(obj.session)
275                                                                                                         +'&id=' + window.escape(list[0]), 
276                                                                                                         {},
277                                                                                                         {
278                                                                                                                 'on_finished' : function(patron) {
279                                                                                                                         obj.patron = patron;
280                                                                                                                         obj.controller.render();
281                                                                                                                 }
282                                                                                                         }
283                                                                                         );
284                                                                                         netscape.security.PrivilegeManager.enablePrivilege(
285                                                                                                 "UniversalXPConnect"
286                                                                                         );
287                                                                                         obj.summary_window = frame.contentWindow;
288                                                                                         obj.patron = obj.summary_window.g.summary.patron;
289                                                                                         obj.controller.render('patron_name');
290                                                                                 }, 0
291                                                                         );
292                                                                 }
293                                                         }
294                                                 );
295                                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
296                                                 obj.search_result = list_frame.contentWindow;
297                                         }
298                                 }
299                         );
300                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
301                         obj.search_window = form_frame.contentWindow;
302                         obj._checkout_spawned = true;
303                 }
304         },
305
306         '_checkout_spawned' : false,
307
308         'refresh_deck' : function() {
309                 var obj = this;
310                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
311                 for (var i = 0; i < obj.right_deck.node.childNodes.length; i++) {
312                         try {
313
314                                 var f = obj.right_deck.node.childNodes[i];
315                                 var w = f.contentWindow;
316                                 if (typeof w.refresh == 'function') {
317                                         w.refresh();
318                                 }
319
320                         } catch(E) {
321                                 dump('refresh_deck: ' + E + '\n');
322                         }
323                 }
324         },
325 }
326
327 dump('exiting patron/display.js\n');