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