]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/copy_editor.js
template management for copy editor
[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                         g.retrieve_templates();
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.length > 0 && g.copies[0].id() < 0) {
68                         document.getElementById('copy_notes').setAttribute('hidden','true');
69                         g.apply("status",5 /* In Process */);
70                         $('save').setAttribute('label','Create Copies');
71                 } else {
72                         g.panes_and_field_names.left_pane = 
73                                 [
74                                         [
75                                                 "Status",
76                                                 { 
77                                                         render: 'typeof fm.status() == "object" ? fm.status().name() : g.data.hash.ccs[ fm.status() ].name()', 
78                                                         input: 'c = function(v){ g.apply("status",v); if (typeof post_c == "function") post_c(v); }; x = util.widgets.make_menulist( util.functional.map_list( g.data.list.ccs, function(obj) { return [ obj.name(), obj.id() ]; } ).sort() ); x.addEventListener("apply",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
79                                                 }
80                                         ]
81                                 ].concat(g.panes_and_field_names.left_pane);
82                 }
83
84                 if (g.copies.length != 1) {
85                         document.getElementById('copy_notes').setAttribute('hidden','true');
86                 }
87
88                 /******************************************************************************************************/
89                 /* Show the Record Details? */
90
91                 if (g.docid) {
92                         document.getElementById('brief_display').setAttribute(
93                                 'src',
94                                 urls.XUL_BIB_BRIEF + '?docid=' + g.docid
95                         );
96                 } else {
97                         document.getElementById('brief_display').setAttribute('hidden','true');
98                 }
99
100                 /******************************************************************************************************/
101                 /* Add stat cats to the panes_and_field_names.right_pane4 */
102
103                 var stat_cat_seen = {};
104
105                 function add_stat_cat(sc) {
106
107                         if (typeof g.data.hash.asc == 'undefined') { g.data.hash.asc = {}; g.data.stash('hash'); }
108
109                         var sc_id = sc;
110
111                         if (typeof sc == 'object') {
112
113                                 sc_id = sc.id();
114                         }
115
116                         if (typeof stat_cat_seen[sc_id] != 'undefined') { return; }
117
118                         stat_cat_seen[ sc_id ] = 1;
119
120                         if (typeof sc != 'object') {
121
122                                 sc = g.network.simple_request(
123                                         'FM_ASC_BATCH_RETRIEVE',
124                                         [ ses(), [ sc_id ] ]
125                                 )[0];
126
127                         }
128
129                         g.data.hash.asc[ sc.id() ] = sc; g.data.stash('hash');
130
131                         var label_name = g.data.hash.aou[ sc.owner() ].shortname() + " : " + sc.name();
132
133                         var temp_array = [
134                                 label_name,
135                                 {
136                                         render: 'var l = util.functional.find_list( fm.stat_cat_entries(), function(e){ return e.stat_cat() == ' 
137                                                 + sc.id() + '; } ); l ? l.value() : "<Unset>";',
138                                         input: 'c = function(v){ g.apply_stat_cat(' + sc.id() + ',v); if (typeof post_c == "function") post_c(v); }; x = util.widgets.make_menulist( util.functional.map_list( g.data.hash.asc[' + sc.id() 
139                                                 + '].entries(), function(obj){ return [ obj.value(), obj.id() ]; } ).sort() ); '
140                                                 + 'x.addEventListener("apply",function(f){ return function(ev) { f(ev.target.value); } }(c),false);',
141                                 }
142                         ];
143
144                         dump('temp_array = ' + js2JSON(temp_array) + '\n');
145
146                         g.panes_and_field_names.right_pane4.push( temp_array );
147                 }
148
149                 /* The stat cats for the pertinent library */
150                 for (var i = 0; i < g.data.list.my_asc.length; i++) {
151                         add_stat_cat( g.data.list.my_asc[i] );  
152                 }
153
154                 /* Other stat cats present on these copies */
155                 for (var i = 0; i < g.copies.length; i++) {
156                         var entries = g.copies[i].stat_cat_entries();
157                         if (!entries) entries = [];
158                         for (var j = 0; j < entries.length; j++) {
159                                 var sc_id = entries[j].stat_cat();
160                                 add_stat_cat( sc_id );
161                         }
162                 }
163
164                 /******************************************************************************************************/
165                 /* Backup copies :) */
166
167                 g.original_copies = js2JSON( g.copies );
168
169                 /******************************************************************************************************/
170                 /* Do it */
171
172                 g.summarize( g.copies );
173                 g.render();
174
175         } catch(E) {
176                 var err_msg = "!! This software has encountered an error.  Please tell your friendly " +
177                         "system administrator or software developer the following:\ncat/copy_editor.xul\n" + E + '\n';
178                 try { g.error.sdump('D_ERROR',err_msg); } catch(E) { dump(err_msg); dump(js2JSON(E)); }
179                 alert(err_msg);
180         }
181 }
182
183 /******************************************************************************************************/
184 /* Retrieve Templates */
185
186 g.retrieve_templates = function() {
187         try {
188                 JSAN.use('util.widgets'); JSAN.use('util.functional');
189                 g.templates = {};
190                 var robj = g.network.simple_request('FM_AUS_RETRIEVE',[ses(),g.data.list.au[0].id()]);
191                 if (typeof robj['staff_client.copy_editor.templates'] != 'undefined') {
192                         g.templates = JSON2js( robj['staff_client.copy_editor.templates'] );
193                 }
194                 util.widgets.remove_children('template_placeholder');
195                 var list = util.functional.map_object_to_list( g.templates, function(obj,i) { return [i, i]; } );
196
197                 g.template_menu = util.widgets.make_menulist( list );
198                 $('template_placeholder').appendChild(g.template_menu);
199         } catch(E) {
200                 g.error.standard_unexpected_error_alert('Error retrieving templates',E);
201         }
202 }
203
204 /******************************************************************************************************/
205 /* Apply Template */
206
207 g.apply_template = function() {
208         try {
209                 var name = g.template_menu.value;
210                 if (g.templates[ name ] != 'undefined') {
211                         var template = g.templates[ name ];
212                         for (var i in template) {
213                                 g.changed[ i ] = template[ i ];
214                                 switch( template[i].type ) {
215                                         case 'attribute' :
216                                                 g.apply(template[i].field,template[i].value);
217                                         break;
218                                         case 'stat_cat' :
219                                                 g.apply_stat_cat(template[i].field,template[i].value);
220                                         break;
221                                 }
222                         }
223                         g.summarize( g.copies );
224                         g.render();
225                 }
226         } catch(E) {
227                 g.error.standard_unexpected_error_alert('Error applying template',E);
228         }
229 }
230
231 /******************************************************************************************************/
232 /* Save as Template */
233
234 g.save_template = function() {
235         try {
236                 var name = window.prompt('Enter template name:','','Save As Template');
237                 if (!name) return;
238                 g.templates[name] = g.changed;
239                 var robj = g.network.simple_request(
240                         'FM_AUS_UPDATE',[ses(),g.data.list.au[0].id(), { 'staff_client.copy_editor.templates' : js2JSON(g.templates) }]
241                 );
242                 if (typeof robj.ilsevent != 'undefined') {
243                         throw(robj);
244                 } else {
245                         alert('Template "' + name + '" saved.');
246                         setTimeout(
247                                 function() {
248                                         try {
249                                                 g.retrieve_templates();
250                                         } catch(E) {
251                                                 g.error.standard_unexpected_error_alert('Error saving template',E);
252                                         }
253                                 },0
254                         );
255                 }
256         } catch(E) {
257                 g.error.standard_unexpected_error_alert('Error saving template',E);
258         }
259 }
260
261 /******************************************************************************************************/
262 /* Delete Template */
263
264 g.delete_template = function() {
265         try {
266                 var name = g.template_menu.value;
267                 if (!name) return;
268                 if (! window.confirm('Delete template "' + name + '"?') ) return;
269                 delete(g.templates[name]);
270                 var robj = g.network.simple_request(
271                         'FM_AUS_UPDATE',[ses(),g.data.list.au[0].id(), { 'staff_client.copy_editor.templates' : js2JSON(g.templates) }]
272                 );
273                 if (typeof robj.ilsevent != 'undefined') {
274                         throw(robj);
275                 } else {
276                         alert('Template "' + name + '" deleted.');
277                         setTimeout(
278                                 function() {
279                                         try {
280                                                 g.retrieve_templates();
281                                         } catch(E) {
282                                                 g.error.standard_unexpected_error_alert('Error deleting template',E);
283                                         }
284                                 },0
285                         );
286                 }
287         } catch(E) {
288                 g.error.standard_unexpected_error_alert('Error deleting template',E);
289         }
290 }
291
292 /******************************************************************************************************/
293 /* Restore backup copies */
294
295 g.reset = function() {
296         g.changed = {};
297         g.copies = JSON2js( g.original_copies );
298         g.summarize( g.copies );
299         g.render();
300 }
301
302 /******************************************************************************************************/
303 /* Apply a value to a specific field on all the copies being edited */
304
305 g.apply = function(field,value) {
306         g.error.sdump('D_TRACE','field = ' + field + '  value = ' + value + '\n');
307         for (var i = 0; i < g.copies.length; i++) {
308                 var copy = g.copies[i];
309                 try {
310                         copy[field]( value ); copy.ischanged('1');
311                 } catch(E) {
312                         alert(E);
313                 }
314         }
315 }
316
317 /******************************************************************************************************/
318 /* Apply a stat cat entry to all the copies being edited */
319
320 g.apply_stat_cat = function(sc_id,entry_id) {
321         g.error.sdump('D_TRACE','sc_id = ' + sc_id + '  entry_id = ' + entry_id + '\n');
322         for (var i = 0; i < g.copies.length; i++) {
323                 var copy = g.copies[i];
324                 try {
325                         copy.ischanged('1');
326                         var temp = copy.stat_cat_entries();
327                         if (!temp) temp = [];
328                         temp = util.functional.filter_list(
329                                 temp,
330                                 function (obj) {
331                                         return (obj.stat_cat() != sc_id);
332                                 }
333                         );
334                         temp.push( 
335                                 util.functional.find_id_object_in_list( 
336                                         g.data.hash.asc[sc_id].entries(), 
337                                         entry_id
338                                 )
339                         );
340                         copy.stat_cat_entries( temp );
341
342                 } catch(E) {
343                         alert(E);
344                 }
345         }
346 }
347
348 /******************************************************************************************************/
349 /* This keeps track of what fields have been edited for styling purposes */
350
351 g.changed = {};
352
353 /******************************************************************************************************/
354 /* These need data from the middle layer to render */
355
356 g.special_exception = {
357         'Call Number' : function(label,value) {
358                 if (value>0) { /* an existing call number */
359                         g.network.request(
360                                 api.FM_ACN_RETRIEVE.app,
361                                 api.FM_ACN_RETRIEVE.method,
362                                 [ value ],
363                                 function(req) {
364                                         var cn = '??? id = ' + value;
365                                         try {
366                                                 cn = req.getResultObject().label();
367                                         } catch(E) {
368                                                 g.error.sdump('D_ERROR','callnumber retrieve: ' + E);
369                                         }
370                                         label.setAttribute('value',cn);
371                                 }
372                         );
373                 } else { /* a yet to be created call number */
374                         if (g.callnumbers) {
375                                 label.setAttribute('value',g.callnumbers[value]);
376                         }
377                 }
378         },
379         'Creator' : function(label,value) {
380                 if (value == null || value == '' || value == 'null') return;
381                 g.network.simple_request(
382                         'FM_AU_RETRIEVE_VIA_ID',
383                         [ ses(), value ],
384                         function(req) {
385                                 var p = '??? id = ' + value;
386                                 try {
387                                         p = req.getResultObject();
388                                         p = p.usrname();
389
390                                 } catch(E) {
391                                         g.error.sdump('D_ERROR','patron retrieve: ' + E);
392                                 }
393                                 label.setAttribute('value',p);
394                         }
395                 );
396         },
397         'Last Editor' : function(label,value) {
398                 if (value == null || value == '' || value == 'null') return;
399                 g.network.simple_request(
400                         'FM_AU_RETRIEVE_VIA_ID',
401                         [ ses(), value ],
402                         function(req) {
403                                 var p = '??? id = ' + value;
404                                 try {
405                                         p = req.getResultObject();
406                                         p = p.usrname();
407
408                                 } catch(E) {
409                                         g.error.sdump('D_ERROR','patron retrieve: ' + E);
410                                 }
411                                 label.setAttribute('value',p);
412                         }
413                 );
414         }
415
416 }
417
418 /******************************************************************************************************/
419 g.readonly_stat_cat_names = [];
420 g.editable_stat_cat_names = [];
421
422 /******************************************************************************************************/
423 /* These get show in the left panel */
424
425 g.panes_and_field_names = {
426
427         'left_pane' :
428 [
429         [
430                 "Barcode",               
431                 {
432                         render: 'fm.barcode();',
433                 }
434         ], 
435         [
436                 "Creation Date",
437                 { 
438                         render: 'util.date.formatted_date( fm.create_date(), "%F");',
439                 }
440         ],
441         [
442                 "Creator",
443                 { 
444                         render: 'fm.creator();',
445                 }
446         ],
447         [
448                 "Last Edit Date",
449                 { 
450                         render: 'util.date.formatted_date( fm.edit_date(), "%F");',
451                 }
452         ],
453         [
454                 "Last Editor",
455                 {
456                         render: 'fm.editor();',
457                 }
458         ],
459
460 ],
461
462 'right_pane' :
463 [
464         [
465                 "Shelving Location",
466                 { 
467                         render: 'typeof fm.location() == "object" ? fm.location().name() : g.data.hash.acpl[ fm.location() ].name()', 
468                         input: 'c = function(v){ g.apply("location",v); if (typeof post_c == "function") post_c(v); }; x = util.widgets.make_menulist( util.functional.map_list( g.data.list.acpl, function(obj) { return [ obj.name(), obj.id() ]; }).sort()); x.addEventListener("apply",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
469
470                 }
471         ],
472         [
473                 "Circulation Library",          
474                 {       
475                         render: 'typeof fm.circ_lib() == "object" ? fm.circ_lib().shortname() : g.data.hash.aou[ fm.circ_lib() ].shortname()',
476                         input: 'c = function(v){ g.apply("circ_lib",v); if (typeof post_c == "function") post_c(v); }; 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("apply",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
477                 } 
478         ],
479         [
480                 "Call Number",  
481                 {
482                         render: 'fm.call_number();',
483                 }
484         ],
485         [
486                 "Copy Number",
487                 { 
488                         render: 'fm.copy_number() == null ? "<Unset>" : fm.copy_number()',
489                         input: 'c = function(v){ g.apply("copy_number",v); if (typeof post_c == "function") post_c(v); }; x = document.createElement("textbox"); x.addEventListener("apply",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
490                 }
491         ],
492
493
494 ],
495
496 'right_pane2' :
497 [
498         [
499                 "Circulate?",
500                 {       
501                         render: 'fm.circulate() == null ? "<Unset>" : ( fm.circulate() == 1 ? "Yes" : "No" )',
502                         input: 'c = function(v){ g.apply("circulate",v); if (typeof post_c == "function") post_c(v); }; x = util.widgets.make_menulist( [ [ "Yes", "1" ], [ "No", "0" ] ] ); x.addEventListener("apply",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
503                 }
504         ],
505         [
506                 "Holdable?",
507                 { 
508                         render: 'fm.holdable() == null ? "<Unset>" : ( fm.holdable() == 1? "Yes" : "No" )', 
509                         input: 'c = function(v){ g.apply("holdable",v); if (typeof post_c == "function") post_c(v); }; x = util.widgets.make_menulist( [ [ "Yes", "1" ], [ "No", "0" ] ] ); x.addEventListener("apply",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
510                 }
511         ],
512
513         [
514                 "Loan Duration",
515                 { 
516                         render: 'switch(fm.loan_duration()){ case 1: case "1": "Short"; break; case 2: case "2": "Normal"; break; case 3:case "3": "Long"; break; }',
517                         input: 'c = function(v){ g.apply("loan_duration",v); if (typeof post_c == "function") post_c(v); }; x = util.widgets.make_menulist( [ [ "Short", "1" ], [ "Normal", "2" ], [ "Long", "3" ] ] ); x.addEventListener("apply",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
518
519                 }
520         ],
521         [
522                 "Fine Level",
523                 {
524                         render: 'switch(fm.fine_level()){ case 1: case "1": "Low"; break; case 2: case "2": "Normal"; break; case 3: case "3": "High"; break; }',
525                         input: 'c = function(v){ g.apply("fine_level",v); if (typeof post_c == "function") post_c(v); }; x = util.widgets.make_menulist( [ [ "Low", "1" ], [ "Normal", "2" ], [ "High", "3" ] ] ); x.addEventListener("apply",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
526                 }
527         ],
528
529          [
530                 "Circulate as Type",    
531                 {       
532                         render: 'fm.circ_as_type() == null ? "<Unset>" : fm.circ_as_type()',
533                         input: 'c = function(v){ g.apply("circ_as_type",v); if (typeof post_c == "function") post_c(v); }; x = document.createElement("textbox"); x.addEventListener("apply",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
534                 } 
535         ],
536         [
537                 "Circulation Modifier",
538                 {       
539                         render: 'fm.circ_modifier() == null ? "<Unset>" : fm.circ_modifier()',
540                         input: 'c = function(v){ g.apply("circ_modifier",v); if (typeof post_c == "function") post_c(v); }; x = document.createElement("textbox"); x.addEventListener("apply",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
541                 }
542         ],
543 ],
544
545 'right_pane3' :
546 [       [
547                 "Alert Message",
548                 {
549                         render: 'fm.alert_message() == null ? "<Unset>" : fm.alert_message()',
550                         input: 'c = function(v){ g.apply("alert_message",v); if (typeof post_c == "function") post_c(v); }; x = document.createElement("textbox"); x.addEventListener("apply",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
551                 }
552         ],
553
554         [
555                 "Deposit?",
556                 { 
557                         render: 'fm.deposit() == null ? "<Unset>" : ( fm.deposit() == 1 ? "Yes" : "No" )',
558                         input: 'c = function(v){ g.apply("deposit",v); if (typeof post_c == "function") post_c(v); }; x = util.widgets.make_menulist( [ [ "Yes", "1" ], [ "No", "0" ] ] ); x.addEventListener("apply",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
559                 }
560         ],
561         [
562                 "Deposit Amount",
563                 { 
564                         render: 'util.money.sanitize( fm.deposit_amount() );',
565                         input: 'c = function(v){ g.apply("deposit_amount",v); if (typeof post_c == "function") post_c(v); }; x = document.createElement("textbox"); x.addEventListener("apply",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
566                 }
567         ],
568         [
569                 "Price",
570                 { 
571                         render: 'util.money.sanitize( fm.price() );', 
572                         input: 'c = function(v){ g.apply("price",v); if (typeof post_c == "function") post_c(v); }; x = document.createElement("textbox"); x.addEventListener("apply",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
573                 }
574         ],
575
576         [
577                 "OPAC Visible?",
578                 { 
579                         render: 'fm.opac_visible() == null ? "<Unset>" : ( fm.opac_visible() == 1 ? "Yes" : "No" )', 
580                         input: 'c = function(v){ g.apply("opac_visible",v); if (typeof post_c == "function") post_c(v); }; x = util.widgets.make_menulist( [ [ "Yes", "1" ], [ "No", "0" ] ] ); x.addEventListener("apply",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
581                 }
582         ],
583         [
584                 "Reference?",
585                 { 
586                         render: 'fm.ref() == null ? "<Unset>" : ( fm.ref() == 1 ? "Yes" : "No" )', 
587                         input: 'c = function(v){ g.apply("ref",v); if (typeof post_c == "function") post_c(v); }; x = util.widgets.make_menulist( [ [ "Yes", "1" ], [ "No", "0" ] ] ); x.addEventListener("apply",function(f){ return function(ev) { f(ev.target.value); } }(c), false);',
588                 }
589         ],
590 ],
591
592 'right_pane4' : 
593 [
594 ]
595
596 };
597
598 /******************************************************************************************************/
599 /* This loops through all our fieldnames and all the copies, tallying up counts for the different values */
600
601 g.summarize = function( copies ) {
602         /******************************************************************************************************/
603         /* Setup */
604
605         JSAN.use('util.date'); JSAN.use('util.money');
606         g.summary = {};
607         g.field_names = [];
608         for (var i in g.panes_and_field_names) {
609                 g.field_names = g.field_names.concat( g.panes_and_field_names[i] );
610         }
611         g.field_names = g.field_names.concat( g.editable_stat_cat_names );
612         g.field_names = g.field_names.concat( g.readonly_stat_cat_names );
613
614         /******************************************************************************************************/
615         /* Loop through the field names */
616
617         for (var i = 0; i < g.field_names.length; i++) {
618
619                 var field_name = g.field_names[i][0];
620                 var render = g.field_names[i][1].render;
621                 g.summary[ field_name ] = {};
622
623                 /******************************************************************************************************/
624                 /* Loop through the copies */
625
626                 for (var j = 0; j < copies.length; j++) {
627
628                         var fm = copies[j];
629                         var cmd = render || ('fm.' + field_name + '();');
630                         var value = '???';
631
632                         /**********************************************************************************************/
633                         /* Try to retrieve the value for this field for this copy */
634
635                         try { 
636                                 value = eval( cmd ); 
637                         } catch(E) { 
638                                 g.error.sdump('D_ERROR','Attempted ' + cmd + '\n' +  E + '\n'); 
639                         }
640                         if (typeof value == 'object' && value != null) {
641                                 alert('FIXME: field_name = ' + field_name + '  value = ' + js2JSON(value) + '\n');
642                         }
643
644                         /**********************************************************************************************/
645                         /* Tally the count */
646
647                         if (g.summary[ field_name ][ value ]) {
648                                 g.summary[ field_name ][ value ]++;
649                         } else {
650                                 g.summary[ field_name ][ value ] = 1;
651                         }
652                 }
653         }
654         g.error.sdump('D_TRACE','summary = ' + js2JSON(g.summary) + '\n');
655 }
656
657 /******************************************************************************************************/
658 /* Display the summarized data and inputs for editing */
659
660 g.render = function() {
661
662         /******************************************************************************************************/
663         /* Library setup and clear any existing interface */
664
665         JSAN.use('util.widgets'); JSAN.use('util.date'); JSAN.use('util.money'); JSAN.use('util.functional');
666
667         for (var i in g.panes_and_field_names) {
668                 var p = document.getElementById(i);
669                 if (p) util.widgets.remove_children(p);
670         }
671
672         /******************************************************************************************************/
673         /* Prepare the panes */
674
675         var groupbox; var caption; var vbox; var grid; var rows;
676         
677         /******************************************************************************************************/
678         /* Loop through the field names */
679
680         for (h in g.panes_and_field_names) {
681                 if (!document.getElementById(h)) continue;
682                 for (var i = 0; i < g.panes_and_field_names[h].length; i++) {
683                         try {
684                                 var f = g.panes_and_field_names[h][i]; var fn = f[0];
685                                 groupbox = document.createElement('groupbox'); document.getElementById(h).appendChild(groupbox);
686                                 if (typeof g.changed[fn] != 'undefined') groupbox.setAttribute('class','copy_editor_field_changed');
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                                 groupbox.appendChild(hbox);
714                                 var hbox2 = document.createElement('hbox');
715                                 groupbox.appendChild(hbox2);
716
717                                 /**************************************************************************************/
718                                 /* Render the input widget */
719
720                                 if (f[1].input && g.edit) {
721                                         g.render_input(hbox,f[1]);
722                                 }
723
724                         } catch(E) {
725                                 g.error.sdump('D_ERROR','copy editor: ' + E + '\n');
726                         }
727                 }
728         }
729 }
730
731 /******************************************************************************************************/
732 /* This actually draws the change button and input widget for a given field */
733 g.render_input = function(node,blob) {
734         try {
735                 // node = hbox ;    groupbox ->  hbox, hbox
736
737                 var groupbox = node.parentNode;
738                 var vbox = node.previousSibling;
739                 var hbox = node;
740                 var hbox2 = node.nextSibling;
741
742                 var input_cmd = blob.input;
743                 var render_cmd = blob.render;
744
745                 var block = false; var first = true;
746
747                 function on_mouseover(ev) {
748                         groupbox.setAttribute('style','background: white');
749                 }
750
751                 function on_mouseout(ev) {
752                         groupbox.setAttribute('style','');
753                 }
754
755                 vbox.addEventListener('mouseover',on_mouseover,false);
756                 vbox.addEventListener('mouseout',on_mouseout,false);
757                 groupbox.addEventListener('mouseover',on_mouseover,false);
758                 groupbox.addEventListener('mouseout',on_mouseout,false);
759                 groupbox.firstChild.addEventListener('mouseover',on_mouseover,false);
760                 groupbox.firstChild.addEventListener('mouseout',on_mouseout,false);
761
762                 function on_click(ev){
763                         try {
764                                 if (block) return; block = true;
765
766                                 function post_c(v) {
767                                         try {
768                                                 /* FIXME - kludgy */
769                                                 var t = input_cmd.match('apply_stat_cat') ? 'stat_cat' : 'attribute';
770                                                 var f;
771                                                 switch(t) {
772                                                         case 'attribute' :
773                                                                 f = input_cmd.match(/apply\("(.+?)",/)[1];
774                                                         break;
775                                                         case 'stat_cat' :
776                                                                 f = input_cmd.match(/apply_stat_cat\((.+?),/)[1];
777                                                         break;
778                                                 }
779                                                 g.changed[ hbox.id ] = { 'type' : t, 'field' : f, 'value' : v };
780                                                 block = false;
781                                                 setTimeout(
782                                                         function() {
783                                                                 g.summarize( g.copies );
784                                                                 g.render();
785                                                         }, 0
786                                                 );
787                                         } catch(E) {
788                                                 g.error.standard_unexpected_error_alert('post_c',E);
789                                         }
790                                 }
791                                 var x; var c; eval( input_cmd );
792                                 if (x) {
793                                         util.widgets.remove_children(vbox);
794                                         util.widgets.remove_children(hbox);
795                                         util.widgets.remove_children(hbox2);
796                                         hbox.appendChild(x);
797                                         var apply = document.createElement('button');
798                                         apply.setAttribute('label','Apply');
799                                         apply.setAttribute('accesskey','A');
800                                         hbox2.appendChild(apply);
801                                         apply.addEventListener('command',function() { c(x.value); },false);
802                                         var cancel = document.createElement('button');
803                                         cancel.setAttribute('label','Cancel');
804                                         cancel.addEventListener('command',function() { setTimeout( function() { g.summarize( g.copies ); g.render(); }, 0); }, false);
805                                         hbox2.appendChild(cancel);
806                                         setTimeout( function() { x.focus(); }, 0 );
807                                 }
808                         } catch(E) {
809                                 g.error.standard_unexpected_error_alert('render_input',E);
810                         }
811                 }
812                 vbox.addEventListener('click',on_click, false);
813                 hbox.addEventListener('click',on_click, false);
814                 groupbox.firstChild.addEventListener('click',on_click, false);
815
816         } catch(E) {
817                 g.error.sdump('D_ERROR',E + '\n');
818         }
819 }
820
821 /******************************************************************************************************/
822 /* store the copies in the global xpcom stash */
823
824 g.stash_and_close = function() {
825         if (g.handle_update) {
826                 try {
827                         var r = g.network.request(
828                                 api.FM_ACP_FLESHED_BATCH_UPDATE.app,
829                                 api.FM_ACP_FLESHED_BATCH_UPDATE.method,
830                                 [ ses(), g.copies ]
831                         );
832                         if (typeof r.ilsevent != 'undefined') {
833                                 g.error.standard_unexpected_error_alert('copy update',r);
834                         }
835                         /* FIXME -- revisit the return value here */
836                 } catch(E) {
837                         alert('copy update error: ' + js2JSON(E));
838                 }
839         }
840         g.data.temp_copies = js2JSON( g.copies );
841         g.data.stash('temp_copies');
842         g.error.sdump('D_CAT','in modal window, g.data.temp_copies = \n' + g.data.temp_copies + '\n');
843         window.close();
844 }
845
846 /******************************************************************************************************/
847 /* spawn copy notes interface */
848
849 g.copy_notes = function() {
850         JSAN.use('util.window'); var win = new util.window();
851         win.open(urls.XUL_COPY_NOTES + '?copy_id=' + window.escape(g.copies[0].id()),'Copy Notes','chrome,resizable,modal');
852 }
853