]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/copy_browser.js
the rest of copy browser
[Evergreen.git] / Open-ILS / xul / staff_client / server / cat / copy_browser.js
1 dump('entering cat.copy_browser.js\n');
2
3 if (typeof cat == 'undefined') cat = {};
4 cat.copy_browser = function (params) {
5         try {
6                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
7                 JSAN.use('util.error'); this.error = new util.error();
8         } catch(E) {
9                 dump('cat.copy_browser: ' + E + '\n');
10         }
11 }
12
13 cat.copy_browser.prototype = {
14
15         'map_tree' : {},
16         'map_acn' : {},
17         'map_acp' : {},
18         'sel_list' : [],
19
20         'init' : function( params ) {
21
22                 try {
23                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
24                         var obj = this;
25
26                         obj.docid = params.docid;
27
28                         JSAN.use('util.network'); obj.network = new util.network();
29                         JSAN.use('OpenILS.data'); obj.data = new OpenILS.data(); obj.data.init({'via':'stash'});
30                         JSAN.use('util.controller'); obj.controller = new util.controller();
31                         obj.controller.init(
32                                 {
33                                         control_map : {
34                                                 'cmd_broken' : [
35                                                         ['command'],
36                                                         function() { alert('Not Yet Implemented'); }
37                                                 ],
38                                                 'cmd_show_my_libs' : [
39                                                         ['command'],
40                                                         function() { 
41                                                                 obj.show_my_libs(); 
42                                                         }
43                                                 ],
44                                                 'cmd_show_all_libs' : [
45                                                         ['command'],
46                                                         function() {
47                                                                 obj.show_all_libs();
48                                                         }
49                                                 ],
50                                                 'cmd_show_libs_with_copies' : [
51                                                         ['command'],
52                                                         function() {
53                                                                 obj.show_libs_with_copies();
54                                                         }
55                                                 ],
56                                                 'cmd_clear' : [
57                                                         ['command'],
58                                                         function() {
59                                                                 obj.map_tree = {};
60                                                                 obj.list.clear();
61                                                         }
62                                                 ],
63                                                 'cmd_add_items' : [
64                                                         ['command'],
65                                                         function() {
66                                                                 try {
67                                                                         JSAN.use('util.functional');
68                                                                         var list = util.functional.map_list(
69                                                                                 util.functional.filter_list(
70                                                                                         obj.sel_list,
71                                                                                         function (o) {
72                                                                                                 return o.split(/_/)[0] == 'acn';
73                                                                                         }
74                                                                                 ),
75                                                                                 function (o) {
76                                                                                         return o.split(/_/)[1];
77                                                                                 }
78                                                                         );
79                                                                         if (list.length == 0) return;
80
81                                                                         var copy_shortcut = {};
82                                                                         list = util.functional.map_list(
83                                                                                 list,
84                                                                                 function (o) {
85                                                                                         var ou_id = obj.map_acn['acn_' + o].owning_lib();
86                                                                                         var volume_id = o;
87                                                                                         var label = obj.map_acn['acn_' + o].label();
88                                                                                         if (!copy_shortcut[ou_id]) copy_shortcut[ou_id] = {};
89                                                                                         copy_shortcut[ou_id][ label ] = volume_id;
90
91                                                                                         return ou_id;
92                                                                                 }
93                                                                         );
94                                                                         /* quick fix */  /* what was this fixing? */
95                                                                         list = []; for (var i in copy_shortcut) { list.push( i ); }
96
97                                                                         var edit = 0;
98                                                                         try {
99                                                                                 edit = obj.network.request(
100                                                                                         api.PERM_MULTI_ORG_CHECK.app,
101                                                                                         api.PERM_MULTI_ORG_CHECK.method,
102                                                                                         [ 
103                                                                                                 ses(), 
104                                                                                                 obj.data.list.au[0].id(), 
105                                                                                                 list,
106                                                                                                 [ 'CREATE_COPY' ]
107                                                                                         ]
108                                                                                 ).length == 0 ? 1 : 0;
109                                                                         } catch(E) {
110                                                                                 obj.error.sdump('D_ERROR','batch permission check: ' + E);
111                                                                         }
112
113                                                                         if (edit==0) return; // no read-only view for this interface
114
115                                                                         var title = 'Add Copy';
116
117                                                                         JSAN.use('util.window'); var win = new util.window();
118                                                                         var w = win.open(
119                                                                                 window.xulG.url_prefix(urls.XUL_VOLUME_COPY_CREATOR)
120                                                                                         +'?doc_id=' + window.escape(obj.docid)
121                                                                                         +'&ou_ids=' + window.escape( js2JSON(list) )
122                                                                                         +'&copy_shortcut=' + window.escape( js2JSON(copy_shortcut) ),
123                                                                                 title,
124                                                                                 'chrome,modal,resizable'
125                                                                         );
126                                                                         obj.refresh_list();
127                                                                 } catch(E) {
128                                                                         obj.error.standard_unexpected_error_alert('copy browser -> add copies',E);
129                                                                 }
130                                                         }
131                                                 ],
132                                                 'cmd_add_items_to_buckets' : [
133                                                         ['command'],
134                                                         function() {
135                                                                 try {
136                                                                         JSAN.use('util.functional');
137
138                                                                         var list = util.functional.filter_list(
139                                                                                 obj.sel_list,
140                                                                                 function (o) {
141                                                                                         return o.split(/_/)[0] == 'acp';
142                                                                                 }
143                                                                         );
144
145                                                                         list = util.functional.map_list(
146                                                                                 list,
147                                                                                 function (o) {
148                                                                                         return o.split(/_/)[1];
149                                                                                 }
150                                                                         );
151                                                                         
152                                                                         JSAN.use('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                                                         }
573                                                 ],
574                                         }
575                                 }
576                         );
577
578                         obj.list_init(params);
579
580                         obj.org_ids = obj.network.simple_request('FM_AOU_IDS_RETRIEVE_VIA_RECORD_ID',[ obj.docid ]);
581
582                         var org = obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ];
583                         obj.show_libs( org );
584
585                 } catch(E) {
586                         this.error.sdump('D_ERROR','cat.copy_browser.init: ' + E + '\n');
587                 }
588         },
589
590         'show_my_libs' : function() {
591                 var obj = this;
592                 try {
593                         var org = obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ];
594                         obj.show_libs( org, true );
595                 
596                         var p_org = obj.data.hash.aou[ org.parent_ou() ];
597                         if (p_org) {
598                                 JSAN.use('util.exec'); var exec = new util.exec();
599                                 var funcs = [];
600                                 for (var i = 0; i < p_org.children().length; i++) {
601                                         funcs.push(
602                                                 function(o) {
603                                                         return function() {
604                                                                 obj.show_libs( o, true );
605                                                         }
606                                                 }( p_org.children()[i] )
607                                         );
608                                 }
609                                 exec.chain( funcs );
610                         }
611                 } catch(E) {
612                         alert(E);
613                 }
614         },
615
616         'show_all_libs' : function() {
617                 var obj = this;
618                 try {
619                         obj.show_my_libs();
620
621                         obj.show_libs( obj.data.tree.aou );
622
623                         JSAN.use('util.exec'); var exec = new util.exec();
624                         var funcs = [];
625                         for (var i = 0; i < obj.data.tree.aou.children().length; i++) {
626                                 funcs.push(
627                                         function(o) {
628                                                 return function() {
629                                                         obj.show_libs( o );
630                                                 }
631                                         }( obj.data.tree.aou.children()[i] )
632                                 );
633                         }
634                         exec.chain( funcs );
635                 } catch(E) {
636                         alert(E);
637                 }
638         },
639
640         'show_libs_with_copies' : function() {
641                 var obj = this;
642                 try {
643                         JSAN.use('util.exec'); var exec = new util.exec();
644                         JSAN.use('util.functional');
645
646                         var orgs = util.functional.map_list(
647                                 obj.org_ids,
648                                 function(id) { return obj.data.hash.aou[id]; }
649                         ).sort(
650                                 function( a, b ) {
651                                         if (a.shortname() < b.shortname()) return -1;
652                                         if (a.shortname() > b.shortname()) return 1;
653                                         return 0;
654                                 }
655                         );
656                         var funcs = [];
657                         for (var i = 0; i < orgs.length; i++) {
658                                 funcs.push(
659                                         function(o) {
660                                                 return function() {
661                                                         obj.show_libs(o,true);
662                                                 }
663                                         }( orgs[i] )
664                                 );
665                         }
666                         exec.chain( funcs );
667                 } catch(E) {
668                         alert(E);
669                 }
670         },
671
672         'show_libs' : function(start_aou,show_open) {
673                 var obj = this;
674                 try {
675                         if (!start_aou) throw('show_libs: Need a start_aou');
676                         JSAN.use('OpenILS.data'); obj.data = new OpenILS.data(); obj.data.init({'via':'stash'});
677                         JSAN.use('util.functional'); JSAN.use('util.exec'); var exec = new util.exec();
678
679                         var funcs = [];
680
681                         var parents = [];
682                         var temp_aou = start_aou;
683                         while ( temp_aou.parent_ou() ) {
684                                 temp_aou = obj.data.hash.aou[ temp_aou.parent_ou() ];
685                                 parents.push( temp_aou );
686                         }
687                         parents.reverse();
688
689                         for (var i = 0; i < parents.length; i++) {
690                                 funcs.push(
691                                         function(o,p) {
692                                                 return function() { 
693                                                         if (show_open) {
694                                                                 obj.append_org(o,p,{'container':'true','open':'true'}); 
695                                                         } else {
696                                                                 obj.append_org(o,p,{'container':'true'}); 
697                                                         }
698                                                 };
699                                         }(parents[i], obj.data.hash.aou[ parents[i].parent_ou() ])
700                                 );
701                         }
702
703                         funcs.push(
704                                 function(o,p) {
705                                         return function() { obj.append_org(o,p); };
706                                 }(start_aou,obj.data.hash.aou[ start_aou.parent_ou() ])
707                         );
708
709                         funcs.push(
710                                 function() {
711                                         if (start_aou.children()) {
712                                                 var x = obj.map_tree[ 'aou_' + start_aou.id() ];
713                                                 x.setAttribute('container','true');
714                                                 if (show_open) x.setAttribute('open','true');
715                                                 for (var i = 0; i < start_aou.children().length; i++) {
716                                                         funcs.push(
717                                                                 function(o,p) {
718                                                                         return function() { obj.append_org(o,p); };
719                                                                 }( start_aou.children()[i], start_aou )
720                                                         );
721                                                 }
722                                         }
723                                 }
724                         );
725
726                         exec.chain( funcs );
727
728                 } catch(E) {
729                         alert(E);
730                 }
731         },
732
733         'on_select' : function(list,twisty) {
734                 var obj = this;
735                 for (var i = 0; i < list.length; i++) {
736                         var node = obj.map_tree[ list[i] ];
737                         //if (node.lastChild.nodeName == 'treechildren') { continue; } else { alert(node.lastChild.nodeName); }
738                         var row_type = list[i].split('_')[0];
739                         var id = list[i].split('_')[1];
740                         switch(row_type) {
741                                 case 'aou' : obj.on_select_org(id,twisty); break;
742                                 case 'acn' : obj.on_select_acn(id,twisty); break;
743                                 default: break;
744                         }
745                 }
746         },
747
748         'on_select_acn' : function(acn_id,twisty) {
749                 var obj = this;
750                 try {
751                         var acn_tree = obj.map_acp[ 'acn_' + acn_id ];
752                         var funcs = [];
753                         if (acn_tree.copies()) {
754                                 for (var i = 0; i < acn_tree.copies().length; i++) {
755                                         funcs.push(
756                                                 function(c,a) {
757                                                         return function() {
758                                                                 obj.append_acp(c,a);
759                                                         }
760                                                 }( acn_tree.copies()[i], acn_tree )
761                                         )
762                                 }
763                         }
764                         JSAN.use('util.exec'); var exec = new util.exec();
765                         exec.chain( funcs );
766                 } catch(E) {
767                         alert(E);
768                 }
769         },
770
771         'on_select_org' : function(org_id,twisty) {
772                 var obj = this;
773                 var org = obj.data.hash.aou[ org_id ];
774                 var funcs = [];
775                 if (org.children()) {
776                         for (var i = 0; i < org.children().length; i++) {
777                                 funcs.push(
778                                         function(o,p) {
779                                                 return function() {
780                                                         obj.append_org(o,p)
781                                                 }
782                                         }(org.children()[i],org)
783                                 );
784                         }
785                 } 
786                 if (obj.map_acn[ 'aou_' + org_id ]) {
787                         for (var i = 0; i < obj.map_acn[ 'aou_' + org_id ].length; i++) {
788                                 funcs.push(
789                                         function(o,a) {
790                                                 return function() {
791                                                         obj.append_acn(o,a);
792                                                 }
793                                         }( org, obj.map_acn[ 'aou_' + org_id ][i] )
794                                 );
795                         }
796                 }
797                 JSAN.use('util.exec'); var exec = new util.exec();
798                 exec.chain( funcs );
799         },
800
801         'append_org' : function (org,parent_org,params) {
802                 var obj = this;
803                 try {
804                         if (obj.map_tree[ 'aou_' + org.id() ]) {
805                                 var x = obj.map_tree[ 'aou_' + org.id() ];
806                                 if (params) {
807                                         for (var i in params) {
808                                                 x.setAttribute(i,params[i]);
809                                         }
810                                 }
811                                 return x;
812                         }
813
814                         var data = {
815                                 'row' : {
816                                         'my' : {
817                                                 'aou' : org,
818                                         }
819                                 },
820                                 'skip_all_columns_except' : [0,1,2],
821                                 'retrieve_id' : 'aou_' + org.id(),
822                         };
823                 
824                         var acn_tree_list;
825                         if ( obj.org_ids.indexOf( org.id() ) == -1 ) {
826                                 if ( obj.data.hash.aout[ org.ou_type() ].can_have_vols() ) {
827                                         data.row.my.volume_count = '0';
828                                         data.row.my.copy_count = '<0>';
829                                 } else {
830                                         data.row.my.volume_count = '';
831                                         data.row.my.copy_count = '';
832                                 }
833                         } else {
834                                 var v_count = 0; var c_count = 0;
835                                 acn_tree_list = obj.network.simple_request(
836                                         'FM_ACN_TREE_LIST_RETRIEVE_VIA_RECORD_ID_AND_ORG_IDS',
837                                         [ ses(), obj.docid, [ org.id() ] ]
838                                 );
839                                 for (var i = 0; i < acn_tree_list.length; i++) {
840                                         v_count++;
841                                         obj.map_acn[ 'acn_' + acn_tree_list[i].id() ] = function(r){return r;}(acn_tree_list[i]);
842                                         var copies = acn_tree_list[i].copies(); if (copies) c_count += copies.length;
843                                         for (var j = 0; j < copies.length; j++) {
844                                                 obj.map_acp[ 'acp_' + copies[j].id() ] = function(r){return r;}(copies[j]);
845                                         }
846                                 }
847                                 data.row.my.volume_count = v_count;
848                                 data.row.my.copy_count = '<' + c_count + '>';
849                         }
850                         if (parent_org) {
851                                 data.node = obj.map_tree[ 'aou_' + parent_org.id() ];
852                         }
853
854                         var node = obj.list.append(data);
855                         if (params) {
856                                 for (var i in params) {
857                                         node.setAttribute(i,params[i]);
858                                 }
859                         }
860                         obj.map_tree[ 'aou_' + org.id() ] = node;
861
862                         if (org.children()) {
863                                 node.setAttribute('container','true');
864                         }
865
866                         if (parent_org) {
867                                 if ( obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ].parent_ou() == parent_org.id() ) {
868                                         data.node.setAttribute('open','true');
869                                 }
870                         } else {
871                                 obj.map_tree[ 'aou_' + org.id() ].setAttribute('open','true');
872                         }
873
874                         if (acn_tree_list) {
875                                 obj.map_acn[ 'aou_' + org.id() ] = acn_tree_list;
876                                 node.setAttribute('container','true');
877                         }
878
879                 } catch(E) {
880                         dump(E+'\n');
881                         alert(E);
882                 }
883         },
884
885         'append_acn' : function( org, acn_tree, params ) {
886                 var obj = this;
887                 try {
888                         if (obj.map_tree[ 'acn_' + acn_tree.id() ]) {
889                                 var x = obj.map_tree[ 'acn_' + acn_tree.id() ];
890                                 if (params) {
891                                         for (var i in params) {
892                                                 x.setAttribute(i,params[i]);
893                                         }
894                                 }
895                                 return x;
896                         }
897
898                         var parent_node = obj.map_tree[ 'aou_' + org.id() ];
899                         var data = {
900                                 'row' : {
901                                         'my' : {
902                                                 'aou' : org,
903                                                 'acn' : acn_tree,
904                                                 'volume_count' : '',
905                                                 'copy_count' : acn_tree.copies() ? acn_tree.copies().length : '0',
906                                         }
907                                 },
908                                 'skip_all_columns_except' : [0,1,2],
909                                 'retrieve_id' : 'acn_' + acn_tree.id(),
910                                 'node' : parent_node,
911                         };
912                         var node = obj.list.append(data);
913                         obj.map_tree[ 'acn_' + acn_tree.id() ] =  node;
914                         if (params) {
915                                 for (var i in params) {
916                                         node.setAttribute(i,params[i]);
917                                 }
918                         }
919                         if (acn_tree.copies()) {
920                                 obj.map_acp[ 'acn_' + acn_tree.id() ] = acn_tree;
921                                 node.setAttribute('container','true');
922                         }
923
924                 } catch(E) {
925                         dump(E+'\n');
926                         alert(E);
927                 }
928         },
929
930         'append_acp' : function( acp_item, acn_tree, params ) {
931                 var obj = this;
932                 try {
933                         if (obj.map_tree[ 'acp_' + acp_item.id() ]) {
934                                 var x = obj.map_tree[ 'acp_' + acp_item.id() ];
935                                 if (params) {
936                                         for (var i in params) {
937                                                 x.setAttribute(i,params[i]);
938                                         }
939                                 }
940                                 return x;
941                         }
942
943                         var parent_node = obj.map_tree[ 'acn_' + acn_tree.id() ];
944                         var data = {
945                                 'row' : {
946                                         'my' : {
947                                                 'aou' : obj.data.hash.aou[ acn_tree.owning_lib() ],
948                                                 'acn' : acn_tree,
949                                                 'acp' : acp_item,
950                                                 'volume_count' : '',
951                                                 'copy_count' : '',
952                                         }
953                                 },
954                                 'retrieve_id' : 'acp_' + acp_item.id(),
955                                 'node' : parent_node,
956                         };
957                         var node = obj.list.append(data);
958                         obj.map_tree[ 'acp_' + acp_item.id() ] =  node;
959                         if (params) {
960                                 for (var i in params) {
961                                         node.setAttribute(i,params[i]);
962                                 }
963                         }
964
965                 } catch(E) {
966                         dump(E+'\n');
967                         alert(E);
968                 }
969         },
970
971         'list_init' : function( params ) {
972
973                 try {
974                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
975                         var obj = this;
976                         
977                         JSAN.use('circ.util');
978                         var columns = [
979                                 {
980                                         'id' : 'tree_location', 'label' : 'Location/Barcode', 'flex' : 1,
981                                         'primary' : true, 'hidden' : false, 
982                                         'render' : 'my.acp ? my.acp.barcode() : my.acn ? my.acn.label() : my.aou ? my.aou.shortname() + " : " + my.aou.name() : "???"'
983                                 },
984                                 {
985                                         'id' : 'volume_count', 'label' : 'Volumes', 'flex' : 0,
986                                         'primary' : false, 'hidden' : false, 
987                                         'render' : 'my.volume_count'
988                                 },
989                                 {
990                                         'id' : 'copy_count', 'label' : 'Copies', 'flex' : 0,
991                                         'primary' : false, 'hidden' : false, 
992                                         'render' : 'my.copy_count'
993                                 },
994                         ].concat(
995                                 circ.util.columns( 
996                                         { 
997                                                 'location' : { 'hidden' : false },
998                                                 'circ_lib' : { 'hidden' : false },
999                                                 'owning_lib' : { 'hidden' : false },
1000                                                 'call_number' : { 'hidden' : false },
1001                                                 'status' : { 'hidden' : false },
1002                                         },
1003                                         {
1004                                                 'just_these' : [
1005                                                         'owning_lib',
1006                                                         'circ_lib',
1007                                                         'call_number',
1008                                                         'copy_number',
1009                                                         'location',
1010                                                         'barcode',
1011                                                         'loan_duration',
1012                                                         'fine_level',
1013                                                         'circulate',
1014                                                         'holdable',
1015                                                         'opac_visible',
1016                                                         'ref',
1017                                                         'deposit',
1018                                                         'deposit_amount',
1019                                                         'price',
1020                                                         'circ_as_type',
1021                                                         'circ_modifier',
1022                                                         'status',
1023                                                         'alert_message',
1024                                                         'acp_id',
1025                                                 ]
1026                                         }
1027                                 )
1028                         );
1029                         JSAN.use('util.list'); obj.list = new util.list('copy_tree');
1030                         obj.list.init(
1031                                 {
1032                                         'columns' : columns,
1033                                         'map_row_to_column' : circ.util.std_map_row_to_column(' '),
1034                                         'retrieve_row' : function(params) {
1035
1036                                                 var row = params.row;
1037
1038                                                 var funcs = [];
1039                                         /*      
1040                                                 if (!row.my.mvr) funcs.push(
1041                                                         function() {
1042
1043                                                                 row.my.mvr = obj.network.request(
1044                                                                         api.MODS_SLIM_RECORD_RETRIEVE_VIA_COPY.app,
1045                                                                         api.MODS_SLIM_RECORD_RETRIEVE_VIA_COPY.method,
1046                                                                         [ row.my.circ.target_copy() ]
1047                                                                 );
1048
1049                                                         }
1050                                                 );
1051                                                 if (!row.my.acp) {
1052                                                         funcs.push(     
1053                                                                 function() {
1054
1055                                                                         row.my.acp = obj.network.request(
1056                                                                                 api.FM_ACP_RETRIEVE.app,
1057                                                                                 api.FM_ACP_RETRIEVE.method,
1058                                                                                 [ row.my.circ.target_copy() ]
1059                                                                         );
1060
1061                                                                         params.row_node.setAttribute( 'retrieve_id',row.my.acp.barcode() );
1062
1063                                                                 }
1064                                                         );
1065                                                 } else {
1066                                                         params.row_node.setAttribute( 'retrieve_id',row.my.acp.barcode() );
1067                                                 }
1068                                         */
1069                                                 funcs.push(
1070                                                         function() {
1071
1072                                                                 if (typeof params.on_retrieve == 'function') {
1073                                                                         params.on_retrieve(row);
1074                                                                 }
1075
1076                                                         }
1077                                                 );
1078
1079                                                 JSAN.use('util.exec'); var exec = new util.exec();
1080                                                 exec.on_error = function(E) {
1081                                                         var err = 'items chain: ' + js2JSON(E);
1082                                                         obj.error.sdump('D_ERROR',err);
1083                                                         return true; /* keep going */
1084                                                 }
1085                                                 exec.chain( funcs );
1086
1087                                                 return row;
1088                                         },
1089                                         'on_click' : function(ev) {
1090                                                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserRead');
1091                                                 var row = {}; var col = {}; var nobj = {};
1092                                                 obj.list.node.treeBoxObject.getCellAt(ev.clientX,ev.clientY,row,col,nobj);
1093                                                 if ((row.value == -1)||(nobj.value != 'twisty')) { return; }
1094                                                 var node = obj.list.node.contentView.getItemAtIndex(row.value);
1095                                                 var list = [ node.getAttribute('retrieve_id') ];
1096                                                 if (typeof obj.on_select == 'function') {
1097                                                         obj.on_select(list,true);
1098                                                 }
1099                                                 if (typeof window.xulG == 'object' && typeof window.xulG.on_select == 'function') {
1100                                                         window.xulG.on_select(list);
1101                                                 }
1102                                         },
1103                                         'on_select' : function(ev) {
1104                                                 JSAN.use('util.functional');
1105                                                 var sel = obj.list.retrieve_selection();
1106                                                 obj.sel_list = util.functional.map_list(
1107                                                         sel,
1108                                                         function(o) { return o.getAttribute('retrieve_id'); }
1109                                                 );
1110                                                 obj.toggle_actions();
1111                                                 if (typeof obj.on_select == 'function') {
1112                                                         obj.on_select(obj.sel_list);
1113                                                 }
1114                                                 if (typeof window.xulG == 'object' && typeof window.xulG.on_select == 'function') {
1115                                                         window.xulG.on_select(obj.sel_list);
1116                                                 }
1117                                         },
1118                                 }
1119                         );
1120
1121                         obj.controller.render();
1122
1123                 } catch(E) {
1124                         this.error.sdump('D_ERROR','cat.copy_browser.list_init: ' + E + '\n');
1125                         alert(E);
1126                 }
1127         },
1128
1129         'toggle_actions' : function() {
1130                 var obj = this;
1131                 try {
1132                         var found_aou = false; var found_acn = false; var found_acp = false;
1133                         for (var i = 0; i < obj.sel_list.length; i++) {
1134                                 var type = obj.sel_list[i].split(/_/)[0];
1135                                 switch(type) {
1136                                         case 'aou' : found_aou = true; break;
1137                                         case 'acn' : found_acn = true; break;
1138                                         case 'acp' : found_acp = true; break;
1139                                 }
1140                         }
1141                         obj.controller.view.cmd_add_items.setAttribute('disabled','true');
1142                         obj.controller.view.cmd_add_items_to_buckets.setAttribute('disabled','true');
1143                         obj.controller.view.cmd_edit_items.setAttribute('disabled','true');
1144                         obj.controller.view.cmd_delete_items.setAttribute('disabled','true');
1145                         obj.controller.view.cmd_print_spine_labels.setAttribute('disabled','true');
1146                         obj.controller.view.cmd_add_volumes.setAttribute('disabled','true');
1147                         obj.controller.view.cmd_edit_volumes.setAttribute('disabled','true');
1148                         obj.controller.view.cmd_delete_volumes.setAttribute('disabled','true');
1149                         obj.controller.view.cmd_mark_volume.setAttribute('disabled','true');
1150                         if (found_aou) {
1151                                 obj.controller.view.cmd_add_volumes.setAttribute('disabled','false');
1152                         }
1153                         if (found_acn) {
1154                                 obj.controller.view.cmd_edit_volumes.setAttribute('disabled','false');
1155                                 obj.controller.view.cmd_delete_volumes.setAttribute('disabled','false');
1156                                 obj.controller.view.cmd_mark_volume.setAttribute('disabled','false');
1157                                 obj.controller.view.cmd_add_items.setAttribute('disabled','false');
1158                         }
1159                         if (found_acp) {
1160                                 obj.controller.view.cmd_add_items_to_buckets.setAttribute('disabled','false');
1161                                 obj.controller.view.cmd_edit_items.setAttribute('disabled','false');
1162                                 obj.controller.view.cmd_delete_items.setAttribute('disabled','false');
1163                                 obj.controller.view.cmd_print_spine_labels.setAttribute('disabled','false');
1164                         }
1165                 } catch(E) {
1166                         obj.error.standard_unexpected_error_alert('Copy Browser Actions',E);
1167                 }
1168         },
1169
1170         'refresh_list' : function() { alert('FIXME: refresh the list now'); },
1171 }
1172
1173 dump('exiting cat.copy_browser.js\n');