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