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