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