]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/copy_editor.js
owning lib dropdown for copy 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: 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                                         case 'owning_lib' :
225                                                 g.apply_owning_lib(template[i].value);
226                                         break;
227                                 }
228                         }
229                         g.summarize( g.copies );
230                         g.render();
231                 }
232         } catch(E) {
233                 g.error.standard_unexpected_error_alert('Error applying template',E);
234         }
235 }
236
237 /******************************************************************************************************/
238 /* Save as Template */
239
240 g.save_template = function() {
241         try {
242                 var name = window.prompt('Enter template name:','','Save As Template');
243                 if (!name) return;
244                 g.templates[name] = g.changed;
245                 var robj = g.network.simple_request(
246                         'FM_AUS_UPDATE',[ses(),g.data.list.au[0].id(), { 'staff_client.copy_editor.templates' : g.templates }]
247                 );
248                 if (typeof robj.ilsevent != 'undefined') {
249                         throw(robj);
250                 } else {
251                         alert('Template "' + name + '" saved.');
252                         setTimeout(
253                                 function() {
254                                         try {
255                                                 g.retrieve_templates();
256                                         } catch(E) {
257                                                 g.error.standard_unexpected_error_alert('Error saving template',E);
258                                         }
259                                 },0
260                         );
261                 }
262         } catch(E) {
263                 g.error.standard_unexpected_error_alert('Error saving template',E);
264         }
265 }
266
267 /******************************************************************************************************/
268 /* Delete Template */
269
270 g.delete_template = function() {
271         try {
272                 var name = g.template_menu.value;
273                 if (!name) return;
274                 if (! window.confirm('Delete template "' + name + '"?') ) return;
275                 delete(g.templates[name]);
276                 var robj = g.network.simple_request(
277                         'FM_AUS_UPDATE',[ses(),g.data.list.au[0].id(), { 'staff_client.copy_editor.templates' : g.templates }]
278                 );
279                 if (typeof robj.ilsevent != 'undefined') {
280                         throw(robj);
281                 } else {
282                         alert('Template "' + name + '" deleted.');
283                         setTimeout(
284                                 function() {
285                                         try {
286                                                 g.retrieve_templates();
287                                         } catch(E) {
288                                                 g.error.standard_unexpected_error_alert('Error deleting template',E);
289                                         }
290                                 },0
291                         );
292                 }
293         } catch(E) {
294                 g.error.standard_unexpected_error_alert('Error deleting template',E);
295         }
296 }
297
298 /******************************************************************************************************/
299 /* Restore backup copies */
300
301 g.reset = function() {
302         g.changed = {};
303         g.copies = JSON2js( g.original_copies );
304         g.summarize( g.copies );
305         g.render();
306 }
307
308 /******************************************************************************************************/
309 /* Apply a value to a specific field on all the copies being edited */
310
311 g.apply = function(field,value) {
312         g.error.sdump('D_TRACE','applying field = <' + field + '>  value = <' + value + '>\n');
313         if (value == '<HACK:KLUDGE:NULL>') value = null;
314         for (var i = 0; i < g.copies.length; i++) {
315                 var copy = g.copies[i];
316                 try {
317                         copy[field]( value ); copy.ischanged('1');
318                 } catch(E) {
319                         alert(E);
320                 }
321         }
322 }
323
324 /******************************************************************************************************/
325 /* Apply a stat cat entry to all the copies being edited.  An entry_id of < 0 signifies the stat cat is being removed. */
326
327 g.apply_stat_cat = function(sc_id,entry_id) {
328         g.error.sdump('D_TRACE','sc_id = ' + sc_id + '  entry_id = ' + entry_id + '\n');
329         for (var i = 0; i < g.copies.length; i++) {
330                 var copy = g.copies[i];
331                 try {
332                         copy.ischanged('1');
333                         var temp = copy.stat_cat_entries();
334                         if (!temp) temp = [];
335                         temp = util.functional.filter_list(
336                                 temp,
337                                 function (obj) {
338                                         return (obj.stat_cat() != sc_id);
339                                 }
340                         );
341                         if (entry_id > -1) temp.push( 
342                                 util.functional.find_id_object_in_list( 
343                                         g.data.hash.asc[sc_id].entries(), 
344                                         entry_id
345                                 )
346                         );
347                         copy.stat_cat_entries( temp );
348
349                 } catch(E) {
350                         g.error.standard_unexpected_error_alert('apply_stat_cat',E);
351                 }
352         }
353 }
354
355 /******************************************************************************************************/
356 /* Apply an "owning lib" to all the copies being edited.  That is, change and auto-vivicating volumes */
357
358 g.apply_owning_lib = function(ou_id) {
359         g.error.sdump('D_TRACE','ou_id = ' + ou_id + '\n');
360         var map_acn = {};
361         for (var i = 0; i < g.copies.length; i++) {
362                 var copy = g.copies[i];
363                 try {
364                         if (!map_acn[copy.call_number()]) {
365                                 var volume = g.network.simple_request('FM_ACN_RETRIEVE',[ copy.call_number() ]);
366                                 if (typeof volume.ilsevent != 'undefined') {
367                                         g.error.standard_unexpected_error_alert('Error retrieving Volume information for copy ' + copy.barcode() + ".  The owning library for this copy won't be changed.",volume);
368                                         continue;
369                                 }
370                                 map_acn[copy.call_number()] = volume;
371                         }
372                         var old_volume = map_acn[copy.call_number()];
373                         var acn_id = g.network.simple_request(
374                                 'FM_ACN_FIND_OR_CREATE',
375                                 [ses(),old_volume.label(),old_volume.record(),ou_id]
376                         );
377                         if (typeof acn_id.ilsevent != 'undefined') {
378                                 g.error.standard_unexpected_error_alert('Error changing owning lib for copy ' + copy.barcode() + ".  The owning library for this copy won't be changed.",acn_id);
379                                 continue;
380                         }
381                         copy.call_number(acn_id);
382                         copy.ischanged('1');
383                 } catch(E) {
384                         g.error.standard_unexpected_error_alert('apply_stat_cat',E);
385                 }
386         }
387 }
388
389
390 /******************************************************************************************************/
391 /* This returns true if none of the copies being edited have a magical status found in my_constants.magical_statuses */
392
393 g.safe_to_edit_copy_status = function() {
394         try {
395                 var safe = true;
396                 for (var i = 0; i < g.copies.length; i++) {
397                         var status = g.copies[i].status(); if (typeof status == 'object') status = status.id();
398                         if (typeof my_constants.magical_statuses[ status ] != 'undefined') safe = false;
399                 }
400                 return safe;
401         } catch(E) {
402                 g.error.standard_unexpected_error_alert('safe_to_edit_copy_status?',E);
403                 return false;
404         }
405 }
406
407 /******************************************************************************************************/
408 /* This concats and uniques all the alert messages for use as the default value for a new alert message */
409
410 g.populate_alert_message_input = function(tb) {
411         try {
412                 var seen = {}; var s = '';
413                 for (var i = 0; i < g.copies.length; i++) {
414                         var msg = g.copies[i].alert_message(); 
415                         if (msg) {
416                                 if (typeof seen[msg] == 'undefined') {
417                                         s += msg + '\n';
418                                         seen[msg] = true;
419                                 }
420                         }
421                 }
422                 tb.setAttribute('value',s);
423         } catch(E) {
424                 g.error.standard_unexpected_error_alert('populate_alert_message_input',E);
425         }
426 }
427
428 /******************************************************************************************************/
429 /* This returns a list of acpl's appropriate for the copies being edited */
430
431 g.get_acpl_list = function() {
432         try {
433
434                 JSAN.use('util.functional');
435
436                 function get(lib_id,only_these) {
437                         g.data.stash_retrieve();
438                         var label = 'acpl_list_for_lib_'+lib_id;
439                         if (typeof g.data[label] == 'undefined') {
440                                 var robj = g.network.simple_request('FM_ACPL_RETRIEVE', [ lib_id ]);
441                                 if (typeof robj.ilsevent != 'undefined') throw(robj);
442                                 var temp_list = [];
443                                 for (var j = 0; j < robj.length; j++) {
444                                         var my_acpl = robj[j];
445                                         if (typeof g.data.hash.acpl[ my_acpl.id() ] == 'undefined') {
446                                                 g.data.hash.acpl[ my_acpl.id() ] = my_acpl;
447                                                 g.data.list.acpl.push( my_acpl );
448                                         }
449                                         if (only_these.indexOf( String( my_acpl.owning_lib() ) ) != -1) {
450                                                 temp_list.push( my_acpl );
451                                         }
452                                 }
453                                 g.data[label] = temp_list; g.data.stash(label,'hash','list');
454                         }
455                         return g.data[label];
456                 }
457
458                 var libs = []; var map_acn = {};
459                 for (var i = 0; i < g.copies.length; i++) {
460                         var cn_id = g.copies[i].call_number();
461                         if (cn_id > 0) {
462                                 if (! map_acn[ cn_id ]) {
463                                         map_acn[ cn_id ] = g.network.simple_request('FM_ACN_RETRIEVE',[ cn_id ]);
464                                         libs.push( map_acn[ cn_id ].owning_lib() );
465                                 }
466                         }
467                 }
468                 if (g.callnumbers) {
469                         for (var i in g.callnumbers) {
470                                 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 ); }
471                         }
472                 }
473                 JSAN.use('util.fm_utils');
474                 var ancestor = util.fm_utils.find_common_aou_ancestor( libs );
475                 if (typeof ancestor == 'object' && ancestor != null) ancestor = ancestor.id();
476
477                 var ancestors = util.fm_utils.find_common_aou_ancestors( libs );
478
479                 if (ancestor) {
480                         return get(ancestor, ancestors);
481                 } else {
482                         return [];
483                 }
484
485         } catch(E) {
486                 g.error.standard_unexpected_error_alert('get_acpl_list',E);
487                 return [];
488         }
489 }
490
491
492 /******************************************************************************************************/
493 /* This keeps track of what fields have been edited for styling purposes */
494
495 g.changed = {};
496
497 /******************************************************************************************************/
498 /* These need data from the middle layer to render */
499
500 g.special_exception = {
501         'Owning Lib : Call Number' : function(label,value) {
502                 JSAN.use('util.widgets');
503                 if (value>0) { /* an existing call number */
504                         g.network.request(
505                                 api.FM_ACN_RETRIEVE.app,
506                                 api.FM_ACN_RETRIEVE.method,
507                                 [ value ],
508                                 function(req) {
509                                         var cn = '??? id = ' + value;
510                                         try {
511                                                 cn = req.getResultObject();
512                                         } catch(E) {
513                                                 g.error.sdump('D_ERROR','callnumber retrieve: ' + E);
514                                         }
515                                         util.widgets.set_text(label,g.data.hash.aou[ cn.owning_lib() ].shortname() + ' : ' + cn.label());
516                                 }
517                         );
518                 } else { /* a yet to be created call number */
519                         if (g.callnumbers) {
520                                 util.widgets.set_text(label,g.data.hash.aou[ g.callnumbers[value].owning_lib ].shortname() + ' : ' + g.callnumbers[value].label);
521                         }
522                 }
523         },
524         'Creator' : function(label,value) {
525                 if (value == null || value == '' || value == 'null') return;
526                 g.network.simple_request(
527                         'FM_AU_RETRIEVE_VIA_ID',
528                         [ ses(), value ],
529                         function(req) {
530                                 var p = '??? id = ' + value;
531                                 try {
532                                         p = req.getResultObject();
533                                         p = p.usrname();
534
535                                 } catch(E) {
536                                         g.error.sdump('D_ERROR','patron retrieve: ' + E);
537                                 }
538                                 JSAN.use('util.widgets');
539                                 util.widgets.set_text(label,p);
540                         }
541                 );
542         },
543         'Last Editor' : function(label,value) {
544                 if (value == null || value == '' || value == 'null') return;
545                 g.network.simple_request(
546                         'FM_AU_RETRIEVE_VIA_ID',
547                         [ ses(), value ],
548                         function(req) {
549                                 var p = '??? id = ' + value;
550                                 try {
551                                         p = req.getResultObject();
552                                         p = p.usrname();
553
554                                 } catch(E) {
555                                         g.error.sdump('D_ERROR','patron retrieve: ' + E);
556                                 }
557                                 util.widgets.set_text(label,p);
558                         }
559                 );
560         }
561
562 }
563
564 /******************************************************************************************************/
565 g.readonly_stat_cat_names = [];
566 g.editable_stat_cat_names = [];
567
568 /******************************************************************************************************/
569 /* These get show in the left panel */
570
571 g.panes_and_field_names = {
572
573         'left_pane' :
574 [
575         [
576                 "Barcode",               
577                 {
578                         render: 'fm.barcode();',
579                 }
580         ], 
581         [
582                 "Creation Date",
583                 { 
584                         render: 'util.date.formatted_date( fm.create_date(), "%F");',
585                 }
586         ],
587         [
588                 "Creator",
589                 { 
590                         render: 'fm.creator();',
591                 }
592         ],
593         [
594                 "Last Edit Date",
595                 { 
596                         render: 'util.date.formatted_date( fm.edit_date(), "%F");',
597                 }
598         ],
599         [
600                 "Last Editor",
601                 {
602                         render: 'fm.editor();',
603                 }
604         ],
605
606 ],
607
608 'right_pane' :
609 [
610         [
611                 "Shelving Location",
612                 { 
613                         render: 'typeof fm.location() == "object" ? fm.location().name() : g.data.lookup("acpl",fm.location()).name()', 
614                         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);',
615
616                 }
617         ],
618         [
619                 "Circulation Library",          
620                 {       
621                         render: 'typeof fm.circ_lib() == "object" ? fm.circ_lib().shortname() : g.data.hash.aou[ fm.circ_lib() ].shortname()',
622                         //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);',
623                         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);',
624                 } 
625         ],
626         [
627                 "Owning Lib : Call Number",     
628                 {
629                         render: 'fm.call_number();',
630                         input: 'c = function(v){ g.apply_owning_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);',
631                 }
632         ],
633         [
634                 "Copy Number",
635                 { 
636                         render: 'fm.copy_number() == null ? "<Unset>" : fm.copy_number()',
637                         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);',
638                 }
639         ],
640
641
642 ],
643
644 'right_pane2' :
645 [
646         [
647                 "Circulate?",
648                 {       
649                         render: 'fm.circulate() == null ? "<Unset>" : ( get_bool( fm.circulate() ) ? "Yes" : "No" )',
650                         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);',
651                 }
652         ],
653         [
654                 "Holdable?",
655                 { 
656                         render: 'fm.holdable() == null ? "<Unset>" : ( get_bool( fm.holdable() ) ? "Yes" : "No" )', 
657                         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);',
658                 }
659         ],
660         [
661                 "Age Protection",
662                 {
663                         render: 'fm.age_protect() == null ? "<Unset>" : ( typeof fm.age_protect() == "object" ? fm.age_protect().name() : g.data.hash.crahp[ fm.age_protect() ].name() )', 
664                         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);',
665                 }
666
667         ],
668         [
669                 "Loan Duration",
670                 { 
671                         render: 'switch(fm.loan_duration()){ case 1: case "1": "Short"; break; case 2: case "2": "Normal"; break; case 3:case "3": "Long"; break; }',
672                         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);',
673
674                 }
675         ],
676         [
677                 "Fine Level",
678                 {
679                         render: 'switch(fm.fine_level()){ case 1: case "1": "Low"; break; case 2: case "2": "Normal"; break; case 3: case "3": "High"; break; }',
680                         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);',
681                 }
682         ],
683
684          [
685                 "Circulate as Type",    
686                 {       
687                         render: 'fm.circ_as_type() == null ? "<Unset>" : g.data.hash.citm[ fm.circ_as_type() ].value()',
688                         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);',
689                 } 
690         ],
691         [
692                 "Circulation Modifier",
693                 {       
694                         render: 'fm.circ_modifier() == null ? "<Unset>" : fm.circ_modifier()',
695                         /*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);',*/
696                         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);',
697                 }
698         ],
699 ],
700
701 'right_pane3' :
702 [       [
703                 "Alert Message",
704                 {
705                         render: 'fm.alert_message() == null ? "<Unset>" : fm.alert_message()',
706                         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);',
707                 }
708         ],
709
710         [
711                 "Deposit?",
712                 { 
713                         render: 'fm.deposit() == null ? "<Unset>" : ( get_bool( fm.deposit() ) ? "Yes" : "No" )',
714                         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);',
715                 }
716         ],
717         [
718                 "Deposit Amount",
719                 { 
720                         render: 'util.money.sanitize( fm.deposit_amount() );',
721                         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);',
722                 }
723         ],
724         [
725                 "Price",
726                 { 
727                         render: 'util.money.sanitize( fm.price() );', 
728                         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);',
729                 }
730         ],
731
732         [
733                 "OPAC Visible?",
734                 { 
735                         render: 'fm.opac_visible() == null ? "<Unset>" : ( get_bool( fm.opac_visible() ) ? "Yes" : "No" )', 
736                         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);',
737                 }
738         ],
739         [
740                 "Reference?",
741                 { 
742                         render: 'fm.ref() == null ? "<Unset>" : ( get_bool( fm.ref() ) ? "Yes" : "No" )', 
743                         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);',
744                 }
745         ],
746 ],
747
748 'right_pane4' : 
749 [
750 ]
751
752 };
753
754 /******************************************************************************************************/
755 /* This loops through all our fieldnames and all the copies, tallying up counts for the different values */
756
757 g.summarize = function( copies ) {
758         /******************************************************************************************************/
759         /* Setup */
760
761         JSAN.use('util.date'); JSAN.use('util.money');
762         g.summary = {};
763         g.field_names = [];
764         for (var i in g.panes_and_field_names) {
765                 g.field_names = g.field_names.concat( g.panes_and_field_names[i] );
766         }
767         g.field_names = g.field_names.concat( g.editable_stat_cat_names );
768         g.field_names = g.field_names.concat( g.readonly_stat_cat_names );
769
770         /******************************************************************************************************/
771         /* Loop through the field names */
772
773         for (var i = 0; i < g.field_names.length; i++) {
774
775                 var field_name = g.field_names[i][0];
776                 var render = g.field_names[i][1].render;
777                 g.summary[ field_name ] = {};
778
779                 /******************************************************************************************************/
780                 /* Loop through the copies */
781
782                 for (var j = 0; j < copies.length; j++) {
783
784                         var fm = copies[j];
785                         var cmd = render || ('fm.' + field_name + '();');
786                         var value = '???';
787
788                         /**********************************************************************************************/
789                         /* Try to retrieve the value for this field for this copy */
790
791                         try { 
792                                 value = eval( cmd ); 
793                         } catch(E) { 
794                                 g.error.sdump('D_ERROR','Attempted ' + cmd + '\n' +  E + '\n'); 
795                         }
796                         if (typeof value == 'object' && value != null) {
797                                 alert('FIXME: field_name = <' + field_name + '>  value = <' + js2JSON(value) + '>\n');
798                         }
799
800                         /**********************************************************************************************/
801                         /* Tally the count */
802
803                         if (g.summary[ field_name ][ value ]) {
804                                 g.summary[ field_name ][ value ]++;
805                         } else {
806                                 g.summary[ field_name ][ value ] = 1;
807                         }
808                 }
809         }
810         g.error.sdump('D_TRACE','summary = ' + js2JSON(g.summary) + '\n');
811 }
812
813 /******************************************************************************************************/
814 /* Display the summarized data and inputs for editing */
815
816 g.render = function() {
817
818         /******************************************************************************************************/
819         /* Library setup and clear any existing interface */
820
821         JSAN.use('util.widgets'); JSAN.use('util.date'); JSAN.use('util.money'); JSAN.use('util.functional');
822
823         for (var i in g.panes_and_field_names) {
824                 var p = document.getElementById(i);
825                 if (p) util.widgets.remove_children(p);
826         }
827
828         /******************************************************************************************************/
829         /* Prepare the panes */
830
831         var groupbox; var caption; var vbox; var grid; var rows;
832         
833         /******************************************************************************************************/
834         /* Loop through the field names */
835
836         for (h in g.panes_and_field_names) {
837                 if (!document.getElementById(h)) continue;
838                 for (var i = 0; i < g.panes_and_field_names[h].length; i++) {
839                         try {
840                                 var f = g.panes_and_field_names[h][i]; var fn = f[0];
841                                 groupbox = document.createElement('groupbox'); document.getElementById(h).appendChild(groupbox);
842                                 if (typeof g.changed[fn] != 'undefined') groupbox.setAttribute('class','copy_editor_field_changed');
843                                 caption = document.createElement('caption'); groupbox.appendChild(caption);
844                                 caption.setAttribute('label',fn); 
845                                 vbox = document.createElement('vbox'); groupbox.appendChild(vbox);
846                                 grid = util.widgets.make_grid( [ { 'flex' : 1 }, {}, {} ] ); vbox.appendChild(grid);
847                                 grid.setAttribute('flex','1');
848                                 rows = grid.lastChild;
849                                 var row;
850                                 
851                                 /**************************************************************************************/
852                                 /* Loop through each value for the field */
853
854                                 for (var j in g.summary[fn]) {
855                                         var value = j; var count = g.summary[fn][j];
856                                         row = document.createElement('row'); rows.appendChild(row);
857                                         var label1 = document.createElement('description'); row.appendChild(label1);
858                                         if (g.special_exception[ fn ]) {
859                                                 g.special_exception[ fn ]( label1, value );
860                                         } else {
861                                                 label1.appendChild( document.createTextNode(value) );
862                                         }
863                                         var label2 = document.createElement('description'); row.appendChild(label2);
864                                         var unit = count == 1 ? 'copy' : 'copies';
865                                         label2.appendChild( document.createTextNode(count + ' ' + unit) );
866                                 }
867                                 var hbox = document.createElement('hbox'); 
868                                 hbox.setAttribute('id',fn);
869                                 groupbox.appendChild(hbox);
870                                 var hbox2 = document.createElement('hbox');
871                                 groupbox.appendChild(hbox2);
872
873                                 /**************************************************************************************/
874                                 /* Render the input widget */
875
876                                 if (f[1].input && g.edit) {
877                                         g.render_input(hbox,f[1]);
878                                 }
879
880                         } catch(E) {
881                                 g.error.sdump('D_ERROR','copy editor: ' + E + '\n');
882                         }
883                 }
884         }
885 }
886
887 /******************************************************************************************************/
888 /* This actually draws the change button and input widget for a given field */
889 g.render_input = function(node,blob) {
890         try {
891                 // node = hbox ;    groupbox ->  hbox, hbox
892
893                 var groupbox = node.parentNode;
894                 var caption = groupbox.firstChild;
895                 var vbox = node.previousSibling;
896                 var hbox = node;
897                 var hbox2 = node.nextSibling;
898
899                 var input_cmd = blob.input;
900                 var render_cmd = blob.render;
901
902                 var block = false; var first = true;
903
904                 function on_mouseover(ev) {
905                         groupbox.setAttribute('style','background: white');
906                 }
907
908                 function on_mouseout(ev) {
909                         groupbox.setAttribute('style','');
910                 }
911
912                 vbox.addEventListener('mouseover',on_mouseover,false);
913                 vbox.addEventListener('mouseout',on_mouseout,false);
914                 groupbox.addEventListener('mouseover',on_mouseover,false);
915                 groupbox.addEventListener('mouseout',on_mouseout,false);
916                 groupbox.firstChild.addEventListener('mouseover',on_mouseover,false);
917                 groupbox.firstChild.addEventListener('mouseout',on_mouseout,false);
918
919                 function on_click(ev){
920                         try {
921                                 if (block) return; block = true;
922
923                                 function post_c(v) {
924                                         try {
925                                                 /* FIXME - kludgy */
926                                                 var t = input_cmd.match('apply_stat_cat') ? 'stat_cat' : ( input_cmd.match('apply_owning_lib') ? 'owning_lib' : 'attribute' );
927                                                 var f;
928                                                 switch(t) {
929                                                         case 'attribute' :
930                                                                 f = input_cmd.match(/apply\("(.+?)",/)[1];
931                                                         break;
932                                                         case 'stat_cat' :
933                                                                 f = input_cmd.match(/apply_stat_cat\((.+?),/)[1];
934                                                         break;
935                                                         case 'owning_lib' :
936                                                                 f = null;
937                                                         break;
938                                                 }
939                                                 g.changed[ hbox.id ] = { 'type' : t, 'field' : f, 'value' : v };
940                                                 block = false;
941                                                 setTimeout(
942                                                         function() {
943                                                                 g.summarize( g.copies );
944                                                                 g.render();
945                                                         }, 0
946                                                 );
947                                         } catch(E) {
948                                                 g.error.standard_unexpected_error_alert('post_c',E);
949                                         }
950                                 }
951                                 var x; var c; eval( input_cmd );
952                                 if (x) {
953                                         util.widgets.remove_children(vbox);
954                                         util.widgets.remove_children(hbox);
955                                         util.widgets.remove_children(hbox2);
956                                         hbox.appendChild(x);
957                                         var apply = document.createElement('button');
958                                         apply.setAttribute('label','Apply');
959                                         apply.setAttribute('accesskey','A');
960                                         hbox2.appendChild(apply);
961                                         apply.addEventListener('command',function() { c(x.value); },false);
962                                         var cancel = document.createElement('button');
963                                         cancel.setAttribute('label','Cancel');
964                                         cancel.addEventListener('command',function() { setTimeout( function() { g.summarize( g.copies ); g.render(); }, 0); }, false);
965                                         hbox2.appendChild(cancel);
966                                         setTimeout( function() { x.focus(); }, 0 );
967                                 }
968                         } catch(E) {
969                                 g.error.standard_unexpected_error_alert('render_input',E);
970                         }
971                 }
972                 vbox.addEventListener('click',on_click, false);
973                 hbox.addEventListener('click',on_click, false);
974                 caption.addEventListener('click',on_click, false);
975                 caption.addEventListener('keypress',function(ev) {
976                         if (ev.keyCode == 13 /* enter */ || ev.keyCode == 77 /* mac enter */) on_click();
977                 }, false);
978                 caption.setAttribute('style','-moz-user-focus: normal');
979                 caption.setAttribute('onfocus','this.setAttribute("class","outline_me")');
980                 caption.setAttribute('onblur','this.setAttribute("class","")');
981
982         } catch(E) {
983                 g.error.sdump('D_ERROR',E + '\n');
984         }
985 }
986
987 /******************************************************************************************************/
988 /* store the copies in the global xpcom stash */
989
990 g.stash_and_close = function() {
991         if (g.handle_update) {
992                 try {
993                         var r = g.network.request(
994                                 api.FM_ACP_FLESHED_BATCH_UPDATE.app,
995                                 api.FM_ACP_FLESHED_BATCH_UPDATE.method,
996                                 [ ses(), g.copies, true ]
997                         );
998                         if (typeof r.ilsevent != 'undefined') {
999                                 g.error.standard_unexpected_error_alert('copy update',r);
1000                         } else {
1001                                 alert('Items added/modified.');
1002                         }
1003                         /* FIXME -- revisit the return value here */
1004                 } catch(E) {
1005                         alert('copy update error: ' + js2JSON(E));
1006                 }
1007         }
1008         g.data.temp_copies = js2JSON( g.copies );
1009         g.data.stash('temp_copies');
1010         g.error.sdump('D_CAT','in modal window, g.data.temp_copies = \n' + g.data.temp_copies + '\n');
1011         window.close();
1012 }
1013
1014 /******************************************************************************************************/
1015 /* spawn copy notes interface */
1016
1017 g.copy_notes = function() {
1018         JSAN.use('util.window'); var win = new util.window();
1019         win.open(urls.XUL_COPY_NOTES + '?copy_id=' + window.escape(g.copies[0].id()),'Copy Notes','chrome,resizable,modal');
1020 }
1021