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