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