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