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