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