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