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