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