]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/copy_browser.js
quick copy buckets from copy browser
[Evergreen.git] / Open-ILS / xul / staff_client / server / cat / copy_browser.js
1 dump('entering cat.copy_browser.js\n');
2
3 if (typeof cat == 'undefined') cat = {};
4 cat.copy_browser = function (params) {
5         try {
6                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
7                 JSAN.use('util.error'); this.error = new util.error();
8         } catch(E) {
9                 dump('cat.copy_browser: ' + E + '\n');
10         }
11 }
12
13 cat.copy_browser.prototype = {
14
15         'map_tree' : {},
16         'map_acn' : {},
17         'map_acp' : {},
18         'sel_list' : [],
19
20         'init' : function( params ) {
21
22                 try {
23                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
24                         var obj = this;
25
26                         obj.docid = params.docid;
27
28                         JSAN.use('util.network'); obj.network = new util.network();
29                         JSAN.use('OpenILS.data'); obj.data = new OpenILS.data(); obj.data.init({'via':'stash'});
30                         JSAN.use('util.controller'); obj.controller = new util.controller();
31                         obj.controller.init(
32                                 {
33                                         control_map : {
34                                                 'cmd_broken' : [
35                                                         ['command'],
36                                                         function() { alert('Not Yet Implemented'); }
37                                                 ],
38                                                 'cmd_show_my_libs' : [
39                                                         ['command'],
40                                                         function() { 
41                                                                 obj.show_my_libs(); 
42                                                         }
43                                                 ],
44                                                 'cmd_show_all_libs' : [
45                                                         ['command'],
46                                                         function() {
47                                                                 obj.show_all_libs();
48                                                         }
49                                                 ],
50                                                 'cmd_show_libs_with_copies' : [
51                                                         ['command'],
52                                                         function() {
53                                                                 obj.show_libs_with_copies();
54                                                         }
55                                                 ],
56                                                 'cmd_clear' : [
57                                                         ['command'],
58                                                         function() {
59                                                                 obj.map_tree = {};
60                                                                 obj.list.clear();
61                                                         }
62                                                 ],
63                                                 'cmd_add_items' : [
64                                                         ['command'],
65                                                         function() {
66                                                                 try {
67                                                                         JSAN.use('util.functional');
68                                                                         var list = util.functional.map_list(
69                                                                                 util.functional.filter_list(
70                                                                                         obj.sel_list,
71                                                                                         function (o) {
72                                                                                                 return o.split(/_/)[0] == 'acn';
73                                                                                         }
74                                                                                 ),
75                                                                                 function (o) {
76                                                                                         return o.split(/_/)[1];
77                                                                                 }
78                                                                         );
79                                                                         if (list.length == 0) return;
80
81                                                                         var copy_shortcut = {};
82                                                                         list = util.functional.map_list(
83                                                                                 list,
84                                                                                 function (o) {
85                                                                                         var ou_id = obj.map_acn['acn_' + o].owning_lib();
86                                                                                         var volume_id = o;
87                                                                                         var label = obj.map_acn['acn_' + o].label();
88                                                                                         if (!copy_shortcut[ou_id]) copy_shortcut[ou_id] = {};
89                                                                                         copy_shortcut[ou_id][ label ] = volume_id;
90
91                                                                                         return ou_id;
92                                                                                 }
93                                                                         );
94                                                                         /* quick fix */  /* what was this fixing? */
95                                                                         list = []; for (var i in copy_shortcut) { list.push( i ); }
96
97                                                                         var edit = 0;
98                                                                         try {
99                                                                                 edit = obj.network.request(
100                                                                                         api.PERM_MULTI_ORG_CHECK.app,
101                                                                                         api.PERM_MULTI_ORG_CHECK.method,
102                                                                                         [ 
103                                                                                                 ses(), 
104                                                                                                 obj.data.list.au[0].id(), 
105                                                                                                 list,
106                                                                                                 [ 'CREATE_COPY' ]
107                                                                                         ]
108                                                                                 ).length == 0 ? 1 : 0;
109                                                                         } catch(E) {
110                                                                                 obj.error.sdump('D_ERROR','batch permission check: ' + E);
111                                                                         }
112
113                                                                         if (edit==0) return; // no read-only view for this interface
114
115                                                                         var title = 'Add Copy';
116
117                                                                         JSAN.use('util.window'); var win = new util.window();
118                                                                         var w = win.open(
119                                                                                 window.xulG.url_prefix(urls.XUL_VOLUME_COPY_CREATOR)
120                                                                                         +'?doc_id=' + window.escape(obj.docid)
121                                                                                         +'&ou_ids=' + window.escape( js2JSON(list) )
122                                                                                         +'&copy_shortcut=' + window.escape( js2JSON(copy_shortcut) ),
123                                                                                 title,
124                                                                                 'chrome,modal,resizable'
125                                                                         );
126                                                                         obj.refresh_list();
127                                                                 } catch(E) {
128                                                                         obj.error.standard_unexpected_error_alert('copy browser -> add copies',E);
129                                                                 }
130                                                         }
131                                                 ],
132                                                 'cmd_add_items_to_buckets' : [
133                                                         ['command'],
134                                                         function() {
135                                                                 try {
136                                                                         JSAN.use('util.functional');
137
138                                                                         var list = util.functional.filter_list(
139                                                                                 obj.sel_list,
140                                                                                 function (o) {
141                                                                                         return o.split(/_/)[0] == 'acp';
142                                                                                 }
143                                                                         );
144
145                                                                         list = util.functional.map_list(
146                                                                                 list,
147                                                                                 function (o) {
148                                                                                         return o.split(/_/)[1];
149                                                                                 }
150                                                                         );
151                                                                 
152                                                                         JSAN.use('cat.util');
153                                                                         cat.util.add_copies_to_bucket( list );
154
155                                                                 } catch(E) {
156                                                                         obj.error.standard_unexpected_error_alert('copy browser -> add copies to bucket',E);
157                                                                 }
158                                                         }
159                                                 ],
160                                                 'cmd_edit_items' : [
161                                                         ['command'],
162                                                         function() {
163                                                                 try {
164                                                                         JSAN.use('util.functional');
165
166                                                                         var list = util.functional.filter_list(
167                                                                                 obj.sel_list,
168                                                                                 function (o) {
169                                                                                         return o.split(/_/)[0] == 'acp';
170                                                                                 }
171                                                                         );
172
173                                                                         list = util.functional.map_list(
174                                                                                 list,
175                                                                                 function (o) {
176                                                                                         return o.split(/_/)[1];
177                                                                                 }
178                                                                         );
179
180                                                                         var edit = 0;
181                                                                         try {
182                                                                                 edit = obj.network.request(
183                                                                                         api.PERM_MULTI_ORG_CHECK.app,
184                                                                                         api.PERM_MULTI_ORG_CHECK.method,
185                                                                                         [ 
186                                                                                                 ses(), 
187                                                                                                 obj.data.list.au[0].id(), 
188                                                                                                 util.functional.map_list(
189                                                                                                         list,
190                                                                                                         function (o) {
191                                                                                                                 return obj.map_acn[ 'acn_' + obj.map_acp[ 'acp_' + o ].call_number() ].owning_lib();
192                                                                                                         }
193                                                                                                 ),
194                                                                                                 [ 'UPDATE_COPY', 'UPDATE_BATCH_COPY' ]
195                                                                                         ]
196                                                                                 ).length == 0 ? 1 : 0;
197                                                                         } catch(E) {
198                                                                                 obj.error.sdump('D_ERROR','batch permission check: ' + E);
199                                                                         }
200
201                                                                         JSAN.use('cat.util'); cat.util.spawn_copy_editor(list,edit);
202
203                                                                 } catch(E) {
204                                                                         obj.error.standard_unexpected_error_alert('Copy Browser -> Edit Items',E);
205                                                                 }
206                                                         }
207                                                 ],
208                                                 'cmd_delete_items' : [
209                                                         ['command'],
210                                                         function() {
211                                                                 try {
212                                                                         JSAN.use('util.functional');
213
214                                                                         var list = util.functional.filter_list(
215                                                                                 obj.sel_list,
216                                                                                 function (o) {
217                                                                                         return o.split(/_/)[0] == 'acp';
218                                                                                 }
219                                                                         );
220
221                                                                         list = util.functional.map_list(
222                                                                                 list,
223                                                                                 function (o) {
224                                                                                         return JSON2js( js2JSON( obj.map_acp[ 'acp_' + o.split(/_/)[1] ] ) );
225                                                                                 }
226                                                                         );
227
228                                                                         var r = obj.error.yns_alert('Are you sure you would like to delete ' + (list.length != 1 ? 'these ' + list.length + ' items' : 'this one item') + '?', 'Delete Items?', 'Delete', 'Cancel', null, 'Check here to confirm this action');
229
230                                                                         if (r == 0) {
231                                                                                 var acn_hash = {}; var acn_list = [];
232                                                                                 for (var i = 0; i < list.length; i++) {
233                                                                                         list[i].isdeleted('1');
234                                                                                         var acn_id = list[i].call_number();
235                                                                                         if ( ! acn_hash[ acn_id ] ) {
236                                                                                                 acn_hash[ acn_id ] = obj.map_acn[ 'acn_' + acn_id ];
237                                                                                                 acn_hash[ acn_id ].copies( [] );
238                                                                                         }
239                                                                                         var temp = acn_hash[ acn_id ].copies();
240                                                                                         temp.push( list[i] );
241                                                                                         acn_hash[ acn_id ].copies( temp );
242                                                                                 }
243                                                                                 for (var i in acn_hash) acn_list.push( acn_hash[i] );
244                                                                                 var robj = obj.network.simple_request(
245                                                                                         'FM_ACN_TREE_UPDATE', 
246                                                                                         [ ses(), acn_list ],
247                                                                                         null,
248                                                                                         {
249                                                                                                 'title' : 'Override Delete Failure?',
250                                                                                                 'overridable_events' : [
251                                                                                                         1208 /* TITLE_LAST_COPY */
252                                                                                                 ]
253                                                                                         }
254                                                                                 );
255                                                                                 if (robj == null) throw(robj);
256                                                                                 if (typeof robj.ilsevent != 'undefined') {
257                                                                                         if ( (robj.ilsevent != 0) && (robj.ilsevent != 1208 /* TITLE_LAST_COPY */) ) throw(robj);
258                                                                                 }
259                                                                                 obj.refresh_list();
260                                                                         }
261
262                                                                         
263                                                                 } catch(E) {
264                                                                         obj.error.standard_unexpected_error_alert('copy browser -> delete items',E);
265                                                                         obj.refresh_list();
266                                                                 }
267                                                         }
268                                                 ],
269                                                 'cmd_print_spine_labels' : [
270                                                         ['command'],
271                                                         function() {
272                                                                 try {
273                                                                         JSAN.use('util.functional');
274                                                                         
275                                                                         var list = util.functional.filter_list(
276                                                                                 obj.sel_list,
277                                                                                 function (o) {
278                                                                                         return o.split(/_/)[0] == 'acp';
279                                                                                 }
280                                                                         );
281
282                                                                         list = util.functional.map_list(
283                                                                                 list,
284                                                                                 function (o) {
285                                                                                         return obj.map_acp[ o ];
286                                                                                 }
287                                                                         );
288
289                                                                         xulG.new_tab(
290                                                                                 xulG.url_prefix( urls.XUL_SPINE_LABEL ) + '?barcodes=' 
291                                                                                 + js2JSON( 
292                                                                                         util.functional.map_list(
293                                                                                                 list,
294                                                                                                 function(o){return o.barcode();}
295                                                                                         ) 
296                                                                                 ),
297                                                                                 { 'tab_name' : 'Spine Labels' },
298                                                                                 {}
299                                                                         );
300                                                                 } catch(E) {
301                                                                         obj.error.standard_unexpected_error_alert('copy browser -> Spine Labels',E);
302                                                                 }
303                                                         }
304                                                 ],
305                                                 'cmd_add_volumes' : [
306                                                         ['command'],
307                                                         function() {
308                                                                 try {
309                                                                         JSAN.use('util.functional');
310                                                                         var list = util.functional.filter_list(
311                                                                                 obj.sel_list,
312                                                                                 function (o) {
313                                                                                         return o.split(/_/)[0] == 'aou';
314                                                                                 }
315                                                                         );
316                                                                         list = util.functional.map_list(
317                                                                                 list,
318                                                                                 function (o) {
319                                                                                         return o.split(/_/)[1];
320                                                                                 }
321                                                                         );              
322                                                                         var edit = 0;
323                                                                         try {
324                                                                                 edit = obj.network.request(
325                                                                                         api.PERM_MULTI_ORG_CHECK.app,
326                                                                                         api.PERM_MULTI_ORG_CHECK.method,
327                                                                                         [ 
328                                                                                                 ses(), 
329                                                                                                 obj.data.list.au[0].id(), 
330                                                                                                 list,
331                                                                                                 [ 'CREATE_VOLUME', 'CREATE_COPY' ]
332                                                                                         ]
333                                                                                 ).length == 0 ? 1 : 0;
334                                                                         } catch(E) {
335                                                                                 obj.error.sdump('D_ERROR','batch permission check: ' + E);
336                                                                         }
337
338                                                                         if (edit==0) {
339                                                                                 alert("You don't have permission to add volumes to that library.");
340                                                                                 return; // no read-only view for this interface
341                                                                         }
342
343                                                                         var title = 'Add Volume/Copy';
344
345                                                                         JSAN.use('util.window'); var win = new util.window();
346                                                                         var w = win.open(
347                                                                                 window.xulG.url_prefix(urls.XUL_VOLUME_COPY_CREATOR)
348                                                                                         +'?doc_id=' + window.escape(obj.docid)
349                                                                                         +'&ou_ids=' + window.escape( js2JSON(list) ),
350                                                                                 title,
351                                                                                 'chrome,modal,resizable'
352                                                                         );
353
354                                                                         obj.refresh_list();
355                                                                 } catch(E) {
356                                                                         obj.error.standard_unexpected_error_alert('copy browser -> add volumes',E);
357                                                                 }
358                                                         }
359                                                 ],
360                                                 'cmd_edit_volumes' : [
361                                                         ['command'],
362                                                         function() {
363                                                                 try {
364                                                                         JSAN.use('util.functional');
365                                                                         var list = util.functional.map_list(
366                                                                                 util.functional.filter_list(
367                                                                                         obj.sel_list,
368                                                                                         function (o) {
369                                                                                                 return o.split(/_/)[0] == 'acn';
370                                                                                         }
371                                                                                 ),
372                                                                                 function (o) {
373                                                                                         return o.split(/_/)[1];
374                                                                                 }
375                                                                         );
376                                                                         if (list.length == 0) return;
377
378                                                                         var edit = 0;
379                                                                         try {
380                                                                                 edit = obj.network.request(
381                                                                                         api.PERM_MULTI_ORG_CHECK.app,
382                                                                                         api.PERM_MULTI_ORG_CHECK.method,
383                                                                                         [ 
384                                                                                                 ses(), 
385                                                                                                 obj.data.list.au[0].id(), 
386                                                                                                 util.functional.map_list(
387                                                                                                         list,
388                                                                                                         function (o) {
389                                                                                                                 return obj.map_acn[ 'acn_' + o ].owning_lib();
390                                                                                                         }
391                                                                                                 ),
392                                                                                                 [ 'UPDATE_VOLUME' ]
393                                                                                         ]
394                                                                                 ).length == 0 ? 1 : 0;
395                                                                         } catch(E) {
396                                                                                 obj.error.sdump('D_ERROR','batch permission check: ' + E);
397                                                                         }
398
399                                                                         if (edit==0) {
400                                                                                 alert("You don't have permission to edit this volume.");
401                                                                                 return; // no read-only view for this interface
402                                                                         }
403
404                                                                         list = util.functional.map_list(
405                                                                                 list,
406                                                                                 function (o) {
407                                                                                         var my_acn = obj.map_acn['acn_' + o];
408                                                                                         return function(r){return r;}(my_acn);
409                                                                                 }
410                                                                         );
411
412                                                                         var title = list.length == 1 ? 'Volume' : 'Volumes';
413
414                                                                         JSAN.use('util.window'); var win = new util.window();
415                                                                         obj.data.volumes_temp = js2JSON( list );
416                                                                         obj.data.stash('volumes_temp');
417                                                                         var w = win.open(
418                                                                                 window.xulG.url_prefix(urls.XUL_VOLUME_EDITOR),
419                                                                                 title,
420                                                                                 'chrome,modal,resizable'
421                                                                         );
422
423                                                                         /* FIXME -- need to unique the temp space, and not rely on modalness of window */
424                                                                         obj.data.stash_retrieve();
425                                                                         var volumes = JSON2js( obj.data.volumes_temp );
426                                                                         obj.error.sdump('D_CAT','in browse, obj.data.temp =\n' + obj.data.temp);
427                                                                         if (volumes=='') return;
428                                                                 
429                                                                         volumes = util.functional.filter_list(
430                                                                                 volumes,
431                                                                                 function (o) {
432                                                                                         return o.ischanged() == '1';
433                                                                                 }
434                                                                         );
435
436                                                                         volumes = util.functional.map_list(
437                                                                                 volumes,
438                                                                                 function (o) {
439                                                                                         o.record( obj.docid ); // staff client 2 did not do this.  Does it matter?
440                                                                                         return o;
441                                                                                 }
442                                                                         );
443
444                                                                         if (volumes.length == 0) return;
445
446                                                                         try {
447                                                                                 var r = obj.network.request(
448                                                                                         api.FM_ACN_TREE_UPDATE.app,
449                                                                                         api.FM_ACN_TREE_UPDATE.method,
450                                                                                         [ ses(), volumes ]
451                                                                                 );
452                                                                                 if (typeof r.ilsevent != 'undefined') throw(r);
453                                                                         } catch(E) {
454                                                                                 obj.error.standard_unexpected_error_alert('volume update error: ',E);
455                                                                         }
456                                                                         obj.refresh_list();
457
458                                                                 } catch(E) {
459                                                                         obj.error.standard_unexpected_error_alert('Copy Browser -> Volume Edit',E);
460                                                                 }
461                                                         }
462                                                 ],
463                                                 'cmd_delete_volumes' : [
464                                                         ['command'],
465                                                         function() {
466                                                                 try {
467                                                                         JSAN.use('util.functional');
468
469                                                                         var list = util.functional.filter_list(
470                                                                                 obj.sel_list,
471                                                                                 function (o) {
472                                                                                         return o.split(/_/)[0] == 'acn';
473                                                                                 }
474                                                                         );
475
476                                                                         list = util.functional.map_list(
477                                                                                 list,
478                                                                                 function (o) {
479                                                                                         return JSON2js( js2JSON( obj.map_acn[ 'acn_' + o.split(/_/)[1] ] ) );
480                                                                                 }
481                                                                         );
482
483                                                                         var r = obj.error.yns_alert('Are you sure you would like to delete ' + (list.length != 1 ? 'these ' + list.length + ' volumes' : 'this one volume') + '?', 'Delete Volumes?', 'Delete', 'Cancel', null, 'Check here to confirm this action');
484
485                                                                         if (r == 0) {
486                                                                                 for (var i = 0; i < list.length; i++) {
487                                                                                         list[i].isdeleted('1');
488                                                                                 }
489                                                                                 var robj = obj.network.simple_request(
490                                                                                         'FM_ACN_TREE_UPDATE', 
491                                                                                         [ ses(), list ],
492                                                                                         null,
493                                                                                         {
494                                                                                                 'title' : 'Override Delete Failure?',
495                                                                                                 'overridable_events' : [
496                                                                                                 ]
497                                                                                         }
498                                                                                 );
499                                                                                 if (robj == null) throw(robj);
500                                                                                 if (typeof robj.ilsevent != 'undefined') {
501                                                                                         if (robj.ilsevent == 1206 /* VOLUME_NOT_EMPTY */) {
502                                                                                                 alert('You must delete all the copies on the volume before you may delete the volume itself.');
503                                                                                                 return;
504                                                                                         }
505                                                                                         if (robj.ilsevent != 0) throw(robj);
506                                                                                 }
507                                                                                 obj.refresh_list();
508                                                                         }
509                                                                 } catch(E) {
510                                                                         obj.error.standard_unexpected_error_alert('copy browser -> delete volumes',E);
511                                                                         obj.refresh_list();
512                                                                 }
513
514                                                         }
515                                                 ],
516                                                 'cmd_mark_volume' : [
517                                                         ['command'],
518                                                         function() {
519                                                                 try {
520                                                                         var list = util.functional.filter_list(
521                                                                                 obj.sel_list,
522                                                                                 function (o) {
523                                                                                         return o.split(/_/)[0] == 'acn';
524                                                                                 }
525                                                                         );
526
527                                                                         list = util.functional.map_list(
528                                                                                 list,
529                                                                                 function (o) {
530                                                                                         return o.split(/_/)[1];
531                                                                                 }
532                                                                         );
533
534                                                                         if (list.length == 1) {
535                                                                                 obj.data.marked_volume = list[0];
536                                                                                 obj.data.stash('marked_volume');
537                                                                                 alert('Volume marked as Copy Transfer Destination');
538                                                                         } else {
539                                                                                 obj.error.yns_alert('Choose just one Volume to mark as Copy Transfer Destination','Limit Selection','OK',null,null,'Check here to confirm this dialog');
540                                                                         }
541                                                                 } catch(E) {
542                                                                         obj.error.standard_unexpected_error_alert('copy browser -> mark volume',E);
543                                                                 }
544                                                         }
545                                                 ],
546                                                 'cmd_refresh_list' : [
547                                                         ['command'],
548                                                         function() {
549                                                                 obj.refresh_list();
550                                                         }
551                                                 ],
552                                         }
553                                 }
554                         );
555
556                         obj.list_init(params);
557
558                         obj.org_ids = obj.network.simple_request('FM_AOU_IDS_RETRIEVE_VIA_RECORD_ID',[ obj.docid ]);
559
560                         var org = obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ];
561                         obj.show_libs( org );
562
563                         obj.show_my_libs();
564
565                 } catch(E) {
566                         this.error.sdump('D_ERROR','cat.copy_browser.init: ' + E + '\n');
567                 }
568         },
569
570         'show_my_libs' : function() {
571                 var obj = this;
572                 try {
573                         var org = obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ];
574                         obj.show_libs( org, true );
575                 
576                         var p_org = obj.data.hash.aou[ org.parent_ou() ];
577                         if (p_org) {
578                                 JSAN.use('util.exec'); var exec = new util.exec();
579                                 var funcs = [];
580                                 for (var i = 0; i < p_org.children().length; i++) {
581                                         funcs.push(
582                                                 function(o) {
583                                                         return function() {
584                                                                 obj.show_libs( o, true );
585                                                         }
586                                                 }( p_org.children()[i] )
587                                         );
588                                 }
589                                 exec.chain( funcs );
590                         }
591                 } catch(E) {
592                         alert(E);
593                 }
594         },
595
596         'show_all_libs' : function() {
597                 var obj = this;
598                 try {
599                         obj.show_my_libs();
600
601                         obj.show_libs( obj.data.tree.aou );
602
603                         JSAN.use('util.exec'); var exec = new util.exec();
604                         var funcs = [];
605                         for (var i = 0; i < obj.data.tree.aou.children().length; i++) {
606                                 funcs.push(
607                                         function(o) {
608                                                 return function() {
609                                                         obj.show_libs( o );
610                                                 }
611                                         }( obj.data.tree.aou.children()[i] )
612                                 );
613                         }
614                         exec.chain( funcs );
615                 } catch(E) {
616                         alert(E);
617                 }
618         },
619
620         'show_libs_with_copies' : function() {
621                 var obj = this;
622                 try {
623                         JSAN.use('util.exec'); var exec = new util.exec();
624                         JSAN.use('util.functional');
625
626                         var orgs = util.functional.map_list(
627                                 obj.org_ids,
628                                 function(id) { return obj.data.hash.aou[id]; }
629                         ).sort(
630                                 function( a, b ) {
631                                         if (a.shortname() < b.shortname()) return -1;
632                                         if (a.shortname() > b.shortname()) return 1;
633                                         return 0;
634                                 }
635                         );
636                         var funcs = [];
637                         for (var i = 0; i < orgs.length; i++) {
638                                 funcs.push(
639                                         function(o) {
640                                                 return function() {
641                                                         obj.show_libs(o,true);
642                                                 }
643                                         }( orgs[i] )
644                                 );
645                         }
646                         exec.chain( funcs );
647                 } catch(E) {
648                         alert(E);
649                 }
650         },
651
652         'show_libs' : function(start_aou,show_open) {
653                 var obj = this;
654                 try {
655                         if (!start_aou) throw('show_libs: Need a start_aou');
656                         JSAN.use('OpenILS.data'); obj.data = new OpenILS.data(); obj.data.init({'via':'stash'});
657                         JSAN.use('util.functional'); JSAN.use('util.exec'); var exec = new util.exec();
658
659                         var funcs = [];
660
661                         var parents = [];
662                         var temp_aou = start_aou;
663                         while ( temp_aou.parent_ou() ) {
664                                 temp_aou = obj.data.hash.aou[ temp_aou.parent_ou() ];
665                                 parents.push( temp_aou );
666                         }
667                         parents.reverse();
668
669                         for (var i = 0; i < parents.length; i++) {
670                                 funcs.push(
671                                         function(o,p) {
672                                                 return function() { 
673                                                         if (show_open) {
674                                                                 obj.append_org(o,p,{'container':'true','open':'true'}); 
675                                                         } else {
676                                                                 obj.append_org(o,p,{'container':'true'}); 
677                                                         }
678                                                 };
679                                         }(parents[i], obj.data.hash.aou[ parents[i].parent_ou() ])
680                                 );
681                         }
682
683                         funcs.push(
684                                 function(o,p) {
685                                         return function() { obj.append_org(o,p); };
686                                 }(start_aou,obj.data.hash.aou[ start_aou.parent_ou() ])
687                         );
688
689                         funcs.push(
690                                 function() {
691                                         if (start_aou.children()) {
692                                                 var x = obj.map_tree[ 'aou_' + start_aou.id() ];
693                                                 x.setAttribute('container','true');
694                                                 if (show_open) x.setAttribute('open','true');
695                                                 for (var i = 0; i < start_aou.children().length; i++) {
696                                                         funcs.push(
697                                                                 function(o,p) {
698                                                                         return function() { obj.append_org(o,p); };
699                                                                 }( start_aou.children()[i], start_aou )
700                                                         );
701                                                 }
702                                         }
703                                 }
704                         );
705
706                         exec.chain( funcs );
707
708                 } catch(E) {
709                         alert(E);
710                 }
711         },
712
713         'on_select' : function(list,twisty) {
714                 var obj = this;
715                 for (var i = 0; i < list.length; i++) {
716                         var node = obj.map_tree[ list[i] ];
717                         //if (node.lastChild.nodeName == 'treechildren') { continue; } else { alert(node.lastChild.nodeName); }
718                         var row_type = list[i].split('_')[0];
719                         var id = list[i].split('_')[1];
720                         switch(row_type) {
721                                 case 'aou' : obj.on_select_org(id,twisty); break;
722                                 case 'acn' : obj.on_select_acn(id,twisty); break;
723                                 default: break;
724                         }
725                 }
726         },
727
728         'on_select_acn' : function(acn_id,twisty) {
729                 var obj = this;
730                 try {
731                         var acn_tree = obj.map_acp[ 'acn_' + acn_id ];
732                         var funcs = [];
733                         if (acn_tree.copies()) {
734                                 for (var i = 0; i < acn_tree.copies().length; i++) {
735                                         funcs.push(
736                                                 function(c,a) {
737                                                         return function() {
738                                                                 obj.append_acp(c,a);
739                                                         }
740                                                 }( acn_tree.copies()[i], acn_tree )
741                                         )
742                                 }
743                         }
744                         JSAN.use('util.exec'); var exec = new util.exec();
745                         exec.chain( funcs );
746                 } catch(E) {
747                         alert(E);
748                 }
749         },
750
751         'on_select_org' : function(org_id,twisty) {
752                 var obj = this;
753                 var org = obj.data.hash.aou[ org_id ];
754                 var funcs = [];
755                 if (org.children()) {
756                         for (var i = 0; i < org.children().length; i++) {
757                                 funcs.push(
758                                         function(o,p) {
759                                                 return function() {
760                                                         obj.append_org(o,p)
761                                                 }
762                                         }(org.children()[i],org)
763                                 );
764                         }
765                 } 
766                 if (obj.map_acn[ 'aou_' + org_id ]) {
767                         for (var i = 0; i < obj.map_acn[ 'aou_' + org_id ].length; i++) {
768                                 funcs.push(
769                                         function(o,a) {
770                                                 return function() {
771                                                         obj.append_acn(o,a);
772                                                 }
773                                         }( org, obj.map_acn[ 'aou_' + org_id ][i] )
774                                 );
775                         }
776                 }
777                 JSAN.use('util.exec'); var exec = new util.exec();
778                 exec.chain( funcs );
779         },
780
781         'append_org' : function (org,parent_org,params) {
782                 var obj = this;
783                 try {
784                         if (obj.map_tree[ 'aou_' + org.id() ]) {
785                                 var x = obj.map_tree[ 'aou_' + org.id() ];
786                                 if (params) {
787                                         for (var i in params) {
788                                                 x.setAttribute(i,params[i]);
789                                         }
790                                 }
791                                 return x;
792                         }
793
794                         var data = {
795                                 'row' : {
796                                         'my' : {
797                                                 'aou' : org,
798                                         }
799                                 },
800                                 'skip_all_columns_except' : [0,1,2],
801                                 'retrieve_id' : 'aou_' + org.id(),
802                         };
803                 
804                         var acn_tree_list;
805                         if ( obj.org_ids.indexOf( org.id() ) == -1 ) {
806                                 if ( obj.data.hash.aout[ org.ou_type() ].can_have_vols() ) {
807                                         data.row.my.volume_count = '0';
808                                         data.row.my.copy_count = '<0>';
809                                 } else {
810                                         data.row.my.volume_count = '';
811                                         data.row.my.copy_count = '';
812                                 }
813                         } else {
814                                 var v_count = 0; var c_count = 0;
815                                 acn_tree_list = obj.network.simple_request(
816                                         'FM_ACN_TREE_LIST_RETRIEVE_VIA_RECORD_ID_AND_ORG_IDS',
817                                         [ ses(), obj.docid, [ org.id() ] ]
818                                 );
819                                 for (var i = 0; i < acn_tree_list.length; i++) {
820                                         v_count++;
821                                         obj.map_acn[ 'acn_' + acn_tree_list[i].id() ] = function(r){return r;}(acn_tree_list[i]);
822                                         var copies = acn_tree_list[i].copies(); if (copies) c_count += copies.length;
823                                         for (var j = 0; j < copies.length; j++) {
824                                                 obj.map_acp[ 'acp_' + copies[j].id() ] = function(r){return r;}(copies[j]);
825                                         }
826                                 }
827                                 data.row.my.volume_count = v_count;
828                                 data.row.my.copy_count = '<' + c_count + '>';
829                         }
830                         if (parent_org) {
831                                 data.node = obj.map_tree[ 'aou_' + parent_org.id() ];
832                         }
833
834                         var node = obj.list.append(data);
835                         if (params) {
836                                 for (var i in params) {
837                                         node.setAttribute(i,params[i]);
838                                 }
839                         }
840                         obj.map_tree[ 'aou_' + org.id() ] = node;
841
842                         if (org.children()) {
843                                 node.setAttribute('container','true');
844                         }
845
846                         if (parent_org) {
847                                 if ( obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ].parent_ou() == parent_org.id() ) {
848                                         data.node.setAttribute('open','true');
849                                 }
850                         } else {
851                                 obj.map_tree[ 'aou_' + org.id() ].setAttribute('open','true');
852                         }
853
854                         if (acn_tree_list) {
855                                 obj.map_acn[ 'aou_' + org.id() ] = acn_tree_list;
856                                 node.setAttribute('container','true');
857                         }
858
859                 } catch(E) {
860                         dump(E+'\n');
861                         alert(E);
862                 }
863         },
864
865         'append_acn' : function( org, acn_tree, params ) {
866                 var obj = this;
867                 try {
868                         if (obj.map_tree[ 'acn_' + acn_tree.id() ]) {
869                                 var x = obj.map_tree[ 'acn_' + acn_tree.id() ];
870                                 if (params) {
871                                         for (var i in params) {
872                                                 x.setAttribute(i,params[i]);
873                                         }
874                                 }
875                                 return x;
876                         }
877
878                         var parent_node = obj.map_tree[ 'aou_' + org.id() ];
879                         var data = {
880                                 'row' : {
881                                         'my' : {
882                                                 'aou' : org,
883                                                 'acn' : acn_tree,
884                                                 'volume_count' : '',
885                                                 'copy_count' : acn_tree.copies() ? acn_tree.copies().length : '0',
886                                         }
887                                 },
888                                 'skip_all_columns_except' : [0,1,2],
889                                 'retrieve_id' : 'acn_' + acn_tree.id(),
890                                 'node' : parent_node,
891                         };
892                         var node = obj.list.append(data);
893                         obj.map_tree[ 'acn_' + acn_tree.id() ] =  node;
894                         if (params) {
895                                 for (var i in params) {
896                                         node.setAttribute(i,params[i]);
897                                 }
898                         }
899                         if (acn_tree.copies()) {
900                                 obj.map_acp[ 'acn_' + acn_tree.id() ] = acn_tree;
901                                 node.setAttribute('container','true');
902                         }
903
904                 } catch(E) {
905                         dump(E+'\n');
906                         alert(E);
907                 }
908         },
909
910         'append_acp' : function( acp_item, acn_tree, params ) {
911                 var obj = this;
912                 try {
913                         if (obj.map_tree[ 'acp_' + acp_item.id() ]) {
914                                 var x = obj.map_tree[ 'acp_' + acp_item.id() ];
915                                 if (params) {
916                                         for (var i in params) {
917                                                 x.setAttribute(i,params[i]);
918                                         }
919                                 }
920                                 return x;
921                         }
922
923                         var parent_node = obj.map_tree[ 'acn_' + acn_tree.id() ];
924                         var data = {
925                                 'row' : {
926                                         'my' : {
927                                                 'aou' : obj.data.hash.aou[ acn_tree.owning_lib() ],
928                                                 'acn' : acn_tree,
929                                                 'acp' : acp_item,
930                                                 'volume_count' : '',
931                                                 'copy_count' : '',
932                                         }
933                                 },
934                                 'retrieve_id' : 'acp_' + acp_item.id(),
935                                 'node' : parent_node,
936                         };
937                         var node = obj.list.append(data);
938                         obj.map_tree[ 'acp_' + acp_item.id() ] =  node;
939                         if (params) {
940                                 for (var i in params) {
941                                         node.setAttribute(i,params[i]);
942                                 }
943                         }
944
945                 } catch(E) {
946                         dump(E+'\n');
947                         alert(E);
948                 }
949         },
950
951         'list_init' : function( params ) {
952
953                 try {
954                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
955                         var obj = this;
956                         
957                         JSAN.use('circ.util');
958                         var columns = [
959                                 {
960                                         'id' : 'tree_location', 'label' : 'Location/Barcode', 'flex' : 1,
961                                         'primary' : true, 'hidden' : false, 
962                                         'render' : 'my.acp ? my.acp.barcode() : my.acn ? my.acn.label() : my.aou ? my.aou.shortname() + " : " + my.aou.name() : "???"'
963                                 },
964                                 {
965                                         'id' : 'volume_count', 'label' : 'Volumes', 'flex' : 0,
966                                         'primary' : false, 'hidden' : false, 
967                                         'render' : 'my.volume_count'
968                                 },
969                                 {
970                                         'id' : 'copy_count', 'label' : 'Copies', 'flex' : 0,
971                                         'primary' : false, 'hidden' : false, 
972                                         'render' : 'my.copy_count'
973                                 },
974                         ].concat(
975                                 circ.util.columns( 
976                                         { 
977                                                 'location' : { 'hidden' : false },
978                                                 'circ_lib' : { 'hidden' : false },
979                                                 'owning_lib' : { 'hidden' : false },
980                                                 'call_number' : { 'hidden' : false },
981                                                 'status' : { 'hidden' : false },
982                                         },
983                                         {
984                                                 'just_these' : [
985                                                         'owning_lib',
986                                                         'circ_lib',
987                                                         'call_number',
988                                                         'copy_number',
989                                                         'location',
990                                                         'barcode',
991                                                         'loan_duration',
992                                                         'fine_level',
993                                                         'circulate',
994                                                         'holdable',
995                                                         'opac_visible',
996                                                         'ref',
997                                                         'deposit',
998                                                         'deposit_amount',
999                                                         'price',
1000                                                         'circ_as_type',
1001                                                         'circ_modifier',
1002                                                         'status',
1003                                                         'alert_message',
1004                                                         'acp_id',
1005                                                 ]
1006                                         }
1007                                 )
1008                         );
1009                         JSAN.use('util.list'); obj.list = new util.list('copy_tree');
1010                         obj.list.init(
1011                                 {
1012                                         'columns' : columns,
1013                                         'map_row_to_column' : circ.util.std_map_row_to_column(' '),
1014                                         'retrieve_row' : function(params) {
1015
1016                                                 var row = params.row;
1017
1018                                                 var funcs = [];
1019                                         /*      
1020                                                 if (!row.my.mvr) funcs.push(
1021                                                         function() {
1022
1023                                                                 row.my.mvr = obj.network.request(
1024                                                                         api.MODS_SLIM_RECORD_RETRIEVE_VIA_COPY.app,
1025                                                                         api.MODS_SLIM_RECORD_RETRIEVE_VIA_COPY.method,
1026                                                                         [ row.my.circ.target_copy() ]
1027                                                                 );
1028
1029                                                         }
1030                                                 );
1031                                                 if (!row.my.acp) {
1032                                                         funcs.push(     
1033                                                                 function() {
1034
1035                                                                         row.my.acp = obj.network.request(
1036                                                                                 api.FM_ACP_RETRIEVE.app,
1037                                                                                 api.FM_ACP_RETRIEVE.method,
1038                                                                                 [ row.my.circ.target_copy() ]
1039                                                                         );
1040
1041                                                                         params.row_node.setAttribute( 'retrieve_id',row.my.acp.barcode() );
1042
1043                                                                 }
1044                                                         );
1045                                                 } else {
1046                                                         params.row_node.setAttribute( 'retrieve_id',row.my.acp.barcode() );
1047                                                 }
1048                                         */
1049                                                 funcs.push(
1050                                                         function() {
1051
1052                                                                 if (typeof params.on_retrieve == 'function') {
1053                                                                         params.on_retrieve(row);
1054                                                                 }
1055
1056                                                         }
1057                                                 );
1058
1059                                                 JSAN.use('util.exec'); var exec = new util.exec();
1060                                                 exec.on_error = function(E) {
1061                                                         var err = 'items chain: ' + js2JSON(E);
1062                                                         obj.error.sdump('D_ERROR',err);
1063                                                         return true; /* keep going */
1064                                                 }
1065                                                 exec.chain( funcs );
1066
1067                                                 return row;
1068                                         },
1069                                         'on_click' : function(ev) {
1070                                                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserRead');
1071                                                 var row = {}; var col = {}; var nobj = {};
1072                                                 obj.list.node.treeBoxObject.getCellAt(ev.clientX,ev.clientY,row,col,nobj);
1073                                                 if ((row.value == -1)||(nobj.value != 'twisty')) { return; }
1074                                                 var node = obj.list.node.contentView.getItemAtIndex(row.value);
1075                                                 var list = [ node.getAttribute('retrieve_id') ];
1076                                                 if (typeof obj.on_select == 'function') {
1077                                                         obj.on_select(list,true);
1078                                                 }
1079                                                 if (typeof window.xulG == 'object' && typeof window.xulG.on_select == 'function') {
1080                                                         window.xulG.on_select(list);
1081                                                 }
1082                                         },
1083                                         'on_select' : function(ev) {
1084                                                 JSAN.use('util.functional');
1085                                                 var sel = obj.list.retrieve_selection();
1086                                                 obj.sel_list = util.functional.map_list(
1087                                                         sel,
1088                                                         function(o) { return o.getAttribute('retrieve_id'); }
1089                                                 );
1090                                                 obj.toggle_actions();
1091                                                 if (typeof obj.on_select == 'function') {
1092                                                         obj.on_select(obj.sel_list);
1093                                                 }
1094                                                 if (typeof window.xulG == 'object' && typeof window.xulG.on_select == 'function') {
1095                                                         window.xulG.on_select(obj.sel_list);
1096                                                 }
1097                                         },
1098                                 }
1099                         );
1100
1101                         obj.controller.render();
1102
1103                 } catch(E) {
1104                         this.error.sdump('D_ERROR','cat.copy_browser.list_init: ' + E + '\n');
1105                         alert(E);
1106                 }
1107         },
1108
1109         'toggle_actions' : function() {
1110                 var obj = this;
1111                 try {
1112                         var found_aou = false; var found_acn = false; var found_acp = false;
1113                         for (var i = 0; i < obj.sel_list.length; i++) {
1114                                 var type = obj.sel_list[i].split(/_/)[0];
1115                                 switch(type) {
1116                                         case 'aou' : found_aou = true; break;
1117                                         case 'acn' : found_acn = true; break;
1118                                         case 'acp' : found_acp = true; break;
1119                                 }
1120                         }
1121                         obj.controller.view.cmd_add_items.setAttribute('disabled','true');
1122                         obj.controller.view.cmd_add_items_to_buckets.setAttribute('disabled','true');
1123                         obj.controller.view.cmd_edit_items.setAttribute('disabled','true');
1124                         obj.controller.view.cmd_delete_items.setAttribute('disabled','true');
1125                         obj.controller.view.cmd_print_spine_labels.setAttribute('disabled','true');
1126                         obj.controller.view.cmd_add_volumes.setAttribute('disabled','true');
1127                         obj.controller.view.cmd_edit_volumes.setAttribute('disabled','true');
1128                         obj.controller.view.cmd_delete_volumes.setAttribute('disabled','true');
1129                         obj.controller.view.cmd_mark_volume.setAttribute('disabled','true');
1130                         if (found_aou) {
1131                                 obj.controller.view.cmd_add_volumes.setAttribute('disabled','false');
1132                         }
1133                         if (found_acn) {
1134                                 obj.controller.view.cmd_edit_volumes.setAttribute('disabled','false');
1135                                 obj.controller.view.cmd_delete_volumes.setAttribute('disabled','false');
1136                                 obj.controller.view.cmd_mark_volume.setAttribute('disabled','false');
1137                                 obj.controller.view.cmd_add_items.setAttribute('disabled','false');
1138                         }
1139                         if (found_acp) {
1140                                 obj.controller.view.cmd_add_items_to_buckets.setAttribute('disabled','false');
1141                                 obj.controller.view.cmd_edit_items.setAttribute('disabled','false');
1142                                 obj.controller.view.cmd_delete_items.setAttribute('disabled','false');
1143                                 obj.controller.view.cmd_print_spine_labels.setAttribute('disabled','false');
1144                         }
1145                 } catch(E) {
1146                         obj.error.standard_unexpected_error_alert('Copy Browser Actions',E);
1147                 }
1148         },
1149
1150         'refresh_list' : function() { 
1151                 try {
1152                         var obj = this;
1153                         obj.list.clear();
1154                         obj.map_tree = {};
1155                         obj.map_acn = {};
1156                         obj.map_acp = {};
1157                         obj.org_ids = obj.network.simple_request('FM_AOU_IDS_RETRIEVE_VIA_RECORD_ID',[ obj.docid ]);
1158                         var org = obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ];
1159                         obj.show_libs( org );
1160                         obj.show_my_libs();
1161                 } catch(E) {
1162                         this.error.standard_unexpected_error_alert('Problem refreshing the volume/copy tree.',E);
1163                 }
1164         },
1165 }
1166
1167 dump('exiting cat.copy_browser.js\n');