c853cb868672296f10f9aa6a589554af99fbd635
[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', 'ngToast','egPatronSearchMod',
11 'egSerialsMod','egSerialsAppDep'])
12
13 .config(['ngToastProvider', function(ngToastProvider) {
14   ngToastProvider.configure({
15     verticalPosition: 'bottom',
16     animation: 'fade'
17   });
18 }])
19
20 .config(function($routeProvider, $locationProvider, $compileProvider) {
21     $locationProvider.html5Mode(true);
22     $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|mailto|blob):/); // grid export
23         
24     var resolver = {delay : ['egCore','egStartup','egUser', function(egCore, egStartup, egUser) {
25         egCore.env.classLoaders.aous = function() {
26             return egCore.org.settings([
27                 'cat.marc_control_number_identifier'
28             ]).then(function(settings) {
29                 // local settings are cached within egOrg.  Caching them
30                 // again in egEnv just simplifies the syntax for access.
31                 egCore.env.aous = settings;
32             });
33         }
34         egCore.env.loadClasses.push('aous');
35         return egStartup.go()
36     }]};
37
38     $routeProvider.when('/cat/catalog/index', {
39         templateUrl: './cat/catalog/t_catalog',
40         controller: 'CatalogCtrl',
41         resolve : resolver
42     });
43
44     // Jump directly to the results page.  Any URL parameter 
45     // supported by the embedded catalog is supported here.
46     $routeProvider.when('/cat/catalog/results', {
47         templateUrl: './cat/catalog/t_catalog',
48         controller: 'CatalogCtrl',
49         resolve : resolver
50     });
51
52     $routeProvider.when('/cat/catalog/retrieve_by_id', {
53         templateUrl: './cat/catalog/t_retrieve_by_id',
54         controller: 'CatalogRecordRetrieve',
55         resolve : resolver
56     });
57
58     $routeProvider.when('/cat/catalog/retrieve_by_tcn', {
59         templateUrl: './cat/catalog/t_retrieve_by_tcn',
60         controller: 'CatalogRecordRetrieve',
61         resolve : resolver
62     });
63
64     $routeProvider.when('/cat/catalog/retrieve_by_authority_id', {
65         templateUrl: './cat/catalog/t_retrieve_by_authority_id',
66         controller: 'CatalogRecordRetrieve',
67         resolve : resolver
68     });
69
70     $routeProvider.when('/cat/catalog/new_bib', {
71         templateUrl: './cat/catalog/t_new_bib',
72         controller: 'NewBibCtrl',
73         resolve : resolver
74     });
75
76     // create some catalog page-specific mappings
77     $routeProvider.when('/cat/catalog/record/:record_id', {
78         templateUrl: './cat/catalog/t_catalog',
79         controller: 'CatalogCtrl',
80         resolve : resolver
81     });
82
83     // create some catalog page-specific mappings
84     $routeProvider.when('/cat/catalog/record/:record_id/:record_tab', {
85         templateUrl: './cat/catalog/t_catalog',
86         controller: 'CatalogCtrl',
87         resolve : resolver
88     });
89
90     $routeProvider.when('/cat/catalog/batchEdit', {
91         templateUrl: './cat/catalog/t_batchedit',
92         controller: 'BatchEditCtrl',
93         resolve : resolver
94     });
95
96     $routeProvider.when('/cat/catalog/batchEdit/:container_type/:container_id', {
97         templateUrl: './cat/catalog/t_batchedit',
98         controller: 'BatchEditCtrl',
99         resolve : resolver
100     });
101
102     $routeProvider.when('/cat/catalog/vandelay', {
103         templateUrl: './cat/catalog/t_vandelay',
104         controller: 'VandelayCtrl',
105         resolve : resolver
106     });
107
108     $routeProvider.when('/cat/catalog/verifyURLs', {
109         templateUrl: './cat/catalog/t_verifyurls',
110         controller: 'URLVerifyCtrl',
111         resolve : resolver
112     });
113
114     $routeProvider.when('/cat/catalog/manageAuthorities', {
115         templateUrl: './cat/catalog/t_manageauthorities',
116         controller: 'ManageAuthoritiesCtrl',
117         resolve : resolver
118     });
119
120     $routeProvider.when('/cat/catalog/authority/:authority_id/marc_edit', {
121         templateUrl: './cat/catalog/t_authority',
122         controller: 'AuthorityCtrl',
123         resolve : resolver
124     });
125
126     $routeProvider.otherwise({redirectTo : '/cat/catalog/index'});
127 })
128
129
130 /**
131  * */
132 .controller('CatalogRecordRetrieve',
133        ['$scope','$routeParams','$location','$q','egCore',
134 function($scope , $routeParams , $location , $q , egCore ) {
135
136     $scope.focusMe = true;
137
138     // jump to the patron checkout UI
139     function loadRecord(record_id) {
140         $location
141         .path('/cat/catalog/record/' + record_id);
142     }
143
144     function loadAuthorityRecord(record_id) {
145         $location
146         .path('/cat/catalog/authority/' + record_id + '/marc_edit');
147     }
148
149     $scope.submitId = function(args) {
150         $scope.recordNotFound = null;
151         if (!args.record_id) return;
152
153         // blur so next time it's set to true it will re-apply select()
154         $scope.selectMe = false;
155
156         return loadRecord(args.record_id);
157     }
158
159     $scope.submitAuthorityId = function(args) {
160         if (!args.record_id) return;
161
162         // blur so next time it's set to true it will re-apply select()
163         $scope.selectMe = false;
164
165         return loadAuthorityRecord(args.record_id);
166     }
167
168     $scope.submitTCN = function(args) {
169         $scope.recordNotFound = null;
170         $scope.moreRecordsFound = null;
171         if (!args.record_tcn) return;
172
173         // blur so next time it's set to true it will re-apply select()
174         $scope.selectMe = false;
175
176         // lookup TCN
177         egCore.net.request(
178             'open-ils.search',
179             'open-ils.search.biblio.tcn',
180             args.record_tcn)
181
182         .then(function(resp) { // get_barcodes
183
184             if (resp.count) {
185                 return $q.when(resp);
186             } else {
187                 // Search again including deleted records
188                 return egCore.net.request('open-ils.search', 
189                     'open-ils.search.biblio.tcn', args.record_tcn, true);
190             }
191
192         }).then(function(resp2) {
193
194             if (!resp2.count) {
195                 $scope.recordNotFound = args.record_tcn;
196                 $scope.selectMe = true;
197                 return;
198             }
199
200             if (resp2.count > 1) {
201                 $scope.moreRecordsFound = args.record_tcn;
202                 $scope.selectMe = true;
203                 return;
204             }
205
206             var record_id = resp2.ids[0];
207             return loadRecord(record_id);
208         });
209     }
210
211 }])
212
213 .controller('NewBibCtrl',
214        ['$scope','$routeParams','$location','$window','$q','egCore',
215         'egGridDataProvider','egHoldGridActions','$timeout','holdingsSvc',
216 function($scope , $routeParams , $location , $window , $q , egCore) {
217
218     $scope.have_template = false;
219     $scope.marc_template = '';
220     $scope.stop_unload = false;
221     $scope.template_list = [];
222     $scope.template_name = '';
223     $scope.new_bib_id = 0;
224
225     egCore.net.request(
226         'open-ils.cat',
227         'open-ils.cat.marc_template.types.retrieve'
228     ).then(function(resp) {
229         angular.forEach(resp, function(name) {
230             $scope.template_list.push(name);
231         });
232         $scope.template_list.sort();
233     });
234     $scope.template_name = egCore.hatch.getSessionItem('eg.cat.last_bib_marc_template');
235     if (!$scope.template_name) {
236         egCore.hatch.getItem('cat.default_bib_marc_template').then(function(template) {
237             $scope.template_name = template;
238         });
239     }
240
241     $scope.loadTemplate = function() {
242         if ($scope.template_name) {
243             egCore.net.request(
244                 'open-ils.cat',
245                 'open-ils.cat.biblio.marc_template.retrieve',
246                 $scope.template_name
247             ).then(function(template) {
248                 $scope.marc_template = template;
249                 $scope.have_template = true;
250                 egCore.hatch.setSessionItem('eg.cat.last_bib_marc_template', $scope.template_name);
251             });
252         }
253     }
254
255     $scope.setDefaultTemplate = function() {
256         var hatch_key = "cat.default_bib_marc_template";
257         if ($scope.template_name) {
258             egCore.hatch.setItem(hatch_key, $scope.template_name);
259         } else {
260             egCore.hatch.removeItem(hatch_key);
261         }
262     }
263
264     $scope.$watch('new_bib_id', function(newVal, oldVal) {
265         if (newVal) {
266             $location.path('/cat/catalog/record/' + $scope.new_bib_id);
267         }
268     });
269     
270
271 }])
272 .controller('CatalogCtrl',
273        ['$scope','$routeParams','$location','$window','$q','egCore','egHolds','egCirc','egConfirmDialog','ngToast',
274         'egGridDataProvider','egHoldGridActions','egProgressDialog','$timeout','$uibModal','holdingsSvc','egUser','conjoinedSvc',
275         '$cookies','egSerialsCoreSvc',
276 function($scope , $routeParams , $location , $window , $q , egCore , egHolds , egCirc , egConfirmDialog , ngToast ,
277          egGridDataProvider , egHoldGridActions , egProgressDialog , $timeout , $uibModal , holdingsSvc , egUser , conjoinedSvc,
278          $cookies , egSerialsCoreSvc
279 ) {
280
281     var holdingsSvcInst = new holdingsSvc();
282
283     // set record ID on page load if available...
284     $scope.record_id = $routeParams.record_id;
285     $scope.summary_pane_record;
286
287     if ($scope.record_id) {
288         // TODO: Apply tab-specific title contexts
289         egCore.strings.setPageTitle(
290             egCore.strings.PAGE_TITLE_BIB_DETAIL,
291             egCore.strings.PAGE_TITLE_CATALOG_CONTEXT,
292             {record_id : $scope.record_id}
293         );
294     } else {
295         // Default to title = Catalog
296         egCore.strings.setPageTitle(
297             egCore.strings.PAGE_TITLE_CATALOG_CONTEXT);
298     }
299
300     if ($routeParams.record_id) $scope.from_route = true;
301     else $scope.from_route = false;
302
303     // set search and preferred library cookies
304     egCore.hatch.getItem('eg.search.search_lib').then(function(val) {
305         $cookies.put('eg_search_lib', val, { path : '/' });
306     });
307     egCore.hatch.getItem('eg.search.pref_lib').then(function(val) {
308         $cookies.put('eg_pref_lib', val, { path : '/' });
309     });
310
311     // will hold a ref to the opac iframe
312     $scope.opac_iframe = null;
313     $scope.parts_iframe = null;
314
315     $scope.search_result_index = 1;
316     $scope.search_result_hit_count = 1;
317
318     $scope.$watch(
319         'opac_iframe.dom.contentWindow.search_result_index',
320         function (n,o) {
321             if (!isNaN(parseInt(n)))
322                 $scope.search_result_index = n + 1;
323         }
324     );
325
326     $scope.$watch(
327         'opac_iframe.dom.contentWindow.search_result_hit_count',
328         function (n,o) {
329             if (!isNaN(parseInt(n)))
330                 $scope.search_result_hit_count = n;
331         }
332     );
333
334     $scope.in_opac_call = false;
335     $scope.opac_call = function (opac_frame_function, force_opac_tab) {
336         if ($scope.opac_iframe) {
337             if (force_opac_tab) $scope.record_tab = 'catalog';
338             $scope.in_opac_call = true;
339             $scope.opac_iframe.dom.contentWindow[opac_frame_function]();
340             if (opac_frame_function == 'rdetailBackToResults') {
341                 $location.update_path('/cat/catalog/index');
342             }
343         }
344     }
345
346     $scope.add_cart_to_record_bucket = function() {
347         var cartkey = $cookies.get('cartcache');
348         if (!cartkey) return;
349         egCore.net.request(
350             'open-ils.actor',
351             'open-ils.actor.anon_cache.get_value',
352             cartkey,
353             'mylist'
354         ).then(function(list) {
355             list = list.map(function(x) {
356                 return parseInt(x);
357             });
358             $scope.add_to_record_bucket(list);
359         });
360     }
361
362     $scope.add_to_record_bucket = function(recs) {
363         if (!angular.isArray(recs)) {
364             recs = [ $scope.record_id ];
365         }
366         return $uibModal.open({
367             templateUrl: './cat/catalog/t_add_to_bucket',
368             backdrop: 'static',
369             animation: true,
370             size: 'md',
371             controller:
372                    ['$scope','$uibModalInstance',
373             function($scope , $uibModalInstance) {
374
375                 $scope.bucket_id = 0;
376                 $scope.newBucketName = '';
377                 $scope.allBuckets = [];
378                 egCore.net.request(
379                     'open-ils.actor',
380                     'open-ils.actor.container.retrieve_by_class.authoritative',
381                     egCore.auth.token(), egCore.auth.user().id(),
382                     'biblio', 'staff_client'
383                 ).then(function(buckets) { $scope.allBuckets = buckets; });
384
385                 $scope.add_to_bucket = function() {
386                     var promises = [];
387                     angular.forEach(recs, function(recId) {
388                         var item = new egCore.idl.cbrebi();
389                         item.bucket($scope.bucket_id);
390                         item.target_biblio_record_entry(recId);
391                         promises.push(egCore.net.request(
392                             'open-ils.actor',
393                             'open-ils.actor.container.item.create',
394                             egCore.auth.token(), 'biblio', item
395                         ));
396                     });
397                     $q.all(promises).then(function(resp) {
398                         $uibModalInstance.close();
399                     });
400                 }
401
402                 $scope.add_to_new_bucket = function() {
403                     var bucket = new egCore.idl.cbreb();
404                     bucket.owner(egCore.auth.user().id());
405                     bucket.name($scope.newBucketName);
406                     bucket.description('');
407                     bucket.btype('staff_client');
408
409                     egCore.net.request(
410                         'open-ils.actor',
411                         'open-ils.actor.container.create',
412                         egCore.auth.token(), 'biblio', bucket
413                     ).then(function(bucket) {
414                         $scope.bucket_id = bucket;
415                         $scope.add_to_bucket();
416                     });
417                 }
418
419                 $scope.cancel = function() {
420                     $uibModalInstance.dismiss();
421                 }
422             }]
423         });
424     }
425
426     $scope.current_overlay_target     = egCore.hatch.getLocalItem('eg.cat.marked_overlay_record');
427     $scope.current_transfer_target    = egCore.hatch.getLocalItem('eg.cat.transfer_target_record');
428     $scope.current_conjoined_target   = egCore.hatch.getLocalItem('eg.cat.marked_conjoined_record');
429
430     $scope.quickReceive = function () {
431         var list = [];
432         var next_per_stream = {};
433
434         var recId = $scope.record_id;
435         return $uibModal.open({
436             templateUrl: './share/t_subscription_select_dialog',
437             backdrop: 'static',
438             controller: ['$scope', '$uibModalInstance',
439                 function($scope, $uibModalInstance) {
440
441                     $scope.focus = true;
442                     $scope.rememberMe = 'eg.serials.quickreceive.last_org';
443                     $scope.record_id = recId;
444                     $scope.ssubId = null;
445
446                     $scope.ok = function() { $uibModalInstance.close($scope.ssubId) }
447                     $scope.cancel = function() { $uibModalInstance.dismiss(); }
448                 }
449             ]
450         }).result.then(function(ssubId) {
451             if (ssubId) {
452                 var promises = [];
453                 promises.push(egSerialsCoreSvc.fetchItemsForSub(ssubId,{status:'Expected'}).then(function(){
454                     angular.forEach(egSerialsCoreSvc.itemTree, function (item) {
455                         if (next_per_stream[item.stream().id()]) return;
456                         if (item.status() == 'Expected') {
457                             next_per_stream[item.stream().id()] = item;
458                             list.push(egCore.idl.Clone(item));
459                         }
460                     });
461                 }));
462
463                 return $q.all(promises).then(function() {
464
465                     if (!list.length) {
466                         ngToast.warning(egCore.strings.SERIALS_NO_ITEMS);
467                         return $q.reject();
468                     }
469
470                     return egSerialsCoreSvc.process_items(
471                         'receive',
472                         $scope.record_id,
473                         list,
474                         true, // barcode
475                         false,// bind
476                         false, // print by default
477                         function() { $scope.holdings_record_id_changed($scope.record_id) }
478                     );
479                 });
480             } else {
481                 ngToast.warning(egCore.strings.SERIALS_NO_SUBS);
482                 return $q.reject();
483             }
484         });
485     }
486
487     $scope.markConjoined = function () {
488         $scope.current_conjoined_target = $scope.record_id;
489         egCore.hatch.setLocalItem('eg.cat.marked_conjoined_record',$scope.record_id);
490         ngToast.create(egCore.strings.MARK_CONJ_TARGET);
491     };
492
493     $scope.markHoldingsTransfer = function () {
494         $scope.current_transfer_target = $scope.record_id;
495         egCore.hatch.setLocalItem('eg.cat.transfer_target_record',$scope.record_id);
496         egCore.hatch.removeLocalItem('eg.cat.transfer_target_lib');
497         egCore.hatch.removeLocalItem('eg.cat.transfer_target_vol');
498         ngToast.create(egCore.strings.MARK_HOLDINGS_TARGET);
499     };
500
501     $scope.markOverlay = function () {
502         $scope.current_overlay_target = $scope.record_id;
503         egCore.hatch.setLocalItem('eg.cat.marked_overlay_record',$scope.record_id);
504         ngToast.create(egCore.strings.MARK_OVERLAY_TARGET);
505     };
506
507     $scope.clearRecordMarks = function () {
508         $scope.current_overlay_target     = null;
509         $scope.current_transfer_target    = null;
510         $scope.current_conjoined_target   = null;
511         $scope.current_hold_transfer_dest = null;
512         egCore.hatch.removeLocalItem('eg.cat.transfer_target_record');
513         egCore.hatch.removeLocalItem('eg.cat.marked_conjoined_record');
514         egCore.hatch.removeLocalItem('eg.cat.marked_overlay_record');
515         egCore.hatch.removeLocalItem('eg.circ.hold.title_transfer_target');
516     }
517
518     $scope.stop_unload = false;
519     $scope.$watch('stop_unload',
520         function(newVal, oldVal) {
521             if (newVal && newVal != oldVal && $scope.opac_iframe) {
522                 $($scope.opac_iframe.dom.contentWindow).on('beforeunload', function(){
523                     return 'There is unsaved data in this record.'
524                 });
525             } else {
526                 if ($scope.opac_iframe)
527                     $($scope.opac_iframe.dom.contentWindow).off('beforeunload');
528             }
529         }
530     );
531
532     // Set the "last bib" cookie, if we have that
533     if ($scope.record_id)
534         egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
535
536     $scope.refresh_record_callback = function (record_id) {
537         egCore.pcrud.retrieve('bre', record_id, {
538             flesh : 1,
539             flesh_fields : {
540                 bre : ['simple_record','creator','editor']
541             }
542         }).then(function(rec) {
543             rec.owner(egCore.org.get(rec.owner()));
544             $scope.summary_pane_record = rec;
545         });
546
547         return record_id;
548     }
549
550     patron_search_dialog = function() {
551         return $uibModal.open({
552             templateUrl: './share/t_patron_selector',
553             backdrop: 'static',
554             size: 'lg',
555             animation: true,
556             controller:
557                    ['$scope','$uibModalInstance','$controller',
558             function($scope , $uibModalInstance , $controller) {
559                 angular.extend(this, $controller('BasePatronSearchCtrl', {$scope : $scope}));
560                 $scope.clearForm();
561                 $scope.need_one_selected = function() {
562                     var items = $scope.gridControls.selectedItems();
563                     return (items.length == 1) ? false : true
564                 }
565                 $scope.ok = function() {
566                     var items = $scope.gridControls.selectedItems();
567                     if (items.length == 1) {
568                         $uibModalInstance.close(items[0].card().barcode());
569                     } else {
570                         $uibModalInstance.close()
571                     }
572                 }
573                 $scope.cancel = function($event) {
574                     $uibModalInstance.dismiss();
575                     $event.preventDefault();
576                 }
577             }]
578         });
579     }
580
581     // also set it when the iframe changes to a new record
582     $scope.handle_page = function(url) {
583
584         if (!url || url == 'about:blank') {
585             // nothing loaded.  If we already have a record ID, leave it.
586             return;
587         }
588
589         var prev_record_id = $scope.record_id;
590         var match = url.match(/\/+opac\/+record\/+(\d+)/);
591         if (match) {
592             $scope.record_id = match[1];
593             egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
594             $scope.holdings_record_id_changed($scope.record_id);
595             conjoinedSvc.fetch($scope.record_id).then(function(){
596                 $scope.conjoinedGridDataProvider.refresh();
597             });
598             init_parts_url();
599             $scope.grid_actions.refresh();
600             $location.update_path('/cat/catalog/record/' + $scope.record_id);
601             // update_path() bypasses the controller for path 
602             // /cat/catalog/record/:record_id. Manually set title here too.
603             egCore.strings.setPageTitle(
604                 egCore.strings.PAGE_TITLE_BIB_DETAIL,
605                 egCore.strings.PAGE_TITLE_CATALOG_CONTEXT,
606                 {record_id : $scope.record_id}
607             );
608         } else {
609             delete $scope.record_id;
610             $scope.from_route = false;
611         }
612
613         // child scope is executing this function, so our digest doesn't fire ... thus,
614         $scope.$apply();
615
616         // don't change tabs if we are using the OPAC nav buttons,
617         // or we didn't change records on the OPAC load
618         if (!$scope.in_opac_call && ($scope.record_id != prev_record_id)) {
619             if ($scope.record_id) {
620                 $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
621                 tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
622             } else {
623                 tab = $routeParams.record_tab || 'catalog';
624             }
625             $scope.set_record_tab(tab);
626         } else {
627             $scope.in_opac_call = false;
628         }
629
630         if ($scope.opac_iframe && $location.path().match(/cat\/catalog/)) {
631             var doc = $scope.opac_iframe.dom.contentWindow.document;
632             $(doc).find('#hold_usr_search').show();
633             $(doc).find('#hold_usr_search').on('click', function() {
634                 patron_search_dialog().result.then(function(barc) {
635                     $(doc).find('#hold_usr_input').val(barc);
636                     $(doc).find('#hold_usr_input').change();
637                 });
638             });
639             $(doc).find('#select_basket_action').on('change', function() {
640                 if (this.options[this.selectedIndex].value && this.options[this.selectedIndex].value == "add_cart_to_bucket") {
641                     $scope.add_cart_to_record_bucket();
642                 }
643             });
644         }
645
646     }
647
648     // xulG catalog handlers
649     $scope.handlers = { }
650
651     // ------------------------------------------------------------------
652     // Conjoined items
653
654     $scope.conjoinedGridControls = {};
655     $scope.conjoinedGridDataProvider = egGridDataProvider.instance({
656         get : function(offset, count) {
657             return this.arrayNotifier(conjoinedSvc.items, offset, count);
658         }
659     });
660
661     $scope.changeConjoinedType = function () {
662         var peers = egCore.idl.Clone($scope.conjoinedGridControls.selectedItems());
663         angular.forEach(peers, function (p) {
664             p.target_copy(p.target_copy().id());
665             p.peer_type(p.peer_type().id());
666         });
667
668         var conjoinedGridDataProviderRef = $scope.conjoinedGridDataProvider;
669
670         return $uibModal.open({
671             templateUrl: './cat/catalog/t_conjoined_selector',
672             backdrop: 'static',
673             animation: true,
674             controller:
675                    ['$scope','$uibModalInstance',
676             function($scope , $uibModalInstance) {
677                 $scope.update = true;
678
679                 $scope.peer_type = null;
680                 $scope.peer_type_list = [];
681                 conjoinedSvc.get_peer_types().then(function(list){
682                     $scope.peer_type_list = list;
683                 });
684     
685                 $scope.ok = function(type) {
686                     var promises = [];
687     
688                     angular.forEach(peers, function (p) {
689                         p.ischanged(1);
690                         p.peer_type(type);
691                         promises.push(egCore.pcrud.update(p));
692                     });
693     
694                     return $q.all(promises)
695                         .then(function(){$uibModalInstance.close()})
696                         .then(function(){return conjoinedSvc.fetch()})
697                         .then(function(){conjoinedGridDataProviderRef.refresh()});
698                 }
699     
700                 $scope.cancel = function($event) {
701                     $uibModalInstance.dismiss();
702                     $event.preventDefault();
703                 }
704             }]
705         });
706         
707     }
708
709     $scope.refreshConjoined = function () {
710         conjoinedSvc.fetch($scope.record_id)
711         .then(function(){$scope.conjoinedGridDataProvider.refresh();});
712     }
713
714     $scope.deleteSelectedConjoined = function () {
715         var peers = $scope.conjoinedGridControls.selectedItems();
716
717         if (peers.length > 0) {
718             egConfirmDialog.open(
719                 egCore.strings.CONFIRM_DELETE_PEERS,
720                 egCore.strings.CONFIRM_DELETE_PEERS_MESSAGE,
721                 {peers : peers.length}
722             ).result.then(function() {
723                 angular.forEach(peers, function (p) {
724                     p.isdeleted(1);
725                 });
726
727                 egCore.pcrud.remove(peers).then(function() {
728                     return conjoinedSvc.fetch();
729                 }).then(function() {
730                     $scope.conjoinedGridDataProvider.refresh();
731                 });
732             });
733         }
734     }
735     if ($scope.record_id)
736         conjoinedSvc.fetch($scope.record_id);
737
738     // ------------------------------------------------------------------
739     // Holdings
740
741     $scope.holdingsGridControls = {
742         activateItem : function (item) {
743             $scope.selectedHoldingsVolCopyEdit();
744         }
745     };
746     $scope.holdingsGridDataProvider = egGridDataProvider.instance({
747         get : function(offset, count) {
748             return this.arrayNotifier(holdingsSvcInst.copies, offset, count);
749         }
750     });
751
752     $scope.add_copies_to_bucket = function() {
753         var copy_list = gatherSelectedHoldingsIds();
754         if (copy_list.length == 0) return;
755
756         return $uibModal.open({
757             templateUrl: './cat/catalog/t_add_to_bucket',
758             backdrop: 'static',
759             animation: true,
760             size: 'md',
761             controller:
762                    ['$scope','$uibModalInstance',
763             function($scope , $uibModalInstance) {
764
765                 $scope.bucket_id = 0;
766                 $scope.newBucketName = '';
767                 $scope.allBuckets = [];
768
769                 egCore.net.request(
770                     'open-ils.actor',
771                     'open-ils.actor.container.retrieve_by_class.authoritative',
772                     egCore.auth.token(), egCore.auth.user().id(),
773                     'copy', 'staff_client'
774                 ).then(function(buckets) { $scope.allBuckets = buckets; });
775
776                 $scope.add_to_bucket = function() {
777                     var promises = [];
778                     angular.forEach(copy_list, function (cp) {
779                         var item = new egCore.idl.ccbi()
780                         item.bucket($scope.bucket_id);
781                         item.target_copy(cp);
782                         promises.push(
783                             egCore.net.request(
784                                 'open-ils.actor',
785                                 'open-ils.actor.container.item.create',
786                                 egCore.auth.token(), 'copy', item
787                             )
788                         );
789
790                         return $q.all(promises).then(function() {
791                             $uibModalInstance.close();
792                         });
793                     });
794                 }
795
796                 $scope.add_to_new_bucket = function() {
797                     var bucket = new egCore.idl.ccb();
798                     bucket.owner(egCore.auth.user().id());
799                     bucket.name($scope.newBucketName);
800                     bucket.description('');
801                     bucket.btype('staff_client');
802
803                     return egCore.net.request(
804                         'open-ils.actor',
805                         'open-ils.actor.container.create',
806                         egCore.auth.token(), 'copy', bucket
807                     ).then(function(bucket) {
808                         $scope.bucket_id = bucket;
809                         $scope.add_to_bucket();
810                     });
811                 }
812
813                 $scope.cancel = function() {
814                     $uibModalInstance.dismiss();
815                 }
816             }]
817         });
818     }
819
820     // TODO: refactor common code between cat/catalog/app.js and cat/item/app.js 
821
822     $scope.need_one_selected = function() {
823         var items = $scope.holdingsGridControls.selectedItems();
824         if (items.length == 1) return false;
825         return true;
826     };
827
828     $scope.make_copies_bookable = function() {
829
830         var copies_by_record = {};
831         var record_list = [];
832         angular.forEach(
833             $scope.holdingsGridControls.selectedItems(),
834             function (item) {
835                 var record_id = item['call_number.record.id'];
836                 if (typeof copies_by_record[ record_id ] == 'undefined') {
837                     copies_by_record[ record_id ] = [];
838                     record_list.push( record_id );
839                 }
840                 copies_by_record[ record_id ].push(item.id);
841             }
842         );
843
844         var promises = [];
845         var combined_results = [];
846         angular.forEach(record_list, function(record_id) {
847             promises.push(
848                 egCore.net.request(
849                     'open-ils.booking',
850                     'open-ils.booking.resources.create_from_copies',
851                     egCore.auth.token(),
852                     copies_by_record[record_id]
853                 ).then(function(results) {
854                     if (results && results['brsrc']) {
855                         combined_results = combined_results.concat(results['brsrc']);
856                     }
857                 })
858             );
859         });
860
861         $q.all(promises).then(function() {
862             if (combined_results.length > 0) {
863                 $uibModal.open({
864                     template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
865                     backdrop: 'static',
866                     animation: true,
867                     size: 'md',
868                     controller:
869                            ['$scope','$location','egCore','$uibModalInstance',
870                     function($scope , $location , egCore , $uibModalInstance) {
871
872                         $scope.funcs = {
873                             ses : egCore.auth.token(),
874                             resultant_brsrc : combined_results.map(function(o) { return o[0]; })
875                         }
876
877                         var booking_path = '/eg/conify/global/booking/resource';
878
879                         $scope.booking_admin_url =
880                             $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
881                     }]
882                 });
883             }
884         });
885     }
886
887     $scope.book_copies_now = function() {
888         var copies_by_record = {};
889         var record_list = [];
890         angular.forEach(
891             $scope.holdingsGridControls.selectedItems(),
892             function (item) {
893                 var record_id = item['call_number.record.id'];
894                 if (typeof copies_by_record[ record_id ] == 'undefined') {
895                     copies_by_record[ record_id ] = [];
896                     record_list.push( record_id );
897                 }
898                 copies_by_record[ record_id ].push(item.id);
899             }
900         );
901
902         var promises = [];
903         var combined_brt = [];
904         var combined_brsrc = [];
905         angular.forEach(record_list, function(record_id) {
906             promises.push(
907                 egCore.net.request(
908                     'open-ils.booking',
909                     'open-ils.booking.resources.create_from_copies',
910                     egCore.auth.token(),
911                     copies_by_record[record_id]
912                 ).then(function(results) {
913                     if (results && results['brt']) {
914                         combined_brt = combined_brt.concat(results['brt']);
915                     }
916                     if (results && results['brsrc']) {
917                         combined_brsrc = combined_brsrc.concat(results['brsrc']);
918                     }
919                 })
920             );
921         });
922
923         $q.all(promises).then(function() {
924             if (combined_brt.length > 0 || combined_brsrc.length > 0) {
925                 $uibModal.open({
926                     template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
927                     backdrop: 'static',
928                     animation: true,
929                     size: 'md',
930                     controller:
931                            ['$scope','$location','egCore','$uibModalInstance',
932                     function($scope , $location , egCore , $uibModalInstance) {
933
934                         $scope.funcs = {
935                             ses : egCore.auth.token(),
936                             bresv_interface_opts : {
937                                 booking_results : {
938                                      brt : combined_brt
939                                     ,brsrc : combined_brsrc
940                                 }
941                             }
942                         }
943
944                         var booking_path = '/eg/booking/reservation';
945
946                         $scope.booking_admin_url =
947                             $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
948
949                     }]
950                 });
951             }
952         });
953     }
954
955
956     $scope.requestItems = function() {
957         var copy_list = gatherSelectedHoldingsIds();
958         if (copy_list.length == 0) return;
959
960         return $uibModal.open({
961             templateUrl: './cat/catalog/t_request_items',
962             animation: true,
963             controller:
964                    ['$scope','$uibModalInstance',
965             function($scope , $uibModalInstance) {
966                 $scope.user = null;
967                 $scope.first_user_fetch = true;
968
969                 $scope.hold_data = {
970                     hold_type : 'C',
971                     copy_list : copy_list,
972                     pickup_lib: egCore.org.get(egCore.auth.user().ws_ou()),
973                     user      : egCore.auth.user().id()
974                 };
975
976                 egUser.get( $scope.hold_data.user ).then(function(u) {
977                     $scope.user = u;
978                     $scope.barcode = u.card().barcode();
979                     $scope.user_name = egUser.format_name(u);
980                     $scope.hold_data.user = u.id();
981                 });
982
983                 $scope.user_name = '';
984                 $scope.barcode = '';
985                 $scope.$watch('barcode', function (n) {
986                     if (!$scope.first_user_fetch) {
987                         egUser.getByBarcode(n).then(function(u) {
988                             $scope.user = u;
989                             $scope.user_name = egUser.format_name(u);
990                             $scope.hold_data.user = u.id();
991                         }, function() {
992                             $scope.user = null;
993                             $scope.user_name = '';
994                             delete $scope.hold_data.user;
995                         });
996                     }
997                     $scope.first_user_fetch = false;
998                 });
999
1000                 $scope.ok = function(h) {
1001                     var args = {
1002                         patronid  : h.user,
1003                         hold_type : h.hold_type,
1004                         pickup_lib: h.pickup_lib.id(),
1005                         depth     : 0
1006                     };
1007
1008                     egCore.net.request(
1009                         'open-ils.circ',
1010                         'open-ils.circ.holds.test_and_create.batch.override',
1011                         egCore.auth.token(), args, h.copy_list
1012                     );
1013
1014                     $uibModalInstance.close();
1015                 }
1016
1017                 $scope.cancel = function($event) {
1018                     $uibModalInstance.dismiss();
1019                     $event.preventDefault();
1020                 }
1021             }]
1022         });
1023     }
1024
1025     $scope.view_place_orders = function() {
1026         if (!$scope.record_id) return;
1027         var url = egCore.env.basePath + 'acq/legacy/lineitem/related/' + $scope.record_id + '?target=bib';
1028         $timeout(function() { $window.open(url, '_blank') });
1029     }
1030
1031     $scope.replaceBarcodes = function() {
1032         var copy_list = gatherSelectedRawCopies();
1033         if (copy_list.length == 0) return;
1034
1035         var holdingsGridDataProviderRef = $scope.holdingsGridDataProvider;
1036
1037         angular.forEach(copy_list, function (cp) {
1038             $uibModal.open({
1039                 templateUrl: './cat/share/t_replace_barcode',
1040                 backdrop: 'static',
1041                 animation: true,
1042                 controller:
1043                            ['$scope','$uibModalInstance',
1044                     function($scope , $uibModalInstance) {
1045                         $scope.isModal = true;
1046                         $scope.focusBarcode = false;
1047                         $scope.focusBarcode2 = true;
1048                         $scope.barcode1 = cp.barcode();
1049
1050                         $scope.updateBarcode = function() {
1051                             $scope.copyNotFound = false;
1052                             $scope.updateOK = false;
1053                 
1054                             egCore.pcrud.search('acp',
1055                                 {deleted : 'f', barcode : $scope.barcode1})
1056                             .then(function(copy) {
1057                 
1058                                 if (!copy) {
1059                                     $scope.focusBarcode = true;
1060                                     $scope.copyNotFound = true;
1061                                     return;
1062                                 }
1063                 
1064                                 $scope.copyId = copy.id();
1065                                 copy.barcode($scope.barcode2);
1066                 
1067                                 egCore.pcrud.update(copy).then(function(stat) {
1068                                     $scope.updateOK = stat;
1069                                     $scope.focusBarcode = true;
1070                                     holdingsSvc.fetchAgain().then(function (){
1071                                         holdingsGridDataProviderRef.refresh();
1072                                     });
1073                                 });
1074
1075                             });
1076                             $uibModalInstance.close();
1077                         }
1078
1079                         $scope.cancel = function($event) {
1080                             $uibModalInstance.dismiss();
1081                             $event.preventDefault();
1082                         }
1083                     }
1084                 ]
1085             });
1086         });
1087     }
1088
1089     var holdings_bChannel = null;
1090     // subscribe to BroadcastChannel for any child VolCopy tabs
1091     // refresh grid if needed to show new updates
1092     // if ($scope.record_tab === 'holdings'){
1093     $scope.$watch('record_tab', function(n){
1094     
1095         if (n === 'holdings'){
1096             if (typeof BroadcastChannel != 'undefined') {
1097                 // we're in holdings tab, connect 2 bChannel
1098                 holdings_bChannel = new BroadcastChannel('eg.holdings.update');
1099                 holdings_bChannel.onmessage = function(e){
1100                     if (e.data
1101                         && e.data.records
1102                         && e.data.records.length
1103                         && e.data.records.includes(Number($scope.record_id))
1104                     ){ // it's for us, refresh grid!
1105                         console.log("Got broadcast from channel eg.holdings.update for records " + e.data.records);
1106                         $scope.holdings_record_id_changed($scope.record_id);
1107                     }
1108                 }
1109             };
1110
1111         } else if (holdings_bChannel){ // we're leaving holding tab, close bChannel
1112             holdings_bChannel.close();
1113         }
1114     
1115     });
1116
1117     // refresh the list of holdings when the record_id is changed.
1118     $scope.holdings_record_id_changed = function(id) {
1119         if ($scope.record_id != id) $scope.record_id = id;
1120         console.log('record id changed to ' + id + ', loading new holdings');
1121         holdingsSvcInst.fetch({
1122             rid : $scope.record_id,
1123             org : $scope.holdings_ou,
1124             copy: $scope.holdings_show_vols ? $scope.holdings_show_copies : false,
1125             vol : $scope.holdings_show_vols,
1126             empty: $scope.holdings_show_empty,
1127             empty_org: $scope.holdings_show_empty_org
1128         }).then(function() {
1129             $scope.holdingsGridDataProvider.refresh();
1130         });
1131     }
1132
1133     // refresh the list of holdings when the filter lib is changed.
1134     $scope.holdings_ou = egCore.org.get(egCore.auth.user().ws_ou());
1135     $scope.holdings_ou_changed = function(org) {
1136         $scope.holdings_ou = org;
1137         holdingsSvcInst.fetch({
1138             rid : $scope.record_id,
1139             org : $scope.holdings_ou,
1140             copy: $scope.holdings_show_vols ? $scope.holdings_show_copies : false,
1141             vol : $scope.holdings_show_vols,
1142             empty: $scope.holdings_show_empty,
1143             empty_org: $scope.holdings_show_empty_org
1144         }).then(function() {
1145             $scope.holdingsGridDataProvider.refresh();
1146         });
1147     }
1148
1149     $scope.holdings_cb_changed = function(cb,newVal,norefresh) {
1150         $scope[cb] = newVal;
1151         var x = $scope.holdings_show_vols ? $scope.holdings_show_copies : false;
1152         $('#holdings_show_copies').prop('checked', x);
1153         egCore.hatch.setItem('cat.' + cb, newVal);
1154         if (!norefresh) holdingsSvcInst.fetch({
1155             rid : $scope.record_id,
1156             org : $scope.holdings_ou,
1157             copy: $scope.holdings_show_vols ? $scope.holdings_show_copies : false,
1158             vol : $scope.holdings_show_vols,
1159             empty: $scope.holdings_show_empty,
1160             empty_org: $scope.holdings_show_empty_org
1161         }).then(function() {
1162             $scope.holdingsGridDataProvider.refresh();
1163         });
1164     }
1165
1166     egCore.hatch.getItem('cat.holdings_show_vols').then(function(x){
1167         if (typeof x ==  'undefined') x = true;
1168         $scope.holdings_cb_changed('holdings_show_vols',x,true);
1169         $('#holdings_show_vols').prop('checked', x);
1170     }).then(function(){
1171         egCore.hatch.getItem('cat.holdings_show_copies').then(function(x){
1172             if (typeof x ==  'undefined') x = true;
1173             $scope.holdings_cb_changed('holdings_show_copies',x,true);
1174             x = $scope.holdings_show_vols ? x : false;
1175             $('#holdings_show_copies').prop('checked', x);
1176         }).then(function(){
1177             egCore.hatch.getItem('cat.holdings_show_empty').then(function(x){
1178                 if (typeof x ==  'undefined') x = true;
1179                 $scope.holdings_cb_changed('holdings_show_empty',x);
1180                 $('#holdings_show_empty').prop('checked', x);
1181             }).then(function(){
1182                 egCore.hatch.getItem('cat.holdings_show_empty_org').then(function(x){
1183                     if (typeof x ==  'undefined') x = true;
1184                     $scope.holdings_cb_changed('holdings_show_empty_org',x);
1185                     $('#holdings_show_empty_org').prop('checked', x);
1186                 })
1187             })
1188         })
1189     });
1190
1191     $scope.vols_not_shown = function () {
1192         return !$scope.holdings_show_vols;
1193     }
1194
1195     $scope.copies_not_shown = function () {
1196         return !$scope.holdings_show_copies;
1197     }
1198
1199     $scope.empty_org_not_shown = function () {
1200         return !$scope.holdings_show_empty_org;
1201     }
1202
1203     $scope.holdings_checkbox_handler = function (item) {
1204         $scope.holdings_cb_changed(item.checkbox,item.checked);
1205     }
1206
1207     function gatherSelectedHoldingsIds () {
1208         var cp_id_list = [];
1209         angular.forEach(
1210             $scope.holdingsGridControls.selectedItems(),
1211             function (item) { cp_id_list = cp_id_list.concat(item.id_list) }
1212         );
1213         return cp_id_list;
1214     }
1215
1216     function gatherSelectedRawCopies () {
1217         var cp_list = [];
1218         angular.forEach(
1219             $scope.holdingsGridControls.selectedItems(),
1220             function (item) { if (item.raw) cp_list = cp_list.concat(item.raw) }
1221         );
1222         return cp_list;
1223     }
1224
1225     function gatherSelectedEmptyVolumeIds () {
1226         var cn_id_list = [];
1227         angular.forEach(
1228             $scope.holdingsGridControls.selectedItems(),
1229             function (item) {
1230                 if (item.copy_count == 0 || (!item.id && item.call_number))
1231                     // we are in a compressed row with no copies, or we are in a single
1232                     // call number row with no copy (testing for presence of 'id')
1233                     // In either case, the call number is 'empty'
1234                     cn_id_list.push(item.call_number.id)
1235             }
1236         );
1237         return cn_id_list;
1238     }
1239
1240     function gatherSelectedVolumeIds () {
1241         var cn_id_list = [];
1242         angular.forEach(
1243             $scope.holdingsGridControls.selectedItems(),
1244             function (item) {
1245                 if (cn_id_list.indexOf(item.call_number.id) == -1)
1246                     cn_id_list.push(item.call_number.id)
1247             }
1248         );
1249         return cn_id_list;
1250     }
1251
1252     $scope.selectedHoldingsDelete = function (vols, copies) {
1253
1254         var cnHash = {};
1255         var perCnCopies = {};
1256
1257         var cn_count = 0;
1258         var cp_count = 0;
1259
1260         angular.forEach(
1261             $scope.holdingsGridControls.selectedItems(),
1262             function (item) {
1263                 if (vols && item.raw_call_number) {
1264                     cnHash[item.call_number.id] = egCore.idl.Clone(item.raw_call_number);
1265                     cnHash[item.call_number.id].isdeleted(1);
1266                     cn_count++;
1267                 } else if (copies) {
1268                     angular.forEach(egCore.idl.Clone(item.raw), function (cp) {
1269                         cp.isdeleted(1);
1270                         cp_count++;
1271                         var cn_id = cp.call_number().id();
1272                         if (!cnHash[cn_id]) {
1273                             cnHash[cn_id] = cp.call_number();
1274                             perCnCopies[cn_id] = [cp];
1275                         } else {
1276                             perCnCopies[cn_id].push(cp);
1277                         }
1278                         cp.call_number(cn_id); // prevent loops in JSON-ification
1279                     });
1280
1281                 }
1282             }
1283         );
1284
1285         angular.forEach(perCnCopies, function (v, k) {
1286             if (vols) {
1287                 cnHash[k].isdeleted(1);
1288                 cn_count++;
1289             }
1290             cnHash[k].copies(v);
1291         });
1292
1293         cnList = [];
1294         angular.forEach(cnHash, function (v, k) {
1295             cnList.push(v);
1296         });
1297
1298         if (cnList.length == 0) return;
1299
1300         var flags = {};
1301         if (vols && copies) flags.force_delete_copies = 1;
1302
1303         egConfirmDialog.open(
1304             egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES,
1305             egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES_MESSAGE,
1306             {copies : cp_count, volumes : cn_count}
1307         ).result.then(function() {
1308             egCore.net.request(
1309                 'open-ils.cat',
1310                 'open-ils.cat.asset.volume.fleshed.batch.update.override',
1311                 egCore.auth.token(), cnList, 1, flags
1312             ).then(function(update_count) {
1313                 holdingsSvcInst.fetchAgain().then(function() {
1314                     $scope.holdingsGridDataProvider.refresh();
1315                 });
1316             });
1317         });
1318     }
1319     $scope.selectedHoldingsCopyDelete = function () { $scope.selectedHoldingsDelete(false,true) }
1320     $scope.selectedHoldingsVolCopyDelete = function () { $scope.selectedHoldingsDelete(true,true) }
1321     $scope.selectedHoldingsEmptyVolCopyDelete = function () { $scope.selectedHoldingsDelete(true,false) }
1322
1323     spawnHoldingsAdd = function (add_vols,add_copies){
1324         var raw = [];
1325         if (!add_vols && add_copies) { // just a copy on existing volumes
1326             angular.forEach(gatherSelectedVolumeIds(), function (v) {
1327                 raw.push( {callnumber : v} );
1328             });
1329         } else if (add_vols) {
1330             if (typeof $scope.holdingsGridControls.selectedItems == "function" &&
1331                 $scope.holdingsGridControls.selectedItems().length > 0) {
1332                 angular.forEach($scope.holdingsGridControls.selectedItems(),
1333                     function (item) {
1334                         raw.push({
1335                             owner : item.owner_id,
1336                             label : ((item.call_number) ? item.call_number.label : null)
1337                         });
1338                     });
1339             } else {
1340                 raw.push({
1341                     owner : egCore.auth.user().ws_ou()
1342                 });
1343             }
1344         }
1345
1346         if (raw.length == 0) raw.push({});
1347
1348         egCore.net.request(
1349             'open-ils.actor',
1350             'open-ils.actor.anon_cache.set_value',
1351             null, 'edit-these-copies', {
1352                 record_id: $scope.record_id,
1353                 raw: raw,
1354                 hide_vols : false,
1355                 hide_copies : !add_copies
1356             }
1357         ).then(function(key) {
1358             if (key) {
1359                 var url = egCore.env.basePath + 'cat/volcopy/' + key;
1360                 $timeout(function() { $window.open(url, '_blank') });
1361             } else {
1362                 alert('Could not create anonymous cache key!');
1363             }
1364         });
1365     }
1366     $scope.selectedHoldingsVolCopyAdd = function () { spawnHoldingsAdd(true,true) }
1367     $scope.selectedHoldingsCopyAdd = function () { spawnHoldingsAdd(false,true) }
1368     $scope.selectedHoldingsVolAdd = function () { spawnHoldingsAdd(true,false) }
1369
1370     spawnHoldingsEdit = function (hide_vols,hide_copies){
1371         egCore.net.request(
1372             'open-ils.actor',
1373             'open-ils.actor.anon_cache.set_value',
1374             null, 'edit-these-copies', {
1375                 record_id: $scope.record_id,
1376                 copies: gatherSelectedHoldingsIds(),
1377                 raw: gatherSelectedEmptyVolumeIds().map(
1378                     function(v){ return { callnumber : v } }
1379                 ),
1380                 hide_vols : hide_vols,
1381                 hide_copies : hide_copies
1382             }
1383         ).then(function(key) {
1384             if (key) {
1385                 var url = egCore.env.basePath + 'cat/volcopy/' + key;
1386                 $timeout(function() { $window.open(url, '_blank') });
1387             } else {
1388                 alert('Could not create anonymous cache key!');
1389             }
1390         });
1391     }
1392     $scope.selectedHoldingsVolCopyEdit = function () { spawnHoldingsEdit(false,false) }
1393     $scope.selectedHoldingsVolEdit = function () { spawnHoldingsEdit(false,true) }
1394     $scope.selectedHoldingsCopyEdit = function () { spawnHoldingsEdit(true,false) }
1395
1396     $scope.selectedHoldingsItemStatus = function (){
1397         var url = egCore.env.basePath + 'cat/item/search/' + gatherSelectedHoldingsIds().join(',')
1398         $timeout(function() { $window.open(url, '_blank') });
1399     }
1400
1401     $scope.markFromSelectedAsHoldingsTarget = function() {
1402         egCore.hatch.setLocalItem(
1403             'eg.cat.transfer_target_lib',
1404             $scope.holdingsGridControls.selectedItems()[0].owner_id
1405         );
1406         egCore.hatch.setLocalItem(
1407             'eg.cat.transfer_target_record',
1408             $scope.record_id
1409         );
1410         if ($scope.holdingsGridControls.selectedItems()[0].call_number.id) { // cn.id missing when vols are collapsed, or we are on an empty lib
1411             egCore.hatch.setLocalItem(
1412                 'eg.cat.transfer_target_vol',
1413                 $scope.holdingsGridControls.selectedItems()[0].call_number.id
1414             );
1415         } else {
1416             // clear out the stale value if we're on a lib-only
1417             // or vol-collapsed row
1418             egCore.hatch.removeLocalItem('eg.cat.transfer_target_vol');
1419         }
1420         ngToast.create(egCore.strings.MARK_HOLDINGS_TARGET);
1421     }
1422
1423     $scope.selectedHoldingsItemStatusDetail = function (){
1424         angular.forEach(
1425             gatherSelectedHoldingsIds(),
1426             function (cid) {
1427                 var url = egCore.env.basePath +
1428                           'cat/item/' + cid;
1429                 $timeout(function() { $window.open(url, '_blank') });
1430             }
1431         );
1432     }
1433
1434     $scope.transferVolumes = function (){
1435         var target_record = egCore.hatch.getLocalItem('eg.cat.transfer_target_record');
1436         var target_lib = egCore.hatch.getLocalItem('eg.cat.transfer_target_lib');
1437         if (!target_lib
1438             && (!target_record || ($scope.record_id == target_record) )
1439         ) return;
1440
1441         var vols_to_move = {};
1442         if (target_lib) {
1443             // we're moving volumes to a different library
1444             var vol_ids = gatherSelectedVolumeIds();
1445             if (vol_ids.length) {
1446                 vols_to_move[target_lib] = vol_ids;
1447
1448                 // if we're *only* switching libs,
1449                 // grab the current record as the target
1450                 target_record = target_record || $scope.record_id;
1451             }
1452         } else {
1453             // we're moving volumes to the same library they exist in
1454             // currently, but on a different record
1455             var items = $scope.holdingsGridControls.selectedItems();
1456             angular.forEach(items, function(item) {
1457                 if (!(item.call_number.owning_lib in vols_to_move)) {
1458                     vols_to_move[item.call_number.owning_lib] = new Array;
1459                 }
1460                 vols_to_move[item.call_number.owning_lib].push(item.call_number.id);
1461             });
1462         }
1463
1464         var promises = [];        
1465         angular.forEach(vols_to_move, function(vols, owning_lib) {
1466             promises.push(egCore.net.request(
1467                 'open-ils.cat',
1468                 'open-ils.cat.asset.volume.batch.transfer.override',
1469                 egCore.auth.token(), {
1470                     docid   : target_record,
1471                     lib     : owning_lib,
1472                     volumes : vols
1473                 }
1474             ));
1475         });
1476         $q.all(promises).then(function(success) {
1477             if (success) {
1478                 ngToast.create(egCore.strings.VOLS_TRANSFERED);
1479                 holdingsSvcInst.fetchAgain().then(function() {
1480                     $scope.holdingsGridDataProvider.refresh();
1481                 });
1482             } else {
1483                 alert('Could not transfer volumes!');
1484             }
1485         });
1486     }
1487
1488     // this "transfers" selected copies to a new owning library,
1489     // auto-creating volumes as required
1490     $scope.transferItemsAutoFill = function() {
1491         var target_record = egCore.hatch.getLocalItem('eg.cat.transfer_target_record');
1492         var target_lib = egCore.hatch.getLocalItem('eg.cat.transfer_target_lib');
1493         if (!target_lib
1494             && (!target_record || ($scope.record_id == target_record) )
1495         ) return;
1496
1497         var items = $scope.holdingsGridControls.selectedItems();
1498         if (!items.length) {
1499             return;
1500         }
1501
1502         var vols_to_move   = {};
1503         var copies_to_move = {};
1504         angular.forEach(items, function(item) {
1505             var needs_move = false;
1506             if (target_lib
1507                 && (item.call_number.owning_lib != target_lib)) {
1508                     item.call_number.owning_lib = target_lib;
1509                     needs_move = true;
1510             }
1511             if (target_record
1512                 && (item.call_number.record != target_record)) {
1513                     item.call_number.record = target_record;
1514                     needs_move = true;
1515             }
1516             if (needs_move) {
1517                 if (item.call_number.id in vols_to_move) {
1518                     copies_to_move[item.call_number.id].push(item.id);
1519                 } else {
1520                     vols_to_move[item.call_number.id] = item.call_number;
1521                     copies_to_move[item.call_number.id] = new Array;
1522                     copies_to_move[item.call_number.id].push(item.id);
1523                 }
1524             }
1525         });
1526
1527         var promises = [];
1528         angular.forEach(vols_to_move, function(vol) {
1529             promises.push(egCore.net.request(
1530                 'open-ils.cat',
1531                 'open-ils.cat.call_number.find_or_create',
1532                 egCore.auth.token(),
1533                 vol.label,
1534                 vol.record, // may be new
1535                 vol.owning_lib, // may be new
1536                 vol.prefix.id,
1537                 vol.suffix.id,
1538                 vol.label_class
1539             ).then(function(resp) {
1540                 var evt = egCore.evt.parse(resp);
1541                 if (evt) return;
1542                 return egCore.net.request(
1543                     'open-ils.cat',
1544                     'open-ils.cat.transfer_copies_to_volume',
1545                     egCore.auth.token(),
1546                     resp.acn_id,
1547                     copies_to_move[vol.id]
1548                 );
1549             }));
1550         });
1551         $q.all(promises).then(function() {
1552             ngToast.create(egCore.strings.ITEMS_TRANSFERED);
1553             holdingsSvcInst.fetchAgain().then(function() {
1554                 $scope.holdingsGridDataProvider.refresh();
1555             });
1556         });
1557     }
1558
1559     $scope.gridCellHandlers = {};
1560     $scope.gridCellHandlers.copyAlertsEdit = function(id) {
1561         egCirc.manage_copy_alerts([id]).then(function() {
1562             // update grid items?
1563         });
1564     };
1565
1566     $scope.transferItems = function (){
1567         var xfer_target = egCore.hatch.getLocalItem('eg.cat.transfer_target_vol');
1568
1569         if (!xfer_target) {
1570             // we have no specific volume, let's try to fill in the
1571             // blanks instead
1572             return $scope.transferItemsAutoFill();
1573         }
1574
1575         var copy_ids = gatherSelectedHoldingsIds();
1576         if (copy_ids.length > 0) {
1577             egCore.net.request(
1578                 'open-ils.cat',
1579                 'open-ils.cat.transfer_copies_to_volume',
1580                 egCore.auth.token(),
1581                 xfer_target,
1582                 copy_ids
1583             ).then(
1584                 function(resp) { // oncomplete
1585                     var evt = egCore.evt.parse(resp);
1586                     if (evt) {
1587                         egConfirmDialog.open(
1588                             egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_TITLE,
1589                             egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_BODY,
1590                             {'evt_desc': evt.desc}
1591                         ).result.then(function() {
1592                             egCore.net.request(
1593                                 'open-ils.cat',
1594                                 'open-ils.cat.transfer_copies_to_volume.override',
1595                                 egCore.auth.token(),
1596                                 xfer_target,
1597                                 copy_ids,
1598                                 { events: ['TITLE_LAST_COPY', 'COPY_DELETE_WARNING'] }
1599                             ).then(function(resp) {
1600                                 holdingsSvcInst.fetchAgain().then(function() {
1601                                     $scope.holdingsGridDataProvider.refresh();
1602                                 });
1603                             });
1604                         });
1605                     } else {
1606                         ngToast.create(egCore.strings.ITEMS_TRANSFERED);
1607                         holdingsSvcInst.fetchAgain().then(function() {
1608                             $scope.holdingsGridDataProvider.refresh();
1609                         });
1610                     }
1611                 },
1612                 null, // onerror
1613                 null // onprogress
1614             )
1615         }
1616     }
1617
1618     $scope.selectedHoldingsItemStatusTgrEvt = function (){
1619         angular.forEach(
1620             gatherSelectedHoldingsIds(),
1621             function (cid) {
1622                 var url = egCore.env.basePath +
1623                           'cat/item/' + cid + '/triggered_events';
1624                 $timeout(function() { $window.open(url, '_blank') });
1625             }
1626         );
1627     }
1628
1629     $scope.selectedHoldingsItemStatusHolds = function (){
1630         angular.forEach(
1631             gatherSelectedHoldingsIds(),
1632             function (cid) {
1633                 var url = egCore.env.basePath +
1634                           'cat/item/' + cid + '/holds';
1635                 $timeout(function() { $window.open(url, '_blank') });
1636             }
1637         );
1638     }
1639
1640     $scope.selectedHoldingsPrintLabels = function() {
1641         egCore.net.request(
1642             'open-ils.actor',
1643             'open-ils.actor.anon_cache.set_value',
1644             null, 'print-labels-these-copies', {
1645                 copies : gatherSelectedHoldingsIds()
1646             }
1647         ).then(function(key) {
1648             if (key) {
1649                 var url = egCore.env.basePath + 'cat/printlabels/' + key;
1650                 $timeout(function() { $window.open(url, '_blank') });
1651             } else {
1652                 alert('Could not create anonymous cache key!');
1653             }
1654         });
1655     }
1656
1657     $scope.selectedHoldingsDamaged = function () {
1658         var copy_list = gatherSelectedRawCopies();
1659         if (copy_list.length == 0) return;
1660
1661         angular.forEach(copy_list, function(cp) {
1662             egCirc.mark_damaged({
1663                 id: cp.id(),
1664                 barcode: cp.barcode(),
1665                 circ_lib: cp.circ_lib().id()
1666             }).then(function() {
1667                 holdingsSvcInst.fetchAgain().then(function() {
1668                     $scope.holdingsGridDataProvider.refresh();
1669                 });
1670             });
1671         });
1672     }
1673
1674     $scope.selectedHoldingsDiscard = function () {
1675         var copy_list = gatherSelectedRawCopies();
1676         if (copy_list.length == 0) return;
1677         egCirc.mark_discard(copy_list.map(function(cp) {
1678             return {id: cp.id(), barcode: cp.barcode()};})).then(function() {
1679                 holdingsSvcInst.fetchAgain().then(function() {
1680                     $scope.holdingsGridDataProvider.refresh();
1681                 });
1682             });
1683     }
1684
1685     $scope.selectedHoldingsMissing = function () {
1686         var copy_list = gatherSelectedRawCopies();
1687         if (copy_list.length == 0) return;
1688         egCirc.mark_missing(copy_list.map(function(cp) {
1689             return {id: cp.id(), barcode: cp.barcode()};})).then(function() {
1690                 holdingsSvcInst.fetchAgain().then(function() {
1691                     $scope.holdingsGridDataProvider.refresh();
1692                 });
1693             });
1694     }
1695
1696     $scope.selectedHoldingsCopyAlertsAdd = function() {
1697         egCirc.add_copy_alerts(gatherSelectedHoldingsIds()).then(function() {
1698             // no need to refresh grid
1699         });
1700     }
1701     $scope.selectedHoldingsCopyAlertsManage = function() {
1702         egCirc.manage_copy_alerts(gatherSelectedHoldingsIds()).then(function() {
1703             // no need to refresh grid
1704         });
1705     }
1706
1707     $scope.attach_to_peer_bib = function() {
1708         var copy_list = gatherSelectedHoldingsIds();
1709         if (copy_list.length == 0) return;
1710
1711         egCore.hatch.getItem('eg.cat.marked_conjoined_record').then(function(target_record) {
1712             if (!target_record) return;
1713
1714             return $uibModal.open({
1715                 templateUrl: './cat/catalog/t_conjoined_selector',
1716                 backdrop: 'static',
1717                 animation: true,
1718                 controller:
1719                        ['$scope','$uibModalInstance',
1720                 function($scope , $uibModalInstance) {
1721                     $scope.update = false;
1722
1723                     $scope.peer_type = null;
1724                     $scope.peer_type_list = [];
1725                     conjoinedSvc.get_peer_types().then(function(list){
1726                         $scope.peer_type_list = list;
1727                     });
1728     
1729                     $scope.ok = function(type) {
1730                         var promises = [];
1731     
1732                         angular.forEach(copy_list, function (cp) {
1733                             var n = new egCore.idl.bpbcm();
1734                             n.isnew(true);
1735                             n.peer_record(target_record);
1736                             n.target_copy(cp);
1737                             n.peer_type(type);
1738                             promises.push(egCore.pcrud.create(n));
1739                         });
1740     
1741                         return $q.all(promises).then(function(){$uibModalInstance.close()});
1742                     }
1743     
1744                     $scope.cancel = function($event) {
1745                         $uibModalInstance.dismiss();
1746                         $event.preventDefault();
1747                     }
1748                 }]
1749             });
1750         });
1751     }
1752
1753
1754     // ------------------------------------------------------------------
1755     // Holds 
1756     var provider = egGridDataProvider.instance({});
1757     var holds = []; // current list of holds
1758     var hold_count = 0;
1759
1760     $scope.hold_grid_data_provider = provider;
1761     $scope.grid_actions = egHoldGridActions;
1762     $scope.grid_actions.refresh = function () { holds = []; hold_count = 0; provider.refresh() };
1763     $scope.hold_grid_controls = {};
1764
1765     provider.get = function(offset, count) {
1766         if ($scope.record_tab != 'holds') return $q.when();
1767
1768         // see if we have the requested range cached
1769         if (holds[offset]) {
1770             return provider.arrayNotifier(holds, offset, count);
1771         }
1772
1773         hold_count = 0;
1774         holds = [];
1775         var restrictions = {
1776                 is_staff_request : 'true',
1777                 fulfillment_time : null,
1778                 cancel_time      : null,
1779                 record_id        : $scope.record_id,
1780                 pickup_lib       : egCore.org.descendants($scope.pickup_ou.id(), true)
1781         };
1782
1783         var order_by = [{ request_time : null }];
1784         if (provider.sort && provider.sort.length) {
1785             order_by = [];
1786             angular.forEach(provider.sort, function (c) {
1787                 if (!angular.isObject(c)) {
1788                     if (c.match(/^hold\./)) {
1789                         var i = c.replace('hold.','');
1790                         var ob = {};
1791                         ob[i] = null;
1792                         order_by.push(ob);
1793                     }
1794                 } else {
1795                     var i = Object.keys(c)[0];
1796                     var direction = c[i];
1797                     if (i.match(/^hold\./)) {
1798                         i = i.replace('hold.','');
1799                         var ob = {}
1800                         ob[i] = {dir:direction};
1801                         order_by.push(ob);
1802                     }
1803                 }
1804             });
1805         }
1806
1807         egProgressDialog.open({max : 1, value : 0});
1808         var first = true;
1809         return egHolds.fetch_wide_holds(
1810             restrictions,
1811             order_by
1812         ).then(function () {
1813                 return provider.arrayNotifier(holds, offset, count);
1814             },
1815             null,
1816             function(hold_data) {
1817                 if (first) {
1818                     hold_count = hold_data;
1819                     first = false;
1820                     egProgressDialog.update({max:hold_count});
1821                 } else {
1822                     egProgressDialog.increment();
1823                     var new_item = { id : hold_data.id, hold : hold_data };
1824                     new_item.status_string =
1825                         egCore.strings['HOLD_STATUS_' + hold_data.hold_status]
1826                         || hold_data.hold_status;
1827
1828                     holds.push(new_item);
1829                 }
1830             }
1831         ).finally(egProgressDialog.close);
1832
1833     }
1834
1835     $scope.detail_view = function(action, user_data, items) {
1836         if (h = items[0]) {
1837             $scope.detail_hold_id = h.hold.id;
1838         }
1839     }
1840
1841     $scope.list_view = function(items) {
1842          $scope.detail_hold_id = null;
1843     }
1844
1845     // refresh the list of record holds when the pickup lib is changed.
1846     $scope.pickup_ou = egCore.org.get(egCore.auth.user().ws_ou());
1847     $scope.pickup_ou_changed = function(org) {
1848         $scope.pickup_ou = org;
1849         holds = []
1850         hold_count = 0;
1851         provider.refresh();
1852     }
1853
1854     function map_prefix_to_subhash (h,pf) {
1855         var newhash = {};
1856         angular.forEach(Object.keys(h), function (k) {
1857             if (k.startsWith(pf)) {
1858                 var nk = k.substr(pf.length);
1859                 newhash[nk] = h[k];
1860             }
1861         });
1862         return newhash;
1863     }
1864
1865     $scope.print_holds = function() {
1866         var pholds = [];
1867         angular.forEach(holds, function(item) {
1868             pholds.push({
1869                 hold : item.hold,
1870                 status_string : item.status_string,
1871                 patron_first : item.hold.usr_first_given_name,
1872                 patron_last : item.hold.usr_family_name,
1873                 patron_alias : item.hold.usr_alias,
1874                 patron_barcode : item.hold.ucard_barcode,
1875                 copy : map_prefix_to_subhash(item.hold,'cp_'),
1876                 volume : map_prefix_to_subhash(item.hold,'cn_'),
1877                 title : item.hold.title,
1878                 author : item.hold.author
1879             });
1880         });
1881
1882         egCore.print.print({
1883             context : 'receipt', 
1884             template : 'holds_for_bib', 
1885             scope : {holds : pholds}
1886         });
1887     }
1888
1889     $scope.current_hold_transfer_dest = egCore.hatch.getLocalItem ('eg.circ.hold.title_transfer_target');
1890
1891     $scope.mark_hold_transfer_dest = function() {
1892         $scope.current_hold_transfer_dest = $scope.record_id;
1893         egCore.hatch.setLocalItem(
1894             'eg.circ.hold.title_transfer_target', $scope.record_id);
1895         ngToast.create(egCore.strings.HOLD_TRANSFER_DEST_MARKED);
1896     }
1897
1898     // UI presents this option as "all holds"
1899     $scope.transfer_holds_to_marked = function() {
1900         var hold_ids = $scope.hold_grid_controls.allItems().map(
1901             function(hold_data) {return hold_data.hold.id});
1902         egHolds.transfer_to_marked_title(hold_ids);
1903     }
1904
1905     // ------------------------------------------------------------------
1906     // Initialize the selected tab
1907
1908     // we explicitly initialize catalog_url because otherwise Firefox
1909     // ends up setting it to $BASE_URL/{{url}}, which then messes
1910     // things up. See LP#1708951
1911     $scope.catalog_url = '';
1912
1913     function init_cat_url() {
1914         // Set the initial catalog URL.  This only happens once.
1915         // The URL is otherwise generated through user navigation.
1916         if ($scope.catalog_url) return;
1917
1918         var url = $location.absUrl().replace(/\/staff.*/, '/opac/advanced');
1919
1920         // A record ID in the path indicates a request for the record-
1921         // specific page.
1922         if ($routeParams.record_id) {
1923             url = url.replace(/advanced/, '/record/' + $scope.record_id);
1924         }
1925
1926         // Jumping directly to the results page by passing a search
1927         // query via the URL.  Copy all URL params to the iframe url.
1928         if ($location.path().match(/catalog\/results/)) {
1929             url = url.replace(/advanced/, '/results?');
1930             var first = true;
1931             angular.forEach($location.search(), function(val, key) {
1932                 if (!first) url += '&';
1933                 first = false;
1934                 url += encodeURIComponent(key) 
1935                     + '=' + encodeURIComponent(val);
1936             });
1937         }
1938
1939         // if we're displaying the advanced search form, select
1940         // whatever default pane the user has chosen via workstation
1941         // preference
1942         if (url.match(/\/opac\/advanced$/)) {
1943             egCore.hatch.getItem('eg.search.adv_pane').then(function(adv_pane_val){
1944                 if (adv_pane_val) {
1945                     url += '?pane=' + encodeURIComponent(adv_pane_val);
1946                 }
1947
1948                 $scope.catalog_url = url;
1949             });
1950         } else {
1951             $scope.catalog_url = url;
1952         }
1953
1954     }
1955
1956     function init_parts_url() {
1957         $scope.parts_url = $location
1958             .absUrl()
1959             .replace(
1960                 /\/staff.*/,
1961                 '/conify/global/biblio/monograph_part?r='+$scope.record_id
1962             );
1963     }
1964
1965     $scope.set_record_tab = function(tab) {
1966         $scope.record_tab = tab;
1967
1968         switch(tab) {
1969
1970             case 'monoparts':
1971                 init_parts_url();
1972                 break;
1973
1974             case 'catalog':
1975                 init_cat_url();
1976                 break;
1977
1978             case 'holds':
1979                 $scope.detail_hold_record_id = $scope.record_id; 
1980                 // refresh the holds grid
1981                 provider.refresh();
1982
1983                 break;
1984         }
1985     }
1986
1987     $scope.set_default_record_tab = function() {
1988         egCore.hatch.setLocalItem(
1989             'eg.cat.default_record_tab', $scope.record_tab);
1990         $timeout(function(){$scope.default_tab = $scope.record_tab});
1991     }
1992
1993     var tab;
1994     if ($scope.record_id) {
1995         $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
1996         tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
1997
1998     } else {
1999         tab = $routeParams.record_tab || 'catalog';
2000     }
2001     $scope.set_record_tab(tab);
2002
2003 }])
2004
2005 .controller('AuthorityCtrl',
2006        ['$scope','$routeParams','$location','$window','$q','egCore',
2007 function($scope , $routeParams , $location , $window , $q , egCore) {
2008
2009     // set record ID on page load if available...
2010     $scope.authority_id = $routeParams.authority_id;
2011
2012     if ($routeParams.authority_id) $scope.from_route = true;
2013     else $scope.from_route = false;
2014
2015     $scope.stop_unload = false;
2016 }])
2017
2018 .controller('URLVerifyCtrl',
2019        ['$scope','$location',
2020 function($scope , $location) {
2021     $scope.verifyurls_url = $location.absUrl().replace(/\/staff.*/, '/url_verify/sessions');
2022 }])
2023
2024 .controller('VandelayCtrl',
2025        ['$scope','$location', 'egCore', '$uibModal',
2026 function($scope , $location, egCore, $uibModal) {
2027     $scope.vandelay_url = $location.absUrl().replace(/\/staff\/cat\/catalog\/vandelay/, '/vandelay/vandelay');
2028     $scope.funcs = {};
2029     $scope.funcs.edit_marc_modal = function(bre, callback){
2030         var marcArgs = { 'marc_xml': bre.marc() };
2031         var vqbibrecId = bre.id();
2032         $uibModal.open({
2033             templateUrl: './cat/catalog/t_edit_marc_modal',
2034             backdrop: 'static',
2035             size: 'lg',
2036             controller: ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
2037                 $scope.focusMe = true;
2038                 $scope.recordId = vqbibrecId;
2039                 $scope.args = marcArgs;
2040                 $scope.dirty_flag = false;
2041                 $scope.ok = function(marg){
2042                     $uibModalInstance.close(marg);
2043                 };
2044                 $scope.cancel = function(){ $uibModalInstance.dismiss() }
2045             }]
2046         }).result.then(function(res){
2047             var new_xml = res.marc_xml;
2048             egCore.pcrud.retrieve('vqbr', vqbibrecId).then(function(vqbib){
2049                 vqbib.marc(new_xml);
2050                 egCore.pcrud.update(vqbib).then( function(){ callback(vqbibrecId); });
2051             });
2052         });
2053     };
2054 }])
2055
2056 .controller('ManageAuthoritiesCtrl',
2057        ['$scope','$location',
2058 function($scope , $location) {
2059     $scope.manageauthorities_url = $location.absUrl().replace(/\/staff.*/, '/cat/authority/list');
2060 }])
2061
2062 .controller('BatchEditCtrl',
2063        ['$scope','$location','$routeParams',
2064 function($scope , $location , $routeParams) {
2065     $scope.batchedit_url = $location.absUrl().replace(/\/eg.*/, '/opac/extras/merge_template');
2066     if ($routeParams.container_type) {
2067         switch ($routeParams.container_type) {
2068             case 'bucket':
2069                 $scope.batchedit_url += '?recordSource=b&containerid=' + $routeParams.container_id;
2070                 break;
2071             case 'record':
2072                 $scope.batchedit_url += '?recordSource=r&recid=' + $routeParams.container_id;
2073                 break;
2074         };
2075     }
2076 }])
2077
2078  
2079 .filter('boolText', function(){
2080     return function (v) {
2081         return v == 't';
2082     }
2083 })
2084
2085 .factory('conjoinedSvc', 
2086        ['egCore','$q',
2087 function(egCore , $q) {
2088
2089     var service = {
2090         items : [], // record search results
2091         index : 0, // search grid index
2092         rid : null
2093     };
2094
2095     service.flesh = {   
2096         flesh : 4, 
2097         flesh_fields : {
2098             bpbcm : ['target_copy','peer_type'],
2099             acp : ['call_number'],
2100             acn : ['record'],
2101             bre : ['simple_record']
2102         },
2103         // avoid fetching the MARC blob by specifying which
2104         // fields on the bre to select.  More may be needed.
2105         // note that fleshed fields are explicitly selected.
2106         select : { bre : ['id'] },
2107         order_by : { bpbcm : ['id'] },
2108     }
2109
2110     // resolved with the last received copy
2111     service.fetch = function(rid) {
2112         if (!rid && !service.rid) return $q.when();
2113
2114         if (rid) service.rid = rid;
2115         service.items = [];
2116         service.index = 0;
2117
2118         return egCore.pcrud.search(
2119             'bpbcm',
2120             {peer_record : service.rid},
2121             service.flesh,
2122             {atomic : true}
2123         ).then( function(list) { // finished
2124             service.items = list;
2125             return service.items;
2126         });
2127     }
2128
2129     // returns a promise resolved with the list of peer bib types
2130     service.get_peer_types = function() {
2131         if (egCore.env.bpt)
2132             return $q.when(egCore.env.bpt.list);
2133
2134         return egCore.pcrud.retrieveAll('bpt', null, {atomic : true})
2135         .then(function(list) {
2136             egCore.env.absorbList(list, 'bpt');
2137             return list;
2138         });
2139     };
2140
2141     return service;
2142 }])
2143
2144