]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/copy_browser.js
Restore the Replace Barcode option in Holdings Maintenance, as the Edit Item menu...
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / cat / copy_browser.js
1 dump('entering cat.copy_browser.js\n');
2 // vim:noet:sw=4:ts=4:
3
4 if (typeof cat == 'undefined') cat = {};
5 cat.copy_browser = function (params) {
6     try {
7         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
8         JSAN.use('util.error'); this.error = new util.error();
9     } catch(E) {
10         dump('cat.copy_browser: ' + E + '\n');
11     }
12 }
13
14 cat.copy_browser.prototype = {
15
16     'map_tree' : {},
17     'map_acn' : {},
18     'map_acp' : {},
19     'sel_list' : [],
20     'funcs' : [],
21
22     'init' : function( params ) {
23
24         try {
25             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
26             var obj = this;
27
28             obj.docid = params.docid;
29
30             JSAN.use('util.network'); obj.network = new util.network();
31             JSAN.use('OpenILS.data'); obj.data = new OpenILS.data(); obj.data.init({'via':'stash'});
32
33             obj.controller_init(params);
34
35             obj.list_init(params);
36
37             obj.controller.render();
38
39             obj.default_depth = obj.depth_menu_init();
40             obj.default_lib = obj.data.hash.aou[ obj.library_menu_init() ];
41
42             document.getElementById('show_acns').addEventListener(
43                 'command',
44                 function(ev) {
45                     JSAN.use('util.file'); var file = new util.file('copy_browser_prefs.'+obj.data.server_unadorned);
46                     util.widgets.save_attributes(file, { 'lib_menu' : [ 'value' ], 'show_acns' : [ 'checked' ], 'show_acps' : [ 'checked' ] });
47                 },
48                 false
49             );
50
51             document.getElementById('show_acps').addEventListener(
52                 'command',
53                 function(ev) {
54                     JSAN.use('util.file'); var file = new util.file('copy_browser_prefs.'+obj.data.server_unadorned);
55                     util.widgets.save_attributes(file, { 'lib_menu' : [ 'value' ], 'show_acns' : [ 'checked' ], 'show_acps' : [ 'checked' ] });
56                 },
57                 false
58             );
59
60             obj.show_my_libs( obj.default_lib.id() );
61
62             JSAN.use('util.exec'); var exec = new util.exec(20); exec.timer(obj.funcs,100);
63
64             obj.show_consortial_count();
65
66         } catch(E) {
67             this.error.standard_unexpected_error_alert('cat.copy_browser.init: ',E);
68         }
69     },
70
71     'controller_init' : function(params) {
72         var obj = this;
73         try {
74             JSAN.use('util.controller'); obj.controller = new util.controller();
75             obj.controller.init(
76                 {
77                     control_map : {
78                         'save_columns' : [ [ 'command' ], function() { obj.list.save_columns(); } ],
79                         'sel_clip' : [
80                             ['command'],
81                             function() { obj.list.clipboard(); }
82                         ],
83                         'cmd_broken' : [
84                             ['command'],
85                             function() { 
86                                 alert(document.getElementById('commonStrings').getString('common.unimplemented'));
87                             }
88                         ],
89                         'cmd_show_my_libs' : [
90                             ['command'],
91                             function() { 
92                                 obj.show_my_libs(); 
93                             }
94                         ],
95                         'cmd_show_all_libs' : [
96                             ['command'],
97                             function() {
98                                 obj.show_all_libs();
99                             }
100                         ],
101                         'cmd_show_libs_with_copies' : [
102                             ['command'],
103                             function() {
104                                 obj.show_libs_with_copies();
105                             }
106                         ],
107                         'cmd_clear' : [
108                             ['command'],
109                             function() {
110                                 obj.map_tree = {};
111                                 obj.list.clear();
112                             }
113                         ],
114                         'sel_mark_items_damaged' : [
115                             ['command'],
116                             function() {
117                                 JSAN.use('cat.util'); JSAN.use('util.functional');
118
119                                 var list = util.functional.filter_list( obj.sel_list, function (o) { return o.split(/_/)[0] == 'acp'; });
120
121                                 list = util.functional.map_list( list, function (o) { return o.split(/_/)[1]; });
122
123                                 cat.util.mark_item_damaged( list );
124
125                                 obj.refresh_list();
126                             }
127                         ],
128                         'sel_mark_items_missing' : [
129                             ['command'],
130                             function() {
131                                 JSAN.use('cat.util'); JSAN.use('util.functional');
132
133                                 var list = util.functional.filter_list( obj.sel_list, function (o) { return o.split(/_/)[0] == 'acp'; });
134
135                                 list = util.functional.map_list( list, function (o) { return o.split(/_/)[1]; });
136
137                                 cat.util.mark_item_missing( list );
138
139                                 obj.refresh_list();
140                             }
141                         ],
142                         'sel_patron' : [
143                             ['command'],
144                             function() {
145                                 JSAN.use('util.functional');
146
147                                 var list = util.functional.filter_list(
148                                     obj.sel_list,
149                                     function (o) {
150                                         return o.split(/_/)[0] == 'acp';
151                                     }
152                                 );
153
154                                 list = util.functional.map_list(
155                                     list,
156                                     function (o) {
157                                         return { 'copy_id' : o.split(/_/)[1] };
158                                     }
159                                 );
160                                 
161                                 JSAN.use('circ.util');
162                                 circ.util.show_last_few_circs(list);
163                             }
164                         ],
165                         'sel_copy_details' : [
166                             ['command'],
167                             function() {
168                                 JSAN.use('util.functional');
169
170                                 var list = util.functional.filter_list(
171                                     obj.sel_list,
172                                     function (o) {
173                                         return o.split(/_/)[0] == 'acp';
174                                     }
175                                 );
176
177                                 JSAN.use('circ.util');
178                                 circ.util.item_details_new(
179                                     util.functional.map_list(
180                                         list, function (o) {
181                                             return obj.map_acp[o].barcode();
182                                         }
183                                     )
184                                 );
185                             }
186                         ],
187                         'cmd_create_brt' : [
188                             ['command'],
189                             function() {
190                                 JSAN.use("cat.util");
191                                 JSAN.use("util.functional");
192
193                                 /* Filter selected rows that aren"t copies. */
194                                 var list = util.functional.filter_list(
195                                     obj.sel_list,
196                                     function (o) {
197                                         return o.split(/_/)[0] == "acp";
198                                     }
199                                 );
200                                 var results = cat.util.make_bookable(
201                                     util.functional.map_list(
202                                         list, function (o) {
203                                             return obj.map_acp[o].id();
204                                         }
205                                     )
206                                 );
207                                 if (results && results["brsrc"]) {
208                                     cat.util.edit_new_brsrc(results["brsrc"]);
209                                 }
210                             }
211                         ],
212                         'cmd_book_item_now' : [
213                             ['command'],
214                             function() {
215                                 JSAN.use("cat.util");
216                                 JSAN.use("util.functional");
217
218                                 /* Filter selected rows that aren"t copies. */
219                                 var list = util.functional.filter_list(
220                                     obj.sel_list,
221                                     function (o) {
222                                         return o.split(/_/)[0] == "acp";
223                                     }
224                                 );
225                                 var results = cat.util.make_bookable(
226                                     util.functional.map_list(
227                                         list, function (o) {
228                                             return obj.map_acp[o].id();
229                                         }
230                                     )
231                                 );
232                                 if (results) {
233                                     cat.util.edit_new_bresv(results);
234                                 }
235                             }
236                         ],
237                         'cmd_add_items' : [
238                             ['command'],
239                             function() {
240                                 try {
241                                     JSAN.use('util.functional');
242                                     var list = util.functional.map_list(
243                                         util.functional.filter_list(
244                                             obj.sel_list,
245                                             function (o) {
246                                                 return o.split(/_/)[0] == 'acn';
247                                             }
248                                         ),
249                                         function (o) {
250                                             return o.split(/_/)[1];
251                                         }
252                                     );
253                                     if (list.length == 0) return;
254
255                                     var copy_shortcut = {};
256                                     list = util.functional.map_list(
257                                         list,
258                                         function (o) {
259                                             var call_number = obj.map_acn['acn_'+o];
260                                             var ou_id = call_number.owning_lib();
261                                             var volume_id = o;
262                                             var label = call_number.label();
263                                             var acnc_id = typeof call_number.label_class() == 'object'
264                                                 ? call_number.label_class().id()
265                                                 : call_number.label_class();
266                                             var acnp_id = typeof call_number.prefix() == 'object'
267                                                 ? call_number.prefix().id()
268                                                 : call_number.prefix();
269                                             var acns_id = typeof call_number.suffix() == 'object'
270                                                 ? call_number.suffix().id()
271                                                 : call_number.suffix();
272                                             if (!copy_shortcut[ou_id]) copy_shortcut[ou_id] = {};
273                                             var callnumber_composite_key = acnc_id + ':' + acnp_id + ':' + label + ':' + acns_id;
274                                             copy_shortcut[ou_id][ callnumber_composite_key ] = volume_id;
275
276                                             return ou_id;
277                                         }
278                                     );
279                                     /* quick fix */  /* what was this fixing? */
280                                     list = []; for (var i in copy_shortcut) { list.push( i ); }
281
282                                     var edit = 0;
283                                     try {
284                                         edit = obj.network.request(
285                                             api.PERM_MULTI_ORG_CHECK.app,
286                                             api.PERM_MULTI_ORG_CHECK.method,
287                                             [ 
288                                                 ses(), 
289                                                 obj.data.list.au[0].id(), 
290                                                 list,
291                                                 [ 'CREATE_COPY' ]
292                                             ]
293                                         ).length == 0 ? 1 : 0;
294                                     } catch(E) {
295                                         obj.error.sdump('D_ERROR','batch permission check: ' + E);
296                                     }
297
298                                     if (edit==0) return; // no read-only view for this interface
299
300                                     var title = document.getElementById('catStrings').getString('staff.cat.copy_browser.add_item.title');
301
302                                     var url;
303                                     var unified_interface = String( obj.data.hash.aous['ui.unified_volume_copy_editor'] ) == 'true';
304                                     if (unified_interface) {
305                                         var horizontal_interface = String( obj.data.hash.aous['ui.cat.volume_copy_editor.horizontal'] ) == 'true';
306                                         url = xulG.url_prefix( horizontal_interface ? urls.XUL_VOLUME_COPY_CREATOR_HORIZONTAL : urls.XUL_VOLUME_COPY_CREATOR );
307                                     } else {
308                                         url = xulG.url_prefix( urls.XUL_VOLUME_COPY_CREATOR_ORIGINAL );
309                                     }
310
311                                     var w = xulG.new_tab(
312                                         url,
313                                         { 'tab_name' : title },
314                                         {
315                                             'doc_id' : obj.docid, 
316                                             'ou_ids' : list, 
317                                             'copy_shortcut' : copy_shortcut,
318                                             'onrefresh' : function() { obj.refresh_list(); }
319                                         }
320                                     );
321                                 } catch(E) {
322                                     obj.error.standard_unexpected_error_alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.add_item.error'),E);
323                                 }
324                             }
325                         ],
326                         'cmd_add_items_to_buckets' : [
327                             ['command'],
328                             function() {
329                                 try {
330                                     JSAN.use('util.functional');
331
332                                     var list = util.functional.filter_list(
333                                         obj.sel_list,
334                                         function (o) {
335                                             return o.split(/_/)[0] == 'acp';
336                                         }
337                                     );
338
339                                     list = util.functional.map_list(
340                                         list,
341                                         function (o) {
342                                             return o.split(/_/)[1];
343                                         }
344                                     );
345                                 
346                                     JSAN.use('cat.util');
347                                     cat.util.add_copies_to_bucket( list );
348
349                                 } catch(E) {
350                                     obj.error.standard_unexpected_error_alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.add_items_bucket.error'),E);
351                                 }
352                             }
353                         ],
354                         'cmd_edit_items' : [
355                             ['command'],
356                             function() {
357                                 try {
358                                     var unified_interface = String( obj.data.hash.aous['ui.unified_volume_copy_editor'] ) == 'true';
359                                     if (!unified_interface) {
360                                         obj.controller.control_map['old_cmd_edit_items'][1]();
361                                         return;
362                                     }
363
364                                     JSAN.use('util.functional');
365
366                                     var list = util.functional.filter_list(
367                                         obj.sel_list,
368                                         function (o) {
369                                             return o.split(/_/)[0] == 'acp';
370                                         }
371                                     );
372
373                                     list = util.functional.map_list(
374                                         list,
375                                         function (o) {
376                                             var cloned_copy_obj = JSON2js( js2JSON( obj.map_acp[ o ] ) );
377                                             cloned_copy_obj.call_number( obj.map_acn[ 'acn_' + cloned_copy_obj.call_number() ] );
378                                             return cloned_copy_obj;
379                                         }
380                                     );
381
382                                     xulG.volume_item_creator( {'existing_copies':list, 'onrefresh' : function() { obj.refresh_list(); } } );
383
384                                 } catch(E) {
385                                     obj.error.standard_unexpected_error_alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.edit_items.error'),E);
386                                     obj.refresh_list();
387                                 }
388                             }
389                         ],
390                         'cmd_replace_barcode' : [
391                             ['command'],
392                             function() {
393                                 try {
394                                     JSAN.use('util.functional');
395
396                                     var list = util.functional.filter_list(
397                                         obj.sel_list,
398                                         function (o) {
399                                             return o.split(/_/)[0] == 'acp';
400                                         }
401                                     );
402
403                                     list = util.functional.map_list(
404                                         list,
405                                         function (o) {
406                                             var cloned_copy_obj = JSON2js( js2JSON( obj.map_acp[ o ] ) );
407                                             cloned_copy_obj.call_number( obj.map_acn[ 'acn_' + cloned_copy_obj.call_number() ] );
408                                             return cloned_copy_obj;
409                                         }
410                                     );
411
412                                     xulG.volume_item_creator( {'existing_copies':list, 'onrefresh' : function() { obj.refresh_list(); } } );
413
414                                 } catch(E) {
415                                     obj.error.standard_unexpected_error_alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.edit_items.error'),E);
416                                     obj.refresh_list();
417                                 }
418                             }
419                         ],
420
421                         'old_cmd_edit_items' : [
422                             ['command'],
423                             function() {
424                                 try {
425                                     JSAN.use('util.functional');
426
427                                     var list = util.functional.filter_list(
428                                         obj.sel_list,
429                                         function (o) {
430                                             return o.split(/_/)[0] == 'acp';
431                                         }
432                                     );
433
434                                     list = util.functional.map_list(
435                                         list,
436                                         function (o) {
437                                             return o.split(/_/)[1];
438                                         }
439                                     );
440
441                                     JSAN.use('cat.util'); cat.util.spawn_copy_editor( { 'copy_ids' : list, 'edit' : 1 } );
442                                     obj.refresh_list();
443
444                                 } catch(E) {
445                                     obj.error.standard_unexpected_error_alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.edit_items.error'),E);
446                                 }
447                             }
448                         ],
449                         'cmd_delete_items' : [
450                             ['command'],
451                             function() {
452                                 try {
453                                     JSAN.use('util.functional');
454
455                                     var list = util.functional.filter_list(
456                                         obj.sel_list,
457                                         function (o) {
458                                             return o.split(/_/)[0] == 'acp';
459                                         }
460                                     );
461
462                                     list = util.functional.map_list(
463                                         list,
464                                         function (o) {
465                                             return JSON2js( js2JSON( obj.map_acp[ 'acp_' + o.split(/_/)[1] ] ) );
466                                         }
467                                     );
468
469                                     var delete_msg;
470                                     if (list.length != 1) {
471                                         delete_msg = document.getElementById('catStrings').getFormattedString('staff.cat.copy_browser.delete_items.confirm.plural', [list.length]);
472                                     } else {
473                                         delete_msg = document.getElementById('catStrings').getString('staff.cat.copy_browser.delete_items.confirm');
474                                     }
475                                     var r = obj.error.yns_alert(
476                                             delete_msg,
477                                             document.getElementById('catStrings').getString('staff.cat.copy_browser.delete_items.title'),
478                                             document.getElementById('catStrings').getString('staff.cat.copy_browser.delete_items.delete'),
479                                             document.getElementById('catStrings').getString('staff.cat.copy_browser.delete_items.cancel'),
480                                             null,
481                                             document.getElementById('commonStrings').getString('common.confirm')
482                                     );
483
484                                     if (r == 0) {
485                                         var acn_hash = {}; var acn_list = [];
486                                         for (var i = 0; i < list.length; i++) {
487                                             list[i].isdeleted('1');
488                                             var acn_id = list[i].call_number();
489                                             if ( ! acn_hash[ acn_id ] ) {
490                                                 acn_hash[ acn_id ] = obj.map_acn[ 'acn_' + acn_id ];
491                                                 acn_hash[ acn_id ].copies( [] );
492                                             }
493                                             var temp = acn_hash[ acn_id ].copies();
494                                             temp.push( list[i] );
495                                             acn_hash[ acn_id ].copies( temp );
496                                         }
497                                         for (var i in acn_hash) acn_list.push( acn_hash[i] );
498                                         var robj = obj.network.simple_request(
499                                             'FM_ACN_TREE_UPDATE', 
500                                             [ ses(), acn_list, true ],
501                                             null,
502                                             {
503                                                 'title' : document.getElementById('catStrings').getString('staff.cat.copy_browser.delete_items.override'),
504                                                 'overridable_events' : [
505                                                     1208 /* TITLE_LAST_COPY */,
506                                                     1227 /* COPY_DELETE_WARNING */,
507                                                 ]
508                                             }
509                                         );
510                                         if (robj == null) throw(robj);
511                                         if (typeof robj.ilsevent != 'undefined') {
512                                             if ( (robj.ilsevent != 0) && (robj.ilsevent != 1227 /* COPY_DELETE_WARNING */) && (robj.ilsevent != 1208 /* TITLE_LAST_COPY */) ) throw(robj);
513                                         }
514                                         obj.refresh_list();
515                                     }
516
517                                     
518                                 } catch(E) {
519                                     obj.error.standard_unexpected_error_alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.delete_items.error'),E);
520                                     obj.refresh_list();
521                                 }
522                             }
523                         ],
524                         'cmd_print_spine_labels' : [
525                             ['command'],
526                             function() {
527                                 try {
528                                     JSAN.use('util.functional');
529                                     
530                                     var list = util.functional.filter_list(
531                                         obj.sel_list,
532                                         function (o) {
533                                             return o.split(/_/)[0] == 'acp';
534                                         }
535                                     );
536
537                                     list = util.functional.map_list(
538                                         list,
539                                         function (o) {
540                                             return obj.map_acp[ o ];
541                                         }
542                                     );
543
544                                     xulG.new_tab(
545                                         xulG.url_prefix( urls.XUL_SPINE_LABEL ),
546                                         { 'tab_name' : document.getElementById('catStrings').getString('staff.cat.copy_browser.print_spine.tab') },
547                                         {
548                                             'barcodes' : util.functional.map_list( list, function(o){return o.barcode();})
549                                         }
550                                     );
551                                 } catch(E) {
552                                     obj.error.standard_unexpected_error_alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.print_spine.error'),E);
553                                 }
554                             }
555                         ],
556                         'cmd_add_volumes' : [
557                             ['command'],
558                             function() {
559                                 try {
560                                     JSAN.use('util.functional');
561                                     var list = util.functional.filter_list(
562                                         obj.sel_list,
563                                         function (o) {
564                                             return o.split(/_/)[0] == 'aou';
565                                         }
566                                     );
567                                     list = util.functional.map_list(
568                                         list,
569                                         function (o) {
570                                             return o.split(/_/)[1];
571                                         }
572                                     );        
573                                     var edit = 0;
574                                     try {
575                                         edit = obj.network.request(
576                                             api.PERM_MULTI_ORG_CHECK.app,
577                                             api.PERM_MULTI_ORG_CHECK.method,
578                                             [ 
579                                                 ses(), 
580                                                 obj.data.list.au[0].id(), 
581                                                 list,
582                                                 [ 'CREATE_VOLUME', 'CREATE_COPY' ]
583                                             ]
584                                         ).length == 0 ? 1 : 0;
585                                     } catch(E) {
586                                         obj.error.sdump('D_ERROR','batch permission check: ' + E);
587                                     }
588
589                                     if (edit==0) {
590                                         alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.add_volume.permission_error'));
591                                         return; // no read-only view for this interface
592                                     }
593
594                                     var title = document.getElementById('catStrings').getString('staff.cat.copy_browser.add_volume.title');
595
596                                     var horizontal_interface = String( obj.data.hash.aous['ui.cat.volume_copy_editor.horizontal'] ) == 'true';
597                                     var url = window.xulG.url_prefix( horizontal_interface ? urls.XUL_VOLUME_COPY_CREATOR_HORIZONTAL : urls.XUL_VOLUME_COPY_CREATOR );
598                                     var w = xulG.new_tab(
599                                         url,
600                                         { 'tab_name' : title },
601                                         { 'doc_id' : obj.docid, 'ou_ids' : list, 'onrefresh' : function() { obj.refresh_list(); } }
602                                     );
603
604                                 } catch(E) {
605                                     obj.error.standard_unexpected_error_alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.add_volume.error'),E);
606                                 }
607                             }
608                         ],
609                         'cmd_edit_volumes' : [
610                             ['command'],
611                             function() {
612                                 try {
613                                     JSAN.use('util.functional');
614                                     var volumes = util.functional.map_list(
615                                         util.functional.filter_list(
616                                             obj.sel_list,
617                                             function (o) {
618                                                 return o.split(/_/)[0] == 'acn';
619                                             }
620                                         ),
621                                         function (o) {
622                                             return o.split(/_/)[1];
623                                         }
624                                     );
625                                     volumes = util.functional.map_list(
626                                         volumes,
627                                         function (o) {
628                                             var my_acn = obj.map_acn['acn_' + o];
629                                             return function(r){return r;}(my_acn);
630                                         }
631                                     );
632
633                                     JSAN.use('cat.util'); 
634                                     if ( cat.util.batch_edit_volumes( volumes ) ) {
635                                         obj.refresh_list();
636                                     }
637
638                                 } catch(E) {
639                                     obj.error.standard_unexpected_error_alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.edit_volume.exception'),E);
640                                 }
641                             }
642                         ],
643                         'cmd_delete_volumes' : [
644                             ['command'],
645                             function() {
646                                 try {
647                                     JSAN.use('util.functional');
648
649                                     var list = util.functional.filter_list(
650                                         obj.sel_list,
651                                         function (o) {
652                                             return o.split(/_/)[0] == 'acn';
653                                         }
654                                     );
655
656                                     list = util.functional.map_list(
657                                         list,
658                                         function (o) {
659                                             return JSON2js( js2JSON( obj.map_acn[ 'acn_' + o.split(/_/)[1] ] ) );
660                                         }
661                                     );
662
663                                     var del_prompt;
664                                     if (list.length == 1) {
665                                         del_prompt = document.getElementById('catStrings').getString('staff.cat.copy_browser.delete_volume.prompt');
666                                     } else {
667                                         del_prompt = document.getElementById('catStrings').getFormattedString('staff.cat.copy_browser.delete_volume.prompt.plural', [list.length]);
668                                     }
669
670                                     var r = obj.error.yns_alert(
671                                             del_prompt,
672                                             document.getElementById('catStrings').getString('staff.cat.copy_browser.delete_volume.title'),
673                                             document.getElementById('catStrings').getString('staff.cat.copy_browser.delete_volume.delete'),
674                                             document.getElementById('catStrings').getString('staff.cat.copy_browser.delete_volume.cancel'),
675                                             null,
676                                             document.getElementById('commonStrings').getString('common.confirm')
677                                     );
678
679                                     if (r == 0) {
680                                         for (var i = 0; i < list.length; i++) {
681                                             list[i].isdeleted('1');
682                                         }
683                                         var robj = obj.network.simple_request(
684                                             'FM_ACN_TREE_UPDATE', 
685                                             [ ses(), list, true ],
686                                             null,
687                                             {
688                                                 'title' : document.getElementById('catStrings').getString('staff.cat.copy_browser.delete_volume.override'),
689                                                 'overridable_events' : [
690                                                 ]
691                                             }
692                                         );
693                                         if (robj == null) throw(robj);
694                                         if (typeof robj.ilsevent != 'undefined') {
695                                             if (robj.ilsevent == 1206 /* VOLUME_NOT_EMPTY */) {
696                                                 alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.delete_volume.copies_remain'));
697                                                 return;
698                                             }
699                                             if (robj.ilsevent != 0) throw(robj);
700                                         }
701                                         obj.refresh_list();
702                                     }
703                                 } catch(E) {
704                                     obj.error.standard_unexpected_error_alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.delete_volume.exception'),E);
705                                     obj.refresh_list();
706                                 }
707
708                             }
709                         ],
710                         'cmd_mark_library' : [
711                             ['command'],
712                             function() {
713                                 try {
714                                     var list = util.functional.filter_list(
715                                         obj.sel_list,
716                                         function (o) {
717                                             return o.split(/_/)[0] == 'aou';
718                                         }
719                                     );
720
721                                     list = util.functional.map_list(
722                                         list,
723                                         function (o) {
724                                             return o.split(/_/)[1];
725                                         }
726                                     );
727
728                                     if (list.length == 1) {
729                                         obj.data.marked_library = { 'lib' : list[0], 'docid' : obj.docid };
730                                         obj.data.stash('marked_library');
731                                         alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.mark_library.alert'));
732                                     } else {
733                                         obj.error.yns_alert(
734                                                 document.getElementById('catStrings').getString('staff.cat.copy_browser.mark_library.prompt'),
735                                                 document.getElementById('catStrings').getString('staff.cat.copy_browser.mark_library.title'),
736                                                 document.getElementById('commonStrings').getString('common.ok'),
737                                                 null,
738                                                 null,
739                                                 document.getElementById('commonStrings').getString('common.confirm')
740                                                 );
741                                     }
742                                 } catch(E) {
743                                     obj.error.standard_unexpected_error_alert('copy browser -> mark library',E);
744                                 }
745                             }
746                         ],
747
748                         'cmd_mark_volume' : [
749                             ['command'],
750                             function() {
751                                 try {
752                                     var list = util.functional.filter_list(
753                                         obj.sel_list,
754                                         function (o) {
755                                             return o.split(/_/)[0] == 'acn';
756                                         }
757                                     );
758
759                                     list = util.functional.map_list(
760                                         list,
761                                         function (o) {
762                                             return o.split(/_/)[1];
763                                         }
764                                     );
765
766                                     if (list.length == 1) {
767                                         obj.data.marked_volume = list[0];
768                                         obj.data.stash('marked_volume');
769                                         alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.mark_volume.alert'));
770                                     } else {
771                                         obj.error.yns_alert(
772                                                 document.getElementById('catStrings').getString('staff.cat.copy_browser.mark_volume.prompt'),
773                                                 document.getElementById('catStrings').getString('staff.cat.copy_browser.mark_volume.title'),
774                                                 document.getElementById('commonStrings').getString('common.ok'),
775                                                 null,
776                                                 null,
777                                                 document.getElementById('commonStrings').getString('common.confirm')
778                                                 );
779                                     }
780                                 } catch(E) {
781                                     obj.error.standard_unexpected_error_alert('copy browser -> mark volume',E);
782                                 }
783                             }
784                         ],
785                         'cmd_refresh_list' : [
786                             ['command'],
787                             function() {
788                                 obj.refresh_list();
789                             }
790                         ],
791                         'cmd_transfer_volume' : [
792                             ['command'],
793                             function() {
794                                 try {
795                                     obj.data.stash_retrieve();
796                                     if (!obj.data.marked_library) {
797                                         alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer_volume.alert'));
798                                         return;
799                                     }
800                                     
801                                     JSAN.use('util.functional');
802
803                                     var list = util.functional.filter_list(
804                                         obj.sel_list,
805                                         function (o) {
806                                             return o.split(/_/)[0] == 'acn';
807                                         }
808                                     );
809
810                                     list = util.functional.map_list(
811                                         list,
812                                         function (o) {
813                                             return o.split(/_/)[1];
814                                         }
815                                     );
816
817                                     netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite');
818
819                                     var acn_list = util.functional.map_list(
820                                         list,
821                                         function (o) {
822                                             return obj.map_acn[ 'acn_' + o ].label();
823                                         }
824                                     ).join(document.getElementById('commonStrings').getString('common.grouping_string'));
825
826                                     var xml = '<vbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" flex="1" style="overflow: auto">';
827                                     xml += '<description>';
828                                     xml += document.getElementById('catStrings').getFormattedString('staff.cat.copy_browser.transfer.prompt', [acn_list, obj.data.hash.aou[ obj.data.marked_library.lib ].shortname()]);
829                                     xml += '</description>';
830                                     xml += '<hbox><button label="' + document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.submit.label') + '" name="fancy_submit"/>';
831                                     xml += '<button label="' 
832                                         + document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.cancel.label') 
833                                         + '" accesskey="' 
834                                         + document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.cancel.accesskey') 
835                                         + '" name="fancy_cancel"/></hbox>';
836                                     xml += '<iframe style="overflow: scroll" flex="1" src="' + urls.XUL_BIB_BRIEF + '?docid=' + obj.data.marked_library.docid + '"/>';
837                                     xml += '</vbox>';
838                                     JSAN.use('OpenILS.data');
839                                     var data = new OpenILS.data(); data.init({'via':'stash'});
840                                     //data.temp_transfer = xml; data.stash('temp_transfer');
841                                     JSAN.use('util.window'); var win = new util.window();
842                                     var fancy_prompt_data = win.open(
843                                         urls.XUL_FANCY_PROMPT,
844                                         //+ '?xml_in_stash=temp_transfer'
845                                         //+ '&title=' + window.escape('Volume Transfer'),
846                                         'fancy_prompt', 'chrome,resizable,modal,width=500,height=300',
847                                         {
848                                             'xml' : xml,
849                                             'title' : document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.title')
850                                         }
851                                     );
852
853                                     if (fancy_prompt_data.fancy_status == 'incomplete') {
854                                         alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.incomplete'));
855                                         return;
856                                     }
857
858                                     var robj = obj.network.simple_request(
859                                         'FM_ACN_TRANSFER', 
860                                         [ ses(), { 'docid' : obj.data.marked_library.docid, 'lib' : obj.data.marked_library.lib, 'volumes' : list } ],
861                                         null,
862                                         {
863                                             'title' : document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.override.failure'),
864                                             'overridable_events' : [
865                                                 1208 /* TITLE_LAST_COPY */,
866                                                 1219 /* COPY_REMOTE_CIRC_LIB */,
867                                             ],
868                                         }
869                                     );
870
871                                     if (typeof robj.ilsevent != 'undefined') {
872                                         if (robj.ilsevent == 1221 /* ORG_CANNOT_HAVE_VOLS */) {
873                                             alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.ineligible_destination'));
874                                         } else {
875                                             throw(robj);
876                                         }
877                                     } else {
878                                         alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.success'));
879                                     }
880
881                                 } catch(E) {
882                                     obj.error.standard_unexpected_error_alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.unexpected_error'),E);
883                                 }
884                                 obj.refresh_list();
885                             }
886                         ],
887
888                         'cmd_transfer_items' : [
889                             ['command'],
890                             function() {
891                                 try {
892                                     obj.data.stash_retrieve();
893                                     if (!obj.data.marked_volume) {
894                                         alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer_items.missing_volume'));
895                                         return;
896                                     }
897                                     
898                                     JSAN.use('util.functional');
899
900                                     var list = util.functional.filter_list(
901                                         obj.sel_list,
902                                         function (o) {
903                                             return o.split(/_/)[0] == 'acp';
904                                         }
905                                     );
906
907                                     list = util.functional.map_list(
908                                         list,
909                                         function (o) {
910                                             return o.split(/_/)[1];
911                                         }
912                                     );
913
914                                     var volume = obj.network.simple_request('FM_ACN_RETRIEVE.authoritative',[ obj.data.marked_volume ]);
915
916                                     JSAN.use('cat.util'); cat.util.transfer_copies( { 
917                                         'copy_ids' : list, 
918                                         'docid' : volume.record(),
919                                         'volume_label' : volume.label(),
920                                         'owning_lib' : volume.owning_lib(),
921                                     } );
922
923                                 } catch(E) {
924                                     obj.error.standard_unexpected_error_alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer_items.unexpected_error'),E);
925                                 }
926                                 obj.refresh_list();
927                             }
928                         ],
929
930                         'cmd_link_as_multi_bib' : [
931                             ['command'],
932                             function() {
933                                 try {
934                                     obj.data.stash_retrieve();
935                                     if (!obj.data.marked_multi_home_record) {
936                                         alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.link_as_multi_bib.missing_bib'));
937                                         return;
938                                     }
939
940                                     JSAN.use('util.functional');
941
942                                     var list = util.functional.filter_list(
943                                         obj.sel_list,
944                                         function (o) {
945                                             return o.split(/_/)[0] == 'acp';
946                                         }
947                                     );
948
949                                     list = util.functional.map_list(
950                                         list,
951                                         function (o) {
952                                             return obj.map_acp[ o ].barcode();
953                                         }
954                                     );
955
956                                     xulG.new_tab(
957                                         window.xulG.url_prefix(urls.MANAGE_MULTI_HOME_ITEMS),
958                                         {},
959                                         { 'docid' : obj.data.marked_multi_home_record, 'barcodes' : list }
960                                     );
961
962                                 } catch(E) {
963                                     alert('Error in copy_browser.js, cmd_link_as_multi_bib: ' + E);
964                                 }
965                                 obj.refresh_list();
966                             }
967                         ]
968                     }
969                 }
970             );
971
972         } catch(E) {
973             this.error.standard_unexpected_error_alert('cat.copy_browser.controller_init(): ',E);
974         }
975     },
976
977     'depth_menu_init' : function(params) {
978         var obj = this;
979         try {
980             var list = [];
981             var max_depth = 0;
982             for (var i = 0; i < obj.data.list.aout.length; i++) {
983                 var type = obj.data.list.aout[i];
984                 var depth = type.depth();
985                 if ( depth > max_depth) { max_depth = depth; }
986                 if (typeof list[depth] == 'undefined') {
987                     list[depth] = [
988                         type.opac_label(),
989                         type.depth(),
990                         false,
991                         ( type.depth() * 2)
992                     ];
993                 } else {
994                     list[depth][0] += ' / ' + type.opac_label();
995                 }
996             }
997             ml = util.widgets.make_menulist( list, max_depth );
998             ml.setAttribute('id','depth_menu'); document.getElementById('x_depth_menu').appendChild(ml);
999             ml.addEventListener(
1000                 'command',
1001                 function(ev) {
1002                     obj.default_depth = ev.target.value;
1003                     if (document.getElementById('refresh_button')) document.getElementById('refresh_button').focus(); 
1004                     JSAN.use('util.file'); var file = new util.file('copy_browser_prefs.'+obj.data.server_unadorned);
1005                     util.widgets.save_attributes(file, { 
1006                         'lib_menu' : [ 'value' ],
1007                         'depth_menu' : [ 'value' ],
1008                         'show_acns' : [ 'checked' ],
1009                         'show_acps' : [ 'checked' ]
1010                     });
1011                 },
1012                 false
1013             );
1014
1015             file = new util.file('copy_browser_prefs.'+obj.data.server_unadorned);
1016             util.widgets.load_attributes(file);
1017             ml.value = ml.getAttribute('value');
1018             if (! ml.value) {
1019                 ml.value = max_depth;
1020                 ml.setAttribute('value',ml.value);
1021             }
1022
1023             return ml.value;
1024         } catch(E) {
1025             alert('Error in copy_browser.js, depth_menu_init(): ' + E);
1026         }
1027     },
1028
1029     'library_menu_init' : function(params) {
1030         var obj = this;
1031         try {
1032
1033             obj.org_ids = obj.network.simple_request('FM_AOU_IDS_RETRIEVE_VIA_RECORD_ID.authoritative',[ obj.docid ]);
1034             if (typeof obj.org_ids.ilsevent != 'undefined') throw(obj.org_ids);
1035             JSAN.use('util.functional'); 
1036             obj.org_ids = util.functional.map_list( obj.org_ids, function (o) { return Number(o); });
1037
1038             var org = obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ];
1039
1040             JSAN.use('util.file'); JSAN.use('util.widgets');
1041
1042             var file; var list_data; var ml; 
1043
1044             file = new util.file('offline_ou_list'); 
1045             if (file._file.exists()) {
1046                 list_data = file.get_object(); file.close();
1047                 for (var i = 0; i < list_data[0].length; i++) { // make sure all entries are enabled
1048                     list_data[0][i][2] = false;
1049                 }
1050                 ml = util.widgets.make_menulist( list_data[0], list_data[1] );
1051                 ml.setAttribute('id','lib_menu'); document.getElementById('x_lib_menu').appendChild(ml);
1052                 for (var i = 0; i < obj.org_ids.length; i++) {
1053                     ml.getElementsByAttribute('value',obj.org_ids[i])[0].setAttribute('class','has_copies');
1054                 }
1055                 ml.firstChild.addEventListener(
1056                     'popupshown',
1057                     function(ev) {
1058                         document.getElementById('legend').setAttribute('hidden','false');
1059                     },
1060                     false
1061                 );
1062                 ml.firstChild.addEventListener(
1063                     'popuphidden',
1064                     function(ev) {
1065                         document.getElementById('legend').setAttribute('hidden','true');
1066                     },
1067                     false
1068                 );
1069                 ml.addEventListener(
1070                     'command',
1071                     function(ev) {
1072                         obj.default_lib = obj.data.hash.aou[ ev.target.value ];
1073                         if (document.getElementById('refresh_button')) document.getElementById('refresh_button').focus(); 
1074                         JSAN.use('util.file'); var file = new util.file('copy_browser_prefs.'+obj.data.server_unadorned);
1075                         util.widgets.save_attributes(file, { 
1076                             'lib_menu' : [ 'value' ],
1077                             'depth_menu' : [ 'value' ],
1078                             'show_acns' : [ 'checked' ],
1079                             'show_acps' : [ 'checked' ]
1080                         });
1081                         obj.refresh_list();
1082                     },
1083                     false
1084                 );
1085             } else {
1086                 throw(document.getElementById('catStrings').getString('staff.cat.copy_browser.missing_library') + '\n');
1087             }
1088
1089             file = new util.file('copy_browser_prefs.'+obj.data.server_unadorned);
1090             util.widgets.load_attributes(file);
1091             ml.value = ml.getAttribute('value');
1092             if (! ml.value) {
1093                 ml.value = org.id();
1094                 ml.setAttribute('value',ml.value);
1095             }
1096
1097             return ml.value;
1098
1099         } catch(E) {
1100             this.error.standard_unexpected_error_alert('cat.copy_browser.library_menu_init(): ',E);
1101         }
1102     },
1103
1104     'show_consortial_count' : function() {
1105         var obj = this;
1106         try {
1107             obj.network.simple_request('FM_ACP_COUNT.authoritative',[ obj.data.tree.aou.id(), obj.docid ],function(req){ 
1108                 try {
1109                     var robj = req.getResultObject();
1110                     var x = document.getElementById('consortial_total');
1111                     if (x) x.setAttribute('value',robj[0].count);
1112                     x = document.getElementById('consortial_available');
1113                     if (x) x.setAttribute('value',robj[0].available);
1114                 } catch(E) {
1115                     obj.error.standard_unexpected_error_alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.consortial_copy_count.error'),E);
1116                 }
1117             });
1118         } catch(E) {
1119             this.error.standard_unexpected_error_alert('cat.copy_browser.show_consortial_count: ',E);
1120         }
1121     },
1122
1123     'show_my_libs' : function(org) {
1124         var obj = this;
1125         try {
1126             if (!org) {
1127                 org = obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ];
1128             } else {
1129                 if (typeof org != 'object') org = obj.data.hash.aou[ org ];
1130             }
1131             obj.show_libs( org, false );
1132 /*        
1133             var p_org = obj.data.hash.aou[ org.parent_ou() ];
1134             if (p_org) {
1135                 obj.funcs.push( function() { 
1136                     document.getElementById('cmd_refresh_list').setAttribute('disabled','true'); 
1137                     document.getElementById('cmd_show_libs_with_copies').setAttribute('disabled','true'); 
1138                     document.getElementById('lib_menu').setAttribute('disabled','true'); 
1139                 } );
1140                 for (var i = 0; i < p_org.children().length; i++) {
1141                     obj.funcs.push(
1142                         function(o) {
1143                             return function() {
1144                                 obj.show_libs( o, false );
1145                             }
1146                         }( p_org.children()[i] )
1147                     );
1148                 }
1149                 obj.funcs.push( function() { 
1150                     document.getElementById('cmd_refresh_list').setAttribute('disabled','false'); 
1151                     document.getElementById('cmd_show_libs_with_copies').setAttribute('disabled','false'); 
1152                     document.getElementById('lib_menu').setAttribute('disabled','false'); 
1153                 } );
1154             }
1155 */
1156         } catch(E) {
1157             alert(E);
1158         }
1159     },
1160
1161     'show_all_libs' : function() {
1162         var obj = this;
1163         try {
1164             obj.show_my_libs();
1165
1166             obj.show_libs( obj.data.tree.aou );
1167
1168             obj.funcs.push( function() { 
1169                 document.getElementById('cmd_refresh_list').setAttribute('disabled','true'); 
1170                 document.getElementById('cmd_show_libs_with_copies').setAttribute('disabled','true'); 
1171                 document.getElementById('lib_menu').setAttribute('disabled','true'); 
1172             } );
1173
1174             for (var i = 0; i < obj.data.tree.aou.children().length; i++) {
1175                 var child = obj.data.tree.aou.children()[i];
1176                 if (obj.data.hash.aout[child.ou_type()].depth() <= obj.default_depth
1177                 && orgIsMine(obj.default_lib,child,obj.default_depth)) {
1178                     obj.funcs.push(
1179                         function(o) {
1180                             return function() {
1181                                 obj.show_libs( o );
1182                             }
1183                         }( child )
1184                     );
1185                 }
1186             }
1187             obj.funcs.push( function() { 
1188                 document.getElementById('cmd_refresh_list').setAttribute('disabled','false'); 
1189                 document.getElementById('cmd_show_libs_with_copies').setAttribute('disabled','false'); 
1190                 document.getElementById('lib_menu').setAttribute('disabled','false'); 
1191             } );
1192
1193         } catch(E) {
1194             alert(E);
1195         }
1196     },
1197
1198     'show_libs_with_copies' : function() {
1199         var obj = this;
1200         try {
1201             JSAN.use('util.functional');
1202
1203             var orgs = util.functional.map_list(
1204                 obj.org_ids,
1205                 function(id) { return obj.data.hash.aou[id]; }
1206             ).sort(
1207                 function( a, b ) {
1208                     if (a.shortname() < b.shortname()) return -1;
1209                     if (a.shortname() > b.shortname()) return 1;
1210                     return 0;
1211                 }
1212             );
1213             obj.funcs.push( function() { 
1214                 document.getElementById('cmd_refresh_list').setAttribute('disabled','true'); 
1215                 document.getElementById('cmd_show_libs_with_copies').setAttribute('disabled','true'); 
1216                 document.getElementById('lib_menu').setAttribute('disabled','true'); 
1217             } );
1218
1219             for (var i = 0; i < orgs.length; i++) {
1220                 obj.funcs.push(
1221                     function(o) {
1222                         return function() {
1223                             obj.show_libs(o,false);
1224                         }
1225                     }( orgs[i] )
1226                 );
1227             }
1228             obj.funcs.push( function() { 
1229                 document.getElementById('cmd_refresh_list').setAttribute('disabled','false'); 
1230                 document.getElementById('cmd_show_libs_with_copies').setAttribute('disabled','false'); 
1231                 document.getElementById('lib_menu').setAttribute('disabled','false'); 
1232             } );
1233
1234         } catch(E) {
1235             alert(E);
1236         }
1237     },
1238
1239     'show_libs' : function(start_aou,show_open) {
1240         var obj = this;
1241         try {
1242             if (!start_aou) throw('show_libs: Need a start_aou');
1243             JSAN.use('OpenILS.data'); obj.data = new OpenILS.data(); obj.data.init({'via':'stash'});
1244             JSAN.use('util.functional'); 
1245
1246             var parents = [];
1247             var temp_aou = start_aou;
1248             while ( temp_aou.parent_ou() ) {
1249                 temp_aou = obj.data.hash.aou[ temp_aou.parent_ou() ];
1250                 parents.push( temp_aou );
1251             }
1252             parents.reverse();
1253
1254             for (var i = 0; i < parents.length; i++) {
1255                 obj.funcs.push(
1256                     function(o,p) {
1257                         return function() { 
1258                             obj.append_org(o,p,{'container':'true','open':'true'}); 
1259                         };
1260                     }(parents[i], obj.data.hash.aou[ parents[i].parent_ou() ])
1261                 );
1262             }
1263
1264             obj.funcs.push(
1265                 function(o,p) {
1266                     return function() { obj.append_org(o,p); };
1267                 }(start_aou,obj.data.hash.aou[ start_aou.parent_ou() ])
1268             );
1269
1270             obj.funcs.push(
1271                 function() {
1272                     if (start_aou.children()) {
1273                         var x = obj.map_tree[ 'aou_' + start_aou.id() ];
1274                         x.setAttribute('container','true');
1275                         if (show_open) x.setAttribute('open','true');
1276                         for (var i = 0; i < start_aou.children().length; i++) {
1277                             var child = start_aou.children()[i];
1278                             if (obj.data.hash.aout[child.ou_type()].depth() <= obj.default_depth
1279                             && orgIsMine(obj.default_lib,child,obj.default_depth)) {
1280                                 obj.funcs.push(
1281                                     function(o,p) {
1282                                         return function() { obj.append_org(o,p); };
1283                                     }( child, start_aou )
1284                                 );
1285                             }
1286                         }
1287                     }
1288                 }
1289             );
1290
1291         } catch(E) {
1292             alert(E);
1293         }
1294     },
1295
1296     'on_select' : function(list,twisty) {
1297         var obj = this;
1298         for (var i = 0; i < list.length; i++) {
1299             var node = obj.map_tree[ list[i] ];
1300             //if (node.lastChild.nodeName == 'treechildren') { continue; } else { alert(node.lastChild.nodeName); }
1301             var row_type = list[i].split('_')[0];
1302             var id = list[i].split('_')[1];
1303             switch(row_type) {
1304                 case 'aou' : obj.on_select_org(id,twisty); break;
1305                 case 'acn' : obj.on_select_acn(id,twisty); break;
1306                 default: break;
1307             }
1308         }
1309     },
1310
1311     'on_select_acn' : function(acn_id,twisty) {
1312         var obj = this;
1313         try {
1314             var acn_tree = obj.map_acp[ 'acn_' + acn_id ];
1315             obj.funcs.push( function() { 
1316                 document.getElementById('cmd_refresh_list').setAttribute('disabled','true'); 
1317                 document.getElementById('cmd_show_libs_with_copies').setAttribute('disabled','true'); 
1318                 document.getElementById('lib_menu').setAttribute('disabled','true'); 
1319             } );
1320             if (acn_tree.copies()) {
1321                 for (var i = 0; i < acn_tree.copies().length; i++) {
1322                     obj.funcs.push(
1323                         function(c,a) {
1324                             return function() {
1325                                 obj.append_acp(c,a);
1326                             }
1327                         }( acn_tree.copies()[i], acn_tree )
1328                     )
1329                 }
1330             }
1331             obj.funcs.push( function() { 
1332                 document.getElementById('cmd_refresh_list').setAttribute('disabled','false'); 
1333                 document.getElementById('cmd_show_libs_with_copies').setAttribute('disabled','false'); 
1334                 document.getElementById('lib_menu').setAttribute('disabled','false'); 
1335             } );
1336         } catch(E) {
1337             alert(E);
1338         }
1339     },
1340
1341     'on_select_org' : function(org_id,twisty) {
1342         var obj = this;
1343         try {
1344             var org = obj.data.hash.aou[ org_id ];
1345             obj.funcs.push( function() { 
1346                 document.getElementById('cmd_refresh_list').setAttribute('disabled','true'); 
1347                 document.getElementById('cmd_show_libs_with_copies').setAttribute('disabled','true'); 
1348                 document.getElementById('lib_menu').setAttribute('disabled','true'); 
1349             } );
1350             if (org.children()) {
1351                 for (var i = 0; i < org.children().length; i++) {
1352                     var child = org.children()[i];
1353                     if (obj.data.hash.aout[child.ou_type()].depth() <= obj.default_depth
1354                     && orgIsMine(obj.default_lib,child,obj.default_depth)) {
1355                         obj.funcs.push(
1356                             function(o,p) {
1357                                 return function() {
1358                                     obj.append_org(o,p)
1359                                 }
1360                             }(child,org)
1361                         );
1362                     }
1363                 }
1364             } 
1365             if (obj.map_acn[ 'aou_' + org_id ]) {
1366                 for (var i = 0; i < obj.map_acn[ 'aou_' + org_id ].length; i++) {
1367                     obj.funcs.push(
1368                         function(o,a) {
1369                             return function() {
1370                                 obj.append_acn(o,a);
1371                             }
1372                         }( org, obj.map_acn[ 'aou_' + org_id ][i] )
1373                     );
1374                 }
1375             }
1376             obj.funcs.push( function() { 
1377                 document.getElementById('cmd_refresh_list').setAttribute('disabled','false'); 
1378                 document.getElementById('cmd_show_libs_with_copies').setAttribute('disabled','false'); 
1379                 document.getElementById('lib_menu').setAttribute('disabled','false'); 
1380             } );
1381         } catch(E) {
1382             alert('Error in copy_browser.js, on_select_org(): ' + E);
1383         }
1384     },
1385
1386     'append_org' : function (org,parent_org,params) {
1387         var obj = this;
1388         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
1389         obj.error.consoleService.logStringMessage('append_org: org = ' + org.shortname() + ' parent_org = ' + (parent_org ? parent_org.shortname() : '') + ' params = ' + js2JSON(params) + '\n');
1390         try {
1391             if (obj.map_tree[ 'aou_' + org.id() ]) {
1392                 var x = obj.map_tree[ 'aou_' + org.id() ];
1393                 if (params) {
1394                     for (var i in params) {
1395                         x.setAttribute(i,params[i]);
1396                     }
1397                 }
1398                 return x;
1399             }
1400
1401             var data = {
1402                 'row' : {
1403                     'my' : {
1404                         'aou' : org,
1405                     }
1406                 },
1407                 'skip_all_columns_except' : [0,1,2],
1408                 'retrieve_id' : 'aou_' + org.id(),
1409                 'to_bottom' : true,
1410                 'no_auto_select' : true,
1411             };
1412         
1413             var acn_tree_list;
1414             if ( obj.org_ids.indexOf( Number( org.id() ) ) == -1 ) {
1415                 if ( get_bool( obj.data.hash.aout[ org.ou_type() ].can_have_vols() ) ) {
1416                     data.row.my.volume_count = '0';
1417                     data.row.my.copy_count = '<0>';
1418                 } else {
1419                     data.row.my.volume_count = '';
1420                     data.row.my.copy_count = '';
1421                 }
1422             } else {
1423                 var v_count = 0; var c_count = 0;
1424                 acn_tree_list = obj.network.simple_request(
1425                     'FM_ACN_TREE_LIST_RETRIEVE_VIA_RECORD_ID_AND_ORG_IDS.authoritative',
1426                     [ ses(), obj.docid, [ org.id() ] ]
1427                 );
1428                 for (var i = 0; i < acn_tree_list.length; i++) {
1429                     v_count++;
1430                     obj.map_acn[ 'acn_' + acn_tree_list[i].id() ] = function(r){return r;}(acn_tree_list[i]);
1431                     var copies = acn_tree_list[i].copies(); if (copies) c_count += copies.length;
1432                     for (var j = 0; j < copies.length; j++) {
1433                         obj.map_acp[ 'acp_' + copies[j].id() ] = function(r){return r;}(copies[j]);
1434                     }
1435                 }
1436                 data.row.my.volume_count = v_count;
1437                 data.row.my.copy_count = '<' + c_count + '>';
1438             }
1439             if (parent_org) {
1440                 data.node = obj.map_tree[ 'aou_' + parent_org.id() ];
1441             }
1442             var nparams = obj.list.append(data);
1443             var node = nparams.my_node;
1444             if (params) {
1445                 for (var i in params) {
1446                     node.setAttribute(i,params[i]);
1447                 }
1448             }
1449             obj.map_tree[ 'aou_' + org.id() ] = node;
1450
1451             if (org.children()) {
1452                 node.setAttribute('container','true');
1453             }
1454
1455             if (parent_org) {
1456                 if ( obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ].parent_ou() == parent_org.id() ) {
1457                     data.node.setAttribute('open','true');
1458                 }
1459             } else {
1460                 obj.map_tree[ 'aou_' + org.id() ].setAttribute('open','true');
1461             }
1462
1463             if (acn_tree_list) {
1464                 obj.map_acn[ 'aou_' + org.id() ] = acn_tree_list;
1465                 node.setAttribute('container','true');
1466             }
1467
1468             if (document.getElementById('show_acns').checked) {
1469                 node.setAttribute('open','true');
1470                 obj.funcs.push( function() { obj.on_select_org( org.id() ); } );
1471             }
1472
1473         } catch(E) {
1474             dump(E+'\n');
1475             alert(E);
1476         }
1477     },
1478
1479     'append_acn' : function( org, acn_tree, params ) {
1480         var obj = this;
1481         try {
1482             if (obj.map_tree[ 'acn_' + acn_tree.id() ]) {
1483                 var x = obj.map_tree[ 'acn_' + acn_tree.id() ];
1484                 if (params) {
1485                     for (var i in params) {
1486                         x.setAttribute(i,params[i]);
1487                     }
1488                 }
1489                 return x;
1490             }
1491
1492             var parent_node = obj.map_tree[ 'aou_' + org.id() ];
1493             var data = {
1494                 'row' : {
1495                     'my' : {
1496                         'aou' : org,
1497                         'acn' : acn_tree,
1498                         'volume_count' : '',
1499                         'copy_count' : acn_tree.copies() ? acn_tree.copies().length : '0',
1500                     }
1501                 },
1502                 'skip_all_columns_except' : [0,1,2],
1503                 'retrieve_id' : 'acn_' + acn_tree.id(),
1504                 'node' : parent_node,
1505                 'to_bottom' : true,
1506                 'no_auto_select' : true,
1507             };
1508             var nparams = obj.list.append(data);
1509             var node = nparams.my_node;
1510             obj.map_tree[ 'acn_' + acn_tree.id() ] =  node;
1511             if (params) {
1512                 for (var i in params) {
1513                     node.setAttribute(i,params[i]);
1514                 }
1515             }
1516             if (acn_tree.copies()) {
1517                 obj.map_acp[ 'acn_' + acn_tree.id() ] = acn_tree;
1518                 node.setAttribute('container','true');
1519             }
1520             if (document.getElementById('show_acps').checked) {
1521                 node.setAttribute('open','true');
1522                 obj.funcs.push( function() { obj.on_select_acn( acn_tree.id() ); } );
1523             }
1524
1525         } catch(E) {
1526             dump(E+'\n');
1527             alert(E);
1528         }
1529     },
1530
1531     'append_acp' : function( acp_item, acn_tree, params ) {
1532         var obj = this;
1533         try {
1534             if (obj.map_tree[ 'acp_' + acp_item.id() ]) {
1535                 var x = obj.map_tree[ 'acp_' + acp_item.id() ];
1536                 if (params) {
1537                     for (var i in params) {
1538                         x.setAttribute(i,params[i]);
1539                     }
1540                 }
1541                 return x;
1542             }
1543
1544             var parent_node = obj.map_tree[ 'acn_' + acn_tree.id() ];
1545             var data = {
1546                 'row' : {
1547                     'my' : {
1548                         'doc_id' : obj.docid,
1549                         'aou' : obj.data.hash.aou[ acn_tree.owning_lib() ],
1550                         'acn' : acn_tree,
1551                         'acp' : acp_item,
1552                         'circ' :
1553                             acp_item.circulations() 
1554                             ? (
1555                                 acp_item.circulations().length > 0
1556                                 ? acp_item.circulations()[0]
1557                                 : null
1558                             ) 
1559                             : null,
1560                         'volume_count' : '',
1561                         'copy_count' : '',
1562                     }
1563                 },
1564                 'retrieve_id' : 'acp_' + acp_item.id(),
1565                 'node' : parent_node,
1566                 'to_bottom' : true,
1567                 'no_auto_select' : true,
1568             };
1569             var nparams = obj.list.append(data);
1570             var node = nparams.my_node;
1571             obj.map_tree[ 'acp_' + acp_item.id() ] =  node;
1572             if (params) {
1573                 for (var i in params) {
1574                     node.setAttribute(i,params[i]);
1575                 }
1576             }
1577
1578         } catch(E) {
1579             dump(E+'\n');
1580             alert(E);
1581         }
1582     },
1583
1584     'list_init' : function( params ) {
1585
1586         try {
1587             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
1588             var obj = this;
1589             
1590             JSAN.use('circ.util');
1591             var columns = [
1592                 {
1593                     'id' : 'tree_location',
1594                     'label' : document.getElementById('catStrings').getString('staff.cat.copy_browser.list_init.tree_location'),
1595                     'flex' : 1, 'primary' : true, 'hidden' : false, 
1596                     'render' : function(my) { return my.acp ? my.acp.barcode() : my.acn ? my.acn.label() : my.aou ? my.aou.shortname() + " : " + my.aou.name() : "???"; },
1597                 },
1598                 {
1599                     'id' : 'volume_count',
1600                     'label' : document.getElementById('catStrings').getString('staff.cat.copy_browser.list_init.volume_count'),
1601                     'flex' : 0, 'primary' : false, 'hidden' : false, 
1602                     'render' : function(my) { return my.volume_count; },
1603                 },
1604                 {
1605                     'id' : 'copy_count',
1606                     'label' : document.getElementById('catStrings').getString('staff.cat.copy_browser.list_init.copy_count'),
1607                     'flex' : 0,
1608                     'primary' : false, 'hidden' : false, 
1609                     'render' : function(my) { return my.copy_count; },
1610                 },
1611             ].concat(
1612                 circ.util.columns( 
1613                     { 
1614                         'location' : { 'hidden' : false },
1615                         'circ_lib' : { 'hidden' : false },
1616                         'owning_lib' : { 'hidden' : false },
1617                         'call_number' : { 'hidden' : false },
1618                         'parts' : { 'hidden' : false },
1619                         'due_date' : { 'hidden' : false },
1620                         'acp_status' : { 'hidden' : false },
1621                     },
1622                     {
1623                         'just_these' : [
1624                             'due_date',
1625                             'owning_lib',
1626                             'circ_lib',
1627                             'label_class',
1628                             'prefix',
1629                             'call_number',
1630                             'suffix',
1631                             'copy_number',
1632                             'parts',
1633                             'location',
1634                             'barcode',
1635                             'loan_duration',
1636                             'fine_level',
1637                             'circulate',
1638                             'holdable',
1639                             'opac_visible',
1640                             'ref',
1641                             'deposit',
1642                             'deposit_amount',
1643                             'price',
1644                             'circ_as_type',
1645                             'circ_modifier',
1646                             'acp_status',
1647                             'alert_message',
1648                             'acp_mint_condition',
1649                             'acp_id'
1650                         ]
1651                     }
1652                 )
1653             );
1654             JSAN.use('util.list'); obj.list = new util.list('copy_tree');
1655             obj.list.init(
1656                 {
1657                     'no_auto_select' : true,
1658                     'columns' : columns,
1659                     'map_row_to_columns' : circ.util.std_map_row_to_columns(' '),
1660                     'retrieve_row' : function(params) {
1661
1662                         var row = params.row;
1663
1664                     /*    
1665                         if (!row.my.mvr) obj.funcs.push(
1666                             function() {
1667
1668                                 row.my.mvr = obj.network.request(
1669                                     api.MODS_SLIM_RECORD_RETRIEVE_VIA_COPY.app,
1670                                     api.MODS_SLIM_RECORD_RETRIEVE_VIA_COPY.method,
1671                                     [ row.my.circ.target_copy() ]
1672                                 );
1673
1674                             }
1675                         );
1676                         if (!row.my.acp) {
1677                             obj.funcs.push(    
1678                                 function() {
1679
1680                                     row.my.acp = obj.network.request(
1681                                         api.FM_ACP_RETRIEVE.app,
1682                                         api.FM_ACP_RETRIEVE.method,
1683                                         [ row.my.circ.target_copy() ]
1684                                     );
1685
1686                                     params.row_node.setAttribute( 'retrieve_id',row.my.acp.barcode() );
1687
1688                                 }
1689                             );
1690                         } else {
1691                             params.row_node.setAttribute( 'retrieve_id',row.my.acp.barcode() );
1692                         }
1693                     */
1694                         obj.funcs.push(
1695                             function() {
1696
1697                                 if (typeof params.on_retrieve == 'function') {
1698                                     params.on_retrieve(row);
1699                                 }
1700
1701                             }
1702                         );
1703
1704                         return row;
1705                     },
1706                     'on_click' : function(ev) {
1707                         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserRead');
1708                         var row = {}; var col = {}; var nobj = {};
1709                         obj.list.node.treeBoxObject.getCellAt(ev.clientX,ev.clientY,row,col,nobj); 
1710                         if ((row.value == -1)||(nobj.value != 'twisty')) { return; }
1711                         var node = obj.list.node.contentView.getItemAtIndex(row.value);
1712                         var list = [ node.getAttribute('retrieve_id') ];
1713                         if (typeof obj.on_select == 'function') {
1714                             obj.on_select(list,true);
1715                         }
1716                         if (typeof window.xulG == 'object' && typeof window.xulG.on_select == 'function') {
1717                             window.xulG.on_select(list);
1718                         }
1719                     },
1720                     'on_select' : function(ev) {
1721                         JSAN.use('util.functional');
1722                         var sel = obj.list.retrieve_selection();
1723                         obj.controller.view.sel_clip.disabled = sel.length < 1;
1724                         obj.sel_list = util.functional.map_list(
1725                             sel,
1726                             function(o) { return o.getAttribute('retrieve_id'); }
1727                         );
1728                         obj.toggle_actions();
1729                         if (typeof obj.on_select == 'function') {
1730                             obj.on_select(obj.sel_list);
1731                         }
1732                         if (typeof window.xulG == 'object' && typeof window.xulG.on_select == 'function') {
1733                             window.xulG.on_select(obj.sel_list);
1734                         }
1735                     },
1736                 }
1737             );
1738
1739         } catch(E) {
1740             this.error.sdump('D_ERROR','cat.copy_browser.list_init: ' + E + '\n');
1741             alert(E);
1742         }
1743     },
1744
1745     'toggle_actions' : function() {
1746         var obj = this;
1747         try {
1748             var found_aou = false; var found_acn = false; var found_acp = false;
1749             var found_aou_with_can_have_vols = false;
1750             var sel_copy_libs = {};
1751             for (var i = 0; i < obj.sel_list.length; i++) {
1752                 var type = obj.sel_list[i].split(/_/)[0];
1753                 switch(type) {
1754                     case 'aou' : 
1755                         found_aou = true; 
1756                         var org = obj.data.hash.aou[ obj.sel_list[i].split(/_/)[1] ];
1757                         if ( get_bool( obj.data.hash.aout[ org.ou_type() ].can_have_vols() ) ) found_aou_with_can_have_vols = true;
1758                     break;
1759                     case 'acn' : found_acn = true; break;
1760                     case 'acp' :
1761                         found_acp = true;
1762                         sel_copy_libs[
1763                             obj.map_acn[
1764                                 "acn_" +
1765                                 obj.map_acp[obj.sel_list[i]].call_number()
1766                             ].owning_lib()
1767                         ] = true;
1768                         break;
1769                 }
1770             }
1771             obj.controller.view.cmd_add_items.setAttribute('disabled','true');
1772             obj.controller.view.cmd_add_items_to_buckets.setAttribute('disabled','true');
1773             obj.controller.view.cmd_edit_items.setAttribute('disabled','true');
1774             obj.controller.view.cmd_replace_barcode.setAttribute('disabled','true');
1775             obj.controller.view.cmd_delete_items.setAttribute('disabled','true');
1776             obj.controller.view.cmd_print_spine_labels.setAttribute('disabled','true');
1777             obj.controller.view.cmd_add_volumes.setAttribute('disabled','true');
1778             obj.controller.view.cmd_mark_library.setAttribute('disabled','true');
1779             obj.controller.view.cmd_edit_volumes.setAttribute('disabled','true');
1780             obj.controller.view.cmd_delete_volumes.setAttribute('disabled','true');
1781             obj.controller.view.cmd_mark_volume.setAttribute('disabled','true');
1782             obj.controller.view.cmd_transfer_volume.setAttribute('disabled','true');
1783             obj.controller.view.cmd_transfer_items.setAttribute('disabled','true');
1784             obj.controller.view.sel_copy_details.setAttribute('disabled','true');
1785             obj.controller.view.cmd_create_brt.setAttribute('disabled','true');
1786             obj.controller.view.cmd_book_item_now.setAttribute('disabled','true');
1787             obj.controller.view.sel_patron.setAttribute('disabled','true');
1788             obj.controller.view.sel_mark_items_damaged.setAttribute('disabled','true');
1789             obj.controller.view.sel_mark_items_missing.setAttribute('disabled','true');
1790             if (found_aou && found_aou_with_can_have_vols) {
1791                 obj.controller.view.cmd_add_volumes.setAttribute('disabled','false');
1792                 obj.controller.view.cmd_mark_library.setAttribute('disabled','false');
1793             }
1794             if (found_acn) {
1795                 obj.controller.view.cmd_edit_volumes.setAttribute('disabled','false');
1796                 obj.controller.view.cmd_delete_volumes.setAttribute('disabled','false');
1797                 obj.controller.view.cmd_mark_volume.setAttribute('disabled','false');
1798                 obj.controller.view.cmd_add_items.setAttribute('disabled','false');
1799                 obj.controller.view.cmd_transfer_volume.setAttribute('disabled','false');
1800             }
1801             if (found_acp) {
1802                 obj.controller.view.sel_mark_items_damaged.setAttribute('disabled','false');
1803                 obj.controller.view.sel_mark_items_missing.setAttribute('disabled','false');
1804                 obj.controller.view.cmd_add_items_to_buckets.setAttribute('disabled','false');
1805                 obj.controller.view.cmd_edit_items.setAttribute('disabled','false');
1806                 obj.controller.view.cmd_replace_barcode.setAttribute('disabled','false');
1807                 obj.controller.view.cmd_delete_items.setAttribute('disabled','false');
1808                 obj.controller.view.cmd_print_spine_labels.setAttribute('disabled','false');
1809                 obj.controller.view.cmd_transfer_items.setAttribute('disabled','false');
1810                 obj.controller.view.sel_copy_details.setAttribute('disabled','false');
1811                 obj.controller.view.cmd_create_brt.setAttribute('disabled','false');
1812                 obj.controller.view.sel_patron.setAttribute('disabled','false');
1813
1814                 var L = 0; for (var k in sel_copy_libs) L++;
1815                 if (L < 2) {
1816                     obj.controller.view.cmd_book_item_now.setAttribute('disabled','false');
1817                 }
1818             }
1819         } catch(E) {
1820             obj.error.standard_unexpected_error_alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.actions.error'),E);
1821         }
1822     },
1823
1824     'refresh_list' : function() { 
1825         try {
1826             var obj = this;
1827             obj.list.clear();
1828             obj.map_tree = {};
1829             obj.map_acn = {};
1830             obj.map_acp = {};
1831             obj.org_ids = obj.network.simple_request('FM_AOU_IDS_RETRIEVE_VIA_RECORD_ID.authoritative',[ obj.docid ]);
1832             if (typeof obj.org_ids.ilsevent != 'undefined') throw(obj.org_ids);
1833             JSAN.use('util.functional'); 
1834             obj.org_ids = util.functional.map_list( obj.org_ids, function (o) { return Number(o); });
1835             obj.show_my_libs( obj.default_lib.id() );
1836             // FIXME - we get a null from the copy_count call if we call it too quickly here
1837             setTimeout( function() { obj.show_consortial_count(); }, 2000 );
1838         } catch(E) {
1839             this.error.standard_unexpected_error_alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.refresh_list.error'),E);
1840         }
1841     },
1842 }
1843
1844 dump('exiting cat.copy_browser.js\n');