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