1 var list; var archived_list; var data; var error; var net; var rows; var archived_rows;
3 function default_focus() { document.getElementById('apply_btn').focus(); } // parent interfaces often call this
5 function penalty_init() {
7 commonStrings = document.getElementById('commonStrings');
8 patronStrings = document.getElementById('patronStrings');
10 if (typeof JSAN == 'undefined') {
12 commonStrings.getString('common.jsan.missing')
16 JSAN.errorLevel = "die"; // none, warn, or die
17 JSAN.addRepository('..');
19 JSAN.use('OpenILS.data'); data = new OpenILS.data(); data.stash_retrieve();
20 XML_HTTP_SERVER = data.server_unadorned;
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');
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);
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);
47 function penalty_cleanup() {
49 window.standing_penalties_event_listeners.removeAll();
52 archived_list.cleanup();
53 archived_list.clear();
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);
60 function init_list() {
63 list = new util.list( 'ausp_list' );
66 'columns' : patron.util.ausp_columns({}),
67 'retrieve_row' : retrieve_row,
68 'on_select' : generate_handle_selection(list)
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);
78 function init_archived_list() {
81 archived_list = new util.list( 'archived_ausp_list' );
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)
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);
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);
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');
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');
121 function populate_list() {
126 for (var i = 0; i < xulG.patron.standing_penalties().length; i++) {
130 'ausp' : xulG.patron.standing_penalties()[i],
131 'csp' : xulG.patron.standing_penalties()[i].standing_penalty(),
136 rows[ xulG.patron.standing_penalties()[i].id() ] = list.append( row_params );
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);
145 function handle_apply_penalty(ev) {
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',
156 if (!my_xulG.id) { return 0; }
158 var penalty = new ausp();
159 penalty.usr( xulG.patron.id() );
161 penalty.standing_penalty( my_xulG.id );
162 penalty.org_unit( ses('ws_ou') );
163 penalty.note( my_xulG.note );
167 generate_request_handler_for_penalty_apply( penalty, my_xulG.id )
170 document.getElementById('progress').hidden = false;
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);
179 function generate_request_handler_for_penalty_apply(penalty,id) {
180 return function(reqobj) {
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()]),
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; } );
200 'csp' : typeof penalty.standing_penalty() == 'object'
201 ? penalty.standing_penalty()
202 : data.hash.csp[ penalty.standing_penalty() ],
207 rows[ penalty.id() ] = list.append( row_params );
210 if (xulG && typeof xulG.refresh == 'function') {
214 document.getElementById('progress').hidden = true;
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);
223 function handle_remove_penalty(ev) {
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;
231 for (var i = 0; i < ids.length; i++) {
232 funcs.push( generate_penalty_remove_function(ids[i]) );
237 if (xulG && typeof xulG.refresh == 'function') {
241 document.getElementById('progress').hidden = true;
243 //patron.util.set_penalty_css(xulG.patron);
246 document.getElementById('progress').hidden = false;
247 JSAN.use('util.exec'); var exec = new util.exec();
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);
256 function generate_penalty_remove_function(id) {
260 var penalty = util.functional.find_list( xulG.patron.standing_penalties(), function(o) { return o.id() == id; } );
261 penalty.isdeleted(1);
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);
267 var node = rows[ id ].treeitem_node;
268 var parentNode = node.parentNode;
269 parentNode.removeChild( node );
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);
280 function handle_edit_penalty(ev) {
283 JSAN.use('util.window');
284 var win = new util.window();
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',
296 'id' : typeof penalty.standing_penalty() == 'object' ? penalty.standing_penalty().id() : penalty.standing_penalty(),
297 'note' : penalty.note()
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) {
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);
318 oncomplete : function gen_func(p,row_id) {
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 );
328 //patron.util.set_penalty_css(xulG.patron);
329 document.getElementById('progress').hidden = true;
339 if (xulG && typeof xulG.refresh == 'function') {
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);
351 function handle_archive_penalty(ev) {
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) {
368 var res = openils.Util.readResponse(r,true);
369 error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.update_error'),res);
373 if (--outstanding_requests==0) {
374 document.getElementById('progress').hidden = true;
377 oncomplete : function gen_func(row_id) {
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]);
389 if (--outstanding_requests==0) {
390 document.getElementById('progress').hidden = true;
392 //patron.util.set_penalty_css(xulG.patron);
399 if (xulG && typeof xulG.refresh == 'function') {
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);
411 function handle_retrieve_archived_penalties() {
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
423 usr : xulG.patron.id(),
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')
435 onerror : function(r) {
437 var res = openils.Util.readResponse(r,true);
438 error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.retrieve_error'),res);
440 error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.retrieve_error'),r);
443 oncomplete : function() {
444 document.getElementById('archived_progress').hidden = true;
446 onresponse : function(r) {
448 var my_ausp = openils.Util.readResponse(r);
453 'csp' : my_ausp.standing_penalty(),
458 archived_rows[ my_ausp.id() ] = archived_list.append( row_params );
460 error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.retrieve_error'),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);