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 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
9 commonStrings = document.getElementById('commonStrings');
10 patronStrings = document.getElementById('patronStrings');
12 if (typeof JSAN == 'undefined') {
14 commonStrings.getString('common.jsan.missing')
18 JSAN.errorLevel = "die"; // none, warn, or die
19 JSAN.addRepository('..');
21 JSAN.use('OpenILS.data'); data = new OpenILS.data(); data.stash_retrieve();
22 XML_HTTP_SERVER = data.server_unadorned;
24 JSAN.use('util.error'); error = new util.error();
25 JSAN.use('util.network'); net = new util.network();
26 JSAN.use('patron.util');
27 JSAN.use('util.list');
28 JSAN.use('util.functional');
29 JSAN.use('util.widgets');
33 document.getElementById('date1').year = document.getElementById('date1').year - 1;
34 document.getElementById('cmd_apply_penalty').addEventListener('command', handle_apply_penalty, false);
35 document.getElementById('cmd_remove_penalty').addEventListener('command', handle_remove_penalty, false);
36 document.getElementById('cmd_edit_penalty').addEventListener('command', handle_edit_penalty, false);
37 document.getElementById('cmd_archive_penalty').addEventListener('command', handle_archive_penalty, false);
38 document.getElementById('cmd_retrieve_archived_penalties').addEventListener('command', handle_retrieve_archived_penalties, false);
43 var err_prefix = 'standing_penalties.js -> penalty_init() : ';
44 if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
48 function init_list() {
51 list = new util.list( 'ausp_list' );
54 'columns' : patron.util.ausp_columns({}),
55 'map_row_to_columns' : patron.util.std_map_row_to_columns(),
56 'retrieve_row' : retrieve_row,
57 'on_select' : generate_handle_selection(list)
62 var err_prefix = 'standing_penalties.js -> init_list() : ';
63 if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
67 function init_archived_list() {
70 archived_list = new util.list( 'archived_ausp_list' );
73 'columns' : patron.util.ausp_columns({}),
74 'map_row_to_columns' : patron.util.std_map_row_to_columns(),
75 '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
76 'on_select' : generate_handle_selection(archived_list)
81 var err_prefix = 'standing_penalties.js -> init_archived_list() : ';
82 if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
87 function retrieve_row (params) { // callback function for fleshing rows in a list
88 params.row_node.setAttribute('retrieve_id',params.row.my.ausp.id());
89 params.on_retrieve(params.row);
93 function generate_handle_selection(which_list) {
94 return function (ev) { // handler for list row selection event
95 var sel = which_list.retrieve_selection();
96 var ids = util.functional.map_list( sel, function(o) { return JSON2js( o.getAttribute('retrieve_id') ); } );
97 if (which_list == list) { // top list
99 document.getElementById('cmd_remove_penalty').setAttribute('disabled','false');
100 document.getElementById('cmd_edit_penalty').setAttribute('disabled','false');
101 document.getElementById('cmd_archive_penalty').setAttribute('disabled','false');
103 document.getElementById('cmd_remove_penalty').setAttribute('disabled','true');
104 document.getElementById('cmd_edit_penalty').setAttribute('disabled','true');
105 document.getElementById('cmd_archive_penalty').setAttribute('disabled','true');
111 function populate_list() {
116 for (var i = 0; i < xulG.patron.standing_penalties().length; i++) {
120 'ausp' : xulG.patron.standing_penalties()[i],
121 'csp' : xulG.patron.standing_penalties()[i].standing_penalty(),
126 rows[ xulG.patron.standing_penalties()[i].id() ] = list.append( row_params );
130 var err_prefix = 'standing_penalties.js -> populate_list() : ';
131 if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
135 function handle_apply_penalty(ev) {
137 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
138 JSAN.use('util.window');
139 var win = new util.window();
140 var my_xulG = win.open(
141 urls.XUL_NEW_STANDING_PENALTY,
142 'new_standing_penalty',
143 'chrome,resizable,modal',
147 if (!my_xulG.id) { return 0; }
149 var penalty = new ausp();
150 penalty.usr( xulG.patron.id() );
152 penalty.standing_penalty( my_xulG.id );
153 penalty.org_unit( ses('ws_ou') );
154 penalty.note( my_xulG.note );
158 generate_request_handler_for_penalty_apply( penalty, my_xulG.id )
161 document.getElementById('progress').hidden = false;
164 alert('error: ' + E);
165 var err_prefix = 'standing_penalties.js -> handle_apply_penalty() : ';
166 if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
170 function generate_request_handler_for_penalty_apply(penalty,id) {
171 return function(reqobj) {
174 var req = reqobj.getResultObject();
175 if (typeof req.ilsevent != 'undefined') {
176 error.standard_unexpected_error_alert(
177 patronStrings.getFormattedString('staff.patron.standing_penalty.apply_error',[data.hash.csp[id].name()]),
182 JSAN.use('patron.util'); JSAN.use('util.functional');
183 //xulG.patron.standing_penalties( xulG.patron.standing_penalties().concat( penalty ) ); // Not good enough for pcrud
184 xulG.patron = patron.util.retrieve_fleshed_au_via_id( ses(), xulG.patron.id() ); // So get the real deal instead
185 penalty = util.functional.find_list( xulG.patron.standing_penalties(), function(o) { return o.id() == req; } );
191 'csp' : data.hash.csp[ penalty.standing_penalty() ],
196 rows[ penalty.id() ] = list.append( row_params );
199 if (xulG && typeof xulG.refresh == 'function') {
203 document.getElementById('progress').hidden = true;
206 var err_prefix = 'standing_penalties.js -> request_handler_for_penalty_apply() : ';
207 if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
212 function handle_remove_penalty(ev) {
215 var sel = list.retrieve_selection();
216 var ids = util.functional.map_list( sel, function(o) { return JSON2js( o.getAttribute('retrieve_id') ); } );
217 if (! ids.length > 0 ) return;
220 for (var i = 0; i < ids.length; i++) {
221 funcs.push( generate_penalty_remove_function(ids[i]) );
226 if (xulG && typeof xulG.refresh == 'function') {
230 document.getElementById('progress').hidden = true;
233 document.getElementById('progress').hidden = false;
234 JSAN.use('util.exec'); var exec = new util.exec();
238 var err_prefix = 'standing_penalties.js -> request_handler_for_penalty_apply() : ';
239 if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
243 function generate_penalty_remove_function(id) {
247 var penalty = util.functional.find_list( xulG.patron.standing_penalties(), function(o) { return o.id() == id; } );
248 penalty.isdeleted(1);
250 var req = net.simple_request( 'FM_AUSP_REMOVE', [ ses(), penalty ] );
251 if (typeof req.ilsevent != 'undefined' || String(req) != '1') {
252 error.standard_unexpected_error_alert(patronStrings.getFormattedString('staff.patron.standing_penalty.remove_error',[id]),req);
254 var node = rows[ id ].my_node;
255 var parentNode = node.parentNode;
256 parentNode.removeChild( node );
261 var err_prefix = 'standing_penalties.js -> penalty_remove_function() : ';
262 if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
267 function handle_edit_penalty(ev) {
270 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
271 JSAN.use('util.window');
272 var win = new util.window();
274 var sel = list.retrieve_selection();
275 var ids = util.functional.map_list( sel, function(o) { return JSON2js( o.getAttribute('retrieve_id') ); } );
276 if (ids.length > 0) {
277 for (var i = 0; i < ids.length; i++) {
278 var penalty = util.functional.find_list( xulG.patron.standing_penalties(), function(o) { return o.id() == ids[i]; } );
279 var my_xulG = win.open(
280 urls.XUL_EDIT_STANDING_PENALTY,
281 'new_standing_penalty',
282 'chrome,resizable,modal',
284 'id' : typeof penalty.standing_penalty() == 'object' ? penalty.standing_penalty().id() : penalty.standing_penalty(),
285 'note' : penalty.note()
288 if (my_xulG.modify) {
289 document.getElementById('progress').hidden = false;
290 penalty.note( my_xulG.note ); /* this is for rendering, and propogates by reference to the object associated with the row in the GUI */
291 penalty.standing_penalty( my_xulG.id );
292 penalty.ischanged( 1 );
293 dojo.require('openils.PermaCrud');
294 var pcrud = new openils.PermaCrud( { authtoken :ses() });
295 pcrud.update( penalty, {
296 timeout : 10, // makes it synchronous
297 onerror : function(r) {
299 document.getElementById('progress').hidden = true;
300 var res = openils.Util.readResponse(r,true);
301 error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.update_error'),res);
306 oncomplete : function gen_func(p,row_id) {
309 var res = openils.Util.readResponse(r,true);
310 /* FIXME - test for success */
311 var row_params = rows[row_id];
312 row_params.row.my.ausp = p;
313 row_params.row.my.csp = p.standing_penalty();
314 list.refresh_row( row_params );
315 document.getElementById('progress').hidden = true;
325 if (xulG && typeof xulG.refresh == 'function') {
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);
337 function handle_archive_penalty(ev) {
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) {
354 var res = openils.Util.readResponse(r,true);
355 error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.update_error'),res);
359 if (--outstanding_requests==0) {
360 document.getElementById('progress').hidden = true;
363 oncomplete : function gen_func(row_id) {
366 var res = openils.Util.readResponse(r,true);
367 /* FIXME - test for success */
368 var node = rows[row_id].my_node;
369 var parentNode = node.parentNode;
370 parentNode.removeChild( node );
371 delete(rows[row_id]);
375 if (--outstanding_requests==0) {
376 document.getElementById('progress').hidden = true;
383 if (xulG && typeof xulG.refresh == 'function') {
390 var err_prefix = 'standing_penalties.js -> handle_archive_penalty() : ';
391 if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
395 function handle_retrieve_archived_penalties() {
397 document.getElementById('archived_progress').hidden = false;
398 archived_list.clear(); archived_rows = {};
399 JSAN.use('util.date');
400 dojo.require('openils.PermaCrud');
401 var pcrud = new openils.PermaCrud( { authtoken :ses() });
402 var date2 = document.getElementById('date2').dateValue;
403 date2.setDate( date2.getDate() + 1 ); // Javascript will wrap into subsequent months
407 usr : xulG.patron.id(),
410 document.getElementById('date1').value,
411 document.getElementById('date2').value == util.date.formatted_date(new Date(),'%F') ?
412 'now' : util.date.formatted_date( date2 ,'%F')
419 onerror : function(r) {
421 var res = openils.Util.readResponse(r,true);
422 error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.retrieve_error'),res);
424 error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.retrieve_error'),r);
427 oncomplete : function() {
428 document.getElementById('archived_progress').hidden = true;
430 onresponse : function(r) {
432 var my_ausp = openils.Util.readResponse(r);
437 'csp' : my_ausp.standing_penalty(),
442 archived_rows[ my_ausp.id() ] = archived_list.append( row_params );
444 error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.retrieve_error'),E);
450 var err_prefix = 'standing_penalties.js -> handle_retrieve_archived_penalties() : ';
451 if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);