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