]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/catalog/app.js
webstaff: Add actions for jumping to item status
[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',
151         'egGridDataProvider','egHoldGridActions','$timeout','holdingsSvc',
152 function($scope , $routeParams , $location , $window , $q , egCore , egHolds, 
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             init_parts_url();
205         } else {
206             delete $scope.record_id;
207             $scope.from_route = false;
208         }
209
210         // child scope is executing this function, so our digest doesn't fire ... thus,
211         $scope.$apply();
212
213         if (!$scope.in_opac_call) {
214             if ($scope.record_id) {
215                 $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
216                 tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
217             } else {
218                 tab = $routeParams.record_tab || 'catalog';
219             }
220             $scope.set_record_tab(tab);
221         } else {
222             $scope.in_opac_call = false;
223         }
224     }
225
226     // xulG catalog handlers
227     $scope.handlers = { }
228
229     // ------------------------------------------------------------------
230     // Holdings
231
232     $scope.holdingsGridControls = {};
233     $scope.holdingsGridDataProvider = egGridDataProvider.instance({
234         get : function(offset, count) {
235             return this.arrayNotifier(holdingsSvc.copies, offset, count);
236         }
237     });
238
239     // refresh the list of holdings when the filter lib is changed.
240     $scope.holdings_ou = egCore.org.get(egCore.auth.user().ws_ou());
241     $scope.holdings_ou_changed = function(org) {
242         $scope.holdings_ou = org;
243         holdingsSvc.fetch({
244             rid : $scope.record_id,
245             org : $scope.holdings_ou,
246             copy: $scope.holdings_show_copies,
247             vol : $scope.holdings_show_vols,
248             empty: $scope.holdings_show_empty
249         }).then(function() {
250             $scope.holdingsGridDataProvider.refresh();
251         });
252     }
253
254     $scope.holdings_show_copies_changed = function(newVal) {
255         $scope.holdings_show_copies = newVal;
256         egCore.hatch.setItem('cat.holdings.show_copies', newVal);
257         holdingsSvc.fetch({
258             rid : $scope.record_id,
259             org : $scope.holdings_ou,
260             copy: $scope.holdings_show_copies,
261             vol : $scope.holdings_show_vols,
262             empty: $scope.holdings_show_empty
263         }).then(function() {
264             $scope.holdingsGridDataProvider.refresh();
265         });
266     }
267
268     $scope.holdings_show_vols_changed = function(newVal) {
269         $scope.holdings_show_vols = newVal;
270         egCore.hatch.setItem('cat.holdings.show_vols', newVal);
271         holdingsSvc.fetch({
272             rid : $scope.record_id,
273             org : $scope.holdings_ou,
274             copy: $scope.holdings_show_copies,
275             vol : $scope.holdings_show_vols,
276             empty: $scope.holdings_show_empty
277         }).then(function() {
278             $scope.holdingsGridDataProvider.refresh();
279         });
280     }
281
282     $scope.holdings_show_empty_changed = function(newVal) {
283         $scope.holdings_show_empty = newVal;
284         egCore.hatch.setItem('cat.holdings.show_empty', newVal);
285         holdingsSvc.fetch({
286             rid : $scope.record_id,
287             org : $scope.holdings_ou,
288             copy: $scope.holdings_show_copies,
289             vol : $scope.holdings_show_vols,
290             empty: $scope.holdings_show_empty
291         }).then(function() {
292             $scope.holdingsGridDataProvider.refresh();
293         });
294     }
295
296     egCore.hatch.getItem('cat.holdings.show_copies').then(function(x){
297         if (typeof x ==  'undefined') x = true;
298         $scope.holdings_show_copies = x;
299     });
300
301     egCore.hatch.getItem('cat.holdings.show_vols').then(function(x){
302         if (typeof x ==  'undefined') x = true;
303         $scope.holdings_show_vols = x;
304     });
305
306     egCore.hatch.getItem('cat.holdings.show_emtpy').then(function(x){
307         if (typeof x ==  'undefined') x = false;
308         $scope.holdings_show_empty = x;
309     });
310
311     $scope.holdings_checkbox_handler = function (item) {
312         $scope[item.checkbox] = item.checked;
313         $scope[item.checkbox + '_changed'](item.checked);
314     }
315
316     $scope.selectedHoldingsItemStatus = function (){
317         var cp_id_list = [];
318         angular.forEach(
319             $scope.holdingsGridControls.selectedItems(),
320             function (item) { cp_id_list = cp_id_list.concat(item.id_list) }
321         );
322         var url = egCore.env.basePath + 'cat/item/search/' + cp_id_list.join(',')
323         $timeout(function() { $window.open(url, '_blank') });
324     }
325
326     $scope.selectedHoldingsItemStatusDetail = function (){
327         var cp_id_list = [];
328         angular.forEach(
329             $scope.holdingsGridControls.selectedItems(),
330             function (item) {
331                 angular.forEach(
332                     item.id_list,
333                     function (cid) {
334                         var url = egCore.env.basePath +
335                                   'cat/item/' + cid;
336                         $timeout(function() { $window.open(url, '_blank') });
337                     }
338                 )
339             }
340         );
341     }
342
343     $scope.selectedHoldingsItemStatusTgrEvt = function (){
344         var cp_id_list = [];
345         angular.forEach(
346             $scope.holdingsGridControls.selectedItems(),
347             function (item) {
348                 angular.forEach(
349                     item.id_list,
350                     function (cid) {
351                         var url = egCore.env.basePath +
352                                   'cat/item/' + cid + '/triggered_events';
353                         $timeout(function() { $window.open(url, '_blank') });
354                     }
355                 )
356             }
357         );
358     }
359
360
361     // ------------------------------------------------------------------
362     // Holds 
363     var provider = egGridDataProvider.instance({});
364     $scope.hold_grid_data_provider = provider;
365     $scope.grid_actions = egHoldGridActions;
366     $scope.grid_actions.refresh = function () { provider.refresh() };
367     $scope.hold_grid_controls = {};
368
369     var hold_ids = []; // current list of holds
370     function fetchHolds(offset, count) {
371         var ids = hold_ids.slice(offset, offset + count);
372         return egHolds.fetch_holds(ids).then(null, null,
373             function(hold_data) { 
374                 return hold_data;
375             }
376         );
377     }
378
379     provider.get = function(offset, count) {
380         if ($scope.record_tab != 'holds') return $q.when();
381         var deferred = $q.defer();
382         hold_ids = []; // no caching ATM
383
384         // fetch the IDs
385         egCore.net.request(
386             'open-ils.circ',
387             'open-ils.circ.holds.retrieve_all_from_title',
388             egCore.auth.token(), $scope.record_id, 
389             {pickup_lib : egCore.org.descendants($scope.pickup_ou.id(), true)}
390         ).then(
391             function(hold_data) {
392                 angular.forEach(hold_data, function(list, type) {
393                     hold_ids = hold_ids.concat(list);
394                 });
395                 fetchHolds(offset, count).then(
396                     deferred.resolve, null, deferred.notify);
397             }
398         );
399
400         return deferred.promise;
401     }
402
403     $scope.detail_view = function(action, user_data, items) {
404         if (h = items[0]) {
405             $scope.detail_hold_id = h.hold.id();
406         }
407     }
408
409     $scope.list_view = function(items) {
410          $scope.detail_hold_id = null;
411     }
412
413     // refresh the list of record holds when the pickup lib is changed.
414     $scope.pickup_ou = egCore.org.get(egCore.auth.user().ws_ou());
415     $scope.pickup_ou_changed = function(org) {
416         $scope.pickup_ou = org;
417         provider.refresh();
418     }
419
420     $scope.print_holds = function() {
421         var holds = [];
422         angular.forEach($scope.hold_grid_controls.allItems(), function(item) {
423             holds.push({
424                 hold : egCore.idl.toHash(item.hold),
425                 patron_last : item.patron_last,
426                 patron_alias : item.patron_alias,
427                 patron_barcode : item.patron_barcode,
428                 copy : egCore.idl.toHash(item.copy),
429                 volume : egCore.idl.toHash(item.volume),
430                 title : item.mvr.title(),
431                 author : item.mvr.author()
432             });
433         });
434
435         egCore.print.print({
436             context : 'receipt', 
437             template : 'holds_for_bib', 
438             scope : {holds : holds}
439         });
440     }
441
442     $scope.mark_hold_transfer_dest = function() {
443         egCore.hatch.setLocalItem(
444             'eg.circ.hold.title_transfer_target', $scope.record_id);
445     }
446
447     // UI presents this option as "all holds"
448     $scope.transfer_holds_to_marked = function() {
449         var hold_ids = $scope.hold_grid_controls.allItems().map(
450             function(hold_data) {return hold_data.hold.id()});
451         egHolds.transfer_to_marked_title(hold_ids);
452     }
453
454     // ------------------------------------------------------------------
455     // Initialize the selected tab
456
457     function init_cat_url() {
458         // Set the initial catalog URL.  This only happens once.
459         // The URL is otherwise generated through user navigation.
460         if ($scope.catalog_url) return; 
461
462         var url = $location.absUrl().replace(/\/staff.*/, '/opac/advanced');
463
464         // A record ID in the path indicates a request for the record-
465         // specific page.
466         if ($routeParams.record_id) {
467             url = url.replace(/advanced/, '/record/' + $scope.record_id);
468         }
469
470         $scope.catalog_url = url;
471     }
472
473     function init_parts_url() {
474         $scope.parts_url = $location
475             .absUrl()
476             .replace(
477                 /\/staff.*/,
478                 '/conify/global/biblio/monograph_part?r='+$scope.record_id
479             );
480     }
481
482     $scope.set_record_tab = function(tab) {
483         $scope.record_tab = tab;
484
485         switch(tab) {
486
487             case 'monoparts':
488                 init_parts_url();
489                 break;
490
491             case 'catalog':
492                 init_cat_url();
493                 break;
494
495             case 'holds':
496                 $scope.detail_hold_record_id = $scope.record_id; 
497                 // refresh the holds grid
498                 provider.refresh();
499                 break;
500         }
501     }
502
503     $scope.set_default_record_tab = function() {
504         egCore.hatch.setLocalItem(
505             'eg.cat.default_record_tab', $scope.record_tab);
506         $timeout(function(){$scope.default_tab = $scope.record_tab});
507     }
508
509     var tab;
510     if ($scope.record_id) {
511         $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
512         tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
513
514
515         $timeout(function(){
516             holdingsSvc.fetch({
517                 rid : $scope.record_id,
518                 org : $scope.holdings_ou,
519                 copy: $scope.holdings_show_copies,
520                 vol : $scope.holdings_show_vols,
521                 empty: $scope.holdings_show_empty
522             }).then(function() {
523                 $scope.holdingsGridDataProvider.refresh();
524             });
525         });
526
527     } else {
528         tab = $routeParams.record_tab || 'catalog';
529     }
530     $scope.set_record_tab(tab);
531
532 }])
533
534 .controller('URLVerifyCtrl',
535        ['$scope','$location',
536 function($scope , $location) {
537     $scope.verifyurls_url = $location.absUrl().replace(/\/staff.*/, '/url_verify/sessions');
538 }])
539
540 .controller('VandelayCtrl',
541        ['$scope','$location',
542 function($scope , $location) {
543     $scope.vandelay_url = $location.absUrl().replace(/\/staff.*/, '/vandelay/vandelay');
544 }])
545
546 .controller('ManageAuthoritiesCtrl',
547        ['$scope','$location',
548 function($scope , $location) {
549     $scope.manageauthorities_url = $location.absUrl().replace(/\/staff.*/, '/cat/authority/list');
550 }])
551
552 .controller('BatchEditCtrl',
553        ['$scope','$location','$routeParams',
554 function($scope , $location , $routeParams) {
555     $scope.batchedit_url = $location.absUrl().replace(/\/eg.*/, '/opac/extras/merge_template');
556     if ($routeParams.container_type) {
557         switch ($routeParams.container_type) {
558             case 'bucket':
559                 $scope.batchedit_url += '?recordSource=b&containerid=' + $routeParams.container_id;
560                 break;
561             case 'record':
562                 $scope.batchedit_url += '?recordSource=r&recid=' + $routeParams.container_id;
563                 break;
564         };
565     }
566 }])
567
568  
569 .filter('boolText', function(){
570     return function (v) {
571         return v == 't';
572     }
573 })
574
575 .factory('holdingsSvc', 
576        ['egCore','$q',
577 function(egCore , $q) {
578
579     var service = {
580         ongoing : false,
581         copies : [], // record search results
582         index : 0, // search grid index
583         org : null,
584         rid : null
585     };
586
587     service.flesh = {   
588         flesh : 2, 
589         flesh_fields : {
590             acp : ['status','location'],
591             acn : ['prefix','suffix','copies']
592         }
593     }
594
595     // resolved with the last received copy
596     service.fetch = function(opts) {
597         if (service.ongoing) return $q.when();
598
599         var rid = opts.rid;
600         var org = opts.org;
601         var copy = opts.copy;
602         var vol = opts.vol;
603         var empty = opts.empty;
604
605         if (!rid) return $q.when();
606         if (!org) return $q.when();
607
608         service.ongoing = true;
609
610         service.rid = rid;
611         service.org = org;
612         service.copies = [];
613         service.index = 0;
614
615         var org_list = egCore.org.descendants(org.id(), true);
616
617         return egCore.pcrud.search(
618             'acn',
619             {record : rid, owning_lib : org_list, deleted : 'f'},
620             service.flesh
621         ).then(
622             function() { // finished
623                 service.copies = service.copies.sort(
624                     function (a, b) {
625                         function compare_array (x, y, i) {
626                             if (x[i] && y[i]) { // both have values
627                                 if (x[i] == y[i]) { // need to look deeper
628                                     return compare_array(x, y, ++i);
629                                 }
630
631                                 if (x[i] < y[i]) { // x is first
632                                     return -1;
633                                 } else if (x[i] > y[i]) { // y is first
634                                     return 1;
635                                 }
636
637                             } else { // no orgs to compare ...
638                                 if (x[i]) return -1;
639                                 if (y[i]) return 1;
640                             }
641                             return 0;
642                         }
643
644                         var owner_order = compare_array(a.owner_list, b.owner_list, 0);
645                         if (!owner_order) {
646                             // now compare on CN label
647                             if (a.call_number.label < b.call_number.label) return -1;
648                             if (a.call_number.label > b.call_number.label) return 1;
649
650                             // try copy number
651                             if (a.copy_number < b.copy_number) return -1;
652                             if (a.copy_number > b.copy_number) return 1;
653
654                             // finally, barcode
655                             if (a.barcode < b.barcode) return -1;
656                             if (a.barcode > b.barcode) return 1;
657                         }
658                         return owner_order;
659                     }
660                 );
661
662                 // create a label using just the unique part of the owner list
663                 var index = 0;
664                 var prev_owner_list;
665                 angular.forEach(service.copies, function (cp) {
666                     if (!prev_owner_list) {
667                         cp.owner_label = cp.owner_list.join(' ... ');
668                     } else {
669                         var current_owner_list = cp.owner_list.slice();
670                         while (current_owner_list[1] && prev_owner_list[1] && current_owner_list[0] == prev_owner_list[0]) {
671                             current_owner_list.shift();
672                             prev_owner_list.shift();
673                         }
674                         cp.owner_label = current_owner_list.join(' ... ');
675                     }
676
677                     cp.index = index++;
678                     prev_owner_list = cp.owner_list.slice();
679                 });
680
681                 var new_list = service.copies;
682                 if (!copy || !vol) { // collapse copy rows, supply a count instead
683
684                     index = 0;
685                     var cp_list = [];
686                     var prev_key;
687                     var current_blob = {};
688                     angular.forEach(new_list, function (cp) {
689                         if (!prev_key) {
690                             prev_key = cp.owner_list.join('') + cp.call_number.label;
691                             if (cp.barcode) current_blob.copy_count = 1;
692                             current_blob.index = index++;
693                             current_blob.id_list = cp.id_list;
694                             current_blob.call_number = cp.call_number;
695                             current_blob.owner_list = cp.owner_list;
696                             current_blob.owner_label = cp.owner_label;
697                         } else {
698                             var current_key = cp.owner_list.join('') + cp.call_number.label;
699                             if (prev_key == current_key) { // collapse into current_blob
700                                 current_blob.copy_count++;
701                                 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
702                             } else {
703                                 current_blob.barcode = current_blob.copy_count;
704                                 cp_list.push(current_blob);
705                                 prev_key = current_key;
706                                 current_blob = {};
707                                 if (cp.barcode) current_blob.copy_count = 1;
708                                 current_blob.index = index++;
709                                 current_blob.id_list = cp.id_list;
710                                 current_blob.owner_label = cp.owner_label;
711                                 current_blob.call_number = cp.call_number;
712                                 current_blob.owner_list = cp.owner_list;
713                             }
714                         }
715                     });
716
717                     current_blob.barcode = current_blob.copy_count;
718                     cp_list.push(current_blob);
719                     new_list = cp_list;
720
721                     if (!vol) { // do the same for vol rows
722
723                         index = 0;
724                         var cn_list = [];
725                         prev_key = '';
726                         var current_blob = {};
727                         angular.forEach(cp_list, function (cp) {
728                             if (!prev_key) {
729                                 prev_key = cp.owner_list.join('');
730                                 current_blob.index = index++;
731                                 current_blob.id_list = cp.id_list;
732                                 current_blob.cn_count = 1;
733                                 current_blob.copy_count = cp.copy_count;
734                                 current_blob.owner_list = cp.owner_list;
735                                 current_blob.owner_label = cp.owner_label;
736                             } else {
737                                 var current_key = cp.owner_list.join('');
738                                 if (prev_key == current_key) { // collapse into current_blob
739                                     current_blob.cn_count++;
740                                     current_blob.copy_count += cp.copy_count;
741                                     current_blob.id_list = current_blob.id_list.concat(cp.id_list);
742                                 } else {
743                                     current_blob.barcode = current_blob.copy_count;
744                                     current_blob.call_number = { label : current_blob.cn_count };
745                                     cn_list.push(current_blob);
746                                     prev_key = current_key;
747                                     current_blob = {};
748                                     current_blob.index = index++;
749                                     current_blob.id_list = cp.id_list;
750                                     current_blob.owner_label = cp.owner_label;
751                                     current_blob.cn_count = 1;
752                                     current_blob.copy_count = cp.copy_count;
753                                     current_blob.owner_list = cp.owner_list;
754                                 }
755                             }
756                         });
757     
758                         current_blob.barcode = current_blob.copy_count;
759                         current_blob.call_number = { label : current_blob.cn_count };
760                         cn_list.push(current_blob);
761                         new_list = cn_list;
762     
763                     }
764                 }
765
766                 service.copies = new_list;
767                 service.ongoing = false;
768             },
769
770             null, // error
771
772             // notify reads the stream of copies, one at a time.
773             function(cn) {
774
775                 var copies = cn.copies();
776                 cn.copies([]);
777
778                 angular.forEach(copies, function (cp) {
779                     cp.call_number(cn);
780                 });
781
782                 var flat = egCore.idl.toHash(copies);
783                 var owner = egCore.org.get(flat[0].call_number.owning_lib);
784
785                 var owner_name_list = [];
786                 while (owner.parent_ou()) { // we're going to skip the top of the tree...
787                     owner_name_list.unshift(owner.name());
788                     owner = egCore.org.get(owner.parent_ou());
789                 }
790
791                 angular.forEach(flat, function (cp) {
792                     cp.owner_list = owner_name_list;
793                     cp.id_list = [cp.id];
794                 });
795
796                 service.copies = service.copies.concat(flat);
797
798                 if (empty && flat.length == 0) {
799                     service.copies.push({
800                         owner_list : owner_name_list,
801                         call_number: egCore.idl.toHash(cn)
802                     });
803                 }
804
805                 return cn;
806             }
807         );
808     }
809
810     return service;
811 }])
812
813