]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/catalog/app.js
webstaff: Add "add items to bucket" action to holdings maint
[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', 'egUserMod'])
11
12 .config(function($routeProvider, $locationProvider, $compileProvider) {
13     $locationProvider.html5Mode(true);
14     $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|blob):/); // grid export
15
16     var resolver = {delay : ['egCore','egStartup','egUser', function(egCore, egStartup, egUser) {
17         egCore.env.classLoaders.aous = function() {
18             return egCore.org.settings([
19                 'cat.marc_control_number_identifier'
20             ]).then(function(settings) {
21                 // local settings are cached within egOrg.  Caching them
22                 // again in egEnv just simplifies the syntax for access.
23                 egCore.env.aous = settings;
24             });
25         }
26         egCore.env.loadClasses.push('aous');
27         return egStartup.go()
28     }]};
29
30     $routeProvider.when('/cat/catalog/index', {
31         templateUrl: './cat/catalog/t_catalog',
32         controller: 'CatalogCtrl',
33         resolve : resolver
34     });
35
36     $routeProvider.when('/cat/catalog/retrieve_by_id', {
37         templateUrl: './cat/catalog/t_retrieve_by_id',
38         controller: 'CatalogRecordRetrieve',
39         resolve : resolver
40     });
41
42     $routeProvider.when('/cat/catalog/retrieve_by_tcn', {
43         templateUrl: './cat/catalog/t_retrieve_by_tcn',
44         controller: 'CatalogRecordRetrieve',
45         resolve : resolver
46     });
47
48     $routeProvider.when('/cat/catalog/new_bib', {
49         templateUrl: './cat/catalog/t_new_bib',
50         controller: 'NewBibCtrl',
51         resolve : resolver
52     });
53
54     // create some catalog page-specific mappings
55     $routeProvider.when('/cat/catalog/record/:record_id', {
56         templateUrl: './cat/catalog/t_catalog',
57         controller: 'CatalogCtrl',
58         resolve : resolver
59     });
60
61     // create some catalog page-specific mappings
62     $routeProvider.when('/cat/catalog/record/:record_id/:record_tab', {
63         templateUrl: './cat/catalog/t_catalog',
64         controller: 'CatalogCtrl',
65         resolve : resolver
66     });
67
68     $routeProvider.when('/cat/catalog/batchEdit', {
69         templateUrl: './cat/catalog/t_batchedit',
70         controller: 'BatchEditCtrl',
71         resolve : resolver
72     });
73
74     $routeProvider.when('/cat/catalog/batchEdit/:container_type/:container_id', {
75         templateUrl: './cat/catalog/t_batchedit',
76         controller: 'BatchEditCtrl',
77         resolve : resolver
78     });
79
80     $routeProvider.when('/cat/catalog/vandelay', {
81         templateUrl: './cat/catalog/t_vandelay',
82         controller: 'VandelayCtrl',
83         resolve : resolver
84     });
85
86     $routeProvider.when('/cat/catalog/verifyURLs', {
87         templateUrl: './cat/catalog/t_verifyurls',
88         controller: 'URLVerifyCtrl',
89         resolve : resolver
90     });
91
92     $routeProvider.when('/cat/catalog/manageAuthorities', {
93         templateUrl: './cat/catalog/t_manageauthorities',
94         controller: 'ManageAuthoritiesCtrl',
95         resolve : resolver
96     });
97
98     $routeProvider.when('/cat/catalog/authority/:authority_id/marc_edit', {
99         templateUrl: './cat/catalog/t_authority',
100         controller: 'AuthorityCtrl',
101         resolve : resolver
102     });
103
104     $routeProvider.otherwise({redirectTo : '/cat/catalog/index'});
105 })
106
107
108 /**
109  * */
110 .controller('CatalogRecordRetrieve',
111        ['$scope','$routeParams','$location','$q','egCore',
112 function($scope , $routeParams , $location , $q , egCore ) {
113
114     $scope.focusMe = true;
115
116     // jump to the patron checkout UI
117     function loadRecord(record_id) {
118         $location
119         .path('/cat/catalog/record/' + record_id);
120     }
121
122     $scope.submitId = function(args) {
123         $scope.recordNotFound = null;
124         if (!args.record_id) return;
125
126         // blur so next time it's set to true it will re-apply select()
127         $scope.selectMe = false;
128
129         return loadRecord(args.record_id);
130     }
131
132     $scope.submitTCN = function(args) {
133         $scope.recordNotFound = null;
134         $scope.moreRecordsFound = null;
135         if (!args.record_tcn) return;
136
137         // blur so next time it's set to true it will re-apply select()
138         $scope.selectMe = false;
139
140         // lookup TCN
141         egCore.net.request(
142             'open-ils.search',
143             'open-ils.search.biblio.tcn',
144             args.record_tcn)
145
146         .then(function(resp) { // get_barcodes
147
148             if (evt = egCore.evt.parse(resp)) {
149                 alert(evt); // FIXME
150                 return;
151             }
152
153             if (!resp.count) {
154                 $scope.recordNotFound = args.record_tcn;
155                 $scope.selectMe = true;
156                 return;
157             }
158
159             if (resp.count > 1) {
160                 $scope.moreRecordsFound = args.record_tcn;
161                 $scope.selectMe = true;
162                 return;
163             }
164
165             var record_id = resp.ids[0];
166             return loadRecord(record_id);
167         });
168     }
169
170 }])
171
172 .controller('NewBibCtrl',
173        ['$scope','$routeParams','$location','$window','$q','egCore',
174         'egGridDataProvider','egHoldGridActions','$timeout','holdingsSvc',
175 function($scope , $routeParams , $location , $window , $q , egCore) {
176
177     $scope.have_template = false;
178     $scope.marc_template = '';
179     $scope.stop_unload = false;
180     $scope.template_list = [];
181     $scope.template_name = '';
182     $scope.new_bib_id = 0;
183
184     egCore.net.request(
185         'open-ils.cat',
186         'open-ils.cat.marc_template.types.retrieve'
187     ).then(function(resp) {
188         angular.forEach(resp, function(name) {
189             $scope.template_list.push(name);
190         });
191         $scope.template_list.sort();
192     });
193     egCore.hatch.getItem('cat.default_bib_marc_template').then(function(template) {
194         $scope.template_name = template;
195     });
196
197     $scope.loadTemplate = function() {
198         if ($scope.template_name) {
199             egCore.net.request(
200                 'open-ils.cat',
201                 'open-ils.cat.biblio.marc_template.retrieve',
202                 $scope.template_name
203             ).then(function(template) {
204                 $scope.marc_template = template;
205                 $scope.have_template = true;
206             });
207         }
208     }
209
210     $scope.setDefaultTemplate = function() {
211         var hatch_key = "cat.default_bib_marc_template";
212         if ($scope.template_name) {
213             egCore.hatch.setItem(hatch_key, $scope.template_name);
214         } else {
215             egCore.hatch.removeItem(hatch_key);
216         }
217     }
218
219     $scope.$watch('new_bib_id', function(newVal, oldVal) {
220         if (newVal) {
221             $location.path('/cat/catalog/record/' + $scope.new_bib_id);
222         }
223     });
224     
225
226 }])
227
228 .controller('CatalogCtrl',
229        ['$scope','$routeParams','$location','$window','$q','egCore','egHolds','egCirc','egConfirmDialog',
230         'egGridDataProvider','egHoldGridActions','$timeout','$modal','holdingsSvc','egUser','conjoinedSvc',
231 function($scope , $routeParams , $location , $window , $q , egCore , egHolds , egCirc,  egConfirmDialog,
232          egGridDataProvider , egHoldGridActions , $timeout , $modal , holdingsSvc , egUser , conjoinedSvc) {
233
234     // set record ID on page load if available...
235     $scope.record_id = $routeParams.record_id;
236
237     if ($routeParams.record_id) $scope.from_route = true;
238     else $scope.from_route = false;
239
240     // will hold a ref to the opac iframe
241     $scope.opac_iframe = null;
242     $scope.parts_iframe = null;
243
244     $scope.in_opac_call = false;
245     $scope.opac_call = function (opac_frame_function, force_opac_tab) {
246         if ($scope.opac_iframe) {
247             if (force_opac_tab) $scope.record_tab = 'catalog';
248             $scope.in_opac_call = true;
249             $scope.opac_iframe.dom.contentWindow[opac_frame_function]();
250         }
251     }
252
253     $scope.add_to_record_bucket = function() {
254         var recId = $scope.record_id;
255         return $modal.open({
256             templateUrl: './cat/catalog/t_add_to_bucket',
257             animation: true,
258             size: 'md',
259             controller:
260                    ['$scope','$modalInstance',
261             function($scope , $modalInstance) {
262
263                 $scope.bucket_id = 0;
264                 $scope.newBucketName = '';
265                 $scope.allBuckets = [];
266                 egCore.net.request(
267                     'open-ils.actor',
268                     'open-ils.actor.container.retrieve_by_class.authoritative',
269                     egCore.auth.token(), egCore.auth.user().id(),
270                     'biblio', 'staff_client'
271                 ).then(function(buckets) { $scope.allBuckets = buckets; });
272
273                 $scope.add_to_bucket = function() {
274                     var item = new egCore.idl.cbrebi();
275                     item.bucket($scope.bucket_id);
276                     item.target_biblio_record_entry(recId);
277                     egCore.net.request(
278                         'open-ils.actor',
279                         'open-ils.actor.container.item.create',
280                         egCore.auth.token(), 'biblio', item
281                     ).then(function(resp) {
282                         $modalInstance.close();
283                     });
284                 }
285
286                 $scope.add_to_new_bucket = function() {
287                     var bucket = new egCore.idl.cbreb();
288                     bucket.owner(egCore.auth.user().id());
289                     bucket.name($scope.newBucketName);
290                     bucket.description('');
291                     bucket.btype('staff_client');
292
293                     egCore.net.request(
294                         'open-ils.actor',
295                         'open-ils.actor.container.create',
296                         egCore.auth.token(), 'biblio', bucket
297                     ).then(function(bucket) {
298                         $scope.bucket_id = bucket;
299                         $scope.add_to_bucket();
300                     });
301                 }
302
303                 $scope.cancel = function() {
304                     $modalInstance.dismiss();
305                 }
306             }]
307         });
308     }
309
310     $scope.stop_unload = false;
311     $scope.$watch('stop_unload',
312         function(newVal, oldVal) {
313             if (newVal && newVal != oldVal && $scope.opac_iframe) {
314                 $($scope.opac_iframe.dom.contentWindow).on('beforeunload', function(){
315                     return 'There is unsaved data in this record.'
316                 });
317             } else {
318                 if ($scope.opac_iframe)
319                     $($scope.opac_iframe.dom.contentWindow).off('beforeunload');
320             }
321         }
322     );
323
324     // Set the "last bib" cookie, if we have that
325     if ($scope.record_id)
326         egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
327
328     // also set it when the iframe changes to a new record
329     $scope.handle_page = function(url) {
330
331         if (!url || url == 'about:blank') {
332             // nothing loaded.  If we already have a record ID, leave it.
333             return;
334         }
335
336         var match = url.match(/\/+opac\/+record\/+(\d+)/);
337         if (match) {
338             $scope.record_id = match[1];
339             egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
340             $scope.holdings_record_id_changed($scope.record_id);
341             conjoinedSvc.fetch($scope.record_id).then(function(){
342                 $scope.conjoinedGridDataProvider.refresh();
343             });
344             init_parts_url();
345         } else {
346             delete $scope.record_id;
347             $scope.from_route = false;
348         }
349
350         // child scope is executing this function, so our digest doesn't fire ... thus,
351         $scope.$apply();
352
353         if (!$scope.in_opac_call) {
354             if ($scope.record_id) {
355                 $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
356                 tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
357             } else {
358                 tab = $routeParams.record_tab || 'catalog';
359             }
360             $scope.set_record_tab(tab);
361         } else {
362             $scope.in_opac_call = false;
363         }
364     }
365
366     // xulG catalog handlers
367     $scope.handlers = { }
368
369     // ------------------------------------------------------------------
370     // Conjoined items
371
372     $scope.conjoinedGridControls = {};
373     $scope.conjoinedGridDataProvider = egGridDataProvider.instance({
374         get : function(offset, count) {
375             return this.arrayNotifier(conjoinedSvc.items, offset, count);
376         }
377     });
378
379     $scope.changeConjoinedType = function () {
380         var peers = egCore.idl.Clone($scope.conjoinedGridControls.selectedItems());
381         angular.forEach(peers, function (p) {
382             p.target_copy(p.target_copy().id());
383             p.peer_type(p.peer_type().id());
384         });
385
386         var conjoinedGridDataProviderRef = $scope.conjoinedGridDataProvider;
387
388         return $modal.open({
389             templateUrl: './cat/catalog/t_conjoined_selector',
390             animation: true,
391             controller:
392                    ['$scope','$modalInstance',
393             function($scope , $modalInstance) {
394                 $scope.update = true;
395
396                 $scope.peer_type = null;
397                 $scope.peer_type_list = [];
398                 conjoinedSvc.get_peer_types().then(function(list){
399                     $scope.peer_type_list = list;
400                 });
401     
402                 $scope.ok = function(type) {
403                     var promises = [];
404     
405                     angular.forEach(peers, function (p) {
406                         p.ischanged(1);
407                         p.peer_type(type);
408                         promises.push(egCore.pcrud.update(p));
409                     });
410     
411                     return $q.all(promises)
412                         .then(function(){$modalInstance.close()})
413                         .then(function(){return conjoinedSvc.fetch()})
414                         .then(function(){conjoinedGridDataProviderRef.refresh()});
415                 }
416     
417                 $scope.cancel = function($event) {
418                     $modalInstance.dismiss();
419                     $event.preventDefault();
420                 }
421             }]
422         });
423         
424     }
425
426     $scope.refreshConjoined = function () {
427         conjoinedSvc.fetch($scope.record_id)
428         .then(function(){$scope.conjoinedGridDataProvider.refresh();});
429     }
430
431     $scope.deleteSelectedConjoined = function () {
432         var peers = $scope.conjoinedGridControls.selectedItems();
433
434         if (peers.length > 0) {
435             egConfirmDialog.open(
436                 egCore.strings.CONFIRM_DELETE_PEERS,
437                 egCore.strings.CONFIRM_DELETE_PEERS_MESSAGE,
438                 {peers : peers.length}
439             ).result.then(function() {
440                 angular.forEach(peers, function (p) {
441                     p.isdeleted(1);
442                 });
443
444                 egCore.pcrud.remove(peers).then(function() {
445                     return conjoinedSvc.fetch();
446                 }).then(function() {
447                     $scope.conjoinedGridDataProvider.refresh();
448                 });
449             });
450         }
451     }
452     if ($scope.record_id)
453         conjoinedSvc.fetch($scope.record_id);
454
455     // ------------------------------------------------------------------
456     // Holdings
457
458     $scope.holdingsGridControls = {};
459     $scope.holdingsGridDataProvider = egGridDataProvider.instance({
460         get : function(offset, count) {
461             return this.arrayNotifier(holdingsSvc.copies, offset, count);
462         }
463     });
464
465     $scope.add_copies_to_bucket = function() {
466         var copy_list = gatherSelectedHoldingsIds();
467         if (copy_list.length == 0) return;
468
469         return $modal.open({
470             templateUrl: './cat/catalog/t_add_to_bucket',
471             animation: true,
472             size: 'md',
473             controller:
474                    ['$scope','$modalInstance',
475             function($scope , $modalInstance) {
476
477                 $scope.bucket_id = 0;
478                 $scope.newBucketName = '';
479                 $scope.allBuckets = [];
480
481                 egCore.net.request(
482                     'open-ils.actor',
483                     'open-ils.actor.container.retrieve_by_class.authoritative',
484                     egCore.auth.token(), egCore.auth.user().id(),
485                     'copy', 'staff_client'
486                 ).then(function(buckets) { $scope.allBuckets = buckets; });
487
488                 $scope.add_to_bucket = function() {
489                     var promises = [];
490                     angular.forEach(copy_list, function (cp) {
491                         var item = new egCore.idl.ccbi()
492                         item.bucket($scope.bucket_id);
493                         item.target_copy(cp);
494                         promises.push(
495                             egCore.net.request(
496                                 'open-ils.actor',
497                                 'open-ils.actor.container.item.create',
498                                 egCore.auth.token(), 'copy', item
499                             )
500                         );
501
502                         return $q.all(promises).then(function() {
503                             $modalInstance.close();
504                         });
505                     });
506                 }
507
508                 $scope.add_to_new_bucket = function() {
509                     var bucket = new egCore.idl.ccb();
510                     bucket.owner(egCore.auth.user().id());
511                     bucket.name($scope.newBucketName);
512                     bucket.description('');
513                     bucket.btype('staff_client');
514
515                     return egCore.net.request(
516                         'open-ils.actor',
517                         'open-ils.actor.container.create',
518                         egCore.auth.token(), 'copy', bucket
519                     ).then(function(bucket) {
520                         $scope.bucket_id = bucket;
521                         $scope.add_to_bucket();
522                     });
523                 }
524
525                 $scope.cancel = function() {
526                     $modalInstance.dismiss();
527                 }
528             }]
529         });
530     }
531
532     $scope.requestItems = function() {
533         var copy_list = gatherSelectedHoldingsIds();
534         if (copy_list.length == 0) return;
535
536         return $modal.open({
537             templateUrl: './cat/catalog/t_request_items',
538             animation: true,
539             controller:
540                    ['$scope','$modalInstance',
541             function($scope , $modalInstance) {
542                 $scope.user = null;
543                 $scope.first_user_fetch = true;
544
545                 $scope.hold_data = {
546                     hold_type : 'C',
547                     copy_list : copy_list,
548                     pickup_lib: egCore.org.get(egCore.auth.user().ws_ou()),
549                     user      : egCore.auth.user().id()
550                 };
551
552                 egUser.get( $scope.hold_data.user ).then(function(u) {
553                     $scope.user = u;
554                     $scope.barcode = u.card().barcode();
555                     $scope.user_name = egUser.format_name(u);
556                     $scope.hold_data.user = u.id();
557                 });
558
559                 $scope.user_name = '';
560                 $scope.barcode = '';
561                 $scope.$watch('barcode', function (n) {
562                     if (!$scope.first_user_fetch) {
563                         egUser.getByBarcode(n).then(function(u) {
564                             $scope.user = u;
565                             $scope.user_name = egUser.format_name(u);
566                             $scope.hold_data.user = u.id();
567                         }, function() {
568                             $scope.user = null;
569                             $scope.user_name = '';
570                             delete $scope.hold_data.user;
571                         });
572                     }
573                     $scope.first_user_fetch = false;
574                 });
575
576                 $scope.ok = function(h) {
577                     var args = {
578                         patronid  : h.user,
579                         hold_type : h.hold_type,
580                         pickup_lib: h.pickup_lib.id(),
581                         depth     : 0
582                     };
583
584                     egCore.net.request(
585                         'open-ils.circ',
586                         'open-ils.circ.holds.test_and_create.batch.override',
587                         egCore.auth.token(), args, h.copy_list
588                     );
589
590                     $modalInstance.close();
591                 }
592
593                 $scope.cancel = function($event) {
594                     $modalInstance.dismiss();
595                     $event.preventDefault();
596                 }
597             }]
598         });
599     }
600
601     $scope.replaceBarcodes = function() {
602         var copy_list = gatherSelectedRawCopies();
603         if (copy_list.length == 0) return;
604
605         var holdingsGridDataProviderRef = $scope.holdingsGridDataProvider;
606
607         angular.forEach(copy_list, function (cp) {
608             $modal.open({
609                 templateUrl: './cat/share/t_replace_barcode',
610                 animation: true,
611                 controller:
612                            ['$scope','$modalInstance',
613                     function($scope , $modalInstance) {
614                         $scope.isModal = true;
615                         $scope.focusBarcode = false;
616                         $scope.focusBarcode2 = true;
617                         $scope.barcode1 = cp.barcode();
618
619                         $scope.updateBarcode = function() {
620                             $scope.copyNotFound = false;
621                             $scope.updateOK = false;
622                 
623                             egCore.pcrud.search('acp',
624                                 {deleted : 'f', barcode : $scope.barcode1})
625                             .then(function(copy) {
626                 
627                                 if (!copy) {
628                                     $scope.focusBarcode = true;
629                                     $scope.copyNotFound = true;
630                                     return;
631                                 }
632                 
633                                 $scope.copyId = copy.id();
634                                 copy.barcode($scope.barcode2);
635                 
636                                 egCore.pcrud.update(copy).then(function(stat) {
637                                     $scope.updateOK = stat;
638                                     $scope.focusBarcode = true;
639                                     holdingsSvc.fetchAgain().then(function (){
640                                         holdingsGridDataProviderRef.refresh();
641                                     });
642                                 });
643
644                             });
645                             $modalInstance.close();
646                         }
647
648                         $scope.cancel = function($event) {
649                             $modalInstance.dismiss();
650                             $event.preventDefault();
651                         }
652                     }
653                 ]
654             });
655         });
656     }
657
658     // refresh the list of holdings when the record_id is changed.
659     $scope.holdings_record_id_changed = function(id) {
660         if ($scope.record_id != id) $scope.record_id = id;
661         console.log('record id changed to ' + id + ', loading new holdings');
662         holdingsSvc.fetch({
663             rid : $scope.record_id,
664             org : $scope.holdings_ou,
665             copy: $scope.holdings_show_copies,
666             vol : $scope.holdings_show_vols,
667             empty: $scope.holdings_show_empty
668         }).then(function() {
669             $scope.holdingsGridDataProvider.refresh();
670         });
671     }
672
673     // refresh the list of holdings when the filter lib is changed.
674     $scope.holdings_ou = egCore.org.get(egCore.auth.user().ws_ou());
675     $scope.holdings_ou_changed = function(org) {
676         $scope.holdings_ou = org;
677         holdingsSvc.fetch({
678             rid : $scope.record_id,
679             org : $scope.holdings_ou,
680             copy: $scope.holdings_show_copies,
681             vol : $scope.holdings_show_vols,
682             empty: $scope.holdings_show_empty
683         }).then(function() {
684             $scope.holdingsGridDataProvider.refresh();
685         });
686     }
687
688     $scope.holdings_cb_changed = function(cb,newVal,norefresh) {
689         $scope[cb] = newVal;
690         egCore.hatch.setItem('cat.' + cb, newVal);
691         if (!norefresh) holdingsSvc.fetch({
692             rid : $scope.record_id,
693             org : $scope.holdings_ou,
694             copy: $scope.holdings_show_copies,
695             vol : $scope.holdings_show_vols,
696             empty: $scope.holdings_show_empty
697         }).then(function() {
698             $scope.holdingsGridDataProvider.refresh();
699         });
700     }
701
702     egCore.hatch.getItem('cat.holdings_show_vols').then(function(x){
703         if (typeof x ==  'undefined') x = true;
704         $scope.holdings_cb_changed('holdings_show_vols',x,true);
705         $('#holdings_show_vols').prop('checked', x);
706     }).then(function(){
707         egCore.hatch.getItem('cat.holdings_show_copies').then(function(x){
708             if (typeof x ==  'undefined') x = true;
709             $scope.holdings_cb_changed('holdings_show_copies',x,true);
710             $('#holdings_show_copies').prop('checked', x);
711         }).then(function(){
712             egCore.hatch.getItem('cat.holdings_show_empty').then(function(x){
713                 if (typeof x ==  'undefined') x = true;
714                 $scope.holdings_cb_changed('holdings_show_empty',x);
715                 $('#holdings_show_empty').prop('checked', x);
716             })
717         })
718     });
719
720     $scope.vols_not_shown = function () {
721         return !$scope.holdings_show_vols;
722     }
723
724     $scope.copies_not_shown = function () {
725         return !$scope.holdings_show_copies;
726     }
727
728     $scope.holdings_checkbox_handler = function (item) {
729         $scope.holdings_cb_changed(item.checkbox,item.checked);
730     }
731
732     function gatherSelectedHoldingsIds () {
733         var cp_id_list = [];
734         angular.forEach(
735             $scope.holdingsGridControls.selectedItems(),
736             function (item) { cp_id_list = cp_id_list.concat(item.id_list) }
737         );
738         return cp_id_list;
739     }
740
741     function gatherSelectedRawCopies () {
742         var cp_list = [];
743         angular.forEach(
744             $scope.holdingsGridControls.selectedItems(),
745             function (item) { if (item.raw) cp_list = cp_list.concat(item.raw) }
746         );
747         return cp_list;
748     }
749
750     function gatherSelectedVolumeIds () {
751         var cn_id_list = [];
752         angular.forEach(
753             $scope.holdingsGridControls.selectedItems(),
754             function (item) {
755                 if (cn_id_list.indexOf(item.call_number.id) == -1)
756                     cn_id_list.push(item.call_number.id)
757             }
758         );
759         return cn_id_list;
760     }
761
762     $scope.selectedHoldingsDelete = function (vols, copies) {
763
764         var cnHash = {};
765         var perCnCopies = {};
766
767         var cn_count = 0;
768         var cp_count = 0;
769
770         angular.forEach(
771             $scope.holdingsGridControls.selectedItems(),
772             function (item) {
773                 if (vols && item.raw_call_number) {
774                     cnHash[item.call_number.id] = egCore.idl.Clone(item.raw_call_number);
775                     cnHash[item.call_number.id].isdeleted(1);
776                     cn_count++;
777                 } else if (copies) {
778                     angular.forEach(egCore.idl.Clone(item.raw), function (cp) {
779                         cp.isdeleted(1);
780                         cp_count++;
781                         var cn_id = cp.call_number().id();
782                         if (!cnHash[cn_id]) {
783                             cnHash[cn_id] = cp.call_number();
784                             perCnCopies[cn_id] = [cp];
785                         } else {
786                             perCnCopies[cn_id].push(cp);
787                         }
788                         cp.call_number(cn_id); // prevent loops in JSON-ification
789                     });
790
791                 }
792             }
793         );
794
795         angular.forEach(perCnCopies, function (v, k) {
796             if (vols) {
797                 cnHash[k].isdeleted(1);
798                 cn_count++;
799             }
800             cnHash[k].copies(v);
801         });
802
803         cnList = [];
804         angular.forEach(cnHash, function (v, k) {
805             cnList.push(v);
806         });
807
808         if (cnList.length == 0) return;
809
810         egConfirmDialog.open(
811             egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES,
812             egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES_MESSAGE,
813             {copies : cp_count, volumes : cn_count}
814         ).result.then(function() {
815             egCore.net.request(
816                 'open-ils.cat',
817                 'open-ils.cat.asset.volume.fleshed.batch.update.override',
818                 egCore.auth.token(), cnList, 1, {}
819             ).then(function(update_count) {
820                 $scope.holdingsGridDataProvider.refresh();
821             });
822         });
823     }
824     $scope.selectedHoldingsCopyDelete = function () { $scope.selectedHoldingsDelete(false,true) }
825     $scope.selectedHoldingsVolCopyDelete = function () { $scope.selectedHoldingsDelete(true,true) }
826     $scope.selectedHoldingsEmptyVolCopyDelete = function () { $scope.selectedHoldingsDelete(true,false) }
827
828     spawnHoldingsAdd = function (vols,copies){
829         var raw = [];
830         if (copies) { // just a copy on existing volumes
831             angular.forEach(gatherSelectedVolumeIds(), function (v) {
832                 raw.push( {callnumber : v} );
833             });
834         } else if (vols) {
835             angular.forEach(
836                 $scope.holdingsGridControls.selectedItems(),
837                 function (item) {
838                     raw.push({owner : item.owner_id});
839                 }
840             );
841         }
842
843         egCore.net.request(
844             'open-ils.actor',
845             'open-ils.actor.anon_cache.set_value',
846             null, 'edit-these-copies', {
847                 record_id: $scope.record_id,
848                 raw: raw,
849                 hide_vols : false,
850                 hide_copies : false
851             }
852         ).then(function(key) {
853             if (key) {
854                 var url = egCore.env.basePath + 'cat/volcopy/' + key;
855                 $timeout(function() { $window.open(url, '_blank') });
856             } else {
857                 alert('Could not create anonymous cache key!');
858             }
859         });
860     }
861     $scope.selectedHoldingsVolCopyAdd = function () { spawnHoldingsAdd(true,false) }
862     $scope.selectedHoldingsCopyAdd = function () { spawnHoldingsAdd(false,true) }
863
864     spawnHoldingsEdit = function (hide_vols,hide_copies){
865         egCore.net.request(
866             'open-ils.actor',
867             'open-ils.actor.anon_cache.set_value',
868             null, 'edit-these-copies', {
869                 record_id: $scope.record_id,
870                 copies: gatherSelectedHoldingsIds(),
871                 hide_vols : hide_vols,
872                 hide_copies : hide_copies
873             }
874         ).then(function(key) {
875             if (key) {
876                 var url = egCore.env.basePath + 'cat/volcopy/' + key;
877                 $timeout(function() { $window.open(url, '_blank') });
878             } else {
879                 alert('Could not create anonymous cache key!');
880             }
881         });
882     }
883     $scope.selectedHoldingsVolCopyEdit = function () { spawnHoldingsEdit(false,false) }
884     $scope.selectedHoldingsVolEdit = function () { spawnHoldingsEdit(false,true) }
885     $scope.selectedHoldingsCopyEdit = function () { spawnHoldingsEdit(true,false) }
886
887     $scope.selectedHoldingsItemStatus = function (){
888         var url = egCore.env.basePath + 'cat/item/search/' + gatherSelectedHoldingsIds().join(',')
889         $timeout(function() { $window.open(url, '_blank') });
890     }
891
892     $scope.markVolAsItemTarget = function() {
893         if ($scope.holdingsGridControls.selectedItems()[0].call_number.id) { // cn.id missing when vols are collapsed
894             egCore.hatch.setLocalItem(
895                 'eg.cat.item_transfer_target',
896                 $scope.holdingsGridControls.selectedItems()[0].call_number.id
897             );
898         }
899     }
900
901     $scope.markLibAsVolTarget = function() {
902         egCore.hatch.setLocalItem(
903             'eg.cat.volume_transfer_target',
904             $scope.holdingsGridControls.selectedItems()[0].owner_id
905         );
906     }
907
908     $scope.selectedHoldingsItemStatusDetail = function (){
909         angular.forEach(
910             gatherSelectedHoldingsIds(),
911             function (cid) {
912                 var url = egCore.env.basePath +
913                           'cat/item/' + cid;
914                 $timeout(function() { $window.open(url, '_blank') });
915             }
916         );
917     }
918
919     $scope.transferVolumes = function (){
920         var xfer_target = egCore.hatch.getLocalItem('eg.cat.volume_transfer_target');
921
922         if (xfer_target) {
923             egCore.net.request(
924                 'open-ils.cat',
925                 'open-ils.open-ils.cat.asset.volume.batch.transfer.override',
926                 egCore.auth.token(), {
927                     docid   : $scope.record_id,
928                     lib     : xfer_target,
929                     volumes : gatherSelectedVolumeIds()
930                 }
931             ).then(function(success) {
932                 if (success) {
933                     holdingsSvc.fetchAgain().then(function() {
934                         $scope.holdingsGridDataProvider.refresh();
935                     });
936                 } else {
937                     alert('Could not transfer volumes!');
938                 }
939             });
940         }
941         
942     }
943
944     $scope.transferItems = function (){
945         var xfer_target = egCore.hatch.getLocalItem('eg.cat.item_transfer_target');
946         if (xfer_target) {
947             var copy_list = gatherSelectedRawCopies();
948
949             angular.forEach(copy_list, function (cp) {
950                 cp.call_number(xfer_target);
951             });
952
953             egCore.pcrud.update(
954                 copy_list
955             ).then(function(success) {
956                 if (success) {
957                     holdingsSvc.fetchAgain().then(function() {
958                         $scope.holdingsGridDataProvider.refresh();
959                     });
960                 } else {
961                     alert('Could not transfer items!');
962                 }
963             });
964         }
965         
966     }
967
968     $scope.selectedHoldingsItemStatusTgrEvt = function (){
969         angular.forEach(
970             gatherSelectedHoldingsIds(),
971             function (cid) {
972                 var url = egCore.env.basePath +
973                           'cat/item/' + cid + '/triggered_events';
974                 $timeout(function() { $window.open(url, '_blank') });
975             }
976         );
977     }
978
979     $scope.selectedHoldingsItemStatusHolds = function (){
980         angular.forEach(
981             gatherSelectedHoldingsIds(),
982             function (cid) {
983                 var url = egCore.env.basePath +
984                           'cat/item/' + cid + '/holds';
985                 $timeout(function() { $window.open(url, '_blank') });
986             }
987         );
988     }
989
990     $scope.selectedHoldingsDamaged = function () {
991         egCirc.mark_damaged(gatherSelectedHoldingsIds()).then(function() {
992             holdingsSvc.fetchAgain().then(function() {
993                 $scope.holdingsGridDataProvider.refresh();
994             });
995         });
996     }
997
998     $scope.selectedHoldingsMissing = function () {
999         egCirc.mark_missing(gatherSelectedHoldingsIds()).then(function() {
1000             holdingsSvc.fetchAgain().then(function() {
1001                 $scope.holdingsGridDataProvider.refresh();
1002             });
1003         });
1004     }
1005
1006     $scope.attach_to_peer_bib = function() {
1007         var copy_list = gatherSelectedHoldingsIds();
1008         if (copy_list.length == 0) return;
1009
1010         egCore.hatch.getItem('eg.cat.marked_conjoined_record').then(function(target_record) {
1011             if (!target_record) return;
1012
1013             return $modal.open({
1014                 templateUrl: './cat/catalog/t_conjoined_selector',
1015                 animation: true,
1016                 controller:
1017                        ['$scope','$modalInstance',
1018                 function($scope , $modalInstance) {
1019                     $scope.update = false;
1020
1021                     $scope.peer_type = null;
1022                     $scope.peer_type_list = [];
1023                     conjoinedSvc.get_peer_types().then(function(list){
1024                         $scope.peer_type_list = list;
1025                     });
1026     
1027                     $scope.ok = function(type) {
1028                         var promises = [];
1029     
1030                         angular.forEach(copy_list, function (cp) {
1031                             var n = new egCore.idl.bpbcm();
1032                             n.isnew(true);
1033                             n.peer_record(target_record);
1034                             n.target_copy(cp);
1035                             n.peer_type(type);
1036                             promises.push(egCore.pcrud.create(n));
1037                         });
1038     
1039                         return $q.all(promises).then(function(){$modalInstance.close()});
1040                     }
1041     
1042                     $scope.cancel = function($event) {
1043                         $modalInstance.dismiss();
1044                         $event.preventDefault();
1045                     }
1046                 }]
1047             });
1048         });
1049     }
1050
1051
1052     // ------------------------------------------------------------------
1053     // Holds 
1054     var provider = egGridDataProvider.instance({});
1055     $scope.hold_grid_data_provider = provider;
1056     $scope.grid_actions = egHoldGridActions;
1057     $scope.grid_actions.refresh = function () { provider.refresh() };
1058     $scope.hold_grid_controls = {};
1059
1060     var hold_ids = []; // current list of holds
1061     function fetchHolds(offset, count) {
1062         var ids = hold_ids.slice(offset, offset + count);
1063         return egHolds.fetch_holds(ids).then(null, null,
1064             function(hold_data) { 
1065                 return hold_data;
1066             }
1067         );
1068     }
1069
1070     provider.get = function(offset, count) {
1071         if ($scope.record_tab != 'holds') return $q.when();
1072         var deferred = $q.defer();
1073         hold_ids = []; // no caching ATM
1074
1075         // fetch the IDs
1076         egCore.net.request(
1077             'open-ils.circ',
1078             'open-ils.circ.holds.retrieve_all_from_title',
1079             egCore.auth.token(), $scope.record_id, 
1080             {pickup_lib : egCore.org.descendants($scope.pickup_ou.id(), true)}
1081         ).then(
1082             function(hold_data) {
1083                 angular.forEach(hold_data, function(list, type) {
1084                     hold_ids = hold_ids.concat(list);
1085                 });
1086                 fetchHolds(offset, count).then(
1087                     deferred.resolve, null, deferred.notify);
1088             }
1089         );
1090
1091         return deferred.promise;
1092     }
1093
1094     $scope.detail_view = function(action, user_data, items) {
1095         if (h = items[0]) {
1096             $scope.detail_hold_id = h.hold.id();
1097         }
1098     }
1099
1100     $scope.list_view = function(items) {
1101          $scope.detail_hold_id = null;
1102     }
1103
1104     // refresh the list of record holds when the pickup lib is changed.
1105     $scope.pickup_ou = egCore.org.get(egCore.auth.user().ws_ou());
1106     $scope.pickup_ou_changed = function(org) {
1107         $scope.pickup_ou = org;
1108         provider.refresh();
1109     }
1110
1111     $scope.print_holds = function() {
1112         var holds = [];
1113         angular.forEach($scope.hold_grid_controls.allItems(), function(item) {
1114             holds.push({
1115                 hold : egCore.idl.toHash(item.hold),
1116                 patron_last : item.patron_last,
1117                 patron_alias : item.patron_alias,
1118                 patron_barcode : item.patron_barcode,
1119                 copy : egCore.idl.toHash(item.copy),
1120                 volume : egCore.idl.toHash(item.volume),
1121                 title : item.mvr.title(),
1122                 author : item.mvr.author()
1123             });
1124         });
1125
1126         egCore.print.print({
1127             context : 'receipt', 
1128             template : 'holds_for_bib', 
1129             scope : {holds : holds}
1130         });
1131     }
1132
1133     $scope.mark_hold_transfer_dest = function() {
1134         egCore.hatch.setLocalItem(
1135             'eg.circ.hold.title_transfer_target', $scope.record_id);
1136     }
1137
1138     // UI presents this option as "all holds"
1139     $scope.transfer_holds_to_marked = function() {
1140         var hold_ids = $scope.hold_grid_controls.allItems().map(
1141             function(hold_data) {return hold_data.hold.id()});
1142         egHolds.transfer_to_marked_title(hold_ids);
1143     }
1144
1145     // ------------------------------------------------------------------
1146     // Initialize the selected tab
1147
1148     function init_cat_url() {
1149         // Set the initial catalog URL.  This only happens once.
1150         // The URL is otherwise generated through user navigation.
1151         if ($scope.catalog_url) return; 
1152
1153         var url = $location.absUrl().replace(/\/staff.*/, '/opac/advanced');
1154
1155         // A record ID in the path indicates a request for the record-
1156         // specific page.
1157         if ($routeParams.record_id) {
1158             url = url.replace(/advanced/, '/record/' + $scope.record_id);
1159         }
1160
1161         $scope.catalog_url = url;
1162     }
1163
1164     function init_parts_url() {
1165         $scope.parts_url = $location
1166             .absUrl()
1167             .replace(
1168                 /\/staff.*/,
1169                 '/conify/global/biblio/monograph_part?r='+$scope.record_id
1170             );
1171     }
1172
1173     $scope.set_record_tab = function(tab) {
1174         $scope.record_tab = tab;
1175
1176         switch(tab) {
1177
1178             case 'monoparts':
1179                 init_parts_url();
1180                 break;
1181
1182             case 'catalog':
1183                 init_cat_url();
1184                 break;
1185
1186             case 'holds':
1187                 $scope.detail_hold_record_id = $scope.record_id; 
1188                 // refresh the holds grid
1189                 provider.refresh();
1190                 break;
1191         }
1192     }
1193
1194     $scope.set_default_record_tab = function() {
1195         egCore.hatch.setLocalItem(
1196             'eg.cat.default_record_tab', $scope.record_tab);
1197         $timeout(function(){$scope.default_tab = $scope.record_tab});
1198     }
1199
1200     var tab;
1201     if ($scope.record_id) {
1202         $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
1203         tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
1204
1205     } else {
1206         tab = $routeParams.record_tab || 'catalog';
1207     }
1208     $scope.set_record_tab(tab);
1209
1210 }])
1211
1212 .controller('AuthorityCtrl',
1213        ['$scope','$routeParams','$location','$window','$q','egCore',
1214 function($scope , $routeParams , $location , $window , $q , egCore) {
1215
1216     // set record ID on page load if available...
1217     $scope.authority_id = $routeParams.authority_id;
1218
1219     if ($routeParams.authority_id) $scope.from_route = true;
1220     else $scope.from_route = false;
1221
1222     $scope.stop_unload = false;
1223 }])
1224
1225 .controller('URLVerifyCtrl',
1226        ['$scope','$location',
1227 function($scope , $location) {
1228     $scope.verifyurls_url = $location.absUrl().replace(/\/staff.*/, '/url_verify/sessions');
1229 }])
1230
1231 .controller('VandelayCtrl',
1232        ['$scope','$location',
1233 function($scope , $location) {
1234     $scope.vandelay_url = $location.absUrl().replace(/\/staff.*/, '/vandelay/vandelay');
1235 }])
1236
1237 .controller('ManageAuthoritiesCtrl',
1238        ['$scope','$location',
1239 function($scope , $location) {
1240     $scope.manageauthorities_url = $location.absUrl().replace(/\/staff.*/, '/cat/authority/list');
1241 }])
1242
1243 .controller('BatchEditCtrl',
1244        ['$scope','$location','$routeParams',
1245 function($scope , $location , $routeParams) {
1246     $scope.batchedit_url = $location.absUrl().replace(/\/eg.*/, '/opac/extras/merge_template');
1247     if ($routeParams.container_type) {
1248         switch ($routeParams.container_type) {
1249             case 'bucket':
1250                 $scope.batchedit_url += '?recordSource=b&containerid=' + $routeParams.container_id;
1251                 break;
1252             case 'record':
1253                 $scope.batchedit_url += '?recordSource=r&recid=' + $routeParams.container_id;
1254                 break;
1255         };
1256     }
1257 }])
1258
1259  
1260 .filter('boolText', function(){
1261     return function (v) {
1262         return v == 't';
1263     }
1264 })
1265
1266 .factory('holdingsSvc', 
1267        ['egCore','$q',
1268 function(egCore , $q) {
1269
1270     var service = {
1271         ongoing : false,
1272         copies : [], // record search results
1273         index : 0, // search grid index
1274         org : null,
1275         rid : null
1276     };
1277
1278     service.flesh = {   
1279         flesh : 2, 
1280         flesh_fields : {
1281             acp : ['status','location'],
1282             acn : ['prefix','suffix','copies']
1283         }
1284     }
1285
1286     service.fetchAgain = function() {
1287         return service.fetch({
1288             rid: service.rid,
1289             org: service.org,
1290             copy: service.copy,
1291             vol: service.vol,
1292             empty: service.empty
1293         })
1294     }
1295
1296     // resolved with the last received copy
1297     service.fetch = function(opts) {
1298         if (service.ongoing) {
1299             console.log('Skipping fetch, ongoing = true');
1300             return $q.when();
1301         }
1302
1303         var rid = opts.rid;
1304         var org = opts.org;
1305         var copy = opts.copy;
1306         var vol = opts.vol;
1307         var empty = opts.empty;
1308
1309         if (!rid) return $q.when();
1310         if (!org) return $q.when();
1311
1312         service.ongoing = true;
1313
1314         service.rid = rid;
1315         service.org = org;
1316         service.copy = opts.copy;
1317         service.vol = opts.vol;
1318         service.empty = opts.empty;
1319
1320         service.copies = [];
1321         service.index = 0;
1322
1323         var org_list = egCore.org.descendants(org.id(), true);
1324         console.log('Holdings fetch with: rid='+rid+' org='+org_list+' copy='+copy+' vol='+vol+' empty='+empty);
1325
1326         return egCore.pcrud.search(
1327             'acn',
1328             {record : rid, owning_lib : org_list, deleted : 'f'},
1329             service.flesh
1330         ).then(
1331             function() { // finished
1332                 service.copies = service.copies.sort(
1333                     function (a, b) {
1334                         function compare_array (x, y, i) {
1335                             if (x[i] && y[i]) { // both have values
1336                                 if (x[i] == y[i]) { // need to look deeper
1337                                     return compare_array(x, y, ++i);
1338                                 }
1339
1340                                 if (x[i] < y[i]) { // x is first
1341                                     return -1;
1342                                 } else if (x[i] > y[i]) { // y is first
1343                                     return 1;
1344                                 }
1345
1346                             } else { // no orgs to compare ...
1347                                 if (x[i]) return -1;
1348                                 if (y[i]) return 1;
1349                             }
1350                             return 0;
1351                         }
1352
1353                         var owner_order = compare_array(a.owner_list, b.owner_list, 0);
1354                         if (!owner_order) {
1355                             // now compare on CN label
1356                             if (a.call_number.label < b.call_number.label) return -1;
1357                             if (a.call_number.label > b.call_number.label) return 1;
1358
1359                             // try copy number
1360                             if (a.copy_number < b.copy_number) return -1;
1361                             if (a.copy_number > b.copy_number) return 1;
1362
1363                             // finally, barcode
1364                             if (a.barcode < b.barcode) return -1;
1365                             if (a.barcode > b.barcode) return 1;
1366                         }
1367                         return owner_order;
1368                     }
1369                 );
1370
1371                 // create a label using just the unique part of the owner list
1372                 var index = 0;
1373                 var prev_owner_list;
1374                 angular.forEach(service.copies, function (cp) {
1375                     if (!prev_owner_list) {
1376                         cp.owner_label = cp.owner_list.join(' ... ');
1377                     } else {
1378                         var current_owner_list = cp.owner_list.slice();
1379                         while (current_owner_list[1] && prev_owner_list[1] && current_owner_list[0] == prev_owner_list[0]) {
1380                             current_owner_list.shift();
1381                             prev_owner_list.shift();
1382                         }
1383                         cp.owner_label = current_owner_list.join(' ... ');
1384                     }
1385
1386                     cp.index = index++;
1387                     prev_owner_list = cp.owner_list.slice();
1388                 });
1389
1390                 var new_list = service.copies;
1391                 if (!copy || !vol) { // collapse copy rows, supply a count instead
1392
1393                     index = 0;
1394                     var cp_list = [];
1395                     var prev_key;
1396                     var current_blob = { copy_count : 0 };
1397                     angular.forEach(new_list, function (cp) {
1398                         if (!prev_key) {
1399                             prev_key = cp.owner_list.join('') + cp.call_number.label;
1400                             if (cp.barcode) current_blob.copy_count = 1;
1401                             current_blob.index = index++;
1402                             current_blob.id_list = cp.id_list;
1403                             if (cp.raw) current_blob.raw = cp.raw;
1404                             current_blob.call_number = cp.call_number;
1405                             current_blob.owner_list = cp.owner_list;
1406                             current_blob.owner_label = cp.owner_label;
1407                             current_blob.owner_id = cp.owner_id;
1408                         } else {
1409                             var current_key = cp.owner_list.join('') + cp.call_number.label;
1410                             if (prev_key == current_key) { // collapse into current_blob
1411                                 current_blob.copy_count++;
1412                                 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
1413                                 current_blob.raw = current_blob.raw.concat(cp.raw);
1414                             } else {
1415                                 current_blob.barcode = current_blob.copy_count;
1416                                 cp_list.push(current_blob);
1417                                 prev_key = current_key;
1418                                 current_blob = { copy_count : 0 };
1419                                 if (cp.barcode) current_blob.copy_count = 1;
1420                                 current_blob.index = index++;
1421                                 current_blob.id_list = cp.id_list;
1422                                 if (cp.raw) current_blob.raw = cp.raw;
1423                                 current_blob.owner_label = cp.owner_label;
1424                                 current_blob.owner_id = cp.owner_id;
1425                                 current_blob.call_number = cp.call_number;
1426                                 current_blob.owner_list = cp.owner_list;
1427                             }
1428                         }
1429                     });
1430
1431                     current_blob.barcode = current_blob.copy_count;
1432                     cp_list.push(current_blob);
1433                     new_list = cp_list;
1434
1435                     if (!vol) { // do the same for vol rows
1436
1437                         index = 0;
1438                         var cn_list = [];
1439                         prev_key = '';
1440                         current_blob = { copy_count : 0 };
1441                         angular.forEach(cp_list, function (cp) {
1442                             if (!prev_key) {
1443                                 prev_key = cp.owner_list.join('');
1444                                 current_blob.index = index++;
1445                                 current_blob.id_list = cp.id_list;
1446                                 if (cp.raw) current_blob.raw = cp.raw;
1447                                 current_blob.cn_count = 1;
1448                                 current_blob.copy_count = cp.copy_count;
1449                                 current_blob.owner_list = cp.owner_list;
1450                                 current_blob.owner_label = cp.owner_label;
1451                                 current_blob.owner_id = cp.owner_id;
1452                             } else {
1453                                 var current_key = cp.owner_list.join('');
1454                                 if (prev_key == current_key) { // collapse into current_blob
1455                                     current_blob.cn_count++;
1456                                     current_blob.copy_count += cp.copy_count;
1457                                     current_blob.id_list = current_blob.id_list.concat(cp.id_list);
1458                                     if (cp.raw) current_blob.raw = current_blob.raw.concat(cp.raw);
1459                                 } else {
1460                                     current_blob.barcode = current_blob.copy_count;
1461                                     current_blob.call_number = { label : current_blob.cn_count };
1462                                     cn_list.push(current_blob);
1463                                     prev_key = current_key;
1464                                     current_blob = { copy_count : 0 };
1465                                     current_blob.index = index++;
1466                                     current_blob.id_list = cp.id_list;
1467                                     if (cp.raw) current_blob.raw = cp.raw;
1468                                     current_blob.owner_label = cp.owner_label;
1469                                     current_blob.owner_id = cp.owner_id;
1470                                     current_blob.cn_count = 1;
1471                                     current_blob.copy_count = cp.copy_count;
1472                                     current_blob.owner_list = cp.owner_list;
1473                                 }
1474                             }
1475                         });
1476     
1477                         current_blob.barcode = current_blob.copy_count;
1478                         current_blob.call_number = { label : current_blob.cn_count };
1479                         cn_list.push(current_blob);
1480                         new_list = cn_list;
1481     
1482                     }
1483                 }
1484
1485                 service.copies = new_list;
1486                 service.ongoing = false;
1487             },
1488
1489             null, // error
1490
1491             // notify reads the stream of copies, one at a time.
1492             function(cn) {
1493
1494                 var copies = cn.copies().filter(function(cp){ return cp.deleted() == 'f' });
1495                 cn.copies([]);
1496
1497                 angular.forEach(copies, function (cp) {
1498                     cp.call_number(cn);
1499                 });
1500
1501                 var owner_id = cn.owning_lib();
1502                 var owner = egCore.org.get(owner_id);
1503
1504                 var owner_name_list = [];
1505                 while (owner.parent_ou()) { // we're going to skip the top of the tree...
1506                     owner_name_list.unshift(owner.name());
1507                     owner = egCore.org.get(owner.parent_ou());
1508                 }
1509
1510                 if (copies[0]) {
1511                     var flat = [];
1512                     angular.forEach(copies, function (cp) {
1513                         var flat_cp = egCore.idl.toHash(cp);
1514                         flat_cp.owner_id = owner_id;
1515                         flat_cp.owner_list = owner_name_list;
1516                         flat_cp.id_list = [flat_cp.id];
1517                         flat_cp.raw = [cp];
1518                         flat.push(flat_cp);
1519                     });
1520
1521                     service.copies = service.copies.concat(flat);
1522                 } else if (empty) {
1523                     service.copies.push({
1524                         owner_id   : owner_id,
1525                         owner_list : owner_name_list,
1526                         call_number: egCore.idl.toHash(cn),
1527                         raw_call_number: cn
1528                     });
1529                 }
1530
1531                 return cn;
1532             }
1533         );
1534     }
1535
1536     return service;
1537 }])
1538
1539 .factory('conjoinedSvc', 
1540        ['egCore','$q',
1541 function(egCore , $q) {
1542
1543     var service = {
1544         items : [], // record search results
1545         index : 0, // search grid index
1546         rid : null
1547     };
1548
1549     service.flesh = {   
1550         flesh : 4, 
1551         flesh_fields : {
1552             bpbcm : ['target_copy','peer_type'],
1553             acp : ['call_number'],
1554             acn : ['record'],
1555             bre : ['simple_record']
1556         },
1557         // avoid fetching the MARC blob by specifying which
1558         // fields on the bre to select.  More may be needed.
1559         // note that fleshed fields are explicitly selected.
1560         select : { bre : ['id'] },
1561         order_by : { bpbcm : ['id'] },
1562     }
1563
1564     // resolved with the last received copy
1565     service.fetch = function(rid) {
1566         if (!rid && !service.rid) return $q.when();
1567
1568         if (rid) service.rid = rid;
1569         service.items = [];
1570         service.index = 0;
1571
1572         return egCore.pcrud.search(
1573             'bpbcm',
1574             {peer_record : service.rid},
1575             service.flesh,
1576             {atomic : true}
1577         ).then( function(list) { // finished
1578             service.items = list;
1579             return service.items;
1580         });
1581     }
1582
1583     // returns a promise resolved with the list of peer bib types
1584     service.get_peer_types = function() {
1585         if (egCore.env.bpt)
1586             return $q.when(egCore.env.bpt.list);
1587
1588         return egCore.pcrud.retrieveAll('bpt', null, {atomic : true})
1589         .then(function(list) {
1590             egCore.env.absorbList(list, 'bpt');
1591             return list;
1592         });
1593     };
1594
1595     return service;
1596 }])
1597
1598