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