]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/copy_browser.js
show adjacent libs to the workstation lib. Eventually, we'll want to do this more...
[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                         obj.show_my_libs();
586
587                 } catch(E) {
588                         this.error.sdump('D_ERROR','cat.copy_browser.init: ' + E + '\n');
589                 }
590         },
591
592         'show_my_libs' : function() {
593                 var obj = this;
594                 try {
595                         var org = obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ];
596                         obj.show_libs( org, true );
597                 
598                         var p_org = obj.data.hash.aou[ org.parent_ou() ];
599                         if (p_org) {
600                                 JSAN.use('util.exec'); var exec = new util.exec();
601                                 var funcs = [];
602                                 for (var i = 0; i < p_org.children().length; i++) {
603                                         funcs.push(
604                                                 function(o) {
605                                                         return function() {
606                                                                 obj.show_libs( o, true );
607                                                         }
608                                                 }( p_org.children()[i] )
609                                         );
610                                 }
611                                 exec.chain( funcs );
612                         }
613                 } catch(E) {
614                         alert(E);
615                 }
616         },
617
618         'show_all_libs' : function() {
619                 var obj = this;
620                 try {
621                         obj.show_my_libs();
622
623                         obj.show_libs( obj.data.tree.aou );
624
625                         JSAN.use('util.exec'); var exec = new util.exec();
626                         var funcs = [];
627                         for (var i = 0; i < obj.data.tree.aou.children().length; i++) {
628                                 funcs.push(
629                                         function(o) {
630                                                 return function() {
631                                                         obj.show_libs( o );
632                                                 }
633                                         }( obj.data.tree.aou.children()[i] )
634                                 );
635                         }
636                         exec.chain( funcs );
637                 } catch(E) {
638                         alert(E);
639                 }
640         },
641
642         'show_libs_with_copies' : function() {
643                 var obj = this;
644                 try {
645                         JSAN.use('util.exec'); var exec = new util.exec();
646                         JSAN.use('util.functional');
647
648                         var orgs = util.functional.map_list(
649                                 obj.org_ids,
650                                 function(id) { return obj.data.hash.aou[id]; }
651                         ).sort(
652                                 function( a, b ) {
653                                         if (a.shortname() < b.shortname()) return -1;
654                                         if (a.shortname() > b.shortname()) return 1;
655                                         return 0;
656                                 }
657                         );
658                         var funcs = [];
659                         for (var i = 0; i < orgs.length; i++) {
660                                 funcs.push(
661                                         function(o) {
662                                                 return function() {
663                                                         obj.show_libs(o,true);
664                                                 }
665                                         }( orgs[i] )
666                                 );
667                         }
668                         exec.chain( funcs );
669                 } catch(E) {
670                         alert(E);
671                 }
672         },
673
674         'show_libs' : function(start_aou,show_open) {
675                 var obj = this;
676                 try {
677                         if (!start_aou) throw('show_libs: Need a start_aou');
678                         JSAN.use('OpenILS.data'); obj.data = new OpenILS.data(); obj.data.init({'via':'stash'});
679                         JSAN.use('util.functional'); JSAN.use('util.exec'); var exec = new util.exec();
680
681                         var funcs = [];
682
683                         var parents = [];
684                         var temp_aou = start_aou;
685                         while ( temp_aou.parent_ou() ) {
686                                 temp_aou = obj.data.hash.aou[ temp_aou.parent_ou() ];
687                                 parents.push( temp_aou );
688                         }
689                         parents.reverse();
690
691                         for (var i = 0; i < parents.length; i++) {
692                                 funcs.push(
693                                         function(o,p) {
694                                                 return function() { 
695                                                         if (show_open) {
696                                                                 obj.append_org(o,p,{'container':'true','open':'true'}); 
697                                                         } else {
698                                                                 obj.append_org(o,p,{'container':'true'}); 
699                                                         }
700                                                 };
701                                         }(parents[i], obj.data.hash.aou[ parents[i].parent_ou() ])
702                                 );
703                         }
704
705                         funcs.push(
706                                 function(o,p) {
707                                         return function() { obj.append_org(o,p); };
708                                 }(start_aou,obj.data.hash.aou[ start_aou.parent_ou() ])
709                         );
710
711                         funcs.push(
712                                 function() {
713                                         if (start_aou.children()) {
714                                                 var x = obj.map_tree[ 'aou_' + start_aou.id() ];
715                                                 x.setAttribute('container','true');
716                                                 if (show_open) x.setAttribute('open','true');
717                                                 for (var i = 0; i < start_aou.children().length; i++) {
718                                                         funcs.push(
719                                                                 function(o,p) {
720                                                                         return function() { obj.append_org(o,p); };
721                                                                 }( start_aou.children()[i], start_aou )
722                                                         );
723                                                 }
724                                         }
725                                 }
726                         );
727
728                         exec.chain( funcs );
729
730                 } catch(E) {
731                         alert(E);
732                 }
733         },
734
735         'on_select' : function(list,twisty) {
736                 var obj = this;
737                 for (var i = 0; i < list.length; i++) {
738                         var node = obj.map_tree[ list[i] ];
739                         //if (node.lastChild.nodeName == 'treechildren') { continue; } else { alert(node.lastChild.nodeName); }
740                         var row_type = list[i].split('_')[0];
741                         var id = list[i].split('_')[1];
742                         switch(row_type) {
743                                 case 'aou' : obj.on_select_org(id,twisty); break;
744                                 case 'acn' : obj.on_select_acn(id,twisty); break;
745                                 default: break;
746                         }
747                 }
748         },
749
750         'on_select_acn' : function(acn_id,twisty) {
751                 var obj = this;
752                 try {
753                         var acn_tree = obj.map_acp[ 'acn_' + acn_id ];
754                         var funcs = [];
755                         if (acn_tree.copies()) {
756                                 for (var i = 0; i < acn_tree.copies().length; i++) {
757                                         funcs.push(
758                                                 function(c,a) {
759                                                         return function() {
760                                                                 obj.append_acp(c,a);
761                                                         }
762                                                 }( acn_tree.copies()[i], acn_tree )
763                                         )
764                                 }
765                         }
766                         JSAN.use('util.exec'); var exec = new util.exec();
767                         exec.chain( funcs );
768                 } catch(E) {
769                         alert(E);
770                 }
771         },
772
773         'on_select_org' : function(org_id,twisty) {
774                 var obj = this;
775                 var org = obj.data.hash.aou[ org_id ];
776                 var funcs = [];
777                 if (org.children()) {
778                         for (var i = 0; i < org.children().length; i++) {
779                                 funcs.push(
780                                         function(o,p) {
781                                                 return function() {
782                                                         obj.append_org(o,p)
783                                                 }
784                                         }(org.children()[i],org)
785                                 );
786                         }
787                 } 
788                 if (obj.map_acn[ 'aou_' + org_id ]) {
789                         for (var i = 0; i < obj.map_acn[ 'aou_' + org_id ].length; i++) {
790                                 funcs.push(
791                                         function(o,a) {
792                                                 return function() {
793                                                         obj.append_acn(o,a);
794                                                 }
795                                         }( org, obj.map_acn[ 'aou_' + org_id ][i] )
796                                 );
797                         }
798                 }
799                 JSAN.use('util.exec'); var exec = new util.exec();
800                 exec.chain( funcs );
801         },
802
803         'append_org' : function (org,parent_org,params) {
804                 var obj = this;
805                 try {
806                         if (obj.map_tree[ 'aou_' + org.id() ]) {
807                                 var x = obj.map_tree[ 'aou_' + org.id() ];
808                                 if (params) {
809                                         for (var i in params) {
810                                                 x.setAttribute(i,params[i]);
811                                         }
812                                 }
813                                 return x;
814                         }
815
816                         var data = {
817                                 'row' : {
818                                         'my' : {
819                                                 'aou' : org,
820                                         }
821                                 },
822                                 'skip_all_columns_except' : [0,1,2],
823                                 'retrieve_id' : 'aou_' + org.id(),
824                         };
825                 
826                         var acn_tree_list;
827                         if ( obj.org_ids.indexOf( org.id() ) == -1 ) {
828                                 if ( obj.data.hash.aout[ org.ou_type() ].can_have_vols() ) {
829                                         data.row.my.volume_count = '0';
830                                         data.row.my.copy_count = '<0>';
831                                 } else {
832                                         data.row.my.volume_count = '';
833                                         data.row.my.copy_count = '';
834                                 }
835                         } else {
836                                 var v_count = 0; var c_count = 0;
837                                 acn_tree_list = obj.network.simple_request(
838                                         'FM_ACN_TREE_LIST_RETRIEVE_VIA_RECORD_ID_AND_ORG_IDS',
839                                         [ ses(), obj.docid, [ org.id() ] ]
840                                 );
841                                 for (var i = 0; i < acn_tree_list.length; i++) {
842                                         v_count++;
843                                         obj.map_acn[ 'acn_' + acn_tree_list[i].id() ] = function(r){return r;}(acn_tree_list[i]);
844                                         var copies = acn_tree_list[i].copies(); if (copies) c_count += copies.length;
845                                         for (var j = 0; j < copies.length; j++) {
846                                                 obj.map_acp[ 'acp_' + copies[j].id() ] = function(r){return r;}(copies[j]);
847                                         }
848                                 }
849                                 data.row.my.volume_count = v_count;
850                                 data.row.my.copy_count = '<' + c_count + '>';
851                         }
852                         if (parent_org) {
853                                 data.node = obj.map_tree[ 'aou_' + parent_org.id() ];
854                         }
855
856                         var node = obj.list.append(data);
857                         if (params) {
858                                 for (var i in params) {
859                                         node.setAttribute(i,params[i]);
860                                 }
861                         }
862                         obj.map_tree[ 'aou_' + org.id() ] = node;
863
864                         if (org.children()) {
865                                 node.setAttribute('container','true');
866                         }
867
868                         if (parent_org) {
869                                 if ( obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ].parent_ou() == parent_org.id() ) {
870                                         data.node.setAttribute('open','true');
871                                 }
872                         } else {
873                                 obj.map_tree[ 'aou_' + org.id() ].setAttribute('open','true');
874                         }
875
876                         if (acn_tree_list) {
877                                 obj.map_acn[ 'aou_' + org.id() ] = acn_tree_list;
878                                 node.setAttribute('container','true');
879                         }
880
881                 } catch(E) {
882                         dump(E+'\n');
883                         alert(E);
884                 }
885         },
886
887         'append_acn' : function( org, acn_tree, params ) {
888                 var obj = this;
889                 try {
890                         if (obj.map_tree[ 'acn_' + acn_tree.id() ]) {
891                                 var x = obj.map_tree[ 'acn_' + acn_tree.id() ];
892                                 if (params) {
893                                         for (var i in params) {
894                                                 x.setAttribute(i,params[i]);
895                                         }
896                                 }
897                                 return x;
898                         }
899
900                         var parent_node = obj.map_tree[ 'aou_' + org.id() ];
901                         var data = {
902                                 'row' : {
903                                         'my' : {
904                                                 'aou' : org,
905                                                 'acn' : acn_tree,
906                                                 'volume_count' : '',
907                                                 'copy_count' : acn_tree.copies() ? acn_tree.copies().length : '0',
908                                         }
909                                 },
910                                 'skip_all_columns_except' : [0,1,2],
911                                 'retrieve_id' : 'acn_' + acn_tree.id(),
912                                 'node' : parent_node,
913                         };
914                         var node = obj.list.append(data);
915                         obj.map_tree[ 'acn_' + acn_tree.id() ] =  node;
916                         if (params) {
917                                 for (var i in params) {
918                                         node.setAttribute(i,params[i]);
919                                 }
920                         }
921                         if (acn_tree.copies()) {
922                                 obj.map_acp[ 'acn_' + acn_tree.id() ] = acn_tree;
923                                 node.setAttribute('container','true');
924                         }
925
926                 } catch(E) {
927                         dump(E+'\n');
928                         alert(E);
929                 }
930         },
931
932         'append_acp' : function( acp_item, acn_tree, params ) {
933                 var obj = this;
934                 try {
935                         if (obj.map_tree[ 'acp_' + acp_item.id() ]) {
936                                 var x = obj.map_tree[ 'acp_' + acp_item.id() ];
937                                 if (params) {
938                                         for (var i in params) {
939                                                 x.setAttribute(i,params[i]);
940                                         }
941                                 }
942                                 return x;
943                         }
944
945                         var parent_node = obj.map_tree[ 'acn_' + acn_tree.id() ];
946                         var data = {
947                                 'row' : {
948                                         'my' : {
949                                                 'aou' : obj.data.hash.aou[ acn_tree.owning_lib() ],
950                                                 'acn' : acn_tree,
951                                                 'acp' : acp_item,
952                                                 'volume_count' : '',
953                                                 'copy_count' : '',
954                                         }
955                                 },
956                                 'retrieve_id' : 'acp_' + acp_item.id(),
957                                 'node' : parent_node,
958                         };
959                         var node = obj.list.append(data);
960                         obj.map_tree[ 'acp_' + acp_item.id() ] =  node;
961                         if (params) {
962                                 for (var i in params) {
963                                         node.setAttribute(i,params[i]);
964                                 }
965                         }
966
967                 } catch(E) {
968                         dump(E+'\n');
969                         alert(E);
970                 }
971         },
972
973         'list_init' : function( params ) {
974
975                 try {
976                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
977                         var obj = this;
978                         
979                         JSAN.use('circ.util');
980                         var columns = [
981                                 {
982                                         'id' : 'tree_location', 'label' : 'Location/Barcode', 'flex' : 1,
983                                         'primary' : true, 'hidden' : false, 
984                                         'render' : 'my.acp ? my.acp.barcode() : my.acn ? my.acn.label() : my.aou ? my.aou.shortname() + " : " + my.aou.name() : "???"'
985                                 },
986                                 {
987                                         'id' : 'volume_count', 'label' : 'Volumes', 'flex' : 0,
988                                         'primary' : false, 'hidden' : false, 
989                                         'render' : 'my.volume_count'
990                                 },
991                                 {
992                                         'id' : 'copy_count', 'label' : 'Copies', 'flex' : 0,
993                                         'primary' : false, 'hidden' : false, 
994                                         'render' : 'my.copy_count'
995                                 },
996                         ].concat(
997                                 circ.util.columns( 
998                                         { 
999                                                 'location' : { 'hidden' : false },
1000                                                 'circ_lib' : { 'hidden' : false },
1001                                                 'owning_lib' : { 'hidden' : false },
1002                                                 'call_number' : { 'hidden' : false },
1003                                                 'status' : { 'hidden' : false },
1004                                         },
1005                                         {
1006                                                 'just_these' : [
1007                                                         'owning_lib',
1008                                                         'circ_lib',
1009                                                         'call_number',
1010                                                         'copy_number',
1011                                                         'location',
1012                                                         'barcode',
1013                                                         'loan_duration',
1014                                                         'fine_level',
1015                                                         'circulate',
1016                                                         'holdable',
1017                                                         'opac_visible',
1018                                                         'ref',
1019                                                         'deposit',
1020                                                         'deposit_amount',
1021                                                         'price',
1022                                                         'circ_as_type',
1023                                                         'circ_modifier',
1024                                                         'status',
1025                                                         'alert_message',
1026                                                         'acp_id',
1027                                                 ]
1028                                         }
1029                                 )
1030                         );
1031                         JSAN.use('util.list'); obj.list = new util.list('copy_tree');
1032                         obj.list.init(
1033                                 {
1034                                         'columns' : columns,
1035                                         'map_row_to_column' : circ.util.std_map_row_to_column(' '),
1036                                         'retrieve_row' : function(params) {
1037
1038                                                 var row = params.row;
1039
1040                                                 var funcs = [];
1041                                         /*      
1042                                                 if (!row.my.mvr) funcs.push(
1043                                                         function() {
1044
1045                                                                 row.my.mvr = obj.network.request(
1046                                                                         api.MODS_SLIM_RECORD_RETRIEVE_VIA_COPY.app,
1047                                                                         api.MODS_SLIM_RECORD_RETRIEVE_VIA_COPY.method,
1048                                                                         [ row.my.circ.target_copy() ]
1049                                                                 );
1050
1051                                                         }
1052                                                 );
1053                                                 if (!row.my.acp) {
1054                                                         funcs.push(     
1055                                                                 function() {
1056
1057                                                                         row.my.acp = obj.network.request(
1058                                                                                 api.FM_ACP_RETRIEVE.app,
1059                                                                                 api.FM_ACP_RETRIEVE.method,
1060                                                                                 [ row.my.circ.target_copy() ]
1061                                                                         );
1062
1063                                                                         params.row_node.setAttribute( 'retrieve_id',row.my.acp.barcode() );
1064
1065                                                                 }
1066                                                         );
1067                                                 } else {
1068                                                         params.row_node.setAttribute( 'retrieve_id',row.my.acp.barcode() );
1069                                                 }
1070                                         */
1071                                                 funcs.push(
1072                                                         function() {
1073
1074                                                                 if (typeof params.on_retrieve == 'function') {
1075                                                                         params.on_retrieve(row);
1076                                                                 }
1077
1078                                                         }
1079                                                 );
1080
1081                                                 JSAN.use('util.exec'); var exec = new util.exec();
1082                                                 exec.on_error = function(E) {
1083                                                         var err = 'items chain: ' + js2JSON(E);
1084                                                         obj.error.sdump('D_ERROR',err);
1085                                                         return true; /* keep going */
1086                                                 }
1087                                                 exec.chain( funcs );
1088
1089                                                 return row;
1090                                         },
1091                                         'on_click' : function(ev) {
1092                                                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserRead');
1093                                                 var row = {}; var col = {}; var nobj = {};
1094                                                 obj.list.node.treeBoxObject.getCellAt(ev.clientX,ev.clientY,row,col,nobj);
1095                                                 if ((row.value == -1)||(nobj.value != 'twisty')) { return; }
1096                                                 var node = obj.list.node.contentView.getItemAtIndex(row.value);
1097                                                 var list = [ node.getAttribute('retrieve_id') ];
1098                                                 if (typeof obj.on_select == 'function') {
1099                                                         obj.on_select(list,true);
1100                                                 }
1101                                                 if (typeof window.xulG == 'object' && typeof window.xulG.on_select == 'function') {
1102                                                         window.xulG.on_select(list);
1103                                                 }
1104                                         },
1105                                         'on_select' : function(ev) {
1106                                                 JSAN.use('util.functional');
1107                                                 var sel = obj.list.retrieve_selection();
1108                                                 obj.sel_list = util.functional.map_list(
1109                                                         sel,
1110                                                         function(o) { return o.getAttribute('retrieve_id'); }
1111                                                 );
1112                                                 obj.toggle_actions();
1113                                                 if (typeof obj.on_select == 'function') {
1114                                                         obj.on_select(obj.sel_list);
1115                                                 }
1116                                                 if (typeof window.xulG == 'object' && typeof window.xulG.on_select == 'function') {
1117                                                         window.xulG.on_select(obj.sel_list);
1118                                                 }
1119                                         },
1120                                 }
1121                         );
1122
1123                         obj.controller.render();
1124
1125                 } catch(E) {
1126                         this.error.sdump('D_ERROR','cat.copy_browser.list_init: ' + E + '\n');
1127                         alert(E);
1128                 }
1129         },
1130
1131         'toggle_actions' : function() {
1132                 var obj = this;
1133                 try {
1134                         var found_aou = false; var found_acn = false; var found_acp = false;
1135                         for (var i = 0; i < obj.sel_list.length; i++) {
1136                                 var type = obj.sel_list[i].split(/_/)[0];
1137                                 switch(type) {
1138                                         case 'aou' : found_aou = true; break;
1139                                         case 'acn' : found_acn = true; break;
1140                                         case 'acp' : found_acp = true; break;
1141                                 }
1142                         }
1143                         obj.controller.view.cmd_add_items.setAttribute('disabled','true');
1144                         obj.controller.view.cmd_add_items_to_buckets.setAttribute('disabled','true');
1145                         obj.controller.view.cmd_edit_items.setAttribute('disabled','true');
1146                         obj.controller.view.cmd_delete_items.setAttribute('disabled','true');
1147                         obj.controller.view.cmd_print_spine_labels.setAttribute('disabled','true');
1148                         obj.controller.view.cmd_add_volumes.setAttribute('disabled','true');
1149                         obj.controller.view.cmd_edit_volumes.setAttribute('disabled','true');
1150                         obj.controller.view.cmd_delete_volumes.setAttribute('disabled','true');
1151                         obj.controller.view.cmd_mark_volume.setAttribute('disabled','true');
1152                         if (found_aou) {
1153                                 obj.controller.view.cmd_add_volumes.setAttribute('disabled','false');
1154                         }
1155                         if (found_acn) {
1156                                 obj.controller.view.cmd_edit_volumes.setAttribute('disabled','false');
1157                                 obj.controller.view.cmd_delete_volumes.setAttribute('disabled','false');
1158                                 obj.controller.view.cmd_mark_volume.setAttribute('disabled','false');
1159                                 obj.controller.view.cmd_add_items.setAttribute('disabled','false');
1160                         }
1161                         if (found_acp) {
1162                                 obj.controller.view.cmd_add_items_to_buckets.setAttribute('disabled','false');
1163                                 obj.controller.view.cmd_edit_items.setAttribute('disabled','false');
1164                                 obj.controller.view.cmd_delete_items.setAttribute('disabled','false');
1165                                 obj.controller.view.cmd_print_spine_labels.setAttribute('disabled','false');
1166                         }
1167                 } catch(E) {
1168                         obj.error.standard_unexpected_error_alert('Copy Browser Actions',E);
1169                 }
1170         },
1171
1172         'refresh_list' : function() { 
1173                 try {
1174                         var obj = this;
1175                         obj.list.clear();
1176                         obj.map_tree = {};
1177                         obj.map_acn = {};
1178                         obj.map_acp = {};
1179                         obj.org_ids = obj.network.simple_request('FM_AOU_IDS_RETRIEVE_VIA_RECORD_ID',[ obj.docid ]);
1180                         var org = obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ];
1181                         obj.show_libs( org );
1182                 } catch(E) {
1183                         this.error.standard_unexpected_error_alert('Problem refreshing the volume/copy tree.',E);
1184                 }
1185         },
1186 }
1187
1188 dump('exiting cat.copy_browser.js\n');