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