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