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