]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/catalog/app.js
webstaff: start work on authority linker modal
[Evergreen.git] / Open-ILS / web / js / ui / default / staff / cat / catalog / app.js
1 /**
2  * TPAC Frame App
3  *
4  * currently, this app doesn't use routes for each sub-ui, because 
5  * reloading the catalog each time is sloooow.  better so far to 
6  * swap out divs w/ ng-if / ng-show / ng-hide as needed.
7  *
8  */
9
10 angular.module('egCatalogApp', ['ui.bootstrap','ngRoute','egCoreMod','egGridMod', 'egMarcMod'])
11
12 .config(function($routeProvider, $locationProvider, $compileProvider) {
13     $locationProvider.html5Mode(true);
14     $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|blob):/); // grid export
15
16     var resolver = {delay : ['egCore','egStartup', function(egCore,  egStartup) {
17         egCore.env.classLoaders.aous = function() {
18             return egCore.org.settings([
19                 'cat.marc_control_number_identifier'
20             ]).then(function(settings) {
21                 // local settings are cached within egOrg.  Caching them
22                 // again in egEnv just simplifies the syntax for access.
23                 egCore.env.aous = settings;
24             });
25         }
26         egCore.env.loadClasses.push('aous');
27         return egStartup.go()
28     }]};
29
30     $routeProvider.when('/cat/catalog/index', {
31         templateUrl: './cat/catalog/t_catalog',
32         controller: 'CatalogCtrl',
33         resolve : resolver
34     });
35
36     $routeProvider.when('/cat/catalog/retrieve_by_id', {
37         templateUrl: './cat/catalog/t_retrieve_by_id',
38         controller: 'CatalogRecordRetrieve',
39         resolve : resolver
40     });
41
42     $routeProvider.when('/cat/catalog/retrieve_by_tcn', {
43         templateUrl: './cat/catalog/t_retrieve_by_tcn',
44         controller: 'CatalogRecordRetrieve',
45         resolve : resolver
46     });
47
48     // create some catalog page-specific mappings
49     $routeProvider.when('/cat/catalog/record/:record_id', {
50         templateUrl: './cat/catalog/t_catalog',
51         controller: 'CatalogCtrl',
52         resolve : resolver
53     });
54
55     // create some catalog page-specific mappings
56     $routeProvider.when('/cat/catalog/record/:record_id/:record_tab', {
57         templateUrl: './cat/catalog/t_catalog',
58         controller: 'CatalogCtrl',
59         resolve : resolver
60     });
61
62     $routeProvider.when('/cat/catalog/batchEdit', {
63         templateUrl: './cat/catalog/t_batchedit',
64         controller: 'BatchEditCtrl',
65         resolve : resolver
66     });
67
68     $routeProvider.when('/cat/catalog/batchEdit/:container_type/:container_id', {
69         templateUrl: './cat/catalog/t_batchedit',
70         controller: 'BatchEditCtrl',
71         resolve : resolver
72     });
73
74     $routeProvider.when('/cat/catalog/vandelay', {
75         templateUrl: './cat/catalog/t_vandelay',
76         controller: 'VandelayCtrl',
77         resolve : resolver
78     });
79
80     $routeProvider.when('/cat/catalog/verifyURLs', {
81         templateUrl: './cat/catalog/t_verifyurls',
82         controller: 'URLVerifyCtrl',
83         resolve : resolver
84     });
85
86     $routeProvider.when('/cat/catalog/manageAuthorities', {
87         templateUrl: './cat/catalog/t_manageauthorities',
88         controller: 'ManageAuthoritiesCtrl',
89         resolve : resolver
90     });
91
92     $routeProvider.when('/cat/catalog/authority/:authority_id/marc_edit', {
93         templateUrl: './cat/catalog/t_authority',
94         controller: 'AuthorityCtrl',
95         resolve : resolver
96     });
97
98     $routeProvider.otherwise({redirectTo : '/cat/catalog/index'});
99 })
100
101
102 /**
103  * */
104 .controller('CatalogRecordRetrieve',
105        ['$scope','$routeParams','$location','$q','egCore',
106 function($scope , $routeParams , $location , $q , egCore ) {
107
108     $scope.focusMe = true;
109
110     // jump to the patron checkout UI
111     function loadRecord(record_id) {
112         $location
113         .path('/cat/catalog/record/' + record_id);
114     }
115
116     $scope.submitId = function(args) {
117         $scope.recordNotFound = null;
118         if (!args.record_id) return;
119
120         // blur so next time it's set to true it will re-apply select()
121         $scope.selectMe = false;
122
123         return loadRecord(args.record_id);
124     }
125
126     $scope.submitTCN = function(args) {
127         $scope.recordNotFound = null;
128         $scope.moreRecordsFound = null;
129         if (!args.record_tcn) return;
130
131         // blur so next time it's set to true it will re-apply select()
132         $scope.selectMe = false;
133
134         // lookup TCN
135         egCore.net.request(
136             'open-ils.search',
137             'open-ils.search.biblio.tcn',
138             args.record_tcn)
139
140         .then(function(resp) { // get_barcodes
141
142             if (evt = egCore.evt.parse(resp)) {
143                 alert(evt); // FIXME
144                 return;
145             }
146
147             if (!resp.count) {
148                 $scope.recordNotFound = args.record_tcn;
149                 $scope.selectMe = true;
150                 return;
151             }
152
153             if (resp.count > 1) {
154                 $scope.moreRecordsFound = args.record_tcn;
155                 $scope.selectMe = true;
156                 return;
157             }
158
159             var record_id = resp.ids[0];
160             return loadRecord(record_id);
161         });
162     }
163
164 }])
165
166 .controller('CatalogCtrl',
167        ['$scope','$routeParams','$location','$window','$q','egCore','egHolds','egCirc',
168         'egGridDataProvider','egHoldGridActions','$timeout','holdingsSvc',
169 function($scope , $routeParams , $location , $window , $q , egCore , egHolds , egCirc, 
170          egGridDataProvider , egHoldGridActions , $timeout , holdingsSvc) {
171
172     // set record ID on page load if available...
173     $scope.record_id = $routeParams.record_id;
174
175     if ($routeParams.record_id) $scope.from_route = true;
176     else $scope.from_route = false;
177
178     // will hold a ref to the opac iframe
179     $scope.opac_iframe = null;
180     $scope.parts_iframe = null;
181
182     $scope.in_opac_call = false;
183     $scope.opac_call = function (opac_frame_function, force_opac_tab) {
184         if ($scope.opac_iframe) {
185             if (force_opac_tab) $scope.record_tab = 'catalog';
186             $scope.in_opac_call = true;
187             $scope.opac_iframe.dom.contentWindow[opac_frame_function]();
188         }
189     }
190
191     $scope.stop_unload = false;
192     $scope.$watch('stop_unload',
193         function(newVal, oldVal) {
194             if (newVal && newVal != oldVal && $scope.opac_iframe) {
195                 $($scope.opac_iframe.dom.contentWindow).on('beforeunload', function(){
196                     return 'There is unsaved data in this record.'
197                 });
198             } else {
199                 if ($scope.opac_iframe)
200                     $($scope.opac_iframe.dom.contentWindow).off('beforeunload');
201             }
202         }
203     );
204
205     // Set the "last bib" cookie, if we have that
206     if ($scope.record_id)
207         egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
208
209     // also set it when the iframe changes to a new record
210     $scope.handle_page = function(url) {
211
212         if (!url || url == 'about:blank') {
213             // nothing loaded.  If we already have a record ID, leave it.
214             return;
215         }
216
217         var match = url.match(/\/+opac\/+record\/+(\d+)/);
218         if (match) {
219             $scope.record_id = match[1];
220             egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
221             $scope.holdings_record_id_changed($scope.record_id);
222             init_parts_url();
223         } else {
224             delete $scope.record_id;
225             $scope.from_route = false;
226         }
227
228         // child scope is executing this function, so our digest doesn't fire ... thus,
229         $scope.$apply();
230
231         if (!$scope.in_opac_call) {
232             if ($scope.record_id) {
233                 $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
234                 tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
235             } else {
236                 tab = $routeParams.record_tab || 'catalog';
237             }
238             $scope.set_record_tab(tab);
239         } else {
240             $scope.in_opac_call = false;
241         }
242     }
243
244     // xulG catalog handlers
245     $scope.handlers = { }
246
247     // ------------------------------------------------------------------
248     // Holdings
249
250     $scope.holdingsGridControls = {};
251     $scope.holdingsGridDataProvider = egGridDataProvider.instance({
252         get : function(offset, count) {
253             return this.arrayNotifier(holdingsSvc.copies, offset, count);
254         }
255     });
256
257     // refresh the list of holdings when the record_id is changed.
258     $scope.holdings_record_id_changed = function(id) {
259         if ($scope.record_id != id) $scope.record_id = id;
260         console.log('record id changed to ' + id + ', loading new holdings');
261         holdingsSvc.fetch({
262             rid : $scope.record_id,
263             org : $scope.holdings_ou,
264             copy: $scope.holdings_show_copies,
265             vol : $scope.holdings_show_vols,
266             empty: $scope.holdings_show_empty
267         }).then(function() {
268             $scope.holdingsGridDataProvider.refresh();
269         });
270     }
271
272     // refresh the list of holdings when the filter lib is changed.
273     $scope.holdings_ou = egCore.org.get(egCore.auth.user().ws_ou());
274     $scope.holdings_ou_changed = function(org) {
275         $scope.holdings_ou = org;
276         holdingsSvc.fetch({
277             rid : $scope.record_id,
278             org : $scope.holdings_ou,
279             copy: $scope.holdings_show_copies,
280             vol : $scope.holdings_show_vols,
281             empty: $scope.holdings_show_empty
282         }).then(function() {
283             $scope.holdingsGridDataProvider.refresh();
284         });
285     }
286
287     $scope.holdings_cb_changed = function(cb,newVal,norefresh) {
288         $scope[cb] = newVal;
289         egCore.hatch.setItem('cat.' + cb, newVal);
290         if (!norefresh) holdingsSvc.fetch({
291             rid : $scope.record_id,
292             org : $scope.holdings_ou,
293             copy: $scope.holdings_show_copies,
294             vol : $scope.holdings_show_vols,
295             empty: $scope.holdings_show_empty
296         }).then(function() {
297             $scope.holdingsGridDataProvider.refresh();
298         });
299     }
300
301     egCore.hatch.getItem('cat.holdings_show_vols').then(function(x){
302         if (typeof x ==  'undefined') x = true;
303         $scope.holdings_cb_changed('holdings_show_vols',x,true);
304         $('#holdings_show_vols').prop('checked', x);
305     }).then(function(){
306         egCore.hatch.getItem('cat.holdings_show_copies').then(function(x){
307             if (typeof x ==  'undefined') x = true;
308             $scope.holdings_cb_changed('holdings_show_copies',x,true);
309             $('#holdings_show_copies').prop('checked', x);
310         }).then(function(){
311             egCore.hatch.getItem('cat.holdings_show_empty').then(function(x){
312                 if (typeof x ==  'undefined') x = true;
313                 $scope.holdings_cb_changed('holdings_show_empty',x);
314                 $('#holdings_show_empty').prop('checked', x);
315             })
316         })
317     });
318
319     $scope.holdings_checkbox_handler = function (item) {
320         $scope.holdings_cb_changed(item.checkbox,item.checked);
321     }
322
323     function gatherSelectedHoldingsIds () {
324         var cp_id_list = [];
325         angular.forEach(
326             $scope.holdingsGridControls.selectedItems(),
327             function (item) { cp_id_list = cp_id_list.concat(item.id_list) }
328         );
329         return cp_id_list;
330     }
331
332     spawnHoldingsEdit = function (hide_vols,hide_copies){
333         egCore.net.request(
334             'open-ils.actor',
335             'open-ils.actor.anon_cache.set_value',
336             null, 'edit-these-copies', {
337                 record_id: $scope.record_id,
338                 copies: gatherSelectedHoldingsIds(),
339                 hide_vols : hide_vols,
340                 hide_copies : hide_copies
341             }
342         ).then(function(key) {
343             if (key) {
344                 var url = egCore.env.basePath + 'cat/volcopy/' + key;
345                 $timeout(function() { $window.open(url, '_blank') });
346             } else {
347                 alert('Could not create anonymous cache key!');
348             }
349         });
350     }
351     $scope.selectedHoldingsVolCopyEdit = function () { spawnHoldingsEdit(false,false) }
352     $scope.selectedHoldingsVolEdit = function () { spawnHoldingsEdit(false,true) }
353     $scope.selectedHoldingsCopyEdit = function () { spawnHoldingsEdit(true,false) }
354
355     $scope.selectedHoldingsItemStatus = function (){
356         var url = egCore.env.basePath + 'cat/item/search/' + gatherSelectedHoldingsIds().join(',')
357         $timeout(function() { $window.open(url, '_blank') });
358     }
359
360     $scope.selectedHoldingsItemStatusDetail = function (){
361         angular.forEach(
362             gatherSelectedHoldingsIds(),
363             function (cid) {
364                 var url = egCore.env.basePath +
365                           'cat/item/' + cid;
366                 $timeout(function() { $window.open(url, '_blank') });
367             }
368         );
369     }
370
371     $scope.selectedHoldingsItemStatusTgrEvt = function (){
372         angular.forEach(
373             gatherSelectedHoldingsIds(),
374             function (cid) {
375                 var url = egCore.env.basePath +
376                           'cat/item/' + cid + '/triggered_events';
377                 $timeout(function() { $window.open(url, '_blank') });
378             }
379         );
380     }
381
382     $scope.selectedHoldingsItemStatusHolds = function (){
383         angular.forEach(
384             gatherSelectedHoldingsIds(),
385             function (cid) {
386                 var url = egCore.env.basePath +
387                           'cat/item/' + cid + '/holds';
388                 $timeout(function() { $window.open(url, '_blank') });
389             }
390         );
391     }
392
393     $scope.selectedHoldingsDamaged = function () {
394         egCirc.mark_damaged(gatherSelectedHoldingsIds()).then(function() {
395             holdingsSvc.fetch({
396                 rid : $scope.record_id,
397                 org : $scope.holdings_ou,
398                 copy: $scope.holdings_show_copies,
399                 vol : $scope.holdings_show_vols,
400                 empty: $scope.holdings_show_empty
401             }).then(function() {
402                 $scope.holdingsGridDataProvider.refresh();
403             });
404         });
405     }
406
407     $scope.selectedHoldingsMissing = function () {
408         egCirc.mark_missing(gatherSelectedHoldingsIds()).then(function() {
409             holdingsSvc.fetch({
410                 rid : $scope.record_id,
411                 org : $scope.holdings_ou,
412                 copy: $scope.holdings_show_copies,
413                 vol : $scope.holdings_show_vols,
414                 empty: $scope.holdings_show_empty
415             }).then(function() {
416                 $scope.holdingsGridDataProvider.refresh();
417             });
418         });
419     }
420
421
422     // ------------------------------------------------------------------
423     // Holds 
424     var provider = egGridDataProvider.instance({});
425     $scope.hold_grid_data_provider = provider;
426     $scope.grid_actions = egHoldGridActions;
427     $scope.grid_actions.refresh = function () { provider.refresh() };
428     $scope.hold_grid_controls = {};
429
430     var hold_ids = []; // current list of holds
431     function fetchHolds(offset, count) {
432         var ids = hold_ids.slice(offset, offset + count);
433         return egHolds.fetch_holds(ids).then(null, null,
434             function(hold_data) { 
435                 return hold_data;
436             }
437         );
438     }
439
440     provider.get = function(offset, count) {
441         if ($scope.record_tab != 'holds') return $q.when();
442         var deferred = $q.defer();
443         hold_ids = []; // no caching ATM
444
445         // fetch the IDs
446         egCore.net.request(
447             'open-ils.circ',
448             'open-ils.circ.holds.retrieve_all_from_title',
449             egCore.auth.token(), $scope.record_id, 
450             {pickup_lib : egCore.org.descendants($scope.pickup_ou.id(), true)}
451         ).then(
452             function(hold_data) {
453                 angular.forEach(hold_data, function(list, type) {
454                     hold_ids = hold_ids.concat(list);
455                 });
456                 fetchHolds(offset, count).then(
457                     deferred.resolve, null, deferred.notify);
458             }
459         );
460
461         return deferred.promise;
462     }
463
464     $scope.detail_view = function(action, user_data, items) {
465         if (h = items[0]) {
466             $scope.detail_hold_id = h.hold.id();
467         }
468     }
469
470     $scope.list_view = function(items) {
471          $scope.detail_hold_id = null;
472     }
473
474     // refresh the list of record holds when the pickup lib is changed.
475     $scope.pickup_ou = egCore.org.get(egCore.auth.user().ws_ou());
476     $scope.pickup_ou_changed = function(org) {
477         $scope.pickup_ou = org;
478         provider.refresh();
479     }
480
481     $scope.print_holds = function() {
482         var holds = [];
483         angular.forEach($scope.hold_grid_controls.allItems(), function(item) {
484             holds.push({
485                 hold : egCore.idl.toHash(item.hold),
486                 patron_last : item.patron_last,
487                 patron_alias : item.patron_alias,
488                 patron_barcode : item.patron_barcode,
489                 copy : egCore.idl.toHash(item.copy),
490                 volume : egCore.idl.toHash(item.volume),
491                 title : item.mvr.title(),
492                 author : item.mvr.author()
493             });
494         });
495
496         egCore.print.print({
497             context : 'receipt', 
498             template : 'holds_for_bib', 
499             scope : {holds : holds}
500         });
501     }
502
503     $scope.mark_hold_transfer_dest = function() {
504         egCore.hatch.setLocalItem(
505             'eg.circ.hold.title_transfer_target', $scope.record_id);
506     }
507
508     // UI presents this option as "all holds"
509     $scope.transfer_holds_to_marked = function() {
510         var hold_ids = $scope.hold_grid_controls.allItems().map(
511             function(hold_data) {return hold_data.hold.id()});
512         egHolds.transfer_to_marked_title(hold_ids);
513     }
514
515     // ------------------------------------------------------------------
516     // Initialize the selected tab
517
518     function init_cat_url() {
519         // Set the initial catalog URL.  This only happens once.
520         // The URL is otherwise generated through user navigation.
521         if ($scope.catalog_url) return; 
522
523         var url = $location.absUrl().replace(/\/staff.*/, '/opac/advanced');
524
525         // A record ID in the path indicates a request for the record-
526         // specific page.
527         if ($routeParams.record_id) {
528             url = url.replace(/advanced/, '/record/' + $scope.record_id);
529         }
530
531         $scope.catalog_url = url;
532     }
533
534     function init_parts_url() {
535         $scope.parts_url = $location
536             .absUrl()
537             .replace(
538                 /\/staff.*/,
539                 '/conify/global/biblio/monograph_part?r='+$scope.record_id
540             );
541     }
542
543     $scope.set_record_tab = function(tab) {
544         $scope.record_tab = tab;
545
546         switch(tab) {
547
548             case 'monoparts':
549                 init_parts_url();
550                 break;
551
552             case 'catalog':
553                 init_cat_url();
554                 break;
555
556             case 'holds':
557                 $scope.detail_hold_record_id = $scope.record_id; 
558                 // refresh the holds grid
559                 provider.refresh();
560                 break;
561         }
562     }
563
564     $scope.set_default_record_tab = function() {
565         egCore.hatch.setLocalItem(
566             'eg.cat.default_record_tab', $scope.record_tab);
567         $timeout(function(){$scope.default_tab = $scope.record_tab});
568     }
569
570     var tab;
571     if ($scope.record_id) {
572         $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
573         tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
574
575     } else {
576         tab = $routeParams.record_tab || 'catalog';
577     }
578     $scope.set_record_tab(tab);
579
580 }])
581
582 .controller('AuthorityCtrl',
583        ['$scope','$routeParams','$location','$window','$q','egCore',
584 function($scope , $routeParams , $location , $window , $q , egCore) {
585
586     // set record ID on page load if available...
587     $scope.authority_id = $routeParams.authority_id;
588
589     if ($routeParams.authority_id) $scope.from_route = true;
590     else $scope.from_route = false;
591
592     $scope.stop_unload = false;
593 }])
594
595 .controller('URLVerifyCtrl',
596        ['$scope','$location',
597 function($scope , $location) {
598     $scope.verifyurls_url = $location.absUrl().replace(/\/staff.*/, '/url_verify/sessions');
599 }])
600
601 .controller('VandelayCtrl',
602        ['$scope','$location',
603 function($scope , $location) {
604     $scope.vandelay_url = $location.absUrl().replace(/\/staff.*/, '/vandelay/vandelay');
605 }])
606
607 .controller('ManageAuthoritiesCtrl',
608        ['$scope','$location',
609 function($scope , $location) {
610     $scope.manageauthorities_url = $location.absUrl().replace(/\/staff.*/, '/cat/authority/list');
611 }])
612
613 .controller('BatchEditCtrl',
614        ['$scope','$location','$routeParams',
615 function($scope , $location , $routeParams) {
616     $scope.batchedit_url = $location.absUrl().replace(/\/eg.*/, '/opac/extras/merge_template');
617     if ($routeParams.container_type) {
618         switch ($routeParams.container_type) {
619             case 'bucket':
620                 $scope.batchedit_url += '?recordSource=b&containerid=' + $routeParams.container_id;
621                 break;
622             case 'record':
623                 $scope.batchedit_url += '?recordSource=r&recid=' + $routeParams.container_id;
624                 break;
625         };
626     }
627 }])
628
629  
630 .filter('boolText', function(){
631     return function (v) {
632         return v == 't';
633     }
634 })
635
636 .factory('holdingsSvc', 
637        ['egCore','$q',
638 function(egCore , $q) {
639
640     var service = {
641         ongoing : false,
642         copies : [], // record search results
643         index : 0, // search grid index
644         org : null,
645         rid : null
646     };
647
648     service.flesh = {   
649         flesh : 2, 
650         flesh_fields : {
651             acp : ['status','location'],
652             acn : ['prefix','suffix','copies']
653         }
654     }
655
656     // resolved with the last received copy
657     service.fetch = function(opts) {
658         if (service.ongoing) {
659             console.log('Skipping fetch, ongoing = true');
660             return $q.when();
661         }
662
663         var rid = opts.rid;
664         var org = opts.org;
665         var copy = opts.copy;
666         var vol = opts.vol;
667         var empty = opts.empty;
668
669         if (!rid) return $q.when();
670         if (!org) return $q.when();
671
672         service.ongoing = true;
673
674         service.rid = rid;
675         service.org = org;
676         service.copies = [];
677         service.index = 0;
678
679         var org_list = egCore.org.descendants(org.id(), true);
680         console.log('Holdings fetch with: rid='+rid+' org='+org_list+' copy='+copy+' vol='+vol+' empty='+empty);
681
682         return egCore.pcrud.search(
683             'acn',
684             {record : rid, owning_lib : org_list, deleted : 'f'},
685             service.flesh
686         ).then(
687             function() { // finished
688                 service.copies = service.copies.sort(
689                     function (a, b) {
690                         function compare_array (x, y, i) {
691                             if (x[i] && y[i]) { // both have values
692                                 if (x[i] == y[i]) { // need to look deeper
693                                     return compare_array(x, y, ++i);
694                                 }
695
696                                 if (x[i] < y[i]) { // x is first
697                                     return -1;
698                                 } else if (x[i] > y[i]) { // y is first
699                                     return 1;
700                                 }
701
702                             } else { // no orgs to compare ...
703                                 if (x[i]) return -1;
704                                 if (y[i]) return 1;
705                             }
706                             return 0;
707                         }
708
709                         var owner_order = compare_array(a.owner_list, b.owner_list, 0);
710                         if (!owner_order) {
711                             // now compare on CN label
712                             if (a.call_number.label < b.call_number.label) return -1;
713                             if (a.call_number.label > b.call_number.label) return 1;
714
715                             // try copy number
716                             if (a.copy_number < b.copy_number) return -1;
717                             if (a.copy_number > b.copy_number) return 1;
718
719                             // finally, barcode
720                             if (a.barcode < b.barcode) return -1;
721                             if (a.barcode > b.barcode) return 1;
722                         }
723                         return owner_order;
724                     }
725                 );
726
727                 // create a label using just the unique part of the owner list
728                 var index = 0;
729                 var prev_owner_list;
730                 angular.forEach(service.copies, function (cp) {
731                     if (!prev_owner_list) {
732                         cp.owner_label = cp.owner_list.join(' ... ');
733                     } else {
734                         var current_owner_list = cp.owner_list.slice();
735                         while (current_owner_list[1] && prev_owner_list[1] && current_owner_list[0] == prev_owner_list[0]) {
736                             current_owner_list.shift();
737                             prev_owner_list.shift();
738                         }
739                         cp.owner_label = current_owner_list.join(' ... ');
740                     }
741
742                     cp.index = index++;
743                     prev_owner_list = cp.owner_list.slice();
744                 });
745
746                 var new_list = service.copies;
747                 if (!copy || !vol) { // collapse copy rows, supply a count instead
748
749                     index = 0;
750                     var cp_list = [];
751                     var prev_key;
752                     var current_blob = {};
753                     angular.forEach(new_list, function (cp) {
754                         if (!prev_key) {
755                             prev_key = cp.owner_list.join('') + cp.call_number.label;
756                             if (cp.barcode) current_blob.copy_count = 1;
757                             current_blob.index = index++;
758                             current_blob.id_list = cp.id_list;
759                             current_blob.call_number = cp.call_number;
760                             current_blob.owner_list = cp.owner_list;
761                             current_blob.owner_label = cp.owner_label;
762                         } else {
763                             var current_key = cp.owner_list.join('') + cp.call_number.label;
764                             if (prev_key == current_key) { // collapse into current_blob
765                                 current_blob.copy_count++;
766                                 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
767                             } else {
768                                 current_blob.barcode = current_blob.copy_count;
769                                 cp_list.push(current_blob);
770                                 prev_key = current_key;
771                                 current_blob = {};
772                                 if (cp.barcode) current_blob.copy_count = 1;
773                                 current_blob.index = index++;
774                                 current_blob.id_list = cp.id_list;
775                                 current_blob.owner_label = cp.owner_label;
776                                 current_blob.call_number = cp.call_number;
777                                 current_blob.owner_list = cp.owner_list;
778                             }
779                         }
780                     });
781
782                     current_blob.barcode = current_blob.copy_count;
783                     cp_list.push(current_blob);
784                     new_list = cp_list;
785
786                     if (!vol) { // do the same for vol rows
787
788                         index = 0;
789                         var cn_list = [];
790                         prev_key = '';
791                         var current_blob = {};
792                         angular.forEach(cp_list, function (cp) {
793                             if (!prev_key) {
794                                 prev_key = cp.owner_list.join('');
795                                 current_blob.index = index++;
796                                 current_blob.id_list = cp.id_list;
797                                 current_blob.cn_count = 1;
798                                 current_blob.copy_count = cp.copy_count;
799                                 current_blob.owner_list = cp.owner_list;
800                                 current_blob.owner_label = cp.owner_label;
801                             } else {
802                                 var current_key = cp.owner_list.join('');
803                                 if (prev_key == current_key) { // collapse into current_blob
804                                     current_blob.cn_count++;
805                                     current_blob.copy_count += cp.copy_count;
806                                     current_blob.id_list = current_blob.id_list.concat(cp.id_list);
807                                 } else {
808                                     current_blob.barcode = current_blob.copy_count;
809                                     current_blob.call_number = { label : current_blob.cn_count };
810                                     cn_list.push(current_blob);
811                                     prev_key = current_key;
812                                     current_blob = {};
813                                     current_blob.index = index++;
814                                     current_blob.id_list = cp.id_list;
815                                     current_blob.owner_label = cp.owner_label;
816                                     current_blob.cn_count = 1;
817                                     current_blob.copy_count = cp.copy_count;
818                                     current_blob.owner_list = cp.owner_list;
819                                 }
820                             }
821                         });
822     
823                         current_blob.barcode = current_blob.copy_count;
824                         current_blob.call_number = { label : current_blob.cn_count };
825                         cn_list.push(current_blob);
826                         new_list = cn_list;
827     
828                     }
829                 }
830
831                 service.copies = new_list;
832                 service.ongoing = false;
833             },
834
835             null, // error
836
837             // notify reads the stream of copies, one at a time.
838             function(cn) {
839
840                 var copies = cn.copies();
841                 cn.copies([]);
842
843                 angular.forEach(copies, function (cp) {
844                     cp.call_number(cn);
845                 });
846
847                 var flat = egCore.idl.toHash(copies);
848                 if (flat[0]) {
849                     var owner = egCore.org.get(flat[0].call_number.owning_lib);
850
851                     var owner_name_list = [];
852                     while (owner.parent_ou()) { // we're going to skip the top of the tree...
853                         owner_name_list.unshift(owner.name());
854                         owner = egCore.org.get(owner.parent_ou());
855                     }
856
857                     angular.forEach(flat, function (cp) {
858                         cp.owner_list = owner_name_list;
859                         cp.id_list = [cp.id];
860                     });
861
862                     service.copies = service.copies.concat(flat);
863
864                     if (empty && flat.length == 0) {
865                         service.copies.push({
866                             owner_list : owner_name_list,
867                             call_number: egCore.idl.toHash(cn)
868                         });
869                     }
870                 }
871
872                 return cn;
873             }
874         );
875     }
876
877     return service;
878 }])
879
880