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