]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/copy_editor.js
just show the usrname for Editor and Creator
[Evergreen.git] / Open-ILS / xul / staff_client / server / cat / copy_editor.js
1 var g = {};
2
3 function my_init() {
4         try {
5                 /******************************************************************************************************/
6                 /* setup JSAN and some initial libraries */
7
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');
14
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();
18
19                 g.cgi = new CGI();
20
21                 g.docid = g.cgi.param('docid');
22                 g.handle_update = g.cgi.param('handle_update');
23
24                 /******************************************************************************************************/
25                 /* Get the copy ids from various sources and flesh them */
26
27                 var copy_ids = [];
28                 if (g.cgi.param('copy_ids')) copy_ids = JSON2js( g.cgi.param('copy_ids') );
29                 if (!copy_ids) copy_ids = [];
30                 if (window.xulG && window.xulG.copy_ids) copy_ids = copy_ids.concat( window.xulG.copy_ids );
31
32                 if (copy_ids.length > 0) g.copies = g.network.request(
33                         api.FM_ACP_FLESHED_BATCH_RETRIEVE.app,
34                         api.FM_ACP_FLESHED_BATCH_RETRIEVE.method,
35                         [ copy_ids ]
36                 );
37
38                 /******************************************************************************************************/
39                 /* And other fleshed copies if any */
40
41                 if (!g.copies) g.copies = [];
42                 if (window.xulG && window.xulG.copies) g.copies = g.copies.concat( window.xulG.copies );
43                 if (g.cgi.param('copies')) g.copies = g.copies.concat( JSON2js( g.cgi.param('copies') ) );
44
45                 /******************************************************************************************************/
46                 /* We try to retrieve callnumbers for existing copies, but for new copies, we rely on this */
47
48                 if (window.xulG && window.xulG.callnumbers) g.callnumbers = window.xulG.callnumbers;
49                 if (g.cgi.param('callnumbers')) g.callnumbers =  JSON2js( g.cgi.param('callnumbers') );
50
51                 /******************************************************************************************************/
52                 /* Is the interface an editor or a viewer, single or multi copy, existing copies or new copies? */
53
54                 if (g.cgi.param('edit') == '1') { 
55                         g.edit = true;
56                         document.getElementById('caption').setAttribute('label','Copy Editor'); 
57                         document.getElementById('save').setAttribute('hidden','false'); 
58                 }
59
60                 if (g.cgi.param('single_edit') == '1') {
61                         g.single_edit = true;
62                         document.getElementById('caption').setAttribute('label','Copy Editor'); 
63                         document.getElementById('save').setAttribute('hidden','false'); 
64                 }
65
66                 if (g.copies.length > 0 && g.copies[0].id() < 0) {
67                         document.getElementById('copy_notes').setAttribute('hidden','true');
68                         g.apply("status",5 /* In Process */);
69                 } else {
70                         g.right_pane_field_names.push(
71                                 [
72                                         "Status",
73                                         { 
74                                                 render: 'fm.status().name();', 
75                                                 input: 'c = function(v){ g.apply("status",v); }; 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(f){ return function(ev) { f(ev.target.value); } }(c), false);',
76                                         }
77                                 ]
78                         );
79                 }
80
81                 if (g.copies.length != 1) {
82                         document.getElementById('copy_notes').setAttribute('hidden','true');
83                 }
84
85                 /******************************************************************************************************/
86                 /* Show the Record Details? */
87
88                 if (g.docid) {
89                         document.getElementById('brief_display').setAttribute(
90                                 'src',
91                                 urls.XUL_BIB_BRIEF + '?docid=' + g.docid
92                         );
93                 } else {
94                         document.getElementById('brief_display').setAttribute('hidden','true');
95                 }
96
97                 /******************************************************************************************************/
98                 /* Add stat cats to the right_pane_field_names */
99
100                 var stat_cat_seen = {};
101
102                 function add_stat_cat(sc) {
103
104                         if (typeof g.data.hash.asc == 'undefined') { g.data.hash.asc = {}; g.data.stash('hash'); }
105
106                         var sc_id = sc;
107
108                         if (typeof sc == 'object') {
109
110                                 sc_id = sc.id();
111                         }
112
113                         if (typeof stat_cat_seen[sc_id] != 'undefined') { return; }
114
115                         stat_cat_seen[ sc_id ] = 1;
116
117                         if (typeof sc != 'object') {
118
119                                 sc = g.network.simple_request(
120                                         'FM_ASC_BATCH_RETRIEVE',
121                                         [ ses(), [ sc_id ] ]
122                                 )[0];
123
124                         }
125
126                         g.data.hash.asc[ sc.id() ] = sc; g.data.stash('hash');
127
128                         var label_name = g.data.hash.aou[ sc.owner() ].shortname() + " : " + sc.name();
129
130                         var temp_array = [
131                                 label_name,
132                                 {
133                                         render: 'var l = util.functional.find_list( fm.stat_cat_entries(), function(e){ return e.stat_cat() == ' 
134                                                 + sc.id() + '; } ); l ? l.value() : null;',
135                                         input: 'c = function(v){ g.apply_stat_cat(' + sc.id() + ',v); }; x = util.widgets.make_menulist( util.functional.map_list( g.data.hash.asc[' + sc.id() 
136                                                 + '].entries(), function(obj){ return [ obj.value(), obj.id() ]; } ).sort() ); '
137                                                 + 'x.addEventListener("command",function(f){ return function(ev) { f(ev.target.value); } }(c),false);',
138                                 }
139                         ];
140
141                         dump('temp_array = ' + js2JSON(temp_array) + '\n');
142
143                         g.right_pane_field_names.push( temp_array );
144                 }
145
146                 /* The stat cats for the pertinent library */
147                 for (var i = 0; i < g.data.list.my_asc.length; i++) {
148                         add_stat_cat( g.data.list.my_asc[i] );  
149                 }
150
151                 /* Other stat cats present on these copies */
152                 for (var i = 0; i < g.copies.length; i++) {
153                         var entries = g.copies[i].stat_cat_entries();
154                         if (!entries) entries = [];
155                         for (var j = 0; j < entries.length; j++) {
156                                 var sc_id = entries[j].stat_cat();
157                                 add_stat_cat( sc_id );
158                         }
159                 }
160
161                 /******************************************************************************************************/
162                 /* Do it */
163
164                 g.summarize( g.copies );
165                 g.render();
166
167         } catch(E) {
168                 var err_msg = "!! This software has encountered an error.  Please tell your friendly " +
169                         "system administrator or software developer the following:\ncat/copy_editor.xul\n" + E + '\n';
170                 try { g.error.sdump('D_ERROR',err_msg); } catch(E) { dump(err_msg); dump(js2JSON(E)); }
171                 alert(err_msg);
172         }
173 }
174
175 /******************************************************************************************************/
176 /* Apply a value to a specific field on all the copies being edited */
177
178 g.apply = function(field,value) {
179         g.error.sdump('D_TRACE','field = ' + field + '  value = ' + value + '\n');
180         for (var i = 0; i < g.copies.length; i++) {
181                 var copy = g.copies[i];
182                 try {
183                         copy[field]( value ); copy.ischanged('1');
184                 } catch(E) {
185                         alert(E);
186                 }
187         }
188 }
189
190 /******************************************************************************************************/
191 /* Apply a stat cat entry to all the copies being edited */
192
193 g.apply_stat_cat = function(sc_id,entry_id) {
194         g.error.sdump('D_TRACE','sc_id = ' + sc_id + '  entry_id = ' + entry_id + '\n');
195         for (var i = 0; i < g.copies.length; i++) {
196                 var copy = g.copies[i];
197                 try {
198                         copy.ischanged('1');
199                         var temp = copy.stat_cat_entries();
200                         if (!temp) temp = [];
201                         temp = util.functional.filter_list(
202                                 temp,
203                                 function (obj) {
204                                         return (obj.stat_cat() != sc_id);
205                                 }
206                         );
207                         temp.push( 
208                                 util.functional.find_id_object_in_list( 
209                                         g.data.hash.asc[sc_id].entries(), 
210                                         entry_id
211                                 )
212                         );
213                         copy.stat_cat_entries( temp );
214
215                 } catch(E) {
216                         alert(E);
217                 }
218         }
219 }
220
221
222 /******************************************************************************************************/
223 /* These need data from the middle layer to render */
224
225 g.special_exception = {
226         'Call Number' : function(label,value) {
227                 if (value>0) { /* an existing call number */
228                         g.network.request(
229                                 api.FM_ACN_RETRIEVE.app,
230                                 api.FM_ACN_RETRIEVE.method,
231                                 [ value ],
232                                 function(req) {
233                                         var cn = '??? id = ' + value;
234                                         try {
235                                                 cn = req.getResultObject().label();
236                                         } catch(E) {
237                                                 g.error.sdump('D_ERROR','callnumber retrieve: ' + E);
238                                         }
239                                         label.setAttribute('value',cn);
240                                 }
241                         );
242                 } else { /* a yet to be created call number */
243                         if (g.callnumbers) {
244                                 label.setAttribute('value',g.callnumbers[value]);
245                         }
246                 }
247         },
248         'Creator' : function(label,value) {
249                 if (value == null || value == '' || value == 'null') return;
250                 g.network.simple_request(
251                         'FM_AU_RETRIEVE_VIA_ID',
252                         [ ses(), value ],
253                         function(req) {
254                                 var p = '??? id = ' + value;
255                                 try {
256                                         p = req.getResultObject();
257                                         p = p.usrname();
258
259                                 } catch(E) {
260                                         g.error.sdump('D_ERROR','patron retrieve: ' + E);
261                                 }
262                                 label.setAttribute('value',p);
263                         }
264                 );
265         },
266         'Last Editor' : function(label,value) {
267                 if (value == null || value == '' || value == 'null') return;
268                 g.network.simple_request(
269                         'FM_AU_RETRIEVE_VIA_ID',
270                         [ ses(), value ],
271                         function(req) {
272                                 var p = '??? id = ' + value;
273                                 try {
274                                         p = req.getResultObject();
275                                         p = p.usrname();
276
277                                 } catch(E) {
278                                         g.error.sdump('D_ERROR','patron retrieve: ' + E);
279                                 }
280                                 label.setAttribute('value',p);
281                         }
282                 );
283         }
284
285 }
286
287 /******************************************************************************************************/
288 g.readonly_stat_cat_names = [];
289 g.editable_stat_cat_names = [];
290
291 /******************************************************************************************************/
292 /* These get show in the left panel */
293
294 g.left_pane_field_names = [
295         [
296                 "Barcode",               
297                 {
298                         render: 'fm.barcode();',
299                 }
300         ], 
301         [
302                 "Call Number",  
303                 {
304                         render: 'fm.call_number();',
305                 }
306         ],
307         [
308                 "Creation Date",
309                 { 
310                         render: 'util.date.formatted_date( fm.create_date(), "%F");',
311                 }
312         ],
313         [
314                 "Last Edit Date",
315                 { 
316                         render: 'util.date.formatted_date( fm.edit_date(), "%F");',
317                 }
318         ],
319         [
320                 "Creator",
321                 { 
322                         render: 'fm.creator();',
323                 }
324         ],
325         [
326                 "Last Editor",
327                 {
328                         render: 'fm.editor();',
329                 }
330         ],
331
332 ];
333
334 /******************************************************************************************************/
335 /* These get shown in the right panel */
336
337 g.right_pane_field_names = [
338         [
339                 "Alert Message",
340                 {
341                         render: 'fm.alert_message();',
342                         input: 'c = function(v){ g.apply("alert_message",v); }; x = document.createElement("textbox"); x.addEventListener("change",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
343                 }
344         ],
345          [
346                 "Circulate as Type",    
347                 {       
348                         render: 'fm.circ_as_type();',
349                         input: 'c = function(v){ g.apply("circ_as_type",v); }; x = document.createElement("textbox"); x.addEventListener("change",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
350                 } 
351         ],
352         [
353                 "Circulation Library",          
354                 {       
355                         render: 'fm.circ_lib().shortname();',
356                         input: 'c = function(v){ g.apply("circ_lib",v); }; 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(f){ return function(ev) { f(ev.target.value); } }(c), false);',
357                 } 
358         ],
359         [
360                 "Circulation Modifier",
361                 {       
362                         render: 'fm.circ_modifier();',
363                         input: 'c = function(v){ g.apply("circ_modifier",v); }; x = document.createElement("textbox"); x.addEventListener("change",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
364                 }
365         ],
366         [
367                 "Circulate?",
368                 {       
369                         render: 'fm.circulate() == null ? "<Unset>" : ( fm.circulate() == 1 ? "Yes" : "No" )',
370                         input: 'c = function(v){ g.apply("circulate",v); }; x = util.widgets.make_menulist( [ [ "Yes", "1" ], [ "No", "0" ] ] ); x.addEventListener("command",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
371                 }
372         ],
373         [
374                 "Copy Number",
375                 { 
376                         render: 'fm.copy_number();',
377                         input: 'c = function(v){ g.apply("copy_number",v); }; x = document.createElement("textbox"); x.addEventListener("change",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
378                 }
379         ],
380         [
381                 "Deposit?",
382                 { 
383                         render: 'fm.deposit() ? "Yes" : "No";',
384                         input: 'c = function(v){ g.apply("deposit",v); }; x = util.widgets.make_menulist( [ [ "Yes", "1" ], [ "No", "0" ] ] ); x.addEventListener("command",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
385                 }
386         ],
387         [
388                 "Deposit Amount",
389                 { 
390                         render: 'util.money.sanitize( fm.deposit_amount() );',
391                         input: 'c = function(v){ g.apply("deposit_amount",v); }; x = document.createElement("textbox"); x.addEventListener("change",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
392                 }
393         ],
394         [
395                 "Fine Level",
396                 {
397                         render: 'switch(fm.fine_level()){ case 1: "Low"; break; case 2: "Normal"; break; case 3: "High"; break; }',
398                         input: 'c = function(v){ g.apply("fine_level",v); }; x = util.widgets.make_menulist( [ [ "Low", "1" ], [ "Normal", "2" ], [ "High", "3" ] ] ); x.addEventListener("command",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
399                 }
400         ],
401         [
402                 "Holdable?",
403                 { 
404                         render: 'fm.holdable() ? "Yes" : "No";', 
405                         input: 'c = function(v){ g.apply("holdable",v); }; x = util.widgets.make_menulist( [ [ "Yes", "1" ], [ "No", "0" ] ] ); x.addEventListener("command",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
406                 }
407         ],
408         [
409                 "Loan Duration",
410                 { 
411                         render: 'switch(fm.loan_duration()){ case 1: "Short"; break; case 2: "Normal"; break; case 3: "Long"; break; }',
412                         input: 'c = function(v){ g.apply("loan_duration",v); }; x = util.widgets.make_menulist( [ [ "Short", "1" ], [ "Normal", "2" ], [ "Long", "3" ] ] ); x.addEventListener("command",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
413
414                 }
415         ],
416         [
417                 "Shelving Location",
418                 { 
419                         render: 'fm.location().name();', 
420                         input: 'c = function(v){ g.apply("location",v); }; 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(f){ return function(ev) { f(ev.target.value); } }(c), false);',
421
422                 }
423         ],
424         [
425                 "OPAC Visible?",
426                 { 
427                         render: 'fm.opac_visible() ? "Yes" : "No";', 
428                         input: 'c = function(v){ g.apply("opac_visible",v); }; x = util.widgets.make_menulist( [ [ "Yes", "1" ], [ "No", "0" ] ] ); x.addEventListener("command",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
429                 }
430         ],
431         [
432                 "Price",
433                 { 
434                         render: 'util.money.sanitize( fm.price() );', 
435                         input: 'c = function(v){ g.apply("price",v); }; x = document.createElement("textbox"); x.addEventListener("change",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
436                 }
437         ],
438         [
439                 "Reference?",
440                 { 
441                         render: 'fm.ref() ? "Yes" : "No";', 
442                         input: 'c = function(v){ g.apply("ref",v); }; x = util.widgets.make_menulist( [ [ "Yes", "1" ], [ "No", "0" ] ] ); x.addEventListener("command",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
443                 }
444         ],
445 ];
446
447 /******************************************************************************************************/
448 /* This loops through all our fieldnames and all the copies, tallying up counts for the different values */
449
450 g.summarize = function( copies ) {
451         /******************************************************************************************************/
452         /* Setup */
453
454         JSAN.use('util.date'); JSAN.use('util.money');
455         g.summary = {};
456         g.field_names = g.left_pane_field_names;
457         g.field_names = g.field_names.concat( g.right_pane_field_names );
458         g.field_names = g.field_names.concat( g.editable_stat_cat_names );
459         g.field_names = g.field_names.concat( g.readonly_stat_cat_names );
460
461         /******************************************************************************************************/
462         /* Loop through the field names */
463
464         for (var i = 0; i < g.field_names.length; i++) {
465
466                 var field_name = g.field_names[i][0];
467                 var render = g.field_names[i][1].render;
468                 g.summary[ field_name ] = {};
469
470                 /******************************************************************************************************/
471                 /* Loop through the copies */
472
473                 for (var j = 0; j < copies.length; j++) {
474
475                         var fm = copies[j];
476                         var cmd = render || ('fm.' + field_name + '();');
477                         var value = '???';
478
479                         /**********************************************************************************************/
480                         /* Try to retrieve the value for this field for this copy */
481
482                         try { 
483                                 value = eval( cmd ); 
484                         } catch(E) { 
485                                 g.error.sdump('D_ERROR','Attempted ' + cmd + '\n' +  E + '\n'); 
486                         }
487                         if (typeof value == 'object' && value != null) {
488                                 alert('FIXME: field_name = ' + field_name + '  value = ' + js2JSON(value) + '\n');
489                         }
490
491                         /**********************************************************************************************/
492                         /* Tally the count */
493
494                         if (g.summary[ field_name ][ value ]) {
495                                 g.summary[ field_name ][ value ]++;
496                         } else {
497                                 g.summary[ field_name ][ value ] = 1;
498                         }
499                 }
500         }
501         g.error.sdump('D_TRACE','summary = ' + js2JSON(g.summary) + '\n');
502 }
503
504 /******************************************************************************************************/
505 /* Display the summarized data and inputs for editing */
506
507 g.render = function() {
508
509         /******************************************************************************************************/
510         /* Library setup and clear any existing interface */
511
512         JSAN.use('util.widgets'); JSAN.use('util.date'); JSAN.use('util.money'); JSAN.use('util.functional');
513
514         var cns = document.getElementById('call_number_summary');
515         util.widgets.remove_children( cns );
516         var bcs = document.getElementById('barcode_summary');
517         util.widgets.remove_children( bcs );
518         var rp = document.getElementById('right_pane');
519         util.widgets.remove_children( rp );
520
521         /******************************************************************************************************/
522         /* Make the call number summary */
523
524         var grid = util.widgets.make_grid( [ { 'flex' : '1' } ] );
525         cns.appendChild(grid);
526         for (var i in g.summary['Call Number']) {
527                 var cn_id = i; var count = g.summary['Call Number'][i];
528                 var row = document.createElement('row'); grid.lastChild.appendChild(row);
529                 var cn_label = document.createElement('description'); row.appendChild(cn_label);
530                 g.special_exception['Call Number']( cn_label, cn_id );
531                 var count_label = document.createElement('description'); row.appendChild(count_label);
532                 var unit = count == 1 ? 'copy' : 'copies';
533                 count_label.appendChild( document.createTextNode(count + ' ' + unit) );
534         }
535
536         /******************************************************************************************************/
537         /* List the copy barcodes */
538
539         for (var i in g.summary['Barcode']) {
540                 var bc = i;
541                 var hbox = document.createElement('hbox'); bcs.appendChild(hbox);
542                 var bc_label = document.createElement('description'); hbox.appendChild(bc_label);
543                 bc_label.appendChild( document.createTextNode(bc) );
544         }
545
546         /******************************************************************************************************/
547         /* List the other non-editable fields in this pane */
548
549         var groupbox; var caption; var vbox; var grid; var rows;
550         for (var i = 0; i < g.left_pane_field_names.length; i++) {
551                 try {
552                         var f = g.left_pane_field_names[i]; var fn = f[0];
553                         if (fn == 'Call Number' || fn == 'Barcode') continue;
554                         groupbox = document.createElement('groupbox'); bcs.parentNode.parentNode.appendChild(groupbox);
555                         caption = document.createElement('caption'); groupbox.appendChild(caption);
556                         caption.setAttribute('label',fn);
557                         vbox = document.createElement('vbox'); groupbox.appendChild(vbox);
558                         grid = util.widgets.make_grid( [ { 'flex' : 1 }, {}, {} ] ); vbox.appendChild(grid);
559                         grid.setAttribute('flex','1');
560                         rows = grid.lastChild;
561                         var row;
562                         
563                         /**************************************************************************************/
564                         /* Loop through each value for the field */
565
566                         for (var j in g.summary[fn]) {
567                                 var value = j; var count = g.summary[fn][j];
568                                 row = document.createElement('row'); rows.appendChild(row);
569                                 var label1 = document.createElement('description'); row.appendChild(label1);
570                                 if (g.special_exception[ fn ]) {
571                                         g.special_exception[ fn ]( label1, value );
572                                 } else {
573                                         label1.appendChild( document.createTextNode(value) );
574                                 }
575                                 var label2 = document.createElement('description'); row.appendChild(label2);
576                                 var unit = count == 1 ? 'copy' : 'copies';
577                                 label2.appendChild( document.createTextNode(count + ' ' + unit) );
578                         }
579                         var hbox = document.createElement('hbox'); 
580                         vbox.appendChild(hbox);
581                 } catch(E) {
582                         g.error.sdump('D_ERROR','copy editor: ' + E + '\n');
583                 }
584         }
585
586         /******************************************************************************************************/
587         /* Prepare the right panel, which is different for 1-copy view and multi-copy view */
588
589         if (g.single_edit) {
590                 
591                 /******************************************************************************************************/
592                 /* For a less dangerous batch edit, choose one field here */
593
594                 var gb = document.createElement('groupbox'); rp.appendChild(gb);
595                 var c = document.createElement('caption'); gb.appendChild(c);
596                 c.setAttribute('label','Choose a field to edit');
597                 JSAN.use('util.widgets'); JSAN.use('util.functional');
598                 var ml = util.widgets.make_menulist(
599                         util.functional.map_list(
600                                 g.right_pane_field_names,
601                                 function(o,i) { return [ o[0], i ]; }
602                         )
603                 );
604                 gb.appendChild(ml);
605                 ml.addEventListener(
606                         'command',
607                         function(ev) {
608                                 g.render_input(gb, g.right_pane_field_names[ ev.target.value ][1].input);
609                                 ml.disabled = true;
610                         }, 
611                         false
612                 );
613
614         }
615
616         if (g.copies.length == 1) {
617
618                 /******************************************************************************************************/
619                 /* 1-copy mode has a single groupbox and each field is a row on a grid */
620
621                 var groupbox; var caption; var vbox; var grid; var rows;
622                 groupbox = document.createElement('groupbox'); rp.appendChild(groupbox);
623                 caption = document.createElement('caption'); groupbox.appendChild(caption);
624                 caption.setAttribute('label','Fields');
625                 vbox = document.createElement('vbox'); groupbox.appendChild(vbox);
626                 grid = util.widgets.make_grid( [ {}, { 'flex' : 1 } ] ); vbox.appendChild(grid);
627                 grid.setAttribute('flex','1');
628                 rows = grid.lastChild;
629
630                 /******************************************************************************************************/
631                 /* Loop through the field names */
632
633                 for (var i = 0; i < g.right_pane_field_names.length; i++) {
634                         try {
635                                 var f = g.right_pane_field_names[i]; var fn = f[0];
636                                 var row;
637
638                                 /**************************************************************************************/
639                                 /* Loop through each value for the field */
640
641                                 for (var j in g.summary[fn]) {
642                                         var value = j; var count = g.summary[fn][j];
643                                         row = document.createElement('row'); rows.appendChild(row);
644                                         var label0 = document.createElement('description'); row.appendChild(label0);
645                                         label0.appendChild( document.createTextNode(fn) );
646                                         label0.setAttribute('style','font-weight: bold');
647                                         var label1 = document.createElement('description'); row.appendChild(label1);
648                                         if (g.special_exception[ fn ]) {
649                                                 g.special_exception[ fn ]( label1, value );
650                                         } else {
651                                                 label1.appendChild( document.createTextNode(value) );
652                                         }
653
654                                 }
655
656                                 /**************************************************************************************/
657                                 /* Render the input widget */
658
659                                 var hbox = document.createElement('hbox'); 
660                                 hbox.setAttribute('id',fn);
661                                 row.setAttribute('style','border-bottom: dotted black thin');
662                                 row.appendChild(hbox);
663                                 if (f[1].input && g.edit) {
664                                         g.render_input(hbox,f[1].input);
665                                 }
666
667                         } catch(E) {
668                                 g.error.sdump('D_ERROR','copy editor: ' + E + '\n');
669                         }
670                 }
671
672         } else {
673
674                 /******************************************************************************************************/
675                 /* multi-copy mode has a groupbox for each field */
676
677                 var groupbox; var caption; var vbox; var grid; var rows;
678                 
679                 /******************************************************************************************************/
680                 /* Loop through the field names */
681
682                 for (var i = 0; i < g.right_pane_field_names.length; i++) {
683                         try {
684                                 var f = g.right_pane_field_names[i]; var fn = f[0];
685                                 groupbox = document.createElement('groupbox'); rp.appendChild(groupbox);
686                                 caption = document.createElement('caption'); groupbox.appendChild(caption);
687                                 caption.setAttribute('label',fn);
688                                 vbox = document.createElement('vbox'); groupbox.appendChild(vbox);
689                                 grid = util.widgets.make_grid( [ { 'flex' : 1 }, {}, {} ] ); vbox.appendChild(grid);
690                                 grid.setAttribute('flex','1');
691                                 rows = grid.lastChild;
692                                 var row;
693                                 
694                                 /**************************************************************************************/
695                                 /* Loop through each value for the field */
696
697                                 for (var j in g.summary[fn]) {
698                                         var value = j; var count = g.summary[fn][j];
699                                         row = document.createElement('row'); rows.appendChild(row);
700                                         var label1 = document.createElement('description'); row.appendChild(label1);
701                                         if (g.special_exception[ fn ]) {
702                                                 g.special_exception[ fn ]( label1, value );
703                                         } else {
704                                                 label1.appendChild( document.createTextNode(value) );
705                                         }
706                                         var label2 = document.createElement('description'); row.appendChild(label2);
707                                         var unit = count == 1 ? 'copy' : 'copies';
708                                         label2.appendChild( document.createTextNode(count + ' ' + unit) );
709                                 }
710                                 var hbox = document.createElement('hbox'); 
711                                 hbox.setAttribute('id',fn);
712                                 vbox.appendChild(hbox);
713
714                                 /**************************************************************************************/
715                                 /* Render the input widget */
716
717                                 if (f[1].input && g.edit) {
718                                         g.render_input(hbox,f[1].input);
719                                 }
720                         } catch(E) {
721                                 g.error.sdump('D_ERROR','copy editor: ' + E + '\n');
722                         }
723                 }
724         }
725 }
726
727 /******************************************************************************************************/
728 /* This actually draws the change button and input widget for a given field */
729 g.render_input = function(node,input_cmd) {
730         try {
731                 var spacer = document.createElement('spacer'); node.appendChild(spacer);
732                 spacer.setAttribute('flex','1');
733                 var deck = document.createElement('deck'); node.appendChild(deck);
734                 var btn = document.createElement('button'); deck.appendChild(btn);
735                 deck.setAttribute('style','width: 200px; min-width: 200px;');
736                 btn.setAttribute('label','Change');
737                 var x; var c; eval( input_cmd );
738                 btn.addEventListener('command',
739                         function(f) {
740                                 return function(ev) {
741                                         ev.target.parentNode.selectedIndex = 1;
742                                         c(ev.target.parentNode.lastChild.value);
743                                 }
744                         }(c),
745                         false
746                 );
747                 if (x) deck.appendChild(x);
748
749         } catch(E) {
750                 g.error.sdump('D_ERROR',E + '\n');
751         }
752 }
753
754 /******************************************************************************************************/
755 /* store the copies in the global xpcom stash */
756
757 g.stash_and_close = function() {
758         if (g.handle_update) {
759                 try {
760                         var r = g.network.request(
761                                 api.FM_ACP_FLESHED_BATCH_UPDATE.app,
762                                 api.FM_ACP_FLESHED_BATCH_UPDATE.method,
763                                 [ ses(), g.copies ]
764                         );
765                         if (typeof r.ilsevent != 'undefined') {
766                                 g.error.standard_unexpected_error_alert('copy update',r);
767                         }
768                         /* FIXME -- revisit the return value here */
769                 } catch(E) {
770                         alert('copy update error: ' + js2JSON(E));
771                 }
772         }
773         g.data.temp_copies = js2JSON( g.copies );
774         g.data.stash('temp_copies');
775         g.error.sdump('D_CAT','in modal window, g.data.temp_copies = \n' + g.data.temp_copies + '\n');
776         window.close();
777 }
778
779 /******************************************************************************************************/
780 /* spawn copy notes interface */
781
782 g.copy_notes = function() {
783         JSAN.use('util.window'); var win = new util.window();
784         win.open(urls.XUL_COPY_NOTES + '?copy_id=' + window.escape(g.copies[0].id()),'Copy Notes','chrome,resizable,modal');
785 }
786