]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/patron/standing_penalties.js
LP#1086458: clean up after event listeners in circ/patron interface
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / patron / standing_penalties.js
1 var list; var archived_list; var data; var error; var net; var rows; var archived_rows;
2
3 function default_focus() { document.getElementById('apply_btn').focus(); } // parent interfaces often call this
4
5 function penalty_init() {
6     try {
7         commonStrings = document.getElementById('commonStrings');
8         patronStrings = document.getElementById('patronStrings');
9
10         if (typeof JSAN == 'undefined') {
11             throw(
12                 commonStrings.getString('common.jsan.missing')
13             );
14         }
15
16         JSAN.errorLevel = "die"; // none, warn, or die
17         JSAN.addRepository('..');
18
19         JSAN.use('OpenILS.data'); data = new OpenILS.data(); data.stash_retrieve();
20         XML_HTTP_SERVER = data.server_unadorned;
21
22         JSAN.use('util.error'); error = new util.error();
23         JSAN.use('util.network'); net = new util.network();
24         JSAN.use('patron.util'); 
25         JSAN.use('util.list'); 
26         JSAN.use('util.functional'); 
27         JSAN.use('util.widgets');
28
29         init_list();
30         init_archived_list();
31         window.standing_penalties_event_listeners = new EventListenerList();
32         document.getElementById('date1').year = document.getElementById('date1').year - 1;
33         window.standing_penalties_event_listeners.add(document.getElementById('cmd_apply_penalty'), 'command', handle_apply_penalty, false);
34         window.standing_penalties_event_listeners.add(document.getElementById('cmd_remove_penalty'), 'command', handle_remove_penalty, false);
35         window.standing_penalties_event_listeners.add(document.getElementById('cmd_edit_penalty'), 'command', handle_edit_penalty, false);
36         window.standing_penalties_event_listeners.add(document.getElementById('cmd_archive_penalty'), 'command', handle_archive_penalty, false);
37         window.standing_penalties_event_listeners.add(document.getElementById('cmd_retrieve_archived_penalties'), 'command', handle_retrieve_archived_penalties, false);
38         populate_list();
39         default_focus();
40
41     } catch(E) {
42         var err_prefix = 'standing_penalties.js -> penalty_init() : ';
43         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
44     }
45 }
46
47 function penalty_cleanup() {
48     try {
49         window.standing_penalties_event_listeners.removeAll();
50         list.cleanup();
51         archived_list.cleanup();
52     } catch(E) {
53         var err_prefix = 'standing_penalties.js -> penalty_cleanup() : ';
54         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
55     }
56 }
57
58 function init_list() {
59     try {
60
61         list = new util.list( 'ausp_list' );
62         list.init( 
63             {
64                 'columns' : patron.util.ausp_columns({}),
65                 'retrieve_row' : retrieve_row,
66                 'on_select' : generate_handle_selection(list)
67             } 
68         );
69
70     } catch(E) {
71         var err_prefix = 'standing_penalties.js -> init_list() : ';
72         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
73     }
74 }
75
76 function init_archived_list() {
77     try {
78
79         archived_list = new util.list( 'archived_ausp_list' );
80         archived_list.init( 
81             {
82                 'columns' : patron.util.ausp_columns({}),
83                 'retrieve_row' : retrieve_row, // We're getting fleshed objects for now, but if we move to just ausp.id's, then we'll need to put a per-id fetcher in here
84                 'on_select' : generate_handle_selection(archived_list)
85             } 
86         );
87
88     } catch(E) {
89         var err_prefix = 'standing_penalties.js -> init_archived_list() : ';
90         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
91     }
92 }
93
94
95 function retrieve_row (params) { // callback function for fleshing rows in a list
96     params.treeitem_node.setAttribute('retrieve_id',params.row.my.ausp.id()); 
97     params.on_retrieve(params.row); 
98     return params.row; 
99 }
100
101 function generate_handle_selection(which_list) {
102     return function (ev) { // handler for list row selection event
103         var sel = which_list.retrieve_selection();
104         var ids = util.functional.map_list( sel, function(o) { return JSON2js( o.getAttribute('retrieve_id') ); } );
105         if (which_list == list) { // top list
106             if (ids.length > 0) {
107                 document.getElementById('cmd_remove_penalty').setAttribute('disabled','false');
108                 document.getElementById('cmd_edit_penalty').setAttribute('disabled','false');
109                 document.getElementById('cmd_archive_penalty').setAttribute('disabled','false');
110             } else {
111                 document.getElementById('cmd_remove_penalty').setAttribute('disabled','true');
112                 document.getElementById('cmd_edit_penalty').setAttribute('disabled','true');
113                 document.getElementById('cmd_archive_penalty').setAttribute('disabled','true');
114             }
115         }
116     };
117 }
118
119 function populate_list() {
120     try {
121
122         rows = {};
123         list.clear();
124         for (var i = 0; i < xulG.patron.standing_penalties().length; i++) {
125             var row_params = {
126                 'row' : {
127                     'my' : {
128                         'ausp' : xulG.patron.standing_penalties()[i],
129                         'csp' : xulG.patron.standing_penalties()[i].standing_penalty(),
130                         'au' : xulG.patron,
131                     }
132                 }
133             };
134             rows[ xulG.patron.standing_penalties()[i].id() ] = list.append( row_params );
135         };
136
137     } catch(E) {
138         var err_prefix = 'standing_penalties.js -> populate_list() : ';
139         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
140     }
141 }
142
143 function handle_apply_penalty(ev) {
144     try {
145         JSAN.use('util.window');
146         var win = new util.window();
147         var my_xulG = win.open(
148             urls.XUL_NEW_STANDING_PENALTY,
149             'new_standing_penalty',
150             'chrome,resizable,modal',
151             {}
152         );
153
154         if (!my_xulG.id) { return 0; }
155
156         var penalty = new ausp();
157         penalty.usr( xulG.patron.id() );
158         penalty.isnew( 1 );
159         penalty.standing_penalty( my_xulG.id );
160         penalty.org_unit( ses('ws_ou') );
161         penalty.note( my_xulG.note );
162         net.simple_request(
163             'FM_AUSP_APPLY', 
164             [ ses(), penalty ],
165             generate_request_handler_for_penalty_apply( penalty, my_xulG.id )
166         );
167
168         document.getElementById('progress').hidden = false;
169
170     } catch(E) {
171         alert('error: ' + E);
172         var err_prefix = 'standing_penalties.js -> handle_apply_penalty() : ';
173         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
174     }
175 }
176
177 function generate_request_handler_for_penalty_apply(penalty,id) {
178     return function(reqobj) {
179         try {
180
181             var req = reqobj.getResultObject();
182             if (typeof req.ilsevent != 'undefined') {
183                 error.standard_unexpected_error_alert(
184                     patronStrings.getFormattedString('staff.patron.standing_penalty.apply_error',[data.hash.csp[id].name()]),
185                     req
186                 );
187             } else {
188                 penalty.id(req);
189                 JSAN.use('patron.util'); JSAN.use('util.functional');
190                 //xulG.patron.standing_penalties( xulG.patron.standing_penalties().concat( penalty ) ); // Not good enough for pcrud
191                 xulG.patron = patron.util.retrieve_fleshed_au_via_id( ses(), xulG.patron.id() ); // So get the real deal instead
192                 penalty = util.functional.find_list( xulG.patron.standing_penalties(), function(o) { return o.id() == req; } );
193
194                 var row_params = {
195                     'row' : {
196                         'my' : {
197                             'ausp' : penalty,
198                             'csp' : typeof penalty.standing_penalty() == 'object'
199                                 ? penalty.standing_penalty()
200                                 : data.hash.csp[ penalty.standing_penalty() ],
201                             'au' : xulG.patron,
202                         }
203                     }
204                 };
205                 rows[ penalty.id() ] = list.append( row_params );
206             }
207             /*
208             if (xulG && typeof xulG.refresh == 'function') {
209                 xulG.refresh();
210             }
211             */
212             document.getElementById('progress').hidden = true;
213
214         } catch(E) {
215             var err_prefix = 'standing_penalties.js -> request_handler_for_penalty_apply() : ';
216             if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
217         }
218     };
219 }
220  
221 function handle_remove_penalty(ev) {
222     try {
223
224         var sel = list.retrieve_selection();
225         var ids = util.functional.map_list( sel, function(o) { return JSON2js( o.getAttribute('retrieve_id') ); } );
226         if (! ids.length > 0 ) return;
227
228         var funcs = [];
229         for (var i = 0; i < ids.length; i++) {
230             funcs.push( generate_penalty_remove_function(ids[i]) );
231         } 
232         funcs.push(
233             function() {
234                 /*
235                 if (xulG && typeof xulG.refresh == 'function') {
236                     xulG.refresh();
237                 }
238                 */
239                 document.getElementById('progress').hidden = true;
240
241                 //patron.util.set_penalty_css(xulG.patron);
242             }
243         );
244         document.getElementById('progress').hidden = false;
245         JSAN.use('util.exec'); var exec = new util.exec();
246         exec.chain(funcs);
247
248     } catch(E) {
249         var err_prefix = 'standing_penalties.js -> request_handler_for_penalty_apply() : ';
250         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
251     }
252 }
253
254 function generate_penalty_remove_function(id) {
255     return function() {
256         try {
257
258             var penalty = util.functional.find_list( xulG.patron.standing_penalties(), function(o) { return o.id() == id; } );
259             penalty.isdeleted(1);
260
261             var req = net.simple_request( 'FM_AUSP_REMOVE', [ ses(), penalty ] );
262             if (typeof req.ilsevent != 'undefined' || String(req) != '1') {
263                 error.standard_unexpected_error_alert(patronStrings.getFormattedString('staff.patron.standing_penalty.remove_error',[id]),req);
264             } else {
265                 var node = rows[ id ].treeitem_node;
266                 var parentNode = node.parentNode;
267                 parentNode.removeChild( node );
268                 delete(rows[ id ]);
269             }
270
271         } catch(E) {
272             var err_prefix = 'standing_penalties.js -> penalty_remove_function() : ';
273             if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
274         }
275     }; 
276 }
277
278 function handle_edit_penalty(ev) {
279     try {
280
281         JSAN.use('util.window');
282         var win = new util.window();
283
284         var sel = list.retrieve_selection();
285         var ids = util.functional.map_list( sel, function(o) { return JSON2js( o.getAttribute('retrieve_id') ); } );
286         if (ids.length > 0) {
287             for (var i = 0; i < ids.length; i++) {
288                 var penalty = util.functional.find_list( xulG.patron.standing_penalties(), function(o) { return o.id() == ids[i]; } );
289                 var my_xulG = win.open(
290                     urls.XUL_EDIT_STANDING_PENALTY,
291                     'new_standing_penalty',
292                     'chrome,resizable,modal',
293                     { 
294                         'id' : typeof penalty.standing_penalty() == 'object' ? penalty.standing_penalty().id() : penalty.standing_penalty(), 
295                         'note' : penalty.note() 
296                     }
297                 );
298                 if (my_xulG.modify) {
299                     document.getElementById('progress').hidden = false;
300                     penalty.note( my_xulG.note ); /* this is for rendering, and propogates by reference to the object associated with the row in the GUI */
301                     penalty.standing_penalty( my_xulG.id );
302                     penalty.ischanged( 1 );
303                     dojo.require('openils.PermaCrud');
304                     var pcrud = new openils.PermaCrud( { authtoken :ses() });
305                     pcrud.update( penalty, {
306                         timeout : 10, // makes it synchronous
307                         onerror : function(r) {
308                             try {
309                                 document.getElementById('progress').hidden = true;
310                                 var res = openils.Util.readResponse(r,true);
311                                 error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.update_error'),res);
312                             } catch(E) {
313                                 alert(E);
314                             }
315                         },
316                         oncomplete : function gen_func(p,row_id) {
317                             return function(r) {
318                                 try {
319                                     var res = openils.Util.readResponse(r,true);
320                                     /* FIXME - test for success */
321                                     var row_params = rows[row_id];
322                                     row_params.row.my.ausp = p;
323                                     row_params.row.my.csp = p.standing_penalty();
324                                     list.refresh_row( row_params );
325
326                                     //patron.util.set_penalty_css(xulG.patron);
327                                     document.getElementById('progress').hidden = true;
328                                 } catch(E) {
329                                     alert(E);
330                                 }
331                             }
332                         }(penalty,ids[i])
333                     });
334                 }
335             } 
336             /*
337             if (xulG && typeof xulG.refresh == 'function') {
338                 xulG.refresh();
339             }
340             */
341         }
342
343     } catch(E) {
344         var err_prefix = 'standing_penalties.js -> handle_edit_penalty() : ';
345         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
346     }
347 }
348
349 function handle_archive_penalty(ev) {
350     try {
351         var outstanding_requests = 0;
352         var sel = list.retrieve_selection();
353         var ids = util.functional.map_list( sel, function(o) { return JSON2js( o.getAttribute('retrieve_id') ); } );
354         if (ids.length > 0) {
355             document.getElementById('progress').hidden = false;
356             for (var i = 0; i < ids.length; i++) {
357                 outstanding_requests++;
358                 var penalty = util.functional.find_list( xulG.patron.standing_penalties(), function(o) { return o.id() == ids[i]; } );
359                 penalty.ischanged( 1 );
360                 penalty.stop_date( util.date.formatted_date(new Date(),'%F') );
361                 dojo.require('openils.PermaCrud');
362                 var pcrud = new openils.PermaCrud( { authtoken :ses() });
363                 pcrud.update( penalty, {
364                     onerror : function(r) {
365                         try {
366                             var res = openils.Util.readResponse(r,true);
367                             error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.update_error'),res);
368                         } catch(E) {
369                             alert(E);
370                         }
371                         if (--outstanding_requests==0) {
372                             document.getElementById('progress').hidden = true;
373                         }
374                     },
375                     oncomplete : function gen_func(row_id) {
376                         return function(r) {
377                             try {
378                                 var res = openils.Util.readResponse(r,true);
379                                 /* FIXME - test for success */
380                                 var node = rows[row_id].treeitem_node;
381                                 var parentNode = node.parentNode;
382                                 parentNode.removeChild( node );
383                                 delete(rows[row_id]);
384                             } catch(E) {
385                                 alert(E);
386                             }
387                             if (--outstanding_requests==0) {
388                                 document.getElementById('progress').hidden = true;
389
390                                 //patron.util.set_penalty_css(xulG.patron);
391                             }
392                         }
393                     }(ids[i])
394                 });
395             } 
396             /*
397             if (xulG && typeof xulG.refresh == 'function') {
398                 xulG.refresh();
399             }
400             */
401         }
402
403     } catch(E) {
404         var err_prefix = 'standing_penalties.js -> handle_archive_penalty() : ';
405         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
406     }
407 }
408
409 function handle_retrieve_archived_penalties() {
410     try {
411         document.getElementById('archived_progress').hidden = false;
412         archived_list.clear(); archived_rows = {};
413         JSAN.use('util.date');
414         dojo.require('openils.PermaCrud');
415         var pcrud = new openils.PermaCrud( { authtoken :ses() });
416         var date2 = document.getElementById('date2').dateValue;
417         date2.setDate( date2.getDate() + 1 ); // Javascript will wrap into subsequent months
418         pcrud.search(
419             'ausp',
420             {
421                 usr : xulG.patron.id(),
422                 stop_date : {
423                     'between' : [ 
424                         document.getElementById('date1').value, 
425                         document.getElementById('date2').value == util.date.formatted_date(new Date(),'%F') ? 
426                             'now' : util.date.formatted_date( date2 ,'%F')
427                     ]
428                 }
429             },
430             {
431                 async : true,
432                 streaming : true,
433                 onerror : function(r) {
434                     try {
435                         var res = openils.Util.readResponse(r,true);
436                         error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.retrieve_error'),res);
437                     } catch(E) {
438                         error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.retrieve_error'),r);
439                     }
440                 },
441                 oncomplete : function() {
442                     document.getElementById('archived_progress').hidden = true;
443                 },
444                 onresponse : function(r) {
445                     try {
446                         var my_ausp = openils.Util.readResponse(r);
447                         var row_params = {
448                             'row' : {
449                                 'my' : {
450                                     'ausp' : my_ausp,
451                                     'csp' : my_ausp.standing_penalty(),
452                                     'au' : xulG.patron,
453                                 }
454                             }
455                         };
456                         archived_rows[ my_ausp.id() ] = archived_list.append( row_params );
457                     } catch(E) {
458                         error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.retrieve_error'),E);
459                     }
460                 }
461             }
462         );
463     } catch(E) {
464         var err_prefix = 'standing_penalties.js -> handle_retrieve_archived_penalties() : ';
465         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
466     }
467 }