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