]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/copy_editor.js
some cleanup to make stat cat and meat grinder additions easier
[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('OpenILS.data'); g.data = new OpenILS.data(); g.data.init({'via':'stash'});
16                 JSAN.use('util.network'); g.network = new util.network();
17
18                 g.cgi = new CGI();
19
20                 g.session = g.cgi.param('session') || g.cgi.param('ses');
21
22                 /******************************************************************************************************/
23                 /* Get the copy ids from various sources */
24
25                 var copy_ids = [];
26                 if (g.cgi.param('copy_ids')) copy_ids = JSON2js( g.cgi.param('copy_ids') );
27                 if (!copy_ids) copy_ids = [];
28                 if (window.xulG && window.xulG.copy_ids) copy_ids = copy_ids.concat( window.xulG.copy_ids );
29
30                 if (copy_ids.length > 0) g.copies = g.network.request(
31                         api.FM_ACP_FLESHED_BATCH_RETRIEVE.app,
32                         api.FM_ACP_FLESHED_BATCH_RETRIEVE.method,
33                         [ copy_ids ]
34                 );
35
36                 /******************************************************************************************************/
37                 /* And fleshed copies if any */
38
39                 if (!g.copies) g.copies = [];
40                 if (window.xulG && window.xulG.copies) g.copies = g.copies.concat( window.xulG.copies );
41                 if (g.cgi.param('copies')) g.copies = g.copies.concat( JSON2js( g.cgi.param('copies') ) );
42
43                 /******************************************************************************************************/
44                 /* We try to retrieve callnumbers for existing copies, but for new copies, we rely on this */
45
46                 if (window.xulG && window.xulG.callnumbers) g.callnumbers = window.xulG.callnumbers;
47                 if (g.cgi.param('callnumbers')) g.callnumbers =  JSON2js( g.cgi.param('callnumbers') );
48
49                 /******************************************************************************************************/
50                 /* Is the interface an editor or a viewer? */
51
52                 if (g.cgi.param('edit') == '1') { 
53                         g.edit = true;
54                         document.getElementById('caption').setAttribute('label','Copy Editor'); 
55                         document.getElementById('nav').setAttribute('hidden','false'); 
56                 }
57
58                 /******************************************************************************************************/
59                 /* Do it */
60
61                 g.summarize( g.copies );
62                 g.render();
63
64         } catch(E) {
65                 var err_msg = "!! This software has encountered an error.  Please tell your friendly " +
66                         "system administrator or software developer the following:\ncat/copy_editor.xul\n" + E + '\n';
67                 try { g.error.sdump('D_ERROR',err_msg); } catch(E) { dump(err_msg); dump(js2JSON(E)); }
68                 alert(err_msg);
69         }
70 }
71
72 /******************************************************************************************************/
73 /* Apply a value to a specific field on all the copies being edited */
74
75 g.apply = function(field,value) {
76         g.error.sdump('D_TRACE','field = ' + field + '  value = ' + value + '\n');
77         for (var i = 0; i < g.copies.length; i++) {
78                 var copy = g.copies[i];
79                 try {
80                         copy[field]( value ); copy.ischanged('1');
81                 } catch(E) {
82                         alert(E);
83                 }
84         }
85 }
86
87 /******************************************************************************************************/
88 /* These need data from the middle layer to render */
89
90 g.special_exception = {
91         'Call Number' : function(label,value) {
92                 if (value>0) { /* an existing call number */
93                         g.network.request(
94                                 api.FM_ACN_RETRIEVE.app,
95                                 api.FM_ACN_RETRIEVE.method,
96                                 [ value ],
97                                 function(req) {
98                                         var cn = '??? id = ' + value;
99                                         try {
100                                                 cn = req.getResultObject().label();
101                                         } catch(E) {
102                                                 g.error.sdump('D_ERROR','callnumber retrieve: ' + E);
103                                         }
104                                         label.setAttribute('value',cn);
105                                 }
106                         );
107                 } else { /* a yet to be created call number */
108                         if (g.callnumbers) {
109                                 label.setAttribute('value',g.callnumbers[value]);
110                         }
111                 }
112         },
113         'Creator' : function(label,value) {
114                 g.network.request(
115                         api.FM_AU_RETRIEVE_VIA_ID.app,
116                         api.FM_AU_RETRIEVE_VIA_ID.method,
117                         [ g.session, value ],
118                         function(req) {
119                                 var p = '??? id = ' + value;
120                                 try {
121                                         p = req.getResultObject();
122                                         p = p.card().barcode() + ' : ' + p.family_name();
123
124                                 } catch(E) {
125                                         g.error.sdump('D_ERROR','patron retrieve: ' + E);
126                                 }
127                                 label.setAttribute('value',p);
128                         }
129                 );
130         },
131         'Last Editor' : function(label,value) {
132                 g.network.request(
133                         api.FM_AU_RETRIEVE_VIA_ID.app,
134                         api.FM_AU_RETRIEVE_VIA_ID.method,
135                         [ g.session, value ],
136                         function(req) {
137                                 var p = '??? id = ' + value;
138                                 try {
139                                         p = req.getResultObject();
140                                         p = p.card().barcode() + ' : ' + p.family_name();
141
142                                 } catch(E) {
143                                         g.error.sdump('D_ERROR','patron retrieve: ' + E);
144                                 }
145                                 label.setAttribute('value',p);
146                         }
147                 );
148         }
149
150 }
151
152 /******************************************************************************************************/
153 g.readonly_stat_cat_names = [];
154 g.editable_stat_cat_names = [];
155
156 /******************************************************************************************************/
157 /* These get show in the left panel */
158
159 g.left_pane_field_names = [
160         [
161                 "Barcode",               
162                 {
163                         render: 'fm.barcode();',
164                 }
165         ], 
166         [
167                 "Call Number",  
168                 {
169                         render: 'fm.call_number();',
170                 }
171         ],
172         [
173                 "Creation Date",
174                 { 
175                         render: 'util.date.formatted_date( fm.create_date(), "%F");',
176                 }
177         ],
178         [
179                 "Last Edit Date",
180                 { 
181                         render: 'util.date.formatted_date( fm.edit_date(), "%F");',
182                 }
183         ],
184
185 ];
186
187 /******************************************************************************************************/
188 /* These get shown in the right panel */
189
190 g.right_pane_field_names = [
191         [
192                 "Creator",
193                 { 
194                         render: 'fm.creator();',
195                 }
196         ],
197         [
198                 "Last Editor",
199                 {
200                         render: 'fm.editor();',
201                 }
202         ],
203          [
204                 "Circulate as Type",    
205                 {       
206                         render: 'fm.circ_as_type();',
207                         input: 'x = document.createElement("textbox"); x.addEventListener("change",function(ev) { g.apply("circ_as_type",ev.target.value); }, false);',
208                 } 
209         ],
210         [
211                 "Circulation Library",          
212                 {       
213                         render: 'fm.circ_lib().shortname();',
214                         input: '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("command",function(ev) { g.apply("circ_lib",ev.target.value); }, false);',
215                 } 
216         ],
217         [
218                 "Circulation Modifier",
219                 {       
220                         render: 'fm.circ_modifier();',
221                         input: 'x = document.createElement("textbox"); x.addEventListener("change",function(ev) { g.apply("circ_modifier",ev.target.value); }, false);',
222                 }
223         ],
224         [
225                 "Circulate?",
226                 {       
227                         render: 'fm.circulate() ? "Yes" : "No";',
228                         input: 'x = util.widgets.make_menulist( [ [ "Yes", "1" ], [ "No", "0" ] ] ); x.addEventListener("command",function(ev) { g.apply("circulate",ev.target.value); }, false);',
229                 }
230         ],
231         [
232                 "Copy Number",
233                 { 
234                         render: 'fm.copy_number();',
235                         input: 'x = document.createElement("textbox"); x.addEventListener("change",function(ev) { g.apply("copy_number",ev.target.value); }, false);',
236                 }
237         ],
238         [
239                 "Deposit?",
240                 { 
241                         render: 'fm.deposit() ? "Yes" : "No";',
242                         input: 'x = util.widgets.make_menulist( [ [ "Yes", "1" ], [ "No", "0" ] ] ); x.addEventListener("command",function(ev) { g.apply("deposit",ev.target.value); }, false);',
243                 }
244         ],
245         [
246                 "Deposit Amount",
247                 { 
248                         render: 'util.money.sanitize( fm.deposit_amount() );',
249                         input: 'x = document.createElement("textbox"); x.addEventListener("change",function(ev) { g.apply("deposit_amount",ev.target.value); }, false);',
250                 }
251         ],
252         [
253                 "Fine Level",
254                 {
255                         render: 'switch(fm.fine_level()){ case 1: "Low"; break; case 2: "Normal"; break; case 3: "High"; break; }',
256                         input: 'x = util.widgets.make_menulist( [ [ "Low", "1" ], [ "Normal", "2" ], [ "High", "3" ] ] ); x.addEventListener("command",function(ev) { g.apply("fine_level",ev.target.value); }, false);',
257                 }
258         ],
259         [
260                 "Holdable?",
261                 { 
262                         render: 'fm.holdable() ? "Yes" : "No";', 
263                         input: 'x = util.widgets.make_menulist( [ [ "Yes", "1" ], [ "No", "0" ] ] ); x.addEventListener("command",function(ev) { g.apply("holdable",ev.target.value); }, false);',
264                 }
265         ],
266         [
267                 "Loan Duration",
268                 { 
269                         render: 'switch(fm.loan_duration()){ case 1: "Short"; break; case 2: "Normal"; break; case 3: "Long"; break; }',
270                         input: 'x = util.widgets.make_menulist( [ [ "Short", "1" ], [ "Normal", "2" ], [ "Long", "3" ] ] ); x.addEventListener("command",function(ev) { g.apply("loan_duration",ev.target.value); }, false);',
271
272                 }
273         ],
274         [
275                 "Shelving Location",
276                 { 
277                         render: 'fm.location().name();', 
278                         input: 'x = util.widgets.make_menulist( util.functional.map_list( g.data.list.acpl, function(obj) { return [ obj.name(), obj.id() ]; }).sort()); x.addEventListener("command",function(ev) { g.apply("location",ev.target.value); }, false);',
279
280                 }
281         ],
282         [
283                 "OPAC Visible?",
284                 { 
285                         render: 'fm.opac_visible() ? "Yes" : "No";', 
286                         input: 'x = util.widgets.make_menulist( [ [ "Yes", "1" ], [ "No", "0" ] ] ); x.addEventListener("command",function(ev) { g.apply("opac_visible",ev.target.value); }, false);',
287                 }
288         ],
289         [
290                 "Price",
291                 { 
292                         render: 'util.money.sanitize( fm.price() );', 
293                         input: 'x = document.createElement("textbox"); x.addEventListener("change",function(ev) { g.apply("deposit_amount",ev.target.value); }, false);',
294                 }
295         ],
296         [
297                 "Reference?",
298                 { 
299                         render: 'fm.ref() ? "Yes" : "No";', 
300                         input: 'x = util.widgets.make_menulist( [ [ "Yes", "1" ], [ "No", "0" ] ] ); x.addEventListener("command",function(ev) { g.apply("ref",ev.target.value); }, false);',
301                 }
302         ],
303         [
304                 "Status",
305                 { 
306                         render: 'fm.status().name();', 
307                         input: 'x = util.widgets.make_menulist( util.functional.map_list( g.data.list.ccs, function(obj) { return [ obj.name(), obj.id() ]; } ).sort() ); x.addEventListener("command",function(ev) { g.apply("status",ev.target.value); }, false);',
308
309
310                 }
311         ],
312 ];
313
314 /******************************************************************************************************/
315 /* This loops through all our fieldnames and all the copies, tallying up counts for the different values */
316
317 g.summarize = function( copies ) {
318         /******************************************************************************************************/
319         /* Setup */
320
321         JSAN.use('util.date'); JSAN.use('util.money');
322         g.summary = {};
323         g.field_names = g.left_pane_field_names;
324         g.field_names = g.field_names.concat( g.right_pane_field_names );
325         g.field_names = g.field_names.concat( g.editable_stat_cat_names );
326         g.field_names = g.field_names.concat( g.readonly_stat_cat_names );
327
328         /******************************************************************************************************/
329         /* Loop through the field names */
330
331         for (var i = 0; i < g.field_names.length; i++) {
332
333                 var field_name = g.field_names[i][0];
334                 var render = g.field_names[i][1].render;
335                 g.summary[ field_name ] = {};
336
337                 /******************************************************************************************************/
338                 /* Loop through the copies */
339
340                 for (var j = 0; j < copies.length; j++) {
341
342                         var fm = copies[j];
343                         var cmd = render || ('fm.' + field_name + '();');
344                         var value = '???';
345
346                         /**********************************************************************************************/
347                         /* Try to retrieve the value for this field for this copy */
348
349                         try { 
350                                 value = eval( cmd ); 
351                         } catch(E) { 
352                                 g.error.sdump('D_ERROR','Attempted ' + cmd + '\n' +  E + '\n'); 
353                         }
354                         if (typeof value == 'object' && value != null) {
355                                 alert('FIXME: field_name = ' + field_name + '  value = ' + js2JSON(value) + '\n');
356                         }
357
358                         /**********************************************************************************************/
359                         /* Tally the count */
360
361                         if (g.summary[ field_name ][ value ]) {
362                                 g.summary[ field_name ][ value ]++;
363                         } else {
364                                 g.summary[ field_name ][ value ] = 1;
365                         }
366                 }
367         }
368         g.error.sdump('D_ERROR','summary = ' + js2JSON(g.summary) + '\n');
369 }
370
371 /******************************************************************************************************/
372 /* Display the summarized data and inputs for editing */
373
374 g.render = function() {
375
376         /******************************************************************************************************/
377         /* Library setup and clear any existing interface */
378
379         JSAN.use('util.widgets'); JSAN.use('util.date'); JSAN.use('util.money'); JSAN.use('util.functional');
380
381         var cns = document.getElementById('call_number_summary');
382         util.widgets.remove_children( cns );
383         var bcs = document.getElementById('barcode_summary');
384         util.widgets.remove_children( bcs );
385         var rp = document.getElementById('right_pane');
386         util.widgets.remove_children( rp );
387
388         /******************************************************************************************************/
389         /* Make the call number summary */
390
391         var grid = util.widgets.make_grid( [ { 'flex' : '1' } ] );
392         cns.appendChild(grid);
393         for (var i in g.summary['Call Number']) {
394                 var cn_id = i; var count = g.summary['Call Number'][i];
395                 var row = document.createElement('row'); grid.lastChild.appendChild(row);
396                 var cn_label = document.createElement('label'); row.appendChild(cn_label);
397                 g.special_exception['Call Number']( cn_label, cn_id );
398                 var count_label = document.createElement('label'); row.appendChild(count_label);
399                 var unit = count == 1 ? 'copy' : 'copies';
400                 count_label.setAttribute('value',count + ' ' + unit);
401         }
402
403         /******************************************************************************************************/
404         /* List the copy barcodes */
405
406         for (var i in g.summary['Barcode']) {
407                 var bc = i;
408                 var hbox = document.createElement('hbox'); bcs.appendChild(hbox);
409                 var bc_label = document.createElement('label'); hbox.appendChild(bc_label);
410                 bc_label.setAttribute('value',bc);
411         }
412
413         /******************************************************************************************************/
414         /* List the other non-editable fields in this pane */
415
416         var groupbox; var caption; var vbox; var grid; var rows;
417         for (var i = 0; i < g.left_pane_field_names.length; i++) {
418                 try {
419                         var f = g.left_pane_field_names[i]; var fn = f[0];
420                         if (fn == 'Call Number' || fn == 'Barcode') continue;
421                         groupbox = document.createElement('groupbox'); bcs.parentNode.parentNode.appendChild(groupbox);
422                         caption = document.createElement('caption'); groupbox.appendChild(caption);
423                         caption.setAttribute('label',fn);
424                         vbox = document.createElement('vbox'); groupbox.appendChild(vbox);
425                         grid = util.widgets.make_grid( [ { 'flex' : 1 }, {}, {} ] ); vbox.appendChild(grid);
426                         grid.setAttribute('flex','1');
427                         rows = grid.lastChild;
428                         var row;
429                         
430                         /**************************************************************************************/
431                         /* Loop through each value for the field */
432
433                         for (var j in g.summary[fn]) {
434                                 var value = j; var count = g.summary[fn][j];
435                                 row = document.createElement('row'); rows.appendChild(row);
436                                 var label1 = document.createElement('label'); row.appendChild(label1);
437                                 if (g.special_exception[ fn ]) {
438                                         g.special_exception[ fn ]( label1, value );
439                                 } else {
440                                         label1.setAttribute('value',value);
441                                 }
442                                 var label2 = document.createElement('label'); row.appendChild(label2);
443                                 var unit = count == 1 ? 'copy' : 'copies';
444                                 label2.setAttribute('value',count + ' ' + unit);
445                         }
446                         var hbox = document.createElement('hbox'); 
447                         vbox.appendChild(hbox);
448                 } catch(E) {
449                         g.error.sdump('D_ERROR','copy editor: ' + E + '\n');
450                 }
451         }
452
453         /******************************************************************************************************/
454         /* Prepare the right panel, which is different for 1-copy view and multi-copy view */
455
456         if (g.copies.length == 1) {
457
458                 /******************************************************************************************************/
459                 /* 1-copy mode has a single groupbox and each field is a row on a grid */
460
461                 var groupbox; var caption; var vbox; var grid; var rows;
462                 groupbox = document.createElement('groupbox'); rp.appendChild(groupbox);
463                 caption = document.createElement('caption'); groupbox.appendChild(caption);
464                 caption.setAttribute('label','Fields');
465                 vbox = document.createElement('vbox'); groupbox.appendChild(vbox);
466                 grid = util.widgets.make_grid( [ {}, { 'flex' : 1 } ] ); vbox.appendChild(grid);
467                 grid.setAttribute('flex','1');
468                 rows = grid.lastChild;
469
470                 /******************************************************************************************************/
471                 /* Loop through the field names */
472
473                 for (var i = 0; i < g.right_pane_field_names.length; i++) {
474                         try {
475                                 var f = g.right_pane_field_names[i]; var fn = f[0];
476                                 var row;
477
478                                 /**************************************************************************************/
479                                 /* Loop through each value for the field */
480
481                                 for (var j in g.summary[fn]) {
482                                         var value = j; var count = g.summary[fn][j];
483                                         row = document.createElement('row'); rows.appendChild(row);
484                                         var label0 = document.createElement('label'); row.appendChild(label0);
485                                         label0.setAttribute('value',fn);
486                                         label0.setAttribute('style','font-weight: bold');
487                                         var label1 = document.createElement('label'); row.appendChild(label1);
488                                         if (g.special_exception[ fn ]) {
489                                                 g.special_exception[ fn ]( label1, value );
490                                         } else {
491                                                 label1.setAttribute('value',value);
492                                         }
493
494                                 }
495
496                                 /**************************************************************************************/
497                                 /* Render the input widget */
498
499                                 var hbox = document.createElement('hbox'); 
500                                 row.setAttribute('style','border-bottom: dotted black thin');
501                                 row.appendChild(hbox);
502                                 if (f[1].input && g.edit) {
503                                         try {
504                                                 var spacer = document.createElement('spacer'); hbox.appendChild(spacer);
505                                                 spacer.setAttribute('flex','1');
506                                                 var x; eval( f[1].input );
507                                                 if (x) hbox.appendChild(x);
508
509                                         } catch(E) {
510                                                 g.error.sdump('D_ERROR',E + '\n');
511                                         }
512                                 }
513
514                         } catch(E) {
515                                 g.error.sdump('D_ERROR','copy editor: ' + E + '\n');
516                         }
517                 }
518
519         } else {
520
521                 /******************************************************************************************************/
522                 /* multi-copy mode has a groupbox for each field */
523
524                 var groupbox; var caption; var vbox; var grid; var rows;
525                 
526                 /******************************************************************************************************/
527                 /* Loop through the field names */
528
529                 for (var i = 0; i < g.right_pane_field_names.length; i++) {
530                         try {
531                                 var f = g.right_pane_field_names[i]; var fn = f[0];
532                                 groupbox = document.createElement('groupbox'); rp.appendChild(groupbox);
533                                 caption = document.createElement('caption'); groupbox.appendChild(caption);
534                                 caption.setAttribute('label',fn);
535                                 vbox = document.createElement('vbox'); groupbox.appendChild(vbox);
536                                 grid = util.widgets.make_grid( [ { 'flex' : 1 }, {}, {} ] ); vbox.appendChild(grid);
537                                 grid.setAttribute('flex','1');
538                                 rows = grid.lastChild;
539                                 var row;
540                                 
541                                 /**************************************************************************************/
542                                 /* Loop through each value for the field */
543
544                                 for (var j in g.summary[fn]) {
545                                         var value = j; var count = g.summary[fn][j];
546                                         row = document.createElement('row'); rows.appendChild(row);
547                                         var label1 = document.createElement('label'); row.appendChild(label1);
548                                         if (g.special_exception[ fn ]) {
549                                                 g.special_exception[ fn ]( label1, value );
550                                         } else {
551                                                 label1.setAttribute('value',value);
552                                         }
553                                         var label2 = document.createElement('label'); row.appendChild(label2);
554                                         var unit = count == 1 ? 'copy' : 'copies';
555                                         label2.setAttribute('value',count + ' ' + unit);
556
557                                 }
558                                 var hbox = document.createElement('hbox'); 
559                                 vbox.appendChild(hbox);
560
561                                 /**************************************************************************************/
562                                 /* Render the input widget */
563
564                                 if (f[1].input && g.edit) {
565                                         try {
566                                                 var spacer = document.createElement('spacer'); hbox.appendChild(spacer);
567                                                 spacer.setAttribute('flex','1');
568                                                 var x; eval( f[1].input );
569                                                 if (x) hbox.appendChild(x);
570
571                                         } catch(E) {
572                                                 g.error.sdump('D_ERROR',E + '\n');
573                                         }
574                                 }
575                         } catch(E) {
576                                 g.error.sdump('D_ERROR','copy editor: ' + E + '\n');
577                         }
578                 }
579         }
580 }
581
582 /******************************************************************************************************/
583 /* store the copies in the global xpcom stash */
584
585 g.stash_and_close = function() {
586         g.data.temp = js2JSON( g.copies );
587         g.error.sdump('D_CAT','in modal window, g.data.temp = \n' + g.data.temp + '\n');
588         g.data.stash('temp');
589         window.close();
590 }
591