Retrieving the org tree via API instead of file to get .shortname() again. Eventuall...
[Evergreen.git] / Evergreen / staff_client / chrome / content / evergreen / cat / browse_list.js
1 // find_this_id = document id
2
3 var org_shortname2id = {};
4 var my_treerow;
5 var browse_meter_per = 0;
6 var orgs_with_copies = [];
7 var orgs_with_copies_hash = {};
8
9 function my_init() {
10         timer_init('cat');
11         mw.sdump('D_CAT','TESTING: browse_list.js: ' + mw.G['main_test_variable'] + '\n');
12         var tc = document.getElementById('browse_list_tree_children');
13         build_tree_branch(tc,mw.G['org_tree'],true);
14         my_treerow = document.getElementById( 'org_unit_' + mw.G.user_ou.id() ).firstChild;
15         document.getElementById('browse_meter').value = 0;
16         document.getElementById('browse_meter').setAttribute('real', '0.0');
17         gather_copies();
18         //populate_tree();
19 }
20
21 function refresh_browse_list() {
22         mw.sdump('D_CAT','=-=-=-=-=-=-=-=-=\n\n\nrefresh_browse_list()\n');
23         setTimeout(
24                 function() {
25                         // Prune tree
26                         var nl = document.getElementsByTagName('treeitem');
27                         for (var i = 0; i < nl.length; i++) {
28                                 var v_treeitem = nl[i];
29                                 if (v_treeitem.getAttribute('object_type') == 'volume') {
30                                         var org_treechildren = v_treeitem.parentNode;
31                                         var org_treeitem = org_treechildren.parentNode;
32                                         org_treeitem.removeChild(org_treechildren);
33                                         var org_treerow = org_treeitem.firstChild;
34                                         org_treerow.childNodes[1].setAttribute('label','0');
35                                         org_treerow.childNodes[2].setAttribute('label','0');
36                                 }
37                         }
38                         document.getElementById('browse_meter').value = 0;
39                         document.getElementById('browse_meter').setAttribute('real', '0.0');
40                         gather_copies();
41                 }, 0
42         );
43 }
44
45 function button_toggle_my_libraries(ev) {
46         // label = Hide My Libraries
47         // alt_label = Show My Libraries
48         var target = document.getElementById('browse_list_button1');
49         if (!target) { mw.sdump('D_CAT','eh?\n'); return; }
50         swap_attributes('browse_list_button1','label','alt_label');
51         var toggle = cycle_attribute( target,'toggle', [ '1', '2' ] );
52         for (var i in mw.G.my_orgs) {
53                 var lib = mw.G.my_orgs[i];
54                 var item = document.getElementById('org_unit_'+lib.id());
55                 if (item) {
56                         if (toggle == '2') {
57                                 hide_branch(item);
58                         } else {
59                                 unhide_branch(item);
60                         }
61                 }
62         }
63 }
64
65 function button_toggle_libraries(ev) {
66         // label = Show Other Libraries With Copies
67         // alt_label = Show Just My Libraries
68         var target = document.getElementById('browse_list_button2');
69         if (!target) { mw.sdump('D_CAT','eh?\n'); return; }
70         swap_attributes('browse_list_button2','label','alt_label');
71         var toggle = cycle_attribute( target,'toggle',['1','2'] );
72         if (toggle == '1') {
73                 mw.sdump('D_CAT','Showing just my libraries...\n');
74                 /*hide_branch( document.getElementById('org_unit_PINES') );
75                 for (var i = 0; i < mw.G.my_orgs.length; i++) {
76                         unhide_branch(
77                                 document.getElementById( 'org_unit_' + mw.G.my_orgs[i].id() )
78                         );
79                 }*/
80                 var nl = document.getElementsByTagName('treeitem');
81                 for (var i = 0; i < nl.length; i++) {
82                         var item = nl[i];
83                         if (item.getAttribute('id').substr(0,9) != 'org_unit_') continue;
84                         if (mw.G.my_orgs_hash[ item.getAttribute('myid') ]) {
85                                 //item.setAttribute('hidden','false');
86                                 unhide_branch(item);
87                         } else {
88                                 //item.setAttribute('hidden','true');
89                                 hide_branch(item);
90                         }
91                 }
92         } else {
93                 mw.sdump('D_CAT','Showing other libraries with copies...\n');
94                 var nl = document.getElementsByTagName('treeitem');
95                 for (var i = 0; i < nl.length; i++) {
96                         var item = nl[i];
97                         if (item.getAttribute('copies') == 'true') {
98                                 //item.setAttribute('hidden','false');
99                                 //mw.sdump('D_CAT','\tunhiding ' + item.getAttribute('id') + '\n');
100                                 unhide_branch(item);
101                         } else {
102                                 //item.setAttribute('hidden','true');
103                                 //mw.sdump('D_CAT','\thiding ' + item.getAttribute('id') + '\n');
104                                 //hide_branch(item);
105                         }
106                 }
107
108         }
109 }
110
111 function unhide_branch(item,do_open,do_copies) {
112         if (item.getAttribute('id') == 'org_unit_PINES') { return; }
113         //mw.sdump('D_CAT','Unhiding ' + item.getAttribute('id') + '\n');
114         item.setAttribute('hidden','false');
115         if (do_open) {
116                 item.setAttribute('open','true');
117         }
118         if (do_copies) {
119                 item.setAttribute('copies','true');
120         }
121         if (item.parentNode && item.parentNode.setAttribute) {
122                 var id = item.parentNode.getAttribute('id');
123                 if (id.substr(0,7) != 'browse_' && id != 'org_unit_PINES') {
124                         unhide_branch(item.parentNode,do_open,do_copies);
125                 }
126         }
127 }
128
129 function hide_branch(item,do_open,do_copies) {
130         if (item.getAttribute('id') == 'org_unit_PINES') { return; }
131         //mw.sdump('D_CAT','Hiding ' + item.getAttribute('id') + '\n');
132         item.setAttribute('hidden','true');
133         if (do_open) {
134                 item.setAttribute('open','false');
135         }
136         if (do_copies) {
137                 item.setAttribute('copies','true');
138         }
139         if (item.parentNode && item.parentNode.setAttribute) {
140                 var id = item.parentNode.getAttribute('id');
141                 if (id.substr(0,7) != 'browse_' && id != 'org_unit_PINES') {
142                         hide_branch(item.parentNode,do_open,do_copies);
143                 }
144         }
145 }
146
147 function gather_copies() {
148         // loop the libs
149         mw.sdump('D_CAT','Hello : ' + timer_elapsed('world') + '\n');
150 /*      user_async_request(
151                         'open-ils.cat',
152                         'open-ils.cat.asset.copy_tree.global.retrieve',
153                         [ mw.G['auth_ses'][0], find_this_id  ],
154                         gather_copies_callback
155                 );
156 */
157         var orgs_with_copies = [];
158         try {
159                 orgs_with_copies = user_request(
160                         'open-ils.cat',
161                         'open-ils.cat.actor.org_unit.retrieve_by_title',
162                         [ find_this_id ]
163                 )[0];
164         } catch(E) {
165                 handle_error(E);
166         }
167         for (var i = 0; i < orgs_with_copies.length; i++) {
168                 orgs_with_copies_hash[ orgs_with_copies[i] ] = true;
169         }
170         var bucket = new Array();
171         for (var i = 0; i < mw.G.my_orgs.length; i++ ) {
172                 //if (find_id_object_in_list( mw.G.aout_list, mw.G.my_orgs[i].ou_type() ).can_have_vols() == '0')
173                 if ( mw.G.aout_list[ mw.G.my_orgs[i].ou_type() ].can_have_vols() == '0')
174                         continue;
175                 if ( ! orgs_with_copies_hash[ mw.G.my_orgs[i].id() ] ) continue;
176                 bucket.push( mw.G.my_orgs[i].id() );
177         }
178         document.getElementById('browse_libs').setAttribute('value','Retrieving my copies...');
179         user_async_request(
180                 'open-ils.cat',
181                 'open-ils.cat.asset.copy_tree.retrieve',
182                 [ mw.G['auth_ses'][0], find_this_id, bucket ],
183                 function (request) {
184                         gather_copies_callback(request);
185                         document.getElementById('browse_libs').setAttribute('value','Retrieving copies from other libraries...');
186                         gather_other_copies();
187                 }
188         );
189         mw.sdump('D_CAT','((((((((((((((((( Count = ' + counter_incr('world') + '\n');
190 }
191
192 function gather_other_copies() {
193         var w_s = 10; var c_s = 0;
194         var bucket = new Array();
195
196         var nl = document.getElementsByTagName('treeitem');
197         for (var i = 0; i < nl.length; i++) {
198                 var item = nl[i];
199                 if (item.getAttribute('can_have_vols') == '0') continue;
200                 //if ( find_id_object_in_list( mw.G.my_orgs, item.getAttribute('myid') ) ) { continue; }
201                 if ( mw.G.my_orgs_hash[ item.getAttribute('myid') ] ) { continue; }
202                 if ( ! orgs_with_copies_hash[ item.getAttribute('myid') ] ) continue;
203                 bucket.push( item.getAttribute('myid') );
204                 if (++c_s >= w_s) {
205                         user_async_request(
206                                 'open-ils.cat',
207                                 'open-ils.cat.asset.copy_tree.retrieve',
208                                 [ mw.G['auth_ses'][0], find_this_id, bucket ],
209                                 gather_copies_callback
210                         );
211                         mw.sdump('D_CAT','((((((((((((((((( Count = ' + counter_incr('world') + '\n');
212                         bucket = new Array();
213                         c_s = 0;
214                 }
215         }
216         if (bucket.length > 0) {
217                 user_async_request(
218                         'open-ils.cat',
219                         'open-ils.cat.asset.copy_tree.retrieve',
220                         [ mw.G['auth_ses'][0], find_this_id, bucket ],
221                         gather_copies_callback
222                 );
223                 mw.sdump('D_CAT','((((((((((((((((( Count = ' + counter_incr('world') + '\n');
224         }
225         browse_meter_per = 100 / counter_peek('world');
226         mw.sdump('D_CAT',timer_elapsed('world') + ' : World : ' + timer_elapsed('gather') + '\n');
227 }
228
229 function find_my_treerow_index() {
230         var nl = document.getElementById('browse_list_tree').getElementsByTagName('treerow');
231         var count = 0;
232         //mw.sdump('D_CAT','find_my_treerow:  count = ' + count + '  nl.length = ' + nl.length + '\n');
233         for (var i = 0; i < nl.length; i++) {
234                 var row = nl[i];
235                 var item = row.parentNode;
236                 if (item.getAttribute('id') == 'org_unit_' + mw.G.user_ou.id() ) {
237                         return count;
238                 }
239                 var open_attr = item.getAttribute('open');
240                 var hidden_prop = item.hidden;
241                 //mw.sdump('D_CAT','id = ' + item.getAttribute('id') + '   hidden_attr = ' + hidden_attr + '   hidden_prop = ' + hidden_prop + '\n');
242                 if (hidden_prop.toString() == 'false' && open_attr.toString() == 'true') {
243                         count++;
244                 }
245         }
246         return 0;
247 }
248
249 function gather_copies_callback(request) {
250         mw.sdump('D_CAT','gather_copies_callback : ' + timer_elapsed('gather') + ' : ' + ' count = ' + counter_incr('gather') + '\n');
251         var result = request.getResultObject();
252         var n_volumes = 0; var n_copies = 0; var flag = false;
253         //mw.sdump('D_CAT','copybrowser result = ' + js2JSON(result) + '\n=-=-=\n');
254         for (var y in result) {
255                 if (result[y] && (typeof(result[y])=='object')) {
256                         var volume = result[y]; n_volumes++;
257                         //mw.sdump('D_CAT','\nvolume = ' + js2JSON(volume) + '\n');
258                         mw.sdump('D_CAT',' volume id = ' + volume.id() + '\n');
259                         var lib = find_ou( mw.G.org_tree, volume.owning_lib() );
260                         mw.sdump('D_CAT','+++++++++++++++++++++++++++++++++++++++++++++++++++++++lib = ' + js2JSON(lib) + '\n');
261                         if ( lib.id() == mw.G.user_ou.id() ) { flag = true; }
262                         var callnumber = volume.label();
263                         var copies = volume.copies();
264                         //mw.sdump('D_CAT','\tcopies = ' + js2JSON(copies) + '\n');
265                         var item = document.getElementById('org_unit_'+lib.id());
266                         if (!item) { mw.sdump('D_CAT','skipping\n'); continue; }
267
268                         var item_row = item.firstChild;
269                         var volumes_treecell;
270                         if (item_row.childNodes.length == 1) {
271                                 volumes_treecell = document.createElement('treecell');
272                                 volumes_treecell.setAttribute('label','0');
273                                 item_row.appendChild(volumes_treecell);
274                         } else {
275                                 volumes_treecell = item_row.childNodes[1];
276                         }
277                         volumes_treecell.setAttribute(
278                                 'label',
279                                 parseInt(volumes_treecell.getAttribute('label')) + 1
280                         );
281
282                         var copies_treecell;
283                         if (item_row.childNodes.length < 3) {
284                                 copies_treecell = document.createElement('treecell');
285                                 copies_treecell.setAttribute('label','0');
286                                 item_row.appendChild(copies_treecell);
287                         } else {
288                                 copies_treecell = item_row.childNodes[2];
289                         }
290                         copies_treecell.setAttribute(
291                                 'label',
292                                 parseInt(copies_treecell.getAttribute('label')) + copies.length
293                         );
294
295
296                         var cn_treechildren;
297                         var nl = item.getElementsByTagName('treechildren');
298                         //unhide_branch(item,false,true);
299                         item.setAttribute('copies',true);
300                         if (nl.length == 0) {
301                                 cn_treechildren = document.createElement('treechildren');
302                                 item.appendChild(cn_treechildren);
303                         } else {
304                                 cn_treechildren = nl[0];
305                         }
306
307                         var cn_treeitem = document.createElement('treeitem');
308                                 cn_treeitem.setAttribute('container','true');
309                                 cn_treeitem.setAttribute('open','true');
310                                 cn_treeitem.setAttribute('object_type','volume');
311                                 cn_treeitem.setAttribute('volume_id',volume.id());
312                                 cn_treeitem.setAttribute('callnumber',volume.label());
313                                 cn_treeitem.setAttribute('ou_id',lib.id());
314
315                         cn_treechildren.appendChild(cn_treeitem);
316
317                         var cn_treerow = document.createElement('treerow');
318                         cn_treeitem.appendChild(cn_treerow);
319
320                         var cn_treecell = document.createElement('treecell');
321                                 cn_treecell.setAttribute('label',callnumber);
322                         cn_treerow.appendChild(cn_treecell);
323
324                         var treechildren = document.createElement('treechildren');
325                         cn_treeitem.appendChild(treechildren);
326                         //mw.sdump('D_CAT', 'org_unit_'+lib+' item = '+item.tagName+'\n');
327                         for (var j = 0; j < copies.length; j++) {
328                                 var copy = copies[j]; n_copies++;
329                                 //mw.sdump('D_CAT','barcode: ' + copy.barcode() + '\n');
330
331                                 var treeitem = document.createElement('treeitem');
332                                         treeitem.setAttribute('open','true');
333                                         treeitem.setAttribute('container','true');
334                                         treeitem.setAttribute('class','barcode_row');
335                                         treeitem.setAttribute('object_type','copy');
336                                         treeitem.setAttribute('ou_shortname',lib.shortname());
337                                         treeitem.setAttribute('ou_id',lib.id());
338                                         treeitem.setAttribute('callnumber',callnumber);
339                                         treeitem.setAttribute('barcode',copy.barcode());
340                                         treeitem.setAttribute('copy_id',copy.id());
341                                         treeitem.setAttribute('volume_id',volume.id());
342                                         //treeitem.setAttribute('copy',js2JSON(copy));
343                                 treechildren.appendChild(treeitem);
344
345                                 var treerow = document.createElement('treerow');
346                                 treeitem.appendChild(treerow);
347
348                                 var list = [ 
349                                         copy.barcode() ,
350                                         '',
351                                         '',
352                                         lib.shortname() ,
353                                         callnumber ,
354                                         copy.copy_number() ,
355                                         copy.location(), //mw.G.acpl_hash[ copy.location() ].name() ,
356                                         copy.circ_lib(), //mw.G.org_tree_hash[ copy.circ_lib() ].shortname(),
357                                         yesno( copy.circulate() ) ,
358                                         yesno( copy.ref() ) ,
359                                         yesno( copy.opac_visible() ) ,
360                                         copy.circ_as_type() ,
361                                         copy.circ_modifier() ,
362                                         copy.loan_duration() ,
363                                         copy.fine_level() ,
364                                         copy.create_date() ,
365                                         copy.creator() ,
366                                         copy.edit_date() ,
367                                         copy.editor() ,
368                                         yesno( copy.deposit() ),
369                                         copy.deposit_amount() ,
370                                         copy.price() ,
371                                         mw.G.ccs_hash[ copy.status() ].name()
372                                 ];
373
374                                 for (var i = 0; i < list.length; i++ ) {
375                                         var treecell = document.createElement('treecell');
376                                                 treecell.setAttribute('label',list[i]);
377                                         treerow.appendChild(treecell);
378                                 }
379                                                 
380                         }
381                 } else {
382                         mw.sdump('D_CAT','gather_copies problem: ' + result[y] + '\n');
383                         //throw(result[0]);
384                 }
385         }
386         var tree = document.getElementById('browse_list_tree');
387         if (tree.currentIndex != -1) {
388                 //mw.sdump('D_CAT','currentIndex != -1 = ' + tree.currentIndex + '\n');
389                 //tree.treeBoxObject.scrollToRow( tree.currentIndex );
390                 tree.treeBoxObject.ensureRowIsVisible( tree.currentIndex );
391         } else if (flag) {
392                 //mw.sdump('D_CAT','currentIndex == -1\n');
393                 var find = find_my_treerow_index();
394                 mw.sdump('D_CAT','find = ' + find + ' n_volumes = ' + n_volumes + ' n_copies = ' + n_copies + '\n');
395                 //alert('find = ' + find + ' n_volumes = ' + n_volumes + ' n_copies = ' + n_copies + '\n');
396                 if (find > 0) { 
397                         find = find + n_volumes + n_copies;
398                         tree.view.selection.select( find ); 
399                 }
400                 //tree.treeBoxObject.ensureRowIsVisible( find );
401         }
402         var meter = document.getElementById('browse_meter');
403         var real = parseFloat( meter.getAttribute('real') ) + browse_meter_per;
404         meter.setAttribute('real',real);
405         meter.value = Math.ceil( real );
406         if ( counter_peek('gather') == counter_peek('world') ) {
407                 document.getElementById('browse_libs').setAttribute('value','Finished');
408                 document.getElementById('browse_libs').setAttribute('hidden','true');
409                 meter.value = 100;
410                 meter.setAttribute('hidden','true');
411         }
412         mw.sdump('D_CAT','gather callback   done : ' + timer_elapsed('gather') + '\n');
413 }
414
415
416 function build_tree_branch(treechildren,org,hide) {
417         var id = org.id();
418         var name = org.name();
419         var shortname = org.shortname(); org_shortname2id[shortname] = id;
420         var children = org.children();
421         var flag = mw.G.aout_hash[ org.ou_type() ].can_have_vols() == '1';
422         var item = make_treeitem('org_unit_' + id, name, flag);
423         item.setAttribute('hidden',hide);
424         item.setAttribute('ou_id',org.id());
425         //if ( find_id_object_in_list( mw.G.my_orgs, id ) ) { 
426         if ( mw.G.my_orgs_hash[ id ] ) {
427                 item.setAttribute('open','true'); 
428                 item.setAttribute('hidden','false');
429                 item.setAttribute('myorg','true');
430         }
431         item.setAttribute( 
432                 'can_have_vols', 
433                 //find_id_object_in_list( mw.G.aout_list, org.ou_type() ).can_have_vols() 
434                 mw.G.aout_hash[ org.ou_type() ].can_have_vols()
435         );
436         item.setAttribute( 'myid', org.id() );
437         if (children && (children.length > 0)) {
438                 var n_treechildren = document.createElement('treechildren');
439                 for (var i in children) {
440                         var child = children[i];
441                         build_tree_branch(n_treechildren, child, true);
442                 }
443                 item.appendChild(n_treechildren);
444         }
445         treechildren.appendChild(item);
446 }
447
448 function make_treeitem(id,name,flag) {
449         var treeitem = document.createElement('treeitem');
450                 treeitem.setAttribute('id',id);
451                 treeitem.setAttribute('container','true');
452         var treerow = make_treerow(name,flag);
453                 treeitem.appendChild(treerow);
454                 treeitem.setAttribute('object_type','org_unit');
455         return treeitem;
456 }
457
458 function make_treerow(name,flag) {
459         var treerow = document.createElement('treerow');
460         var treecell = document.createElement('treecell');
461                 treecell.setAttribute('label',name);
462         treerow.appendChild(treecell);
463         treecell = document.createElement('treecell');
464                 if (flag) treecell.setAttribute('label',0);
465         treerow.appendChild(treecell);
466         treecell = document.createElement('treecell');
467                 if (flag) treecell.setAttribute('label',0);
468         treerow.appendChild(treecell);
469
470         return treerow;
471 }
472
473 function get_selected_rows_by_object_type(tree,object_type) {
474         var items = get_list_from_tree_selection(tree);
475         var command = "filter_list(items, function (obj) { return obj.getAttribute('object_type') == '" + object_type + "'; } );";
476         return eval(command);
477 }
478
479 function build_CopyBrowser_Context(ev) {
480         mw.sdump('D_CAT','build_CopyBrowser_Context: ev.target.tagName = ' + ev.target.tagName + '\n');
481         mw.sdump('D_CAT','this = ' + this.tagName + '\n');
482
483         var popup = document.getElementById('browse_menu');
484         empty_widget(popup);
485
486         var volume_flag = 0; var copy_flag = 0; var library_flag = 0;
487
488         var hitlist = document.getElementById('browse_list_tree');
489         var start = new Object(); var end = new Object();
490         var numRanges = hitlist.view.selection.getRangeCount();
491         for (var t=0; t<numRanges; t++){
492                 hitlist.view.selection.getRangeAt(t,start,end);
493                 for (var v=start.value; v<=end.value; v++){
494                         var i = hitlist.contentView.getItemAtIndex(v);
495                         //mw.sdump('D_CAT',i + ':' + i.firstChild.childNodes.length + '\n');
496                         switch( i.getAttribute('object_type') ) {
497                                 case 'volume' : volume_flag++; break;
498                                 case 'copy' : copy_flag++; break;
499                                 case 'org_unit' : if (i.getAttribute('can_have_vols') == '1') library_flag++; break;
500                         }
501                 }
502         }
503         mw.sdump('D_CAT','volume_flag = ' + volume_flag + ' copy_flag = ' + copy_flag + ' library_flag = ' + library_flag + '\n');
504         if (library_flag > 0) {
505                 var menuitem = document.createElement('menuitem');
506                 popup.appendChild(menuitem);
507                         menuitem.setAttribute('label','Add Volumes');
508                         menuitem.setAttribute('command','cmd_volume_add');
509                 popup.appendChild( document.createElement( 'menuseparator' ) );
510         }
511         if (volume_flag > 0) {
512                 var menuitem = document.createElement('menuitem');
513                 popup.appendChild(menuitem);
514                         if (volume_flag > 1) {
515                                 menuitem.setAttribute('label','Edit Volumes');
516                         } else {
517                                 menuitem.setAttribute('label','Edit Volume');
518                         }
519                         menuitem.setAttribute('command','cmd_volume_edit');
520                 popup.appendChild( document.createElement( 'menuseparator' ) );
521                 menuitem = document.createElement('menuitem');
522                 popup.appendChild(menuitem);
523                         menuitem.setAttribute('label','Add Copies');
524                         menuitem.setAttribute('command','cmd_copy_add');
525                 menuitem = document.createElement('menuitem');
526                 popup.appendChild( document.createElement( 'menuseparator' ) );
527         }
528         if (copy_flag > 0) {
529                 var menuitem = document.createElement('menuitem');
530                 popup.appendChild(menuitem);
531                         if (copy_flag > 1) {
532                                 menuitem.setAttribute('label','Edit Copies');
533                         } else {
534                                 menuitem.setAttribute('label','Edit Copy');
535                         }
536                         menuitem.setAttribute('command','cmd_copy_edit');
537                 popup.appendChild( document.createElement( 'menuseparator' ) );
538         }
539         if (volume_flag > 0) {
540                 var menuitem = document.createElement('menuitem');
541                 popup.appendChild(menuitem);
542                         if (volume_flag > 1) {
543                                 menuitem.setAttribute('label','Delete Volumes');
544                         } else {
545                                 menuitem.setAttribute('label','Delete Volume');
546                         }
547                         menuitem.setAttribute('command','cmd_volume_delete');
548                 popup.appendChild( document.createElement( 'menuseparator' ) );
549
550         }
551         if (copy_flag > 0) {
552                 var menuitem = document.createElement('menuitem');
553                 popup.appendChild(menuitem);
554                         if (copy_flag > 1) {
555                                 menuitem.setAttribute('label','Delete Copies');
556                         } else {
557                                 menuitem.setAttribute('label','Delete Copy');
558                         }
559                         menuitem.setAttribute('command','cmd_copy_delete');
560                 popup.appendChild( document.createElement( 'menuseparator' ) );
561         }
562
563         var menuitem = document.createElement('menuitem');
564         popup.appendChild(menuitem);
565                 menuitem.setAttribute('label','Refresh Listing');
566                 menuitem.setAttribute('command','cmd_refresh');
567 }
568
569 function volume_add(tab,params) {
570         mw.sdump('D_CAT','trying to volume_add()\n');
571         params['shortcut'] = 'volume_add';
572         var w;
573         //var items = get_selected_rows(tree);
574         var items = get_list_from_tree_selection(params.tree);
575         items = filter_list(
576                 items,
577                 function (obj) {
578                         return obj.getAttribute('object_type') == 'org_unit';
579                 }
580         );
581         var chrome = 'chrome://evergreen/content/cat/volume_copy_add_wizard.xul';
582         if (tab) {
583                 parentWindow.new_tab('main_tabbox');
584                 w = parentWindow.replace_tab('main_tabbox','Add Volume',chrome);
585         } else {
586                 w = mw.new_window( chrome );
587         }
588         mw.sdump('D_CAT','setting use_this_tree\n');
589         w.tree_items = items;
590         w.record_id = find_this_id;
591         w.params = params;
592 }
593
594 function copy_add(tab,params) {
595         mw.sdump('D_CAT','trying to copy_add()\n');
596         params['shortcut'] = 'copy_add';
597         var w;
598         //var items = get_selected_rows(tree);
599         var items = get_list_from_tree_selection(params.tree);
600         items = filter_list(
601                 items,
602                 function (obj) {
603                         return obj.getAttribute('object_type') == 'volume';
604                 }
605         );
606         var chrome = 'chrome://evergreen/content/cat/copy_add_wizard.xul';
607         if (tab) {
608                 parentWindow.new_tab('main_tabbox');
609                 w = parentWindow.replace_tab('main_tabbox','Add Copy',chrome);
610         } else {
611                 w = mw.new_window( chrome );
612         }
613         mw.sdump('D_CAT','setting use_this_tree\n');
614         w.tree_items = items;
615         w.record_id = find_this_id;
616         w.params = params;
617 }
618
619 function volume_edit(tab,params) {
620         mw.sdump('D_CAT','trying to volume_edit()\n');
621         params['shortcut'] = 'volume_edit';
622         var w;
623         //var items = get_selected_rows(tree);
624         var items = get_list_from_tree_selection(params.tree);
625         items = filter_list(
626                 items,
627                 function (obj) {
628                         return obj.getAttribute('object_type') == 'volume';
629                 }
630         );
631         var chrome = 'chrome://evergreen/content/cat/volume_edit_wizard.xul';
632         if (tab) {
633                 parentWindow.new_tab('main_tabbox');
634                 w = parentWindow.replace_tab('main_tabbox','Edit Volumes',chrome);
635         } else {
636                 w = mw.new_window( chrome );
637         }
638         mw.sdump('D_CAT','setting use_this_tree\n');
639         w.tree_items = items;
640         w.record_id = find_this_id;
641         w.params = params;
642
643 }
644
645 function volume_delete(tab,params) {
646         var volumes = get_selected_rows_by_object_type(params.tree,'volume'); 
647         var cn_list = [];
648         for (var i = 0; i < volumes.length; i++) {
649                 var cn = new acn();
650                 cn.id( volumes[i].getAttribute('volume_id') );
651                 cn.isdeleted( '1' );
652                 cn_list.push( cn );
653         }
654         try {
655                 var result = user_request(
656                                 'open-ils.cat',
657                                 'open-ils.cat.asset.volume_tree.fleshed.batch.update',
658                                 [ mw.G['auth_ses'][0], cn_list]
659                 );
660                 mw.sdump('D_CAT','volume_tree.fleshed.batch.update result: ' + js2JSON(result) + '\n');
661         } catch(E) {
662                 handle_error(E);
663         }
664         refresh_browse_list();
665 }
666
667 function copy_delete(tab,params) {
668         var copies = get_selected_rows_by_object_type(params.tree,'copy'); 
669         var cn_list = [];
670         for (var i = 0; i < copies.length; i++) {
671                 var cn = new acn();
672                 cn.id( copies[i].getAttribute('volume_id') );
673
674                 var cp = new acp();
675                 cp.id( copies[i].getAttribute('copy_id') );
676                 cp.isdeleted( '1' );
677
678                 cn.copies( [ cp ] );
679                 cn_list.push( cn );
680         }
681         try {
682                 var result = user_request(
683                                 'open-ils.cat',
684                                 'open-ils.cat.asset.volume_tree.fleshed.batch.update',
685                                 [ mw.G['auth_ses'][0], cn_list]
686                 );
687                 mw.sdump('D_CAT','volume_tree.fleshed.batch.update result: ' + js2JSON(result) + '\n');
688         } catch(E) {
689                 handle_error(E);
690         }
691         refresh_browse_list();
692 }