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