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