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