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