]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/catalog/app.js
LP1898775: Add basket to bucket in bootstrap
[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.href = '/eg2/staff/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.carousels_available = false;
427     egCore.net.request(
428         'open-ils.actor',
429         'open-ils.actor.carousel.retrieve_manual_by_staff',
430         egCore.auth.token()
431     ).then(function(carousels) { $scope.carousels_available = true; });
432
433     $scope.add_to_carousel = function(recs) {
434         if (!angular.isArray(recs)) {
435             recs = [ $scope.record_id ];
436         }
437         return $uibModal.open({
438             templateUrl: './cat/catalog/t_add_to_carousel',
439             backdrop: 'static',
440             animation: true,
441             size: 'md',
442             controller:
443                    ['$scope','$uibModalInstance',
444             function($scope , $uibModalInstance) {
445                 $scope.bucket_id = 0;
446                 $scope.allCarousels = [];
447                 egCore.net.request(
448                     'open-ils.actor',
449                     'open-ils.actor.carousel.retrieve_manual_by_staff',
450                     egCore.auth.token()
451                 ).then(function(carousels) { $scope.allCarousels = carousels; });
452
453                 $scope.add_to_carousel = function() {
454                     // or more precisely, the carousel's bucket
455                     var promises = [];
456                     angular.forEach(recs, function(recId) {
457                         var item = new egCore.idl.cbrebi();
458                         item.bucket($scope.bucket_id);
459                         item.target_biblio_record_entry(recId);
460                         promises.push(egCore.net.request(
461                             'open-ils.actor',
462                             'open-ils.actor.container.item.create',
463                             egCore.auth.token(), 'biblio', item
464                         ));
465                     });
466                     $q.all(promises).then(function(resp) {
467                         $uibModalInstance.close();
468                     });
469                 }
470
471                 $scope.cancel = function() {
472                     $uibModalInstance.dismiss();
473                 }
474             }]
475         });
476     }
477
478     $scope.current_overlay_target     = egCore.hatch.getLocalItem('eg.cat.marked_overlay_record');
479     $scope.current_transfer_target    = egCore.hatch.getLocalItem('eg.cat.transfer_target_record');
480     $scope.current_conjoined_target   = egCore.hatch.getLocalItem('eg.cat.marked_conjoined_record');
481
482     $scope.quickReceive = function () {
483         var list = [];
484         var next_per_stream = {};
485
486         var recId = $scope.record_id;
487         return $uibModal.open({
488             templateUrl: './share/t_subscription_select_dialog',
489             backdrop: 'static',
490             controller: ['$scope', '$uibModalInstance',
491                 function($scope, $uibModalInstance) {
492
493                     $scope.focus = true;
494                     $scope.rememberMe = 'eg.serials.quickreceive.last_org';
495                     $scope.record_id = recId;
496                     $scope.ssubId = null;
497
498                     $scope.ok = function() { $uibModalInstance.close($scope.ssubId) }
499                     $scope.cancel = function() { $uibModalInstance.dismiss(); }
500                 }
501             ]
502         }).result.then(function(ssubId) {
503             if (ssubId) {
504                 var promises = [];
505                 promises.push(egSerialsCoreSvc.fetchItemsForSub(ssubId,{status:'Expected'}).then(function(){
506                     angular.forEach(egSerialsCoreSvc.itemTree, function (item) {
507                         if (next_per_stream[item.stream().id()]) return;
508                         if (item.status() == 'Expected') {
509                             next_per_stream[item.stream().id()] = item;
510                             list.push(egCore.idl.Clone(item));
511                         }
512                     });
513                 }));
514
515                 return $q.all(promises).then(function() {
516
517                     if (!list.length) {
518                         ngToast.warning(egCore.strings.SERIALS_NO_ITEMS);
519                         return $q.reject();
520                     }
521
522                     return egSerialsCoreSvc.process_items(
523                         'receive',
524                         $scope.record_id,
525                         list,
526                         true, // barcode
527                         false,// bind
528                         false, // print by default
529                         function() { $scope.holdings_record_id_changed($scope.record_id) }
530                     );
531                 });
532             } else {
533                 ngToast.warning(egCore.strings.SERIALS_NO_SUBS);
534                 return $q.reject();
535             }
536         });
537     }
538
539     $scope.markConjoined = function () {
540         $scope.current_conjoined_target = $scope.record_id;
541         egCore.hatch.setLocalItem('eg.cat.marked_conjoined_record',$scope.record_id);
542         ngToast.create(egCore.strings.MARK_CONJ_TARGET);
543     };
544
545     $scope.markHoldingsTransfer = function () {
546         $scope.current_transfer_target = $scope.record_id;
547         egCore.hatch.setLocalItem('eg.cat.transfer_target_record',$scope.record_id);
548         egCore.hatch.removeLocalItem('eg.cat.transfer_target_lib');
549         egCore.hatch.removeLocalItem('eg.cat.transfer_target_vol');
550         ngToast.create(egCore.strings.MARK_HOLDINGS_TARGET);
551     };
552
553     $scope.markOverlay = function () {
554         $scope.current_overlay_target = $scope.record_id;
555         egCore.hatch.setLocalItem('eg.cat.marked_overlay_record',$scope.record_id);
556         ngToast.create(egCore.strings.MARK_OVERLAY_TARGET);
557     };
558
559     $scope.clearRecordMarks = function () {
560         $scope.current_overlay_target     = null;
561         $scope.current_transfer_target    = null;
562         $scope.current_conjoined_target   = null;
563         $scope.current_hold_transfer_dest = null;
564         egCore.hatch.removeLocalItem('eg.cat.transfer_target_record');
565         egCore.hatch.removeLocalItem('eg.cat.marked_conjoined_record');
566         egCore.hatch.removeLocalItem('eg.cat.marked_overlay_record');
567         egCore.hatch.removeLocalItem('eg.circ.hold.title_transfer_target');
568     }
569
570     $scope.stop_unload = false;
571     $scope.$watch('stop_unload',
572         function(newVal, oldVal) {
573             if (newVal && newVal != oldVal && $scope.opac_iframe) {
574                 $($scope.opac_iframe.dom.contentWindow).on('beforeunload', function(){
575                     return 'There is unsaved data in this record.'
576                 });
577             } else {
578                 if ($scope.opac_iframe)
579                     $($scope.opac_iframe.dom.contentWindow).off('beforeunload');
580             }
581         }
582     );
583
584     // Set the "last bib" cookie, if we have that
585     if ($scope.record_id)
586         egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
587
588     $scope.refresh_record_callback = function (record_id) {
589         egCore.pcrud.retrieve('bre', record_id, {
590             flesh : 1,
591             flesh_fields : {
592                 bre : ['simple_record','creator','editor']
593             }
594         }).then(function(rec) {
595             rec.owner(egCore.org.get(rec.owner()));
596             $scope.summary_pane_record = rec;
597         });
598
599         return record_id;
600     }
601
602     patron_search_dialog = function() {
603         return $uibModal.open({
604             templateUrl: './share/t_patron_selector',
605             backdrop: 'static',
606             size: 'lg',
607             animation: true,
608             controller:
609                    ['$scope','$uibModalInstance','$controller',
610             function($scope , $uibModalInstance , $controller) {
611                 angular.extend(this, $controller('BasePatronSearchCtrl', {$scope : $scope}));
612                 $scope.clearForm();
613                 $scope.need_one_selected = function() {
614                     var items = $scope.gridControls.selectedItems();
615                     return (items.length == 1) ? false : true
616                 }
617                 $scope.ok = function() {
618                     var items = $scope.gridControls.selectedItems();
619                     if (items.length == 1) {
620                         $uibModalInstance.close(items[0].card().barcode());
621                     } else {
622                         $uibModalInstance.close()
623                     }
624                 }
625                 $scope.cancel = function($event) {
626                     $uibModalInstance.dismiss();
627                     $event.preventDefault();
628                 }
629             }]
630         });
631     }
632
633     // Map the Angular catalog-only 'item_table' tab to the AngJS
634     // 'catalog' tab.
635     function get_default_record_tab() {
636         var tab = egCore.hatch.getLocalItem('eg.cat.default_record_tab');
637         if (!tab || tab === 'item_table') { return 'catalog'; }
638         return tab;
639     }
640
641     // also set it when the iframe changes to a new record
642     $scope.handle_page = function(url) {
643
644         if (!url || url == 'about:blank') {
645             // nothing loaded.  If we already have a record ID, leave it.
646             return;
647         }
648
649         var prev_record_id = $scope.record_id;
650         var match = url.match(/\/+opac\/+record\/+(\d+)/);
651         if (match) {
652             $scope.record_id = match[1];
653             egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
654             $scope.holdings_record_id_changed($scope.record_id);
655             conjoinedSvc.fetch($scope.record_id).then(function(){
656                 $scope.conjoinedGridDataProvider.refresh();
657             });
658             init_parts_url();
659             $scope.grid_actions.refresh();
660             $location.update_path('/cat/catalog/record/' + $scope.record_id);
661             // update_path() bypasses the controller for path 
662             // /cat/catalog/record/:record_id. Manually set title here too.
663             egCore.strings.setPageTitle(
664                 egCore.strings.PAGE_TITLE_BIB_DETAIL,
665                 egCore.strings.PAGE_TITLE_CATALOG_CONTEXT,
666                 {record_id : $scope.record_id}
667             );
668         } else {
669             delete $scope.record_id;
670             $scope.from_route = false;
671         }
672
673         // child scope is executing this function, so our digest doesn't fire ... thus,
674         $scope.$apply();
675
676         // don't change tabs if we are using the OPAC nav buttons,
677         // or we didn't change records on the OPAC load
678         if (!$scope.in_opac_call && ($scope.record_id != prev_record_id)) {
679             if ($scope.record_id) {
680                 $scope.default_tab = get_default_record_tab();
681                 tab = $routeParams.record_tab || $scope.default_tab;
682             } else {
683                 tab = $routeParams.record_tab || 'catalog';
684             }
685             $scope.set_record_tab(tab);
686         } else {
687             $scope.in_opac_call = false;
688         }
689
690         if ($scope.opac_iframe && $location.path().match(/cat\/catalog/)) {
691             var doc = $scope.opac_iframe.dom.contentWindow.document;
692             $(doc).find('#hold_usr_search').show();
693             $(doc).find('#hold_usr_search').on('click', function() {
694                 patron_search_dialog().result.then(function(barc) {
695                     $(doc).find('#hold_usr_input').val(barc);
696                     $(doc).find('#hold_usr_input').trigger($.Event('keydown', {which: 13}));
697                 });
698             });
699             // Add Cart to Record Bucket, in two flavors:
700             // First, the traditional TPAC, which uses a <select> menu
701             $(doc).find('#select_basket_action').on('change', function() {
702                 if (this.options[this.selectedIndex].value && this.options[this.selectedIndex].value == "add_cart_to_bucket") {
703                     $scope.add_cart_to_record_bucket();
704                 }
705             });
706             // Second, the bootstrap OPAC, which uses a bunch of <a>s styled as a dropdown
707             $(doc).find('a[href="add_cart_to_bucket"]').on('click', function (event) {
708                 event.preventDefault();
709                 $scope.add_cart_to_record_bucket();
710             });
711         }
712
713     }
714
715     // xulG catalog handlers
716     $scope.handlers = { }
717
718     // ------------------------------------------------------------------
719     // Conjoined items
720
721     $scope.conjoinedGridControls = {};
722     $scope.conjoinedGridDataProvider = egGridDataProvider.instance({
723         get : function(offset, count) {
724             return this.arrayNotifier(conjoinedSvc.items, offset, count);
725         }
726     });
727
728     $scope.changeConjoinedType = function () {
729         var peers = egCore.idl.Clone($scope.conjoinedGridControls.selectedItems());
730         angular.forEach(peers, function (p) {
731             p.target_copy(p.target_copy().id());
732             p.peer_type(p.peer_type().id());
733         });
734
735         var conjoinedGridDataProviderRef = $scope.conjoinedGridDataProvider;
736
737         return $uibModal.open({
738             templateUrl: './cat/catalog/t_conjoined_selector',
739             backdrop: 'static',
740             animation: true,
741             controller:
742                    ['$scope','$uibModalInstance',
743             function($scope , $uibModalInstance) {
744                 $scope.update = true;
745
746                 $scope.peer_type = null;
747                 $scope.peer_type_list = [];
748                 conjoinedSvc.get_peer_types().then(function(list){
749                     $scope.peer_type_list = list;
750                 });
751     
752                 $scope.ok = function(type) {
753                     var promises = [];
754     
755                     angular.forEach(peers, function (p) {
756                         p.ischanged(1);
757                         p.peer_type(type);
758                         promises.push(egCore.pcrud.update(p));
759                     });
760     
761                     return $q.all(promises)
762                         .then(function(){$uibModalInstance.close()})
763                         .then(function(){return conjoinedSvc.fetch()})
764                         .then(function(){conjoinedGridDataProviderRef.refresh()});
765                 }
766     
767                 $scope.cancel = function($event) {
768                     $uibModalInstance.dismiss();
769                     $event.preventDefault();
770                 }
771             }]
772         });
773         
774     }
775
776     $scope.refreshConjoined = function () {
777         conjoinedSvc.fetch($scope.record_id)
778         .then(function(){$scope.conjoinedGridDataProvider.refresh();});
779     }
780
781     $scope.deleteSelectedConjoined = function () {
782         var peers = $scope.conjoinedGridControls.selectedItems();
783
784         if (peers.length > 0) {
785             egConfirmDialog.open(
786                 egCore.strings.CONFIRM_DELETE_PEERS,
787                 egCore.strings.CONFIRM_DELETE_PEERS_MESSAGE,
788                 {peers : peers.length}
789             ).result.then(function() {
790                 angular.forEach(peers, function (p) {
791                     p.isdeleted(1);
792                 });
793
794                 egCore.pcrud.remove(peers).then(function() {
795                     return conjoinedSvc.fetch();
796                 }).then(function() {
797                     $scope.conjoinedGridDataProvider.refresh();
798                 });
799             });
800         }
801     }
802     if ($scope.record_id)
803         conjoinedSvc.fetch($scope.record_id);
804
805     // ------------------------------------------------------------------
806     // Holdings
807
808     $scope.holdingsGridControls = {
809         activateItem : function (item) {
810             $scope.selectedHoldingsVolCopyEdit();
811         }
812     };
813     $scope.holdingsGridDataProvider = egGridDataProvider.instance({
814         get : function(offset, count) {
815             return this.arrayNotifier(holdingsSvcInst.copies, offset, count);
816         }
817     });
818
819     $scope.add_copies_to_bucket = function() {
820         var copy_list = gatherSelectedHoldingsIds();
821         if (copy_list.length == 0) return;
822
823         return $uibModal.open({
824             templateUrl: './cat/catalog/t_add_to_bucket',
825             backdrop: 'static',
826             animation: true,
827             size: 'md',
828             controller:
829                    ['$scope','$uibModalInstance',
830             function($scope , $uibModalInstance) {
831
832                 $scope.bucket_id = 0;
833                 $scope.newBucketName = '';
834                 $scope.allBuckets = [];
835
836                 egCore.net.request(
837                     'open-ils.actor',
838                     'open-ils.actor.container.retrieve_by_class.authoritative',
839                     egCore.auth.token(), egCore.auth.user().id(),
840                     'copy', 'staff_client'
841                 ).then(function(buckets) { $scope.allBuckets = buckets; });
842
843                 $scope.add_to_bucket = function() {
844                     var promises = [];
845                     angular.forEach(copy_list, function (cp) {
846                         var item = new egCore.idl.ccbi()
847                         item.bucket($scope.bucket_id);
848                         item.target_copy(cp);
849                         promises.push(
850                             egCore.net.request(
851                                 'open-ils.actor',
852                                 'open-ils.actor.container.item.create',
853                                 egCore.auth.token(), 'copy', item
854                             )
855                         );
856
857                         return $q.all(promises).then(function() {
858                             $uibModalInstance.close();
859                         });
860                     });
861                 }
862
863                 $scope.add_to_new_bucket = function() {
864                     var bucket = new egCore.idl.ccb();
865                     bucket.owner(egCore.auth.user().id());
866                     bucket.name($scope.newBucketName);
867                     bucket.description('');
868                     bucket.btype('staff_client');
869
870                     return egCore.net.request(
871                         'open-ils.actor',
872                         'open-ils.actor.container.create',
873                         egCore.auth.token(), 'copy', bucket
874                     ).then(function(bucket) {
875                         $scope.bucket_id = bucket;
876                         $scope.add_to_bucket();
877                     });
878                 }
879
880                 $scope.cancel = function() {
881                     $uibModalInstance.dismiss();
882                 }
883             }]
884         });
885     }
886
887     // TODO: refactor common code between cat/catalog/app.js and cat/item/app.js 
888
889     $scope.need_one_selected = function() {
890         var items = $scope.holdingsGridControls.selectedItems();
891         if (items.length == 1) return false;
892         return true;
893     };
894
895     $scope.make_copies_bookable = function() {
896
897         var copies_by_record = {};
898         var record_list = [];
899         angular.forEach(
900             $scope.holdingsGridControls.selectedItems(),
901             function (item) {
902                 var record_id = item['call_number.record.id'];
903                 if (typeof copies_by_record[ record_id ] == 'undefined') {
904                     copies_by_record[ record_id ] = [];
905                     record_list.push( record_id );
906                 }
907                 copies_by_record[ record_id ].push(item.id);
908             }
909         );
910
911         var promises = [];
912         var combined_results = [];
913         angular.forEach(record_list, function(record_id) {
914             promises.push(
915                 egCore.net.request(
916                     'open-ils.booking',
917                     'open-ils.booking.resources.create_from_copies',
918                     egCore.auth.token(),
919                     copies_by_record[record_id]
920                 ).then(function(results) {
921                     if (results && results['brsrc']) {
922                         combined_results = combined_results.concat(results['brsrc']);
923                     }
924                 })
925             );
926         });
927
928         $q.all(promises).then(function() {
929             if (combined_results.length > 0) {
930                 $uibModal.open({
931                     template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
932                     backdrop: 'static',
933                     animation: true,
934                     size: 'md',
935                     controller:
936                            ['$scope','$location','egCore','$uibModalInstance',
937                     function($scope , $location , egCore , $uibModalInstance) {
938
939                         $scope.funcs = {
940                             ses : egCore.auth.token(),
941                             resultant_brsrc : combined_results.map(function(o) { return o[0]; })
942                         }
943
944                         var booking_path = '/eg/conify/global/booking/resource';
945
946                         $scope.booking_admin_url =
947                             $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
948                     }]
949                 });
950             }
951         });
952     }
953
954     $scope.book_copies_now = function(items) {
955         location.href = "/eg2/staff/booking/create_reservation/for_resource/" + items[0]['barcode'];
956     }
957
958     $scope.requestItems = function() {
959         var copy_list = gatherSelectedHoldingsIds();
960         if (copy_list.length == 0) return;
961
962         return $uibModal.open({
963             templateUrl: './cat/catalog/t_request_items',
964             animation: true,
965             controller:
966                    ['$scope','$uibModalInstance',
967             function($scope , $uibModalInstance) {
968                 $scope.user = null;
969                 $scope.first_user_fetch = true;
970
971                 $scope.hold_data = {
972                     hold_type : 'C',
973                     copy_list : copy_list,
974                     pickup_lib: egCore.org.get(egCore.auth.user().ws_ou()),
975                     user      : egCore.auth.user().id()
976                 };
977
978                 egUser.get( $scope.hold_data.user ).then(function(u) {
979                     $scope.user = u;
980                     $scope.barcode = u.card().barcode();
981                     $scope.user_name = egUser.format_name(u);
982                     $scope.hold_data.user = u.id();
983                 });
984
985                 $scope.user_name = '';
986                 $scope.barcode = '';
987                 $scope.$watch('barcode', function (n) {
988                     if (!$scope.first_user_fetch) {
989                         egUser.getByBarcode(n).then(function(u) {
990                             $scope.user = u;
991                             $scope.user_name = egUser.format_name(u);
992                             $scope.hold_data.user = u.id();
993                         }, function() {
994                             $scope.user = null;
995                             $scope.user_name = '';
996                             delete $scope.hold_data.user;
997                         });
998                     }
999                     $scope.first_user_fetch = false;
1000                 });
1001
1002                 $scope.ok = function(h) {
1003                     var args = {
1004                         patronid  : h.user,
1005                         hold_type : h.hold_type,
1006                         pickup_lib: h.pickup_lib.id(),
1007                         depth     : 0
1008                     };
1009
1010                     egCore.net.request(
1011                         'open-ils.circ',
1012                         'open-ils.circ.holds.test_and_create.batch.override',
1013                         egCore.auth.token(), args, h.copy_list
1014                     ).then(function() {
1015                         holds = []; // force the holds grid to refetch data.
1016                         $uibModalInstance.close();
1017                     });
1018                 }
1019
1020                 $scope.cancel = function($event) {
1021                     $uibModalInstance.dismiss();
1022                     $event.preventDefault();
1023                 }
1024             }]
1025         });
1026     }
1027
1028     $scope.manage_reservations = function() {
1029         var item = $scope.holdingsGridControls.selectedItems()[0];
1030         if (item)
1031             location.href = "/eg2/staff/booking/manage_reservations/by_resource/" + item.barcode;
1032     }
1033
1034
1035     $scope.view_place_orders = function() {
1036         if (!$scope.record_id) return;
1037         var url = egCore.env.basePath + 'acq/legacy/lineitem/related/' + $scope.record_id + '?target=bib';
1038         $timeout(function() { $window.open(url, '_blank') });
1039     }
1040
1041     $scope.replaceBarcodes = function() {
1042         var copy_list = gatherSelectedRawCopies();
1043         if (copy_list.length == 0) return;
1044
1045         var holdingsGridDataProviderRef = $scope.holdingsGridDataProvider;
1046
1047         angular.forEach(copy_list, function (cp) {
1048             $uibModal.open({
1049                 templateUrl: './cat/share/t_replace_barcode',
1050                 backdrop: 'static',
1051                 animation: true,
1052                 controller:
1053                            ['$scope','$uibModalInstance',
1054                     function($scope , $uibModalInstance) {
1055                         $scope.duplicate_barcode = false;
1056                         $scope.isModal = true;
1057                         $scope.focusBarcode = false;
1058                         $scope.focusBarcode2 = true;
1059                         $scope.barcode1 = cp.barcode();
1060
1061                         // check input to see if it's a duplicate barcode
1062                         $scope.checkCurrentBarcode = function() {
1063                             if (!$scope.duplicate_barcode_string) {
1064                                 $scope.duplicate_barcode_string = window.duplicate_barcode_string;
1065                             }
1066                             var searchParams = {
1067                                 deleted : 'f',
1068                                 'barcode' : $scope.barcode2,
1069                                 id : { '!=' : $scope.copyId }
1070                             };
1071                             egCore.pcrud.search('acp', searchParams).then(function (res) {
1072                                 $scope.duplicate_barcode = res;
1073                             });
1074                         }
1075
1076                         $scope.updateBarcode = function() {
1077                             $scope.copyNotFound = false;
1078                             $scope.updateOK = false;
1079                 
1080                             egCore.pcrud.search('acp',
1081                                 {deleted : 'f', barcode : $scope.barcode1})
1082                             .then(function(copy) {
1083                 
1084                                 if (!copy) {
1085                                     $scope.focusBarcode = true;
1086                                     $scope.copyNotFound = true;
1087                                     return;
1088                                 }
1089                 
1090                                 $scope.copyId = copy.id();
1091                                 copy.barcode($scope.barcode2);
1092                 
1093                                 egCore.pcrud.update(copy).then(function(stat) {
1094                                     $scope.updateOK = stat;
1095                                     $scope.focusBarcode = true;
1096                                     holdingsSvc.fetchAgain().then(function (){
1097                                         holdingsGridDataProviderRef.refresh();
1098                                     });
1099                                 });
1100
1101                             });
1102                             $uibModalInstance.close();
1103                         }
1104
1105                         $scope.cancel = function($event) {
1106                             $uibModalInstance.dismiss();
1107                             $event.preventDefault();
1108                         }
1109                     }
1110                 ]
1111             });
1112         });
1113     }
1114
1115     var holdings_bChannel = null;
1116     // subscribe to BroadcastChannel for any child VolCopy tabs
1117     // refresh grid if needed to show new updates
1118     // if ($scope.record_tab === 'holdings'){
1119     $scope.$watch('record_tab', function(n){
1120     
1121         if (n === 'holdings'){
1122             if (typeof BroadcastChannel != 'undefined') {
1123                 // we're in holdings tab, connect 2 bChannel
1124                 holdings_bChannel = new BroadcastChannel('eg.holdings.update');
1125                 holdings_bChannel.onmessage = function(e){
1126                     if (e.data
1127                         && e.data.records
1128                         && e.data.records.length
1129                         && e.data.records.includes(Number($scope.record_id))
1130                     ){ // it's for us, refresh grid!
1131                         console.log("Got broadcast from channel eg.holdings.update for records " + e.data.records);
1132                         $scope.holdings_record_id_changed($scope.record_id);
1133                     }
1134                 }
1135             };
1136
1137         } else if (holdings_bChannel){ // we're leaving holding tab, close bChannel
1138             holdings_bChannel.close();
1139         }
1140     
1141     });
1142
1143     // refresh the list of holdings when the record_id is changed.
1144     $scope.holdings_record_id_changed = function(id) {
1145         if ($scope.record_id != id) $scope.record_id = id;
1146         console.log('record id changed to ' + id + ', loading new holdings');
1147         holdingsSvcInst.fetch({
1148             rid : $scope.record_id,
1149             org : $scope.holdings_ou,
1150             copy: $scope.holdings_show_vols ? $scope.holdings_show_copies : false,
1151             vol : $scope.holdings_show_vols,
1152             empty: $scope.holdings_show_empty,
1153             empty_org: $scope.holdings_show_empty_org
1154         }).then(function() {
1155             $scope.holdingsGridDataProvider.refresh();
1156         });
1157     }
1158
1159     // refresh the list of holdings when the filter lib is changed.
1160     $scope.holdings_ou = egCore.org.get(egCore.auth.user().ws_ou());
1161     $scope.holdings_ou_changed = function(org) {
1162         $scope.holdings_ou = org;
1163         holdingsSvcInst.fetch({
1164             rid : $scope.record_id,
1165             org : $scope.holdings_ou,
1166             copy: $scope.holdings_show_vols ? $scope.holdings_show_copies : false,
1167             vol : $scope.holdings_show_vols,
1168             empty: $scope.holdings_show_empty,
1169             empty_org: $scope.holdings_show_empty_org
1170         }).then(function() {
1171             $scope.holdingsGridDataProvider.refresh();
1172         });
1173     }
1174
1175     $scope.holdings_cb_changed = function(cb,newVal,norefresh) {
1176         $scope[cb] = newVal;
1177         var x = $scope.holdings_show_vols ? $scope.holdings_show_copies : false;
1178         $('#holdings_show_copies').prop('checked', x);
1179         egCore.hatch.setItem('cat.' + cb, newVal);
1180         if (!norefresh) holdingsSvcInst.fetch({
1181             rid : $scope.record_id,
1182             org : $scope.holdings_ou,
1183             copy: $scope.holdings_show_vols ? $scope.holdings_show_copies : false,
1184             vol : $scope.holdings_show_vols,
1185             empty: $scope.holdings_show_empty,
1186             empty_org: $scope.holdings_show_empty_org
1187         }).then(function() {
1188             $scope.holdingsGridDataProvider.refresh();
1189         });
1190     }
1191
1192     egCore.hatch.getItem('cat.holdings_show_vols').then(function(x){
1193         if (typeof x ==  'undefined') x = true;
1194         $scope.holdings_cb_changed('holdings_show_vols',x,true);
1195         $('#holdings_show_vols').prop('checked', x);
1196     }).then(function(){
1197         egCore.hatch.getItem('cat.holdings_show_copies').then(function(x){
1198             if (typeof x ==  'undefined') x = true;
1199             $scope.holdings_cb_changed('holdings_show_copies',x,true);
1200             x = $scope.holdings_show_vols ? x : false;
1201             $('#holdings_show_copies').prop('checked', x);
1202         }).then(function(){
1203             egCore.hatch.getItem('cat.holdings_show_empty').then(function(x){
1204                 if (typeof x ==  'undefined') x = true;
1205                 $scope.holdings_cb_changed('holdings_show_empty',x);
1206                 $('#holdings_show_empty').prop('checked', x);
1207             }).then(function(){
1208                 egCore.hatch.getItem('cat.holdings_show_empty_org').then(function(x){
1209                     if (typeof x ==  'undefined') x = true;
1210                     $scope.holdings_cb_changed('holdings_show_empty_org',x);
1211                     $('#holdings_show_empty_org').prop('checked', x);
1212                 })
1213             })
1214         })
1215     });
1216
1217     $scope.vols_not_shown = function () {
1218         return !$scope.holdings_show_vols;
1219     }
1220
1221     $scope.copies_not_shown = function () {
1222         return !$scope.holdings_show_copies;
1223     }
1224
1225     $scope.empty_org_not_shown = function () {
1226         return !$scope.holdings_show_empty_org;
1227     }
1228
1229     $scope.holdings_checkbox_handler = function (item) {
1230         $scope.holdings_cb_changed(item.checkbox,item.checked);
1231     }
1232
1233     function gatherSelectedHoldingsIds () {
1234         var cp_id_list = [];
1235         angular.forEach(
1236             $scope.holdingsGridControls.selectedItems(),
1237             function (item) { cp_id_list = cp_id_list.concat(item.id_list) }
1238         );
1239         return cp_id_list;
1240     }
1241
1242     function gatherSelectedRawCopies () {
1243         var cp_list = [];
1244         angular.forEach(
1245             $scope.holdingsGridControls.selectedItems(),
1246             function (item) { if (item.raw) cp_list = cp_list.concat(item.raw) }
1247         );
1248         return cp_list;
1249     }
1250
1251     function gatherSelectedEmptyVolumeIds () {
1252         var cn_id_list = [];
1253         angular.forEach(
1254             $scope.holdingsGridControls.selectedItems(),
1255             function (item) {
1256                 if (item.copy_count == 0 || (!item.id && item.call_number))
1257                     // we are in a compressed row with no copies, or we are in a single
1258                     // call number row with no copy (testing for presence of 'id')
1259                     // In either case, the call number is 'empty'
1260                     cn_id_list.push(item.call_number.id)
1261             }
1262         );
1263         return cn_id_list;
1264     }
1265
1266     function gatherSelectedVolumeIds () {
1267         var cn_id_list = [];
1268         angular.forEach(
1269             $scope.holdingsGridControls.selectedItems(),
1270             function (item) {
1271                 if (cn_id_list.indexOf(item.call_number.id) == -1)
1272                     cn_id_list.push(item.call_number.id)
1273             }
1274         );
1275         return cn_id_list;
1276     }
1277
1278     $scope.selectedHoldingsDelete = function (vols, copies) {
1279
1280         var cnHash = {};
1281         var perCnCopies = {};
1282
1283         var cn_count = 0;
1284         var cp_count = 0;
1285
1286         angular.forEach(
1287             $scope.holdingsGridControls.selectedItems(),
1288             function (item) {
1289                 if (vols && item.raw_call_number) {
1290                     cnHash[item.call_number.id] = egCore.idl.Clone(item.raw_call_number);
1291                     cnHash[item.call_number.id].isdeleted(1);
1292                     cn_count++;
1293                 } else if (copies) {
1294                     angular.forEach(egCore.idl.Clone(item.raw), function (cp) {
1295                         cp.isdeleted(1);
1296                         cp_count++;
1297                         var cn_id = cp.call_number().id();
1298                         if (!cnHash[cn_id]) {
1299                             cnHash[cn_id] = cp.call_number();
1300                             perCnCopies[cn_id] = [cp];
1301                         } else {
1302                             perCnCopies[cn_id].push(cp);
1303                         }
1304                         cp.call_number(cn_id); // prevent loops in JSON-ification
1305                     });
1306
1307                 }
1308             }
1309         );
1310
1311         angular.forEach(perCnCopies, function (v, k) {
1312             if (vols) {
1313                 cnHash[k].isdeleted(1);
1314                 cn_count++;
1315             }
1316             cnHash[k].copies(v);
1317         });
1318
1319         cnList = [];
1320         angular.forEach(cnHash, function (v, k) {
1321             cnList.push(v);
1322         });
1323
1324         if (cnList.length == 0) return;
1325
1326         var flags = {};
1327         if (vols && copies) flags.force_delete_copies = 1;
1328
1329         egConfirmDialog.open(
1330             egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES,
1331             egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES_MESSAGE,
1332             {copies : cp_count, volumes : cn_count}
1333         ).result.then(function() {
1334             egCore.net.request(
1335                 'open-ils.cat',
1336                 'open-ils.cat.asset.volume.fleshed.batch.update',
1337                 egCore.auth.token(), cnList, 1, flags
1338             ).then(function(resp) {
1339                 var evt = egCore.evt.parse(resp);
1340                 if (evt) {
1341                     egConfirmDialog.open(
1342                         egCore.strings.OVERRIDE_DELETE_ITEMS_FROM_CATALOG_TITLE,
1343                         egCore.strings.OVERRIDE_DELETE_ITEMS_FROM_CATALOG_BODY,
1344                         {'evt_desc': evt.desc}
1345                     ).result.then(function() {
1346                         egCore.net.request(
1347                             'open-ils.cat',
1348                             'open-ils.cat.asset.volume.fleshed.batch.update.override',
1349                             egCore.auth.token(), cnList, 1,
1350                             { events: ['TITLE_LAST_COPY', 'COPY_DELETE_WARNING'] }
1351                         ).then(function() {
1352                             holdingsSvcInst.fetchAgain().then(function() {
1353                                 $scope.holdingsGridDataProvider.refresh();
1354                             });
1355                         });
1356                     });
1357                 } else {
1358                     holdingsSvcInst.fetchAgain().then(function() {
1359                         $scope.holdingsGridDataProvider.refresh();
1360                     });
1361                 }
1362             });
1363         });
1364     }
1365     $scope.selectedHoldingsCopyDelete = function () { $scope.selectedHoldingsDelete(false,true) }
1366     $scope.selectedHoldingsVolCopyDelete = function () { $scope.selectedHoldingsDelete(true,true) }
1367     $scope.selectedHoldingsEmptyVolCopyDelete = function () { $scope.selectedHoldingsDelete(true,false) }
1368
1369     spawnHoldingsAdd = function (add_vols,add_copies){
1370         var raw = [];
1371         if (!add_vols && add_copies) { // just a copy on existing volumes
1372             angular.forEach(gatherSelectedVolumeIds(), function (v) {
1373                 raw.push( {callnumber : v} );
1374             });
1375         } else if (add_vols) {
1376             if (typeof $scope.holdingsGridControls.selectedItems == "function" &&
1377                 $scope.holdingsGridControls.selectedItems().length > 0) {
1378                 angular.forEach($scope.holdingsGridControls.selectedItems(),
1379                     function (item) {
1380                         raw.push({
1381                             owner : item.owner_id,
1382                             label : ((item.call_number) ? item.call_number.label : null)
1383                         });
1384                     });
1385             } else {
1386                 raw.push({
1387                     owner : egCore.auth.user().ws_ou()
1388                 });
1389             }
1390         }
1391
1392         if (raw.length == 0) raw.push({});
1393
1394         egCore.net.request(
1395             'open-ils.actor',
1396             'open-ils.actor.anon_cache.set_value',
1397             null, 'edit-these-copies', {
1398                 record_id: $scope.record_id,
1399                 raw: raw,
1400                 hide_vols : false,
1401                 hide_copies : !add_copies
1402             }
1403         ).then(function(key) {
1404             if (key) {
1405                 var url = egCore.env.basePath + 'cat/volcopy/' + key;
1406                 $timeout(function() { $window.open(url, '_blank') });
1407             } else {
1408                 alert('Could not create anonymous cache key!');
1409             }
1410         });
1411     }
1412     $scope.selectedHoldingsVolCopyAdd = function () { spawnHoldingsAdd(true,true) }
1413     $scope.selectedHoldingsCopyAdd = function () { spawnHoldingsAdd(false,true) }
1414     $scope.selectedHoldingsVolAdd = function () { spawnHoldingsAdd(true,false) }
1415
1416     spawnHoldingsEdit = function (hide_vols,hide_copies){
1417         egCore.net.request(
1418             'open-ils.actor',
1419             'open-ils.actor.anon_cache.set_value',
1420             null, 'edit-these-copies', {
1421                 record_id: $scope.record_id,
1422                 copies: gatherSelectedHoldingsIds(),
1423                 raw: gatherSelectedEmptyVolumeIds().map(
1424                     function(v){ return { callnumber : v } }
1425                 ),
1426                 hide_vols : hide_vols,
1427                 hide_copies : hide_copies
1428             }
1429         ).then(function(key) {
1430             if (key) {
1431                 var url = egCore.env.basePath + 'cat/volcopy/' + key;
1432                 $timeout(function() { $window.open(url, '_blank') });
1433             } else {
1434                 alert('Could not create anonymous cache key!');
1435             }
1436         });
1437     }
1438     $scope.selectedHoldingsVolCopyEdit = function () { spawnHoldingsEdit(false,false) }
1439     $scope.selectedHoldingsVolEdit = function () { spawnHoldingsEdit(false,true) }
1440     $scope.selectedHoldingsCopyEdit = function () { spawnHoldingsEdit(true,false) }
1441
1442     $scope.selectedHoldingsItemStatus = function (){
1443         var url = egCore.env.basePath + 'cat/item/search/' + gatherSelectedHoldingsIds().join(',')
1444         $timeout(function() { $window.open(url, '_blank') });
1445     }
1446
1447     $scope.markFromSelectedAsHoldingsTarget = function() {
1448         egCore.hatch.setLocalItem(
1449             'eg.cat.transfer_target_lib',
1450             $scope.holdingsGridControls.selectedItems()[0].owner_id
1451         );
1452         egCore.hatch.setLocalItem(
1453             'eg.cat.transfer_target_record',
1454             $scope.record_id
1455         );
1456         if ($scope.holdingsGridControls.selectedItems()[0].call_number.id) { // cn.id missing when vols are collapsed, or we are on an empty lib
1457             egCore.hatch.setLocalItem(
1458                 'eg.cat.transfer_target_vol',
1459                 $scope.holdingsGridControls.selectedItems()[0].call_number.id
1460             );
1461         } else {
1462             // clear out the stale value if we're on a lib-only
1463             // or vol-collapsed row
1464             egCore.hatch.removeLocalItem('eg.cat.transfer_target_vol');
1465         }
1466         ngToast.create(egCore.strings.MARK_HOLDINGS_TARGET);
1467     }
1468
1469     $scope.selectedHoldingsItemStatusDetail = function (){
1470         angular.forEach(
1471             gatherSelectedHoldingsIds(),
1472             function (cid) {
1473                 var url = egCore.env.basePath +
1474                           'cat/item/' + cid;
1475                 $timeout(function() { $window.open(url, '_blank') });
1476             }
1477         );
1478     }
1479
1480     $scope.transferVolumes = function (){
1481         var target_record = egCore.hatch.getLocalItem('eg.cat.transfer_target_record');
1482         var target_lib = egCore.hatch.getLocalItem('eg.cat.transfer_target_lib');
1483         if (!target_lib
1484             && (!target_record || ($scope.record_id == target_record) )
1485         ) return;
1486
1487         var vols_to_move = {};
1488         if (target_lib) {
1489             // we're moving volumes to a different library
1490             var vol_ids = gatherSelectedVolumeIds();
1491             if (vol_ids.length) {
1492                 vols_to_move[target_lib] = vol_ids;
1493
1494                 // if we're *only* switching libs,
1495                 // grab the current record as the target
1496                 target_record = target_record || $scope.record_id;
1497             }
1498         } else {
1499             // we're moving volumes to the same library they exist in
1500             // currently, but on a different record
1501             var items = $scope.holdingsGridControls.selectedItems();
1502             angular.forEach(items, function(item) {
1503                 if (!(item.call_number.owning_lib in vols_to_move)) {
1504                     vols_to_move[item.call_number.owning_lib] = new Array;
1505                 }
1506                 vols_to_move[item.call_number.owning_lib].push(item.call_number.id);
1507             });
1508         }
1509
1510         var promises = [];        
1511         angular.forEach(vols_to_move, function(vols, owning_lib) {
1512             promises.push(egCore.net.request(
1513                 'open-ils.cat',
1514                 'open-ils.cat.asset.volume.batch.transfer.override',
1515                 egCore.auth.token(), {
1516                     docid   : target_record,
1517                     lib     : owning_lib,
1518                     volumes : vols
1519                 }
1520             ));
1521         });
1522         $q.all(promises).then(function(success) {
1523             if (success) {
1524                 ngToast.create(egCore.strings.VOLS_TRANSFERED);
1525                 holdingsSvcInst.fetchAgain().then(function() {
1526                     $scope.holdingsGridDataProvider.refresh();
1527                 });
1528             } else {
1529                 alert('Could not transfer volumes!');
1530             }
1531         });
1532     }
1533
1534     // this "transfers" selected copies to a new owning library,
1535     // auto-creating volumes as required
1536     $scope.transferItemsAutoFill = function() {
1537         var target_record = egCore.hatch.getLocalItem('eg.cat.transfer_target_record');
1538         var target_lib = egCore.hatch.getLocalItem('eg.cat.transfer_target_lib');
1539         if (!target_lib
1540             && (!target_record || ($scope.record_id == target_record) )
1541         ) return;
1542
1543         var items = $scope.holdingsGridControls.selectedItems();
1544         if (!items.length) {
1545             return;
1546         }
1547
1548         var vols_to_move   = {};
1549         var copies_to_move = {};
1550         angular.forEach(items, function(item) {
1551             var needs_move = false;
1552             if (target_lib
1553                 && (item.call_number.owning_lib != target_lib)) {
1554                     item.call_number.owning_lib = target_lib;
1555                     needs_move = true;
1556             }
1557             if (target_record
1558                 && (item.call_number.record != target_record)) {
1559                     item.call_number.record = target_record;
1560                     needs_move = true;
1561             }
1562             if (needs_move) {
1563                 if (item.call_number.id in vols_to_move) {
1564                     copies_to_move[item.call_number.id].push(item.id);
1565                 } else {
1566                     vols_to_move[item.call_number.id] = item.call_number;
1567                     copies_to_move[item.call_number.id] = new Array;
1568                     copies_to_move[item.call_number.id].push(item.id);
1569                 }
1570             }
1571         });
1572
1573         var promises = [];
1574         angular.forEach(vols_to_move, function(vol) {
1575             promises.push(egCore.net.request(
1576                 'open-ils.cat',
1577                 'open-ils.cat.call_number.find_or_create',
1578                 egCore.auth.token(),
1579                 vol.label,
1580                 vol.record, // may be new
1581                 vol.owning_lib, // may be new
1582                 vol.prefix.id,
1583                 vol.suffix.id,
1584                 vol.label_class
1585             ).then(function(resp) {
1586                 var evt = egCore.evt.parse(resp);
1587                 if (evt) return;
1588                 return egCore.net.request(
1589                     'open-ils.cat',
1590                     'open-ils.cat.transfer_copies_to_volume',
1591                     egCore.auth.token(),
1592                     resp.acn_id,
1593                     copies_to_move[vol.id]
1594                 );
1595             }));
1596         });
1597         $q.all(promises).then(function() {
1598             ngToast.create(egCore.strings.ITEMS_TRANSFERED);
1599             holdingsSvcInst.fetchAgain().then(function() {
1600                 $scope.holdingsGridDataProvider.refresh();
1601             });
1602         });
1603     }
1604
1605     $scope.gridCellHandlers = {};
1606     $scope.gridCellHandlers.copyAlertsEdit = function(id) {
1607         egCirc.manage_copy_alerts([id]).then(function() {
1608             // update grid items?
1609         });
1610     };
1611
1612     $scope.transferItems = function (){
1613         var xfer_target = egCore.hatch.getLocalItem('eg.cat.transfer_target_vol');
1614
1615         if (!xfer_target) {
1616             // we have no specific volume, let's try to fill in the
1617             // blanks instead
1618             return $scope.transferItemsAutoFill();
1619         }
1620
1621         var copy_ids = gatherSelectedHoldingsIds();
1622         if (copy_ids.length > 0) {
1623             egCore.net.request(
1624                 'open-ils.cat',
1625                 'open-ils.cat.transfer_copies_to_volume',
1626                 egCore.auth.token(),
1627                 xfer_target,
1628                 copy_ids
1629             ).then(
1630                 function(resp) { // oncomplete
1631                     var evt = egCore.evt.parse(resp);
1632                     if (evt) {
1633                         egConfirmDialog.open(
1634                             egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_TITLE,
1635                             egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_BODY,
1636                             {'evt_desc': evt.desc}
1637                         ).result.then(function() {
1638                             egCore.net.request(
1639                                 'open-ils.cat',
1640                                 'open-ils.cat.transfer_copies_to_volume.override',
1641                                 egCore.auth.token(),
1642                                 xfer_target,
1643                                 copy_ids,
1644                                 { events: ['TITLE_LAST_COPY', 'COPY_DELETE_WARNING'] }
1645                             ).then(function(resp) {
1646                                 holdingsSvcInst.fetchAgain().then(function() {
1647                                     $scope.holdingsGridDataProvider.refresh();
1648                                 });
1649                             });
1650                         });
1651                     } else {
1652                         ngToast.create(egCore.strings.ITEMS_TRANSFERED);
1653                         holdingsSvcInst.fetchAgain().then(function() {
1654                             $scope.holdingsGridDataProvider.refresh();
1655                         });
1656                     }
1657                 },
1658                 null, // onerror
1659                 null // onprogress
1660             )
1661         }
1662     }
1663
1664     $scope.selectedHoldingsItemStatusTgrEvt = function (){
1665         angular.forEach(
1666             gatherSelectedHoldingsIds(),
1667             function (cid) {
1668                 var url = '/eg2/staff/circ/item/event-log/' + cid;
1669                 $timeout(function() { $window.open(url, '_blank') });
1670             }
1671         );
1672     }
1673
1674     $scope.selectedHoldingsItemStatusHolds = function (){
1675         angular.forEach(
1676             gatherSelectedHoldingsIds(),
1677             function (cid) {
1678                 var url = egCore.env.basePath +
1679                           'cat/item/' + cid + '/holds';
1680                 $timeout(function() { $window.open(url, '_blank') });
1681             }
1682         );
1683     }
1684
1685     $scope.selectedHoldingsPrintLabels = function() {
1686         egCore.net.request(
1687             'open-ils.actor',
1688             'open-ils.actor.anon_cache.set_value',
1689             null, 'print-labels-these-copies', {
1690                 copies : gatherSelectedHoldingsIds()
1691             }
1692         ).then(function(key) {
1693             if (key) {
1694                 var url = egCore.env.basePath + 'cat/printlabels/' + key;
1695                 $timeout(function() { $window.open(url, '_blank') });
1696             } else {
1697                 alert('Could not create anonymous cache key!');
1698             }
1699         });
1700     }
1701
1702     $scope.selectedHoldingsDamaged = function () {
1703         var copy_list = gatherSelectedRawCopies();
1704         if (copy_list.length == 0) return;
1705
1706         angular.forEach(copy_list, function(cp) {
1707             egCirc.mark_damaged({
1708                 id: cp.id(),
1709                 barcode: cp.barcode(),
1710                 circ_lib: cp.circ_lib().id()
1711             }).then(function() {
1712                 holdingsSvcInst.fetchAgain().then(function() {
1713                     $scope.holdingsGridDataProvider.refresh();
1714                 });
1715             });
1716         });
1717     }
1718
1719     $scope.selectedHoldingsDiscard = function () {
1720         var copy_list = gatherSelectedRawCopies();
1721         if (copy_list.length == 0) return;
1722         egCirc.mark_discard(copy_list.map(function(cp) {
1723             return {id: cp.id(), barcode: cp.barcode()};})).then(function() {
1724                 holdingsSvcInst.fetchAgain().then(function() {
1725                     $scope.holdingsGridDataProvider.refresh();
1726                 });
1727             });
1728     }
1729
1730     $scope.selectedHoldingsMissing = function () {
1731         var copy_list = gatherSelectedRawCopies();
1732         if (copy_list.length == 0) return;
1733         egCirc.mark_missing(copy_list.map(function(cp) {
1734             return {id: cp.id(), barcode: cp.barcode()};})).then(function() {
1735                 holdingsSvcInst.fetchAgain().then(function() {
1736                     $scope.holdingsGridDataProvider.refresh();
1737                 });
1738             });
1739     }
1740
1741     $scope.selectedHoldingsCopyAlertsAdd = function() {
1742         egCirc.add_copy_alerts(gatherSelectedHoldingsIds()).then(function() {
1743             // no need to refresh grid
1744         });
1745     }
1746     $scope.selectedHoldingsCopyAlertsManage = function() {
1747         egCirc.manage_copy_alerts(gatherSelectedHoldingsIds()).then(function() {
1748             // no need to refresh grid
1749         });
1750     }
1751
1752     $scope.attach_to_peer_bib = function() {
1753         var copy_list = gatherSelectedHoldingsIds();
1754         if (copy_list.length == 0) return;
1755
1756         egCore.hatch.getItem('eg.cat.marked_conjoined_record').then(function(target_record) {
1757             if (!target_record) return;
1758
1759             return $uibModal.open({
1760                 templateUrl: './cat/catalog/t_conjoined_selector',
1761                 backdrop: 'static',
1762                 animation: true,
1763                 controller:
1764                        ['$scope','$uibModalInstance',
1765                 function($scope , $uibModalInstance) {
1766                     $scope.update = false;
1767
1768                     $scope.peer_type = null;
1769                     $scope.peer_type_list = [];
1770                     conjoinedSvc.get_peer_types().then(function(list){
1771                         $scope.peer_type_list = list;
1772                     });
1773     
1774                     $scope.ok = function(type) {
1775                         var promises = [];
1776     
1777                         angular.forEach(copy_list, function (cp) {
1778                             var n = new egCore.idl.bpbcm();
1779                             n.isnew(true);
1780                             n.peer_record(target_record);
1781                             n.target_copy(cp);
1782                             n.peer_type(type);
1783                             promises.push(egCore.pcrud.create(n));
1784                         });
1785     
1786                         return $q.all(promises).then(function(){$uibModalInstance.close()});
1787                     }
1788     
1789                     $scope.cancel = function($event) {
1790                         $uibModalInstance.dismiss();
1791                         $event.preventDefault();
1792                     }
1793                 }]
1794             });
1795         });
1796     }
1797
1798
1799     // ------------------------------------------------------------------
1800     // Holds 
1801     var provider = egGridDataProvider.instance({});
1802     var holds = []; // current list of holds
1803     var hold_count = 0;
1804     var hold_grid_load_promise;
1805
1806     $scope.hold_grid_data_provider = provider;
1807     $scope.grid_actions = egHoldGridActions;
1808     $scope.grid_actions.refresh = function () { holds = []; hold_count = 0; provider.refresh() };
1809     $scope.hold_grid_controls = {};
1810
1811     provider.get = function(offset, count) {
1812         if ($scope.record_tab != 'holds') return $q.when();
1813
1814         if (hold_grid_load_promise) {
1815             // Active load in progress.
1816             console.debug('Exiting concurrent hold fetch');
1817             return hold_grid_load_promise;
1818         }
1819
1820         // see if we have the requested range cached
1821         if (holds[offset]) {
1822             console.debug(
1823                 'Serving holds from cache with pickup lib', $scope.pickup_ou.id());
1824             return provider.arrayNotifier(holds, offset, count);
1825         }
1826
1827         hold_count = 0;
1828         holds = [];
1829         var restrictions = {
1830                 is_staff_request : 'true',
1831                 fulfillment_time : null,
1832                 cancel_time      : null,
1833                 record_id        : $scope.record_id,
1834                 pickup_lib       : egCore.org.descendants($scope.pickup_ou.id(), true)
1835         };
1836
1837         var order_by = [{ request_time : null }];
1838         // NOTE: Server sort is disabled for now.  See the comment on
1839         // similar code in circ/holds/app.js for details.
1840         if (false && provider.sort && provider.sort.length) {
1841             order_by = [];
1842             angular.forEach(provider.sort, function (c) {
1843                 if (!angular.isObject(c)) {
1844                     if (c.match(/^hold\./)) {
1845                         var i = c.replace('hold.','');
1846                         var ob = {};
1847                         ob[i] = null;
1848                         order_by.push(ob);
1849                     }
1850                 } else {
1851                     var i = Object.keys(c)[0];
1852                     var direction = c[i];
1853                     if (i.match(/^hold\./)) {
1854                         i = i.replace('hold.','');
1855                         var ob = {}
1856                         ob[i] = {dir:direction};
1857                         order_by.push(ob);
1858                     }
1859                 }
1860             });
1861         }
1862
1863         console.debug(
1864             'Fetching holds from network with PU lib', $scope.pickup_ou.id());
1865
1866         egProgressDialog.open({max : 1, value : 0});
1867         var first = true;
1868         hold_grid_load_promise = egHolds.fetch_wide_holds(
1869             restrictions,
1870             order_by
1871         ).then(function () {
1872                 hold_grid_load_promise = null;
1873                 return provider.arrayNotifier(holds, offset, count);
1874             },
1875             null,
1876             function(hold_data) {
1877                 if (first) {
1878                     hold_count = hold_data;
1879                     first = false;
1880                     egProgressDialog.update({max:hold_count});
1881                 } else {
1882                     egProgressDialog.increment();
1883                     var new_item = { id : hold_data.id, hold : hold_data };
1884                     new_item.status_string =
1885                         egCore.strings['HOLD_STATUS_' + hold_data.hold_status]
1886                         || hold_data.hold_status;
1887
1888                     holds.push(new_item);
1889                 }
1890             }
1891         ).finally(function() {
1892             hold_grid_load_promise = null;
1893             egProgressDialog.close();
1894         });
1895
1896         return hold_grid_load_promise;
1897     }
1898
1899     $scope.detail_view = function(action, user_data, items) {
1900         if (h = items[0]) {
1901             $scope.detail_hold_id = h.hold.id;
1902         }
1903     }
1904
1905     $scope.list_view = function(items) {
1906          $scope.detail_hold_id = null;
1907     }
1908
1909     // refresh the list of record holds when the pickup lib is changed.
1910     $scope.pickup_ou = egCore.org.get(egCore.auth.user().ws_ou());
1911     $scope.pickup_ou_changed = function(org) {
1912         if ($scope.pickup_ou && $scope.pickup_ou.id() == org.id()) {
1913             // This fires on every component render, even though the
1914             // value we already have may match.  Avoid duplicate lookups.
1915             return;
1916         }
1917
1918         var promise = hold_grid_load_promise || $q.when();
1919
1920         // Avoid refreshing the grid if it's currently loading data.
1921         promise.finally(function() {
1922
1923             // Previous grid data load complete.  Timeout gives the
1924             // grid a chance to mark itself as load-completed, which
1925             // happens after the data load promise is done.
1926             setTimeout(function() {
1927                 console.debug('Refreshing holds after PU lib change to ', org.id());
1928                 $scope.pickup_ou = org;
1929                 holds = []
1930                 hold_count = 0;
1931                 provider.refresh();
1932             });
1933         })
1934     }
1935
1936     function map_prefix_to_subhash (h,pf) {
1937         var newhash = {};
1938         angular.forEach(Object.keys(h), function (k) {
1939             if (k.startsWith(pf)) {
1940                 var nk = k.substr(pf.length);
1941                 newhash[nk] = h[k];
1942             }
1943         });
1944         return newhash;
1945     }
1946
1947     $scope.print_holds = function() {
1948         var pholds = [];
1949         angular.forEach(holds, function(item) {
1950             pholds.push({
1951                 hold : item.hold,
1952                 status_string : item.status_string,
1953                 patron_first : item.hold.usr_first_given_name,
1954                 patron_last : item.hold.usr_family_name,
1955                 patron_alias : item.hold.usr_alias,
1956                 patron_barcode : item.hold.ucard_barcode,
1957                 copy : map_prefix_to_subhash(item.hold,'cp_'),
1958                 volume : map_prefix_to_subhash(item.hold,'cn_'),
1959                 title : item.hold.title,
1960                 author : item.hold.author
1961             });
1962         });
1963
1964         egCore.print.print({
1965             context : 'receipt', 
1966             template : 'holds_for_bib', 
1967             scope : {holds : pholds}
1968         });
1969     }
1970
1971     $scope.current_hold_transfer_dest = egCore.hatch.getLocalItem ('eg.circ.hold.title_transfer_target');
1972
1973     $scope.mark_hold_transfer_dest = function() {
1974         $scope.current_hold_transfer_dest = $scope.record_id;
1975         egCore.hatch.setLocalItem(
1976             'eg.circ.hold.title_transfer_target', $scope.record_id);
1977         ngToast.create(egCore.strings.HOLD_TRANSFER_DEST_MARKED);
1978     }
1979
1980     // UI presents this option as "all holds"
1981     $scope.transfer_holds_to_marked = function() {
1982         var hold_ids = $scope.hold_grid_controls.allItems().map(
1983             function(hold_data) {return hold_data.hold.id});
1984         egHolds.transfer_to_marked_title(hold_ids);
1985     }
1986
1987     // ------------------------------------------------------------------
1988     // Initialize the selected tab
1989
1990     // we explicitly initialize catalog_url because otherwise Firefox
1991     // ends up setting it to $BASE_URL/{{url}}, which then messes
1992     // things up. See LP#1708951
1993     $scope.catalog_url = '';
1994
1995     function init_cat_url() {
1996         // Set the initial catalog URL.  This only happens once.
1997         // The URL is otherwise generated through user navigation.
1998         if ($scope.catalog_url) return;
1999
2000         var url = $location.absUrl().replace(/\/staff.*/, '/opac/advanced');
2001
2002         // A record ID in the path indicates a request for the record-
2003         // specific page.
2004         if ($routeParams.record_id) {
2005             url = url.replace(/\/advanced/, '/record/' + $scope.record_id);
2006         }
2007
2008         // Jumping directly to the results page by passing a search
2009         // query via the URL.  Copy all URL params to the iframe url.
2010         if ($location.path().match(/catalog\/results/)) {
2011             url = url.replace(/\/advanced/, '/results?');
2012             var first = true;
2013             angular.forEach($location.search(), function(val, key) {
2014                 if (!first) url += '&';
2015                 first = false;
2016                 url += encodeURIComponent(key) 
2017                     + '=' + encodeURIComponent(val);
2018             });
2019         }
2020
2021         // if we're displaying the advanced search form, select
2022         // whatever default pane the user has chosen via workstation
2023         // preference
2024         if (url.match(/\/opac\/advanced$/)) {
2025             egCore.hatch.getItem('eg.search.adv_pane').then(function(adv_pane_val){
2026                 if (adv_pane_val) {
2027                     url += '?pane=' + encodeURIComponent(adv_pane_val);
2028                 }
2029
2030                 $scope.catalog_url = url;
2031             });
2032         } else {
2033             $scope.catalog_url = url;
2034         }
2035
2036     }
2037
2038     function init_parts_url() {
2039         $scope.parts_url = $location
2040             .absUrl()
2041             .replace(
2042                 /\/staff.*/,
2043                 '/conify/global/biblio/monograph_part?r='+$scope.record_id
2044             );
2045     }
2046
2047     $scope.set_record_tab = function(tab) {
2048         $scope.record_tab = tab;
2049
2050         switch(tab) {
2051
2052             case 'monoparts':
2053                 init_parts_url();
2054                 break;
2055
2056             case 'catalog':
2057                 init_cat_url();
2058                 break;
2059
2060             case 'holds':
2061                 $scope.detail_hold_record_id = $scope.record_id; 
2062                 // refresh the holds grid
2063                 provider.refresh();
2064
2065                 break;
2066         }
2067     }
2068
2069     $scope.set_default_record_tab = function() {
2070         egCore.hatch.setLocalItem(
2071             'eg.cat.default_record_tab', $scope.record_tab);
2072         $timeout(function(){$scope.default_tab = $scope.record_tab});
2073     }
2074
2075     var tab;
2076     if ($scope.record_id) {
2077         $scope.default_tab = get_default_record_tab();
2078         tab = $routeParams.record_tab || $scope.default_tab;
2079
2080     } else {
2081         tab = $routeParams.record_tab || 'catalog';
2082     }
2083     $scope.set_record_tab(tab);
2084
2085 }])
2086
2087 .controller('AuthorityCtrl',
2088        ['$scope','$routeParams','$location','$window','$q','egCore',
2089 function($scope , $routeParams , $location , $window , $q , egCore) {
2090
2091     // set record ID on page load if available...
2092     $scope.authority_id = $routeParams.authority_id;
2093
2094     if ($routeParams.authority_id) $scope.from_route = true;
2095     else $scope.from_route = false;
2096
2097     $scope.stop_unload = false;
2098 }])
2099
2100 .controller('URLVerifyCtrl',
2101        ['$scope','$location',
2102 function($scope , $location) {
2103     $scope.verifyurls_url = $location.absUrl().replace(/\/staff.*/, '/url_verify/sessions');
2104 }])
2105
2106 .controller('VandelayCtrl',
2107        ['$scope','$location', 'egCore', '$uibModal',
2108 function($scope , $location, egCore, $uibModal) {
2109     $scope.vandelay_url = $location.absUrl().replace(/\/staff\/cat\/catalog\/vandelay/, '/vandelay/vandelay');
2110     $scope.funcs = {};
2111     $scope.funcs.edit_marc_modal = function(bre, callback){
2112         var marcArgs = { 'marc_xml': bre.marc() };
2113         var vqbibrecId = bre.id();
2114         $uibModal.open({
2115             templateUrl: './cat/catalog/t_edit_marc_modal',
2116             backdrop: 'static',
2117             size: 'lg',
2118             controller: ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
2119                 $scope.focusMe = true;
2120                 $scope.recordId = vqbibrecId;
2121                 $scope.args = marcArgs;
2122                 $scope.dirty_flag = false;
2123                 $scope.ok = function(marg){
2124                     $uibModalInstance.close(marg);
2125                 };
2126                 $scope.cancel = function(){ $uibModalInstance.dismiss() }
2127             }]
2128         }).result.then(function(res){
2129             var new_xml = res.marc_xml;
2130             egCore.pcrud.retrieve('vqbr', vqbibrecId).then(function(vqbib){
2131                 vqbib.marc(new_xml);
2132                 egCore.pcrud.update(vqbib).then( function(){ callback(vqbibrecId); });
2133             });
2134         });
2135     };
2136 }])
2137
2138 .controller('ManageAuthoritiesCtrl',
2139        ['$scope','$location',
2140 function($scope , $location) {
2141     $scope.manageauthorities_url = $location.absUrl().replace(/\/staff.*/, '/cat/authority/list');
2142 }])
2143
2144 .controller('BatchEditCtrl',
2145        ['$scope','$location','$routeParams',
2146 function($scope , $location , $routeParams) {
2147     $scope.batchedit_url = $location.absUrl().replace(/\/eg.*/, '/opac/extras/merge_template');
2148     if ($routeParams.container_type) {
2149         switch ($routeParams.container_type) {
2150             case 'bucket':
2151                 $scope.batchedit_url += '?recordSource=b&containerid=' + $routeParams.container_id;
2152                 break;
2153             case 'record':
2154                 $scope.batchedit_url += '?recordSource=r&recid=' + $routeParams.container_id;
2155                 break;
2156         };
2157     }
2158 }])
2159
2160  
2161 .filter('boolText', function(){
2162     return function (v) {
2163         return v == 't';
2164     }
2165 })
2166
2167 .factory('conjoinedSvc', 
2168        ['egCore','$q',
2169 function(egCore , $q) {
2170
2171     var service = {
2172         items : [], // record search results
2173         index : 0, // search grid index
2174         rid : null
2175     };
2176
2177     service.flesh = {   
2178         flesh : 4, 
2179         flesh_fields : {
2180             bpbcm : ['target_copy','peer_type'],
2181             acp : ['call_number'],
2182             acn : ['record'],
2183             bre : ['simple_record']
2184         },
2185         // avoid fetching the MARC blob by specifying which
2186         // fields on the bre to select.  More may be needed.
2187         // note that fleshed fields are explicitly selected.
2188         select : { bre : ['id'] },
2189         order_by : { bpbcm : ['id'] },
2190     }
2191
2192     // resolved with the last received copy
2193     service.fetch = function(rid) {
2194         if (!rid && !service.rid) return $q.when();
2195
2196         if (rid) service.rid = rid;
2197         service.items = [];
2198         service.index = 0;
2199
2200         return egCore.pcrud.search(
2201             'bpbcm',
2202             {peer_record : service.rid},
2203             service.flesh,
2204             {atomic : true}
2205         ).then( function(list) { // finished
2206             service.items = list;
2207             return service.items;
2208         });
2209     }
2210
2211     // returns a promise resolved with the list of peer bib types
2212     service.get_peer_types = function() {
2213         if (egCore.env.bpt)
2214             return $q.when(egCore.env.bpt.list);
2215
2216         return egCore.pcrud.retrieveAll('bpt', null, {atomic : true})
2217         .then(function(list) {
2218             egCore.env.absorbList(list, 'bpt');
2219             return list;
2220         });
2221     };
2222
2223     return service;
2224 }])
2225
2226