5 /******************************************************************************************************/
6 /* setup JSAN and some initial libraries */
8 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
9 if (typeof JSAN == 'undefined') { throw( "The JSAN library object is missing."); }
10 JSAN.errorLevel = "die"; // none, warn, or die
11 JSAN.addRepository('/xul/server/');
12 JSAN.use('util.error'); g.error = new util.error();
13 g.error.sdump('D_TRACE','my_init() for cat/copy_editor.xul');
15 JSAN.use('util.functional');
16 JSAN.use('OpenILS.data'); g.data = new OpenILS.data(); g.data.init({'via':'stash'});
17 JSAN.use('util.network'); g.network = new util.network();
21 g.session = g.cgi.param('session') || g.cgi.param('ses');
22 g.docid = g.cgi.param('docid');
23 g.handle_update = g.cgi.param('handle_update');
25 /******************************************************************************************************/
26 /* Get the copy ids from various sources and flesh them */
29 if (g.cgi.param('copy_ids')) copy_ids = JSON2js( g.cgi.param('copy_ids') );
30 if (!copy_ids) copy_ids = [];
31 if (window.xulG && window.xulG.copy_ids) copy_ids = copy_ids.concat( window.xulG.copy_ids );
33 if (copy_ids.length > 0) g.copies = g.network.request(
34 api.FM_ACP_FLESHED_BATCH_RETRIEVE.app,
35 api.FM_ACP_FLESHED_BATCH_RETRIEVE.method,
39 /******************************************************************************************************/
40 /* And other fleshed copies if any */
42 if (!g.copies) g.copies = [];
43 if (window.xulG && window.xulG.copies) g.copies = g.copies.concat( window.xulG.copies );
44 if (g.cgi.param('copies')) g.copies = g.copies.concat( JSON2js( g.cgi.param('copies') ) );
46 /******************************************************************************************************/
47 /* We try to retrieve callnumbers for existing copies, but for new copies, we rely on this */
49 if (window.xulG && window.xulG.callnumbers) g.callnumbers = window.xulG.callnumbers;
50 if (g.cgi.param('callnumbers')) g.callnumbers = JSON2js( g.cgi.param('callnumbers') );
52 /******************************************************************************************************/
53 /* Is the interface an editor or a viewer? */
55 if (g.cgi.param('edit') == '1') {
57 document.getElementById('caption').setAttribute('label','Copy Editor');
58 document.getElementById('nav').setAttribute('hidden','false');
61 if (g.cgi.param('single_edit') == '1') {
63 document.getElementById('caption').setAttribute('label','Copy Editor');
64 document.getElementById('nav').setAttribute('hidden','false');
67 /******************************************************************************************************/
68 /* Show the Record Details? */
71 document.getElementById('brief_display').setAttribute(
73 urls.XUL_BIB_BRIEF + '?docid=' + g.docid
76 document.getElementById('brief_display').setAttribute('hidden','true');
79 /******************************************************************************************************/
80 /* Add stat cats to the right_pane_field_names */
82 var stat_cat_seen = {};
84 function add_stat_cat(sc) {
86 if (typeof g.data.hash.asc == 'undefined') { g.data.hash.asc = {}; g.data.stash('hash'); }
90 if (typeof sc == 'object') {
95 if (typeof stat_cat_seen[sc_id] != 'undefined') { return; }
97 stat_cat_seen[ sc_id ] = 1;
99 if (typeof sc != 'object') {
101 sc = g.network.simple_request(
102 'FM_ASC_BATCH_RETRIEVE',
103 [ g.session, [ sc_id ] ]
108 g.data.hash.asc[ sc.id() ] = sc; g.data.stash('hash');
110 var label_name = g.data.hash.aou[ sc.owner() ].shortname() + " : " + sc.name();
115 render: 'var l = util.functional.find_list( fm.stat_cat_entries(), function(e){ return e.stat_cat() == '
116 + sc.id() + '; } ); l ? l.value() : null;',
117 input: 'x = util.widgets.make_menulist( util.functional.map_list( g.data.hash.asc[' + sc.id()
118 + '].entries(), function(obj){ return [ obj.value(), obj.id() ]; } ).sort() ); '
119 + 'x.addEventListener("command",function(ev) { g.apply_stat_cat(' + sc.id()
120 + ', ev.target.value); } ,false);',
124 dump('temp_array = ' + js2JSON(temp_array) + '\n');
126 g.right_pane_field_names.push( temp_array );
129 /* The stat cats for the pertinent library */
130 for (var i = 0; i < g.data.list.my_asc.length; i++) {
131 add_stat_cat( g.data.list.my_asc[i] );
134 /* Other stat cats present on these copies */
135 for (var i = 0; i < g.copies.length; i++) {
136 var entries = g.copies[i].stat_cat_entries();
137 if (!entries) entries = [];
138 for (var j = 0; j < entries.length; j++) {
139 var sc_id = entries[j].stat_cat();
140 add_stat_cat( sc_id );
144 /******************************************************************************************************/
147 g.summarize( g.copies );
151 var err_msg = "!! This software has encountered an error. Please tell your friendly " +
152 "system administrator or software developer the following:\ncat/copy_editor.xul\n" + E + '\n';
153 try { g.error.sdump('D_ERROR',err_msg); } catch(E) { dump(err_msg); dump(js2JSON(E)); }
158 /******************************************************************************************************/
159 /* Apply a value to a specific field on all the copies being edited */
161 g.apply = function(field,value) {
162 g.error.sdump('D_TRACE','field = ' + field + ' value = ' + value + '\n');
163 for (var i = 0; i < g.copies.length; i++) {
164 var copy = g.copies[i];
166 copy[field]( value ); copy.ischanged('1');
173 /******************************************************************************************************/
174 /* Apply a stat cat entry to all the copies being edited */
176 g.apply_stat_cat = function(sc_id,entry_id) {
177 g.error.sdump('D_TRACE','sc_id = ' + sc_id + ' entry_id = ' + entry_id + '\n');
178 for (var i = 0; i < g.copies.length; i++) {
179 var copy = g.copies[i];
182 var temp = copy.stat_cat_entries();
183 if (!temp) temp = [];
184 temp = util.functional.filter_list(
187 return (obj.stat_cat() != sc_id);
191 util.functional.find_id_object_in_list(
192 g.data.hash.asc[sc_id].entries(),
196 copy.stat_cat_entries( temp );
205 /******************************************************************************************************/
206 /* These need data from the middle layer to render */
208 g.special_exception = {
209 'Call Number' : function(label,value) {
210 if (value>0) { /* an existing call number */
212 api.FM_ACN_RETRIEVE.app,
213 api.FM_ACN_RETRIEVE.method,
216 var cn = '??? id = ' + value;
218 cn = req.getResultObject().label();
220 g.error.sdump('D_ERROR','callnumber retrieve: ' + E);
222 label.setAttribute('value',cn);
225 } else { /* a yet to be created call number */
227 label.setAttribute('value',g.callnumbers[value]);
231 'Creator' : function(label,value) {
233 api.FM_AU_RETRIEVE_VIA_ID.app,
234 api.FM_AU_RETRIEVE_VIA_ID.method,
235 [ g.session, value ],
237 var p = '??? id = ' + value;
239 p = req.getResultObject();
240 p = p.card().barcode() + ' : ' + p.family_name();
243 g.error.sdump('D_ERROR','patron retrieve: ' + E);
245 label.setAttribute('value',p);
249 'Last Editor' : function(label,value) {
251 api.FM_AU_RETRIEVE_VIA_ID.app,
252 api.FM_AU_RETRIEVE_VIA_ID.method,
253 [ g.session, value ],
255 var p = '??? id = ' + value;
257 p = req.getResultObject();
258 p = p.card().barcode() + ' : ' + p.family_name();
261 g.error.sdump('D_ERROR','patron retrieve: ' + E);
263 label.setAttribute('value',p);
270 /******************************************************************************************************/
271 g.readonly_stat_cat_names = [];
272 g.editable_stat_cat_names = [];
274 /******************************************************************************************************/
275 /* These get show in the left panel */
277 g.left_pane_field_names = [
281 render: 'fm.barcode();',
287 render: 'fm.call_number();',
293 render: 'util.date.formatted_date( fm.create_date(), "%F");',
299 render: 'util.date.formatted_date( fm.edit_date(), "%F");',
305 /******************************************************************************************************/
306 /* These get shown in the right panel */
308 g.right_pane_field_names = [
312 render: 'fm.creator();',
318 render: 'fm.editor();',
324 render: 'fm.circ_as_type();',
325 input: 'x = document.createElement("textbox"); x.addEventListener("change",function(ev) { g.apply("circ_as_type",ev.target.value); }, false);',
329 "Circulation Library",
331 render: 'fm.circ_lib().shortname();',
332 input: 'x = util.widgets.make_menulist( util.functional.map_list( util.functional.filter_list(g.data.list.my_aou, function(obj) { return g.data.hash.aout[ obj.ou_type() ].can_have_vols(); }), function(obj) { return [ obj.shortname(), obj.id() ]; }).sort() ); x.addEventListener("command",function(ev) { g.apply("circ_lib",ev.target.value); }, false);',
336 "Circulation Modifier",
338 render: 'fm.circ_modifier();',
339 input: 'x = document.createElement("textbox"); x.addEventListener("change",function(ev) { g.apply("circ_modifier",ev.target.value); }, false);',
345 render: 'fm.circulate() ? "Yes" : "No";',
346 input: 'x = util.widgets.make_menulist( [ [ "Yes", "1" ], [ "No", "0" ] ] ); x.addEventListener("command",function(ev) { g.apply("circulate",ev.target.value); }, false);',
352 render: 'fm.copy_number();',
353 input: 'x = document.createElement("textbox"); x.addEventListener("change",function(ev) { g.apply("copy_number",ev.target.value); }, false);',
359 render: 'fm.deposit() ? "Yes" : "No";',
360 input: 'x = util.widgets.make_menulist( [ [ "Yes", "1" ], [ "No", "0" ] ] ); x.addEventListener("command",function(ev) { g.apply("deposit",ev.target.value); }, false);',
366 render: 'util.money.sanitize( fm.deposit_amount() );',
367 input: 'x = document.createElement("textbox"); x.addEventListener("change",function(ev) { g.apply("deposit_amount",ev.target.value); }, false);',
373 render: 'switch(fm.fine_level()){ case 1: "Low"; break; case 2: "Normal"; break; case 3: "High"; break; }',
374 input: 'x = util.widgets.make_menulist( [ [ "Low", "1" ], [ "Normal", "2" ], [ "High", "3" ] ] ); x.addEventListener("command",function(ev) { g.apply("fine_level",ev.target.value); }, false);',
380 render: 'fm.holdable() ? "Yes" : "No";',
381 input: 'x = util.widgets.make_menulist( [ [ "Yes", "1" ], [ "No", "0" ] ] ); x.addEventListener("command",function(ev) { g.apply("holdable",ev.target.value); }, false);',
387 render: 'switch(fm.loan_duration()){ case 1: "Short"; break; case 2: "Normal"; break; case 3: "Long"; break; }',
388 input: 'x = util.widgets.make_menulist( [ [ "Short", "1" ], [ "Normal", "2" ], [ "Long", "3" ] ] ); x.addEventListener("command",function(ev) { g.apply("loan_duration",ev.target.value); }, false);',
395 render: 'fm.location().name();',
396 input: 'x = util.widgets.make_menulist( util.functional.map_list( g.data.list.acpl, function(obj) { return [ obj.name(), obj.id() ]; }).sort()); x.addEventListener("command",function(ev) { g.apply("location",ev.target.value); }, false);',
403 render: 'fm.opac_visible() ? "Yes" : "No";',
404 input: 'x = util.widgets.make_menulist( [ [ "Yes", "1" ], [ "No", "0" ] ] ); x.addEventListener("command",function(ev) { g.apply("opac_visible",ev.target.value); }, false);',
410 render: 'util.money.sanitize( fm.price() );',
411 input: 'x = document.createElement("textbox"); x.addEventListener("change",function(ev) { g.apply("deposit_amount",ev.target.value); }, false);',
417 render: 'fm.ref() ? "Yes" : "No";',
418 input: 'x = util.widgets.make_menulist( [ [ "Yes", "1" ], [ "No", "0" ] ] ); x.addEventListener("command",function(ev) { g.apply("ref",ev.target.value); }, false);',
424 render: 'fm.status().name();',
425 input: 'x = util.widgets.make_menulist( util.functional.map_list( g.data.list.ccs, function(obj) { return [ obj.name(), obj.id() ]; } ).sort() ); x.addEventListener("command",function(ev) { g.apply("status",ev.target.value); }, false);',
432 /******************************************************************************************************/
433 /* This loops through all our fieldnames and all the copies, tallying up counts for the different values */
435 g.summarize = function( copies ) {
436 /******************************************************************************************************/
439 JSAN.use('util.date'); JSAN.use('util.money');
441 g.field_names = g.left_pane_field_names;
442 g.field_names = g.field_names.concat( g.right_pane_field_names );
443 g.field_names = g.field_names.concat( g.editable_stat_cat_names );
444 g.field_names = g.field_names.concat( g.readonly_stat_cat_names );
446 /******************************************************************************************************/
447 /* Loop through the field names */
449 for (var i = 0; i < g.field_names.length; i++) {
451 var field_name = g.field_names[i][0];
452 var render = g.field_names[i][1].render;
453 g.summary[ field_name ] = {};
455 /******************************************************************************************************/
456 /* Loop through the copies */
458 for (var j = 0; j < copies.length; j++) {
461 var cmd = render || ('fm.' + field_name + '();');
464 /**********************************************************************************************/
465 /* Try to retrieve the value for this field for this copy */
470 g.error.sdump('D_ERROR','Attempted ' + cmd + '\n' + E + '\n');
472 if (typeof value == 'object' && value != null) {
473 alert('FIXME: field_name = ' + field_name + ' value = ' + js2JSON(value) + '\n');
476 /**********************************************************************************************/
477 /* Tally the count */
479 if (g.summary[ field_name ][ value ]) {
480 g.summary[ field_name ][ value ]++;
482 g.summary[ field_name ][ value ] = 1;
486 g.error.sdump('D_TRACE','summary = ' + js2JSON(g.summary) + '\n');
489 /******************************************************************************************************/
490 /* Display the summarized data and inputs for editing */
492 g.render = function() {
494 /******************************************************************************************************/
495 /* Library setup and clear any existing interface */
497 JSAN.use('util.widgets'); JSAN.use('util.date'); JSAN.use('util.money'); JSAN.use('util.functional');
499 var cns = document.getElementById('call_number_summary');
500 util.widgets.remove_children( cns );
501 var bcs = document.getElementById('barcode_summary');
502 util.widgets.remove_children( bcs );
503 var rp = document.getElementById('right_pane');
504 util.widgets.remove_children( rp );
506 /******************************************************************************************************/
507 /* Make the call number summary */
509 var grid = util.widgets.make_grid( [ { 'flex' : '1' } ] );
510 cns.appendChild(grid);
511 for (var i in g.summary['Call Number']) {
512 var cn_id = i; var count = g.summary['Call Number'][i];
513 var row = document.createElement('row'); grid.lastChild.appendChild(row);
514 var cn_label = document.createElement('label'); row.appendChild(cn_label);
515 g.special_exception['Call Number']( cn_label, cn_id );
516 var count_label = document.createElement('label'); row.appendChild(count_label);
517 var unit = count == 1 ? 'copy' : 'copies';
518 count_label.setAttribute('value',count + ' ' + unit);
521 /******************************************************************************************************/
522 /* List the copy barcodes */
524 for (var i in g.summary['Barcode']) {
526 var hbox = document.createElement('hbox'); bcs.appendChild(hbox);
527 var bc_label = document.createElement('label'); hbox.appendChild(bc_label);
528 bc_label.setAttribute('value',bc);
531 /******************************************************************************************************/
532 /* List the other non-editable fields in this pane */
534 var groupbox; var caption; var vbox; var grid; var rows;
535 for (var i = 0; i < g.left_pane_field_names.length; i++) {
537 var f = g.left_pane_field_names[i]; var fn = f[0];
538 if (fn == 'Call Number' || fn == 'Barcode') continue;
539 groupbox = document.createElement('groupbox'); bcs.parentNode.parentNode.appendChild(groupbox);
540 caption = document.createElement('caption'); groupbox.appendChild(caption);
541 caption.setAttribute('label',fn);
542 vbox = document.createElement('vbox'); groupbox.appendChild(vbox);
543 grid = util.widgets.make_grid( [ { 'flex' : 1 }, {}, {} ] ); vbox.appendChild(grid);
544 grid.setAttribute('flex','1');
545 rows = grid.lastChild;
548 /**************************************************************************************/
549 /* Loop through each value for the field */
551 for (var j in g.summary[fn]) {
552 var value = j; var count = g.summary[fn][j];
553 row = document.createElement('row'); rows.appendChild(row);
554 var label1 = document.createElement('label'); row.appendChild(label1);
555 if (g.special_exception[ fn ]) {
556 g.special_exception[ fn ]( label1, value );
558 label1.setAttribute('value',value);
560 var label2 = document.createElement('label'); row.appendChild(label2);
561 var unit = count == 1 ? 'copy' : 'copies';
562 label2.setAttribute('value',count + ' ' + unit);
564 var hbox = document.createElement('hbox');
565 vbox.appendChild(hbox);
567 g.error.sdump('D_ERROR','copy editor: ' + E + '\n');
571 /******************************************************************************************************/
572 /* Prepare the right panel, which is different for 1-copy view and multi-copy view */
576 /******************************************************************************************************/
577 /* For a less dangerous batch edit, choose one field here */
579 var gb = document.createElement('groupbox'); rp.appendChild(gb);
580 var c = document.createElement('caption'); gb.appendChild(c);
581 c.setAttribute('label','Choose a field to edit');
582 JSAN.use('util.widgets'); JSAN.use('util.functional');
583 var ml = util.widgets.make_menulist(
584 util.functional.map_list(
585 g.right_pane_field_names,
586 function(o,i) { return [ o[0], i ]; }
593 g.render_input(gb, g.right_pane_field_names[ ev.target.value ][1].input);
601 if (g.copies.length == 1) {
603 /******************************************************************************************************/
604 /* 1-copy mode has a single groupbox and each field is a row on a grid */
606 var groupbox; var caption; var vbox; var grid; var rows;
607 groupbox = document.createElement('groupbox'); rp.appendChild(groupbox);
608 caption = document.createElement('caption'); groupbox.appendChild(caption);
609 caption.setAttribute('label','Fields');
610 vbox = document.createElement('vbox'); groupbox.appendChild(vbox);
611 grid = util.widgets.make_grid( [ {}, { 'flex' : 1 } ] ); vbox.appendChild(grid);
612 grid.setAttribute('flex','1');
613 rows = grid.lastChild;
615 /******************************************************************************************************/
616 /* Loop through the field names */
618 for (var i = 0; i < g.right_pane_field_names.length; i++) {
620 var f = g.right_pane_field_names[i]; var fn = f[0];
623 /**************************************************************************************/
624 /* Loop through each value for the field */
626 for (var j in g.summary[fn]) {
627 var value = j; var count = g.summary[fn][j];
628 row = document.createElement('row'); rows.appendChild(row);
629 var label0 = document.createElement('label'); row.appendChild(label0);
630 label0.setAttribute('value',fn);
631 label0.setAttribute('style','font-weight: bold');
632 var label1 = document.createElement('label'); row.appendChild(label1);
633 if (g.special_exception[ fn ]) {
634 g.special_exception[ fn ]( label1, value );
636 label1.setAttribute('value',value);
641 /**************************************************************************************/
642 /* Render the input widget */
644 var hbox = document.createElement('hbox');
645 hbox.setAttribute('id',fn);
646 row.setAttribute('style','border-bottom: dotted black thin');
647 row.appendChild(hbox);
648 if (f[1].input && g.edit) {
649 g.render_input(hbox,f[1].input);
653 g.error.sdump('D_ERROR','copy editor: ' + E + '\n');
659 /******************************************************************************************************/
660 /* multi-copy mode has a groupbox for each field */
662 var groupbox; var caption; var vbox; var grid; var rows;
664 /******************************************************************************************************/
665 /* Loop through the field names */
667 for (var i = 0; i < g.right_pane_field_names.length; i++) {
669 var f = g.right_pane_field_names[i]; var fn = f[0];
670 groupbox = document.createElement('groupbox'); rp.appendChild(groupbox);
671 caption = document.createElement('caption'); groupbox.appendChild(caption);
672 caption.setAttribute('label',fn);
673 vbox = document.createElement('vbox'); groupbox.appendChild(vbox);
674 grid = util.widgets.make_grid( [ { 'flex' : 1 }, {}, {} ] ); vbox.appendChild(grid);
675 grid.setAttribute('flex','1');
676 rows = grid.lastChild;
679 /**************************************************************************************/
680 /* Loop through each value for the field */
682 for (var j in g.summary[fn]) {
683 var value = j; var count = g.summary[fn][j];
684 row = document.createElement('row'); rows.appendChild(row);
685 var label1 = document.createElement('label'); row.appendChild(label1);
686 if (g.special_exception[ fn ]) {
687 g.special_exception[ fn ]( label1, value );
689 label1.setAttribute('value',value);
691 var label2 = document.createElement('label'); row.appendChild(label2);
692 var unit = count == 1 ? 'copy' : 'copies';
693 label2.setAttribute('value',count + ' ' + unit);
696 var hbox = document.createElement('hbox');
697 hbox.setAttribute('id',fn);
698 vbox.appendChild(hbox);
700 /**************************************************************************************/
701 /* Render the input widget */
703 if (f[1].input && g.edit) {
704 g.render_input(hbox,f[1].input);
707 g.error.sdump('D_ERROR','copy editor: ' + E + '\n');
713 /******************************************************************************************************/
714 /* This actually draws the change button and input widget for a given field */
715 g.render_input = function(node,input_cmd) {
717 var spacer = document.createElement('spacer'); node.appendChild(spacer);
718 spacer.setAttribute('flex','1');
719 var deck = document.createElement('deck'); node.appendChild(deck);
720 var btn = document.createElement('button'); deck.appendChild(btn);
721 deck.setAttribute('style','width: 200px; min-width: 200px;');
722 btn.setAttribute('label','Change');
723 btn.setAttribute('oncommand','this.parentNode.selectedIndex = 1;');
724 var x; eval( input_cmd );
725 if (x) deck.appendChild(x);
728 g.error.sdump('D_ERROR',E + '\n');
732 /******************************************************************************************************/
733 /* store the copies in the global xpcom stash */
735 g.stash_and_close = function() {
736 if (g.handle_update) {
738 var r = g.network.request(
739 api.FM_ACP_FLESHED_BATCH_UPDATE.app,
740 api.FM_ACP_FLESHED_BATCH_UPDATE.method,
741 [ g.session, g.copies ]
743 /* FIXME -- revisit the return value here */
745 alert('copy update error: ' + js2JSON(E));
748 g.data.temp = js2JSON( g.copies );
749 g.error.sdump('D_CAT','in modal window, g.data.temp = \n' + g.data.temp + '\n');
750 g.data.stash('temp');