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