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