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