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