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