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