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