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