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