LP#1669907: add comment about underlying cause of the bug
[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'])
11
12 .config(['ngToastProvider', function(ngToastProvider) {
13   ngToastProvider.configure({
14     verticalPosition: 'bottom',
15     animation: 'fade'
16   });
17 }])
18
19 .config(function($routeProvider, $locationProvider, $compileProvider) {
20     $locationProvider.html5Mode(true);
21     $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|blob):/); // grid export
22
23     var resolver = {delay : ['egCore','egStartup','egUser', function(egCore, egStartup, egUser) {
24         egCore.env.classLoaders.aous = function() {
25             return egCore.org.settings([
26                 'cat.marc_control_number_identifier'
27             ]).then(function(settings) {
28                 // local settings are cached within egOrg.  Caching them
29                 // again in egEnv just simplifies the syntax for access.
30                 egCore.env.aous = settings;
31             });
32         }
33         egCore.env.loadClasses.push('aous');
34         return egStartup.go()
35     }]};
36
37     $routeProvider.when('/cat/catalog/index', {
38         templateUrl: './cat/catalog/t_catalog',
39         controller: 'CatalogCtrl',
40         resolve : resolver
41     });
42
43     // Jump directly to the results page.  Any URL parameter 
44     // supported by the embedded catalog is supported here.
45     $routeProvider.when('/cat/catalog/results', {
46         templateUrl: './cat/catalog/t_catalog',
47         controller: 'CatalogCtrl',
48         resolve : resolver
49     });
50
51     $routeProvider.when('/cat/catalog/retrieve_by_id', {
52         templateUrl: './cat/catalog/t_retrieve_by_id',
53         controller: 'CatalogRecordRetrieve',
54         resolve : resolver
55     });
56
57     $routeProvider.when('/cat/catalog/retrieve_by_tcn', {
58         templateUrl: './cat/catalog/t_retrieve_by_tcn',
59         controller: 'CatalogRecordRetrieve',
60         resolve : resolver
61     });
62
63     $routeProvider.when('/cat/catalog/new_bib', {
64         templateUrl: './cat/catalog/t_new_bib',
65         controller: 'NewBibCtrl',
66         resolve : resolver
67     });
68
69     // create some catalog page-specific mappings
70     $routeProvider.when('/cat/catalog/record/:record_id', {
71         templateUrl: './cat/catalog/t_catalog',
72         controller: 'CatalogCtrl',
73         resolve : resolver
74     });
75
76     // create some catalog page-specific mappings
77     $routeProvider.when('/cat/catalog/record/:record_id/:record_tab', {
78         templateUrl: './cat/catalog/t_catalog',
79         controller: 'CatalogCtrl',
80         resolve : resolver
81     });
82
83     $routeProvider.when('/cat/catalog/batchEdit', {
84         templateUrl: './cat/catalog/t_batchedit',
85         controller: 'BatchEditCtrl',
86         resolve : resolver
87     });
88
89     $routeProvider.when('/cat/catalog/batchEdit/:container_type/:container_id', {
90         templateUrl: './cat/catalog/t_batchedit',
91         controller: 'BatchEditCtrl',
92         resolve : resolver
93     });
94
95     $routeProvider.when('/cat/catalog/vandelay', {
96         templateUrl: './cat/catalog/t_vandelay',
97         controller: 'VandelayCtrl',
98         resolve : resolver
99     });
100
101     $routeProvider.when('/cat/catalog/verifyURLs', {
102         templateUrl: './cat/catalog/t_verifyurls',
103         controller: 'URLVerifyCtrl',
104         resolve : resolver
105     });
106
107     $routeProvider.when('/cat/catalog/manageAuthorities', {
108         templateUrl: './cat/catalog/t_manageauthorities',
109         controller: 'ManageAuthoritiesCtrl',
110         resolve : resolver
111     });
112
113     $routeProvider.when('/cat/catalog/authority/:authority_id/marc_edit', {
114         templateUrl: './cat/catalog/t_authority',
115         controller: 'AuthorityCtrl',
116         resolve : resolver
117     });
118
119     $routeProvider.otherwise({redirectTo : '/cat/catalog/index'});
120 })
121
122
123 /**
124  * */
125 .controller('CatalogRecordRetrieve',
126        ['$scope','$routeParams','$location','$q','egCore',
127 function($scope , $routeParams , $location , $q , egCore ) {
128
129     $scope.focusMe = true;
130
131     // jump to the patron checkout UI
132     function loadRecord(record_id) {
133         $location
134         .path('/cat/catalog/record/' + record_id);
135     }
136
137     $scope.submitId = function(args) {
138         $scope.recordNotFound = null;
139         if (!args.record_id) return;
140
141         // blur so next time it's set to true it will re-apply select()
142         $scope.selectMe = false;
143
144         return loadRecord(args.record_id);
145     }
146
147     $scope.submitTCN = function(args) {
148         $scope.recordNotFound = null;
149         $scope.moreRecordsFound = null;
150         if (!args.record_tcn) return;
151
152         // blur so next time it's set to true it will re-apply select()
153         $scope.selectMe = false;
154
155         // lookup TCN
156         egCore.net.request(
157             'open-ils.search',
158             'open-ils.search.biblio.tcn',
159             args.record_tcn)
160
161         .then(function(resp) { // get_barcodes
162
163             if (evt = egCore.evt.parse(resp)) {
164                 alert(evt); // FIXME
165                 return;
166             }
167
168             if (!resp.count) {
169                 $scope.recordNotFound = args.record_tcn;
170                 $scope.selectMe = true;
171                 return;
172             }
173
174             if (resp.count > 1) {
175                 $scope.moreRecordsFound = args.record_tcn;
176                 $scope.selectMe = true;
177                 return;
178             }
179
180             var record_id = resp.ids[0];
181             return loadRecord(record_id);
182         });
183     }
184
185 }])
186
187 .controller('NewBibCtrl',
188        ['$scope','$routeParams','$location','$window','$q','egCore',
189         'egGridDataProvider','egHoldGridActions','$timeout','holdingsSvc',
190 function($scope , $routeParams , $location , $window , $q , egCore) {
191
192     $scope.have_template = false;
193     $scope.marc_template = '';
194     $scope.stop_unload = false;
195     $scope.template_list = [];
196     $scope.template_name = '';
197     $scope.new_bib_id = 0;
198
199     egCore.net.request(
200         'open-ils.cat',
201         'open-ils.cat.marc_template.types.retrieve'
202     ).then(function(resp) {
203         angular.forEach(resp, function(name) {
204             $scope.template_list.push(name);
205         });
206         $scope.template_list.sort();
207     });
208     $scope.template_name = egCore.hatch.getSessionItem('eg.cat.last_bib_marc_template');
209     if (!$scope.template_name) {
210         egCore.hatch.getItem('cat.default_bib_marc_template').then(function(template) {
211             $scope.template_name = template;
212         });
213     }
214
215     $scope.loadTemplate = function() {
216         if ($scope.template_name) {
217             egCore.net.request(
218                 'open-ils.cat',
219                 'open-ils.cat.biblio.marc_template.retrieve',
220                 $scope.template_name
221             ).then(function(template) {
222                 $scope.marc_template = template;
223                 $scope.have_template = true;
224                 egCore.hatch.setSessionItem('eg.cat.last_bib_marc_template', $scope.template_name);
225             });
226         }
227     }
228
229     $scope.setDefaultTemplate = function() {
230         var hatch_key = "cat.default_bib_marc_template";
231         if ($scope.template_name) {
232             egCore.hatch.setItem(hatch_key, $scope.template_name);
233         } else {
234             egCore.hatch.removeItem(hatch_key);
235         }
236     }
237
238     $scope.$watch('new_bib_id', function(newVal, oldVal) {
239         if (newVal) {
240             $location.path('/cat/catalog/record/' + $scope.new_bib_id);
241         }
242     });
243     
244
245 }])
246 .controller('CatalogCtrl',
247        ['$scope','$routeParams','$location','$window','$q','egCore','egHolds','egCirc','egConfirmDialog','ngToast',
248         'egGridDataProvider','egHoldGridActions','egProgressDialog','$timeout','$uibModal','holdingsSvc','egUser','conjoinedSvc',
249         '$cookies',
250 function($scope , $routeParams , $location , $window , $q , egCore , egHolds , egCirc , egConfirmDialog , ngToast ,
251          egGridDataProvider , egHoldGridActions , egProgressDialog , $timeout , $uibModal , holdingsSvc , egUser , conjoinedSvc,
252          $cookies
253 ) {
254
255     var holdingsSvcInst = new holdingsSvc();
256
257     // set record ID on page load if available...
258     $scope.record_id = $routeParams.record_id;
259     $scope.summary_pane_record;
260
261     if ($routeParams.record_id) $scope.from_route = true;
262     else $scope.from_route = false;
263
264     // set search and preferred library cookies
265     egCore.hatch.getItem('eg.search.search_lib').then(function(val) {
266         $cookies.put('eg_search_lib', val, { path : '/' });
267     });
268     egCore.hatch.getItem('eg.search.pref_lib').then(function(val) {
269         $cookies.put('eg_pref_lib', val, { path : '/' });
270     });
271
272     // will hold a ref to the opac iframe
273     $scope.opac_iframe = null;
274     $scope.parts_iframe = null;
275
276     $scope.search_result_index = 1;
277     $scope.search_result_hit_count = 1;
278
279     $scope.$watch(
280         'opac_iframe.dom.contentWindow.search_result_index',
281         function (n,o) {
282             if (!isNaN(parseInt(n)))
283                 $scope.search_result_index = n + 1;
284         }
285     );
286
287     $scope.$watch(
288         'opac_iframe.dom.contentWindow.search_result_hit_count',
289         function (n,o) {
290             if (!isNaN(parseInt(n)))
291                 $scope.search_result_hit_count = n;
292         }
293     );
294
295     $scope.in_opac_call = false;
296     $scope.opac_call = function (opac_frame_function, force_opac_tab) {
297         if ($scope.opac_iframe) {
298             if (force_opac_tab) $scope.record_tab = 'catalog';
299             $scope.in_opac_call = true;
300             $scope.opac_iframe.dom.contentWindow[opac_frame_function]();
301             if (opac_frame_function == 'rdetailBackToResults') {
302                 $location.update_path('/cat/catalog/index');
303             }
304         }
305     }
306
307     $scope.add_to_record_bucket = function() {
308         var recId = $scope.record_id;
309         return $uibModal.open({
310             templateUrl: './cat/catalog/t_add_to_bucket',
311             animation: true,
312             size: 'md',
313             controller:
314                    ['$scope','$uibModalInstance',
315             function($scope , $uibModalInstance) {
316
317                 $scope.bucket_id = 0;
318                 $scope.newBucketName = '';
319                 $scope.allBuckets = [];
320                 egCore.net.request(
321                     'open-ils.actor',
322                     'open-ils.actor.container.retrieve_by_class.authoritative',
323                     egCore.auth.token(), egCore.auth.user().id(),
324                     'biblio', 'staff_client'
325                 ).then(function(buckets) { $scope.allBuckets = buckets; });
326
327                 $scope.add_to_bucket = function() {
328                     var item = new egCore.idl.cbrebi();
329                     item.bucket($scope.bucket_id);
330                     item.target_biblio_record_entry(recId);
331                     egCore.net.request(
332                         'open-ils.actor',
333                         'open-ils.actor.container.item.create',
334                         egCore.auth.token(), 'biblio', item
335                     ).then(function(resp) {
336                         $uibModalInstance.close();
337                     });
338                 }
339
340                 $scope.add_to_new_bucket = function() {
341                     var bucket = new egCore.idl.cbreb();
342                     bucket.owner(egCore.auth.user().id());
343                     bucket.name($scope.newBucketName);
344                     bucket.description('');
345                     bucket.btype('staff_client');
346
347                     egCore.net.request(
348                         'open-ils.actor',
349                         'open-ils.actor.container.create',
350                         egCore.auth.token(), 'biblio', bucket
351                     ).then(function(bucket) {
352                         $scope.bucket_id = bucket;
353                         $scope.add_to_bucket();
354                     });
355                 }
356
357                 $scope.cancel = function() {
358                     $uibModalInstance.dismiss();
359                 }
360             }]
361         });
362     }
363
364     $scope.current_overlay_target     = egCore.hatch.getLocalItem('eg.cat.marked_overlay_record');
365     $scope.current_voltransfer_target = egCore.hatch.getLocalItem('eg.cat.marked_volume_transfer_record');
366     $scope.current_conjoined_target   = egCore.hatch.getLocalItem('eg.cat.marked_conjoined_record');
367
368     $scope.markConjoined = function () {
369         $scope.current_conjoined_target = $scope.record_id;
370         egCore.hatch.setLocalItem('eg.cat.marked_conjoined_record',$scope.record_id);
371         ngToast.create(egCore.strings.MARK_CONJ_TARGET);
372     };
373
374     $scope.markVolTransfer = function () {
375         ngToast.create(egCore.strings.MARK_VOL_TARGET);
376         $scope.current_voltransfer_target = $scope.record_id;
377         egCore.hatch.setLocalItem('eg.cat.marked_volume_transfer_record',$scope.record_id);
378     };
379
380     $scope.markOverlay = function () {
381         $scope.current_overlay_target = $scope.record_id;
382         egCore.hatch.setLocalItem('eg.cat.marked_overlay_record',$scope.record_id);
383         ngToast.create(egCore.strings.MARK_OVERLAY_TARGET);
384     };
385
386     $scope.clearRecordMarks = function () {
387         $scope.current_overlay_target     = null;
388         $scope.current_voltransfer_target = null;
389         $scope.current_conjoined_target   = null;
390         $scope.current_hold_transfer_dest = null;
391         egCore.hatch.removeLocalItem('eg.cat.marked_volume_transfer_record');
392         egCore.hatch.removeLocalItem('eg.cat.marked_conjoined_record');
393         egCore.hatch.removeLocalItem('eg.cat.marked_overlay_record');
394         egCore.hatch.removeLocalItem('eg.circ.hold.title_transfer_target');
395     }
396
397     $scope.stop_unload = false;
398     $scope.$watch('stop_unload',
399         function(newVal, oldVal) {
400             if (newVal && newVal != oldVal && $scope.opac_iframe) {
401                 $($scope.opac_iframe.dom.contentWindow).on('beforeunload', function(){
402                     return 'There is unsaved data in this record.'
403                 });
404             } else {
405                 if ($scope.opac_iframe)
406                     $($scope.opac_iframe.dom.contentWindow).off('beforeunload');
407             }
408         }
409     );
410
411     // Set the "last bib" cookie, if we have that
412     if ($scope.record_id)
413         egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
414
415     $scope.refresh_record_callback = function (record_id) {
416         egCore.pcrud.retrieve('bre', record_id, {
417             flesh : 1,
418             flesh_fields : {
419                 bre : ['simple_record','creator','editor']
420             }
421         }).then(function(rec) {
422             rec.owner(egCore.org.get(rec.owner()));
423             $scope.summary_pane_record = rec;
424         });
425
426         return record_id;
427     }
428
429     // also set it when the iframe changes to a new record
430     $scope.handle_page = function(url) {
431
432         if (!url || url == 'about:blank') {
433             // nothing loaded.  If we already have a record ID, leave it.
434             return;
435         }
436
437         var match = url.match(/\/+opac\/+record\/+(\d+)/);
438         if (match) {
439             $scope.record_id = match[1];
440             egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
441             $scope.holdings_record_id_changed($scope.record_id);
442             conjoinedSvc.fetch($scope.record_id).then(function(){
443                 $scope.conjoinedGridDataProvider.refresh();
444             });
445             egHolds.fetch_holds(hold_ids).then($scope.hold_grid_data_provider.refresh);
446             init_parts_url();
447             $location.update_path('/cat/catalog/record/' + $scope.record_id);
448         } else {
449             delete $scope.record_id;
450             $scope.from_route = false;
451         }
452
453         // child scope is executing this function, so our digest doesn't fire ... thus,
454         $scope.$apply();
455
456         if (!$scope.in_opac_call) {
457             if ($scope.record_id) {
458                 $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
459                 tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
460             } else {
461                 tab = $routeParams.record_tab || 'catalog';
462             }
463             $scope.set_record_tab(tab);
464         } else {
465             $scope.in_opac_call = false;
466         }
467     }
468
469     // xulG catalog handlers
470     $scope.handlers = { }
471
472     // ------------------------------------------------------------------
473     // Conjoined items
474
475     $scope.conjoinedGridControls = {};
476     $scope.conjoinedGridDataProvider = egGridDataProvider.instance({
477         get : function(offset, count) {
478             return this.arrayNotifier(conjoinedSvc.items, offset, count);
479         }
480     });
481
482     $scope.changeConjoinedType = function () {
483         var peers = egCore.idl.Clone($scope.conjoinedGridControls.selectedItems());
484         angular.forEach(peers, function (p) {
485             p.target_copy(p.target_copy().id());
486             p.peer_type(p.peer_type().id());
487         });
488
489         var conjoinedGridDataProviderRef = $scope.conjoinedGridDataProvider;
490
491         return $uibModal.open({
492             templateUrl: './cat/catalog/t_conjoined_selector',
493             animation: true,
494             controller:
495                    ['$scope','$uibModalInstance',
496             function($scope , $uibModalInstance) {
497                 $scope.update = true;
498
499                 $scope.peer_type = null;
500                 $scope.peer_type_list = [];
501                 conjoinedSvc.get_peer_types().then(function(list){
502                     $scope.peer_type_list = list;
503                 });
504     
505                 $scope.ok = function(type) {
506                     var promises = [];
507     
508                     angular.forEach(peers, function (p) {
509                         p.ischanged(1);
510                         p.peer_type(type);
511                         promises.push(egCore.pcrud.update(p));
512                     });
513     
514                     return $q.all(promises)
515                         .then(function(){$uibModalInstance.close()})
516                         .then(function(){return conjoinedSvc.fetch()})
517                         .then(function(){conjoinedGridDataProviderRef.refresh()});
518                 }
519     
520                 $scope.cancel = function($event) {
521                     $uibModalInstance.dismiss();
522                     $event.preventDefault();
523                 }
524             }]
525         });
526         
527     }
528
529     $scope.refreshConjoined = function () {
530         conjoinedSvc.fetch($scope.record_id)
531         .then(function(){$scope.conjoinedGridDataProvider.refresh();});
532     }
533
534     $scope.deleteSelectedConjoined = function () {
535         var peers = $scope.conjoinedGridControls.selectedItems();
536
537         if (peers.length > 0) {
538             egConfirmDialog.open(
539                 egCore.strings.CONFIRM_DELETE_PEERS,
540                 egCore.strings.CONFIRM_DELETE_PEERS_MESSAGE,
541                 {peers : peers.length}
542             ).result.then(function() {
543                 angular.forEach(peers, function (p) {
544                     p.isdeleted(1);
545                 });
546
547                 egCore.pcrud.remove(peers).then(function() {
548                     return conjoinedSvc.fetch();
549                 }).then(function() {
550                     $scope.conjoinedGridDataProvider.refresh();
551                 });
552             });
553         }
554     }
555     if ($scope.record_id)
556         conjoinedSvc.fetch($scope.record_id);
557
558     // ------------------------------------------------------------------
559     // Holdings
560
561     $scope.holdingsGridControls = {
562         activateItem : function (item) {
563             $scope.selectedHoldingsVolCopyEdit();
564         }
565     };
566     $scope.holdingsGridDataProvider = egGridDataProvider.instance({
567         get : function(offset, count) {
568             return this.arrayNotifier(holdingsSvcInst.copies, offset, count);
569         }
570     });
571
572     $scope.add_copies_to_bucket = function() {
573         var copy_list = gatherSelectedHoldingsIds();
574         if (copy_list.length == 0) return;
575
576         return $uibModal.open({
577             templateUrl: './cat/catalog/t_add_to_bucket',
578             animation: true,
579             size: 'md',
580             controller:
581                    ['$scope','$uibModalInstance',
582             function($scope , $uibModalInstance) {
583
584                 $scope.bucket_id = 0;
585                 $scope.newBucketName = '';
586                 $scope.allBuckets = [];
587
588                 egCore.net.request(
589                     'open-ils.actor',
590                     'open-ils.actor.container.retrieve_by_class.authoritative',
591                     egCore.auth.token(), egCore.auth.user().id(),
592                     'copy', 'staff_client'
593                 ).then(function(buckets) { $scope.allBuckets = buckets; });
594
595                 $scope.add_to_bucket = function() {
596                     var promises = [];
597                     angular.forEach(copy_list, function (cp) {
598                         var item = new egCore.idl.ccbi()
599                         item.bucket($scope.bucket_id);
600                         item.target_copy(cp);
601                         promises.push(
602                             egCore.net.request(
603                                 'open-ils.actor',
604                                 'open-ils.actor.container.item.create',
605                                 egCore.auth.token(), 'copy', item
606                             )
607                         );
608
609                         return $q.all(promises).then(function() {
610                             $uibModalInstance.close();
611                         });
612                     });
613                 }
614
615                 $scope.add_to_new_bucket = function() {
616                     var bucket = new egCore.idl.ccb();
617                     bucket.owner(egCore.auth.user().id());
618                     bucket.name($scope.newBucketName);
619                     bucket.description('');
620                     bucket.btype('staff_client');
621
622                     return egCore.net.request(
623                         'open-ils.actor',
624                         'open-ils.actor.container.create',
625                         egCore.auth.token(), 'copy', bucket
626                     ).then(function(bucket) {
627                         $scope.bucket_id = bucket;
628                         $scope.add_to_bucket();
629                     });
630                 }
631
632                 $scope.cancel = function() {
633                     $uibModalInstance.dismiss();
634                 }
635             }]
636         });
637     }
638
639     // TODO: refactor common code between cat/catalog/app.js and cat/item/app.js 
640
641     $scope.need_one_selected = function() {
642         var items = $scope.holdingsGridControls.selectedItems();
643         if (items.length == 1) return false;
644         return true;
645     };
646
647     $scope.make_copies_bookable = function() {
648
649         var copies_by_record = {};
650         var record_list = [];
651         angular.forEach(
652             $scope.holdingsGridControls.selectedItems(),
653             function (item) {
654                 var record_id = item['call_number.record.id'];
655                 if (typeof copies_by_record[ record_id ] == 'undefined') {
656                     copies_by_record[ record_id ] = [];
657                     record_list.push( record_id );
658                 }
659                 copies_by_record[ record_id ].push(item.id);
660             }
661         );
662
663         var promises = [];
664         var combined_results = [];
665         angular.forEach(record_list, function(record_id) {
666             promises.push(
667                 egCore.net.request(
668                     'open-ils.booking',
669                     'open-ils.booking.resources.create_from_copies',
670                     egCore.auth.token(),
671                     copies_by_record[record_id]
672                 ).then(function(results) {
673                     if (results && results['brsrc']) {
674                         combined_results = combined_results.concat(results['brsrc']);
675                     }
676                 })
677             );
678         });
679
680         $q.all(promises).then(function() {
681             if (combined_results.length > 0) {
682                 $uibModal.open({
683                     template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
684                     animation: true,
685                     size: 'md',
686                     controller:
687                            ['$scope','$location','egCore','$uibModalInstance',
688                     function($scope , $location , egCore , $uibModalInstance) {
689
690                         $scope.funcs = {
691                             ses : egCore.auth.token(),
692                             resultant_brsrc : combined_results.map(function(o) { return o[0]; })
693                         }
694
695                         var booking_path = '/eg/conify/global/booking/resource';
696
697                         $scope.booking_admin_url =
698                             $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
699                     }]
700                 });
701             }
702         });
703     }
704
705     $scope.book_copies_now = function() {
706         var copies_by_record = {};
707         var record_list = [];
708         angular.forEach(
709             $scope.holdingsGridControls.selectedItems(),
710             function (item) {
711                 var record_id = item['call_number.record.id'];
712                 if (typeof copies_by_record[ record_id ] == 'undefined') {
713                     copies_by_record[ record_id ] = [];
714                     record_list.push( record_id );
715                 }
716                 copies_by_record[ record_id ].push(item.id);
717             }
718         );
719
720         var promises = [];
721         var combined_brt = [];
722         var combined_brsrc = [];
723         angular.forEach(record_list, function(record_id) {
724             promises.push(
725                 egCore.net.request(
726                     'open-ils.booking',
727                     'open-ils.booking.resources.create_from_copies',
728                     egCore.auth.token(),
729                     copies_by_record[record_id]
730                 ).then(function(results) {
731                     if (results && results['brt']) {
732                         combined_brt = combined_brt.concat(results['brt']);
733                     }
734                     if (results && results['brsrc']) {
735                         combined_brsrc = combined_brsrc.concat(results['brsrc']);
736                     }
737                 })
738             );
739         });
740
741         $q.all(promises).then(function() {
742             if (combined_brt.length > 0 || combined_brsrc.length > 0) {
743                 $uibModal.open({
744                     template: '<eg-embed-frame url="booking_admin_url" handlers="funcs"></eg-embed-frame>',
745                     animation: true,
746                     size: 'md',
747                     controller:
748                            ['$scope','$location','egCore','$uibModalInstance',
749                     function($scope , $location , egCore , $uibModalInstance) {
750
751                         $scope.funcs = {
752                             ses : egCore.auth.token(),
753                             bresv_interface_opts : {
754                                 booking_results : {
755                                      brt : combined_brt
756                                     ,brsrc : combined_brsrc
757                                 }
758                             }
759                         }
760
761                         var booking_path = '/eg/booking/reservation';
762
763                         $scope.booking_admin_url =
764                             $location.absUrl().replace(/\/eg\/staff.*/, booking_path);
765
766                     }]
767                 });
768             }
769         });
770     }
771
772
773     $scope.requestItems = function() {
774         var copy_list = gatherSelectedHoldingsIds();
775         if (copy_list.length == 0) return;
776
777         return $uibModal.open({
778             templateUrl: './cat/catalog/t_request_items',
779             animation: true,
780             controller:
781                    ['$scope','$uibModalInstance',
782             function($scope , $uibModalInstance) {
783                 $scope.user = null;
784                 $scope.first_user_fetch = true;
785
786                 $scope.hold_data = {
787                     hold_type : 'C',
788                     copy_list : copy_list,
789                     pickup_lib: egCore.org.get(egCore.auth.user().ws_ou()),
790                     user      : egCore.auth.user().id()
791                 };
792
793                 egUser.get( $scope.hold_data.user ).then(function(u) {
794                     $scope.user = u;
795                     $scope.barcode = u.card().barcode();
796                     $scope.user_name = egUser.format_name(u);
797                     $scope.hold_data.user = u.id();
798                 });
799
800                 $scope.user_name = '';
801                 $scope.barcode = '';
802                 $scope.$watch('barcode', function (n) {
803                     if (!$scope.first_user_fetch) {
804                         egUser.getByBarcode(n).then(function(u) {
805                             $scope.user = u;
806                             $scope.user_name = egUser.format_name(u);
807                             $scope.hold_data.user = u.id();
808                         }, function() {
809                             $scope.user = null;
810                             $scope.user_name = '';
811                             delete $scope.hold_data.user;
812                         });
813                     }
814                     $scope.first_user_fetch = false;
815                 });
816
817                 $scope.ok = function(h) {
818                     var args = {
819                         patronid  : h.user,
820                         hold_type : h.hold_type,
821                         pickup_lib: h.pickup_lib.id(),
822                         depth     : 0
823                     };
824
825                     egCore.net.request(
826                         'open-ils.circ',
827                         'open-ils.circ.holds.test_and_create.batch.override',
828                         egCore.auth.token(), args, h.copy_list
829                     );
830
831                     $uibModalInstance.close();
832                 }
833
834                 $scope.cancel = function($event) {
835                     $uibModalInstance.dismiss();
836                     $event.preventDefault();
837                 }
838             }]
839         });
840     }
841
842     $scope.view_place_orders = function() {
843         if (!$scope.record_id) return;
844         var url = egCore.env.basePath + 'acq/legacy/lineitem/related/' + $scope.record_id + '?target=bib';
845         $timeout(function() { $window.open(url, '_blank') });
846     }
847
848     $scope.replaceBarcodes = function() {
849         var copy_list = gatherSelectedRawCopies();
850         if (copy_list.length == 0) return;
851
852         var holdingsGridDataProviderRef = $scope.holdingsGridDataProvider;
853
854         angular.forEach(copy_list, function (cp) {
855             $uibModal.open({
856                 templateUrl: './cat/share/t_replace_barcode',
857                 animation: true,
858                 controller:
859                            ['$scope','$uibModalInstance',
860                     function($scope , $uibModalInstance) {
861                         $scope.isModal = true;
862                         $scope.focusBarcode = false;
863                         $scope.focusBarcode2 = true;
864                         $scope.barcode1 = cp.barcode();
865
866                         $scope.updateBarcode = function() {
867                             $scope.copyNotFound = false;
868                             $scope.updateOK = false;
869                 
870                             egCore.pcrud.search('acp',
871                                 {deleted : 'f', barcode : $scope.barcode1})
872                             .then(function(copy) {
873                 
874                                 if (!copy) {
875                                     $scope.focusBarcode = true;
876                                     $scope.copyNotFound = true;
877                                     return;
878                                 }
879                 
880                                 $scope.copyId = copy.id();
881                                 copy.barcode($scope.barcode2);
882                 
883                                 egCore.pcrud.update(copy).then(function(stat) {
884                                     $scope.updateOK = stat;
885                                     $scope.focusBarcode = true;
886                                     holdingsSvc.fetchAgain().then(function (){
887                                         holdingsGridDataProviderRef.refresh();
888                                     });
889                                 });
890
891                             });
892                             $uibModalInstance.close();
893                         }
894
895                         $scope.cancel = function($event) {
896                             $uibModalInstance.dismiss();
897                             $event.preventDefault();
898                         }
899                     }
900                 ]
901             });
902         });
903     }
904
905     // refresh the list of holdings when the record_id is changed.
906     $scope.holdings_record_id_changed = function(id) {
907         if ($scope.record_id != id) $scope.record_id = id;
908         console.log('record id changed to ' + id + ', loading new holdings');
909         holdingsSvcInst.fetch({
910             rid : $scope.record_id,
911             org : $scope.holdings_ou,
912             copy: $scope.holdings_show_copies,
913             vol : $scope.holdings_show_vols,
914             empty: $scope.holdings_show_empty
915         }).then(function() {
916             $scope.holdingsGridDataProvider.refresh();
917         });
918     }
919
920     // refresh the list of holdings when the filter lib is changed.
921     $scope.holdings_ou = egCore.org.get(egCore.auth.user().ws_ou());
922     $scope.holdings_ou_changed = function(org) {
923         $scope.holdings_ou = org;
924         holdingsSvcInst.fetch({
925             rid : $scope.record_id,
926             org : $scope.holdings_ou,
927             copy: $scope.holdings_show_copies,
928             vol : $scope.holdings_show_vols,
929             empty: $scope.holdings_show_empty
930         }).then(function() {
931             $scope.holdingsGridDataProvider.refresh();
932         });
933     }
934
935     $scope.holdings_cb_changed = function(cb,newVal,norefresh) {
936         $scope[cb] = newVal;
937         egCore.hatch.setItem('cat.' + cb, newVal);
938         if (!norefresh) holdingsSvcInst.fetch({
939             rid : $scope.record_id,
940             org : $scope.holdings_ou,
941             copy: $scope.holdings_show_copies,
942             vol : $scope.holdings_show_vols,
943             empty: $scope.holdings_show_empty
944         }).then(function() {
945             $scope.holdingsGridDataProvider.refresh();
946         });
947     }
948
949     egCore.hatch.getItem('cat.holdings_show_vols').then(function(x){
950         if (typeof x ==  'undefined') x = true;
951         $scope.holdings_cb_changed('holdings_show_vols',x,true);
952         $('#holdings_show_vols').prop('checked', x);
953     }).then(function(){
954         egCore.hatch.getItem('cat.holdings_show_copies').then(function(x){
955             if (typeof x ==  'undefined') x = true;
956             $scope.holdings_cb_changed('holdings_show_copies',x,true);
957             $('#holdings_show_copies').prop('checked', x);
958         }).then(function(){
959             egCore.hatch.getItem('cat.holdings_show_empty').then(function(x){
960                 if (typeof x ==  'undefined') x = true;
961                 $scope.holdings_cb_changed('holdings_show_empty',x);
962                 $('#holdings_show_empty').prop('checked', x);
963             })
964         })
965     });
966
967     $scope.vols_not_shown = function () {
968         return !$scope.holdings_show_vols;
969     }
970
971     $scope.copies_not_shown = function () {
972         return !$scope.holdings_show_copies;
973     }
974
975     $scope.holdings_checkbox_handler = function (item) {
976         $scope.holdings_cb_changed(item.checkbox,item.checked);
977     }
978
979     function gatherSelectedHoldingsIds () {
980         var cp_id_list = [];
981         angular.forEach(
982             $scope.holdingsGridControls.selectedItems(),
983             function (item) { cp_id_list = cp_id_list.concat(item.id_list) }
984         );
985         return cp_id_list;
986     }
987
988     function gatherSelectedRawCopies () {
989         var cp_list = [];
990         angular.forEach(
991             $scope.holdingsGridControls.selectedItems(),
992             function (item) { if (item.raw) cp_list = cp_list.concat(item.raw) }
993         );
994         return cp_list;
995     }
996
997     function gatherSelectedEmptyVolumeIds () {
998         var cn_id_list = [];
999         angular.forEach(
1000             $scope.holdingsGridControls.selectedItems(),
1001             function (item) {
1002                 if (item.copy_count == 0)
1003                     cn_id_list.push(item.call_number.id)
1004             }
1005         );
1006         return cn_id_list;
1007     }
1008
1009     function gatherSelectedVolumeIds () {
1010         var cn_id_list = [];
1011         angular.forEach(
1012             $scope.holdingsGridControls.selectedItems(),
1013             function (item) {
1014                 if (cn_id_list.indexOf(item.call_number.id) == -1)
1015                     cn_id_list.push(item.call_number.id)
1016             }
1017         );
1018         return cn_id_list;
1019     }
1020
1021     $scope.selectedHoldingsDelete = function (vols, copies) {
1022
1023         var cnHash = {};
1024         var perCnCopies = {};
1025
1026         var cn_count = 0;
1027         var cp_count = 0;
1028
1029         angular.forEach(
1030             $scope.holdingsGridControls.selectedItems(),
1031             function (item) {
1032                 if (vols && item.raw_call_number) {
1033                     cnHash[item.call_number.id] = egCore.idl.Clone(item.raw_call_number);
1034                     cnHash[item.call_number.id].isdeleted(1);
1035                     cn_count++;
1036                 } else if (copies) {
1037                     angular.forEach(egCore.idl.Clone(item.raw), function (cp) {
1038                         cp.isdeleted(1);
1039                         cp_count++;
1040                         var cn_id = cp.call_number().id();
1041                         if (!cnHash[cn_id]) {
1042                             cnHash[cn_id] = cp.call_number();
1043                             perCnCopies[cn_id] = [cp];
1044                         } else {
1045                             perCnCopies[cn_id].push(cp);
1046                         }
1047                         cp.call_number(cn_id); // prevent loops in JSON-ification
1048                     });
1049
1050                 }
1051             }
1052         );
1053
1054         angular.forEach(perCnCopies, function (v, k) {
1055             if (vols) {
1056                 cnHash[k].isdeleted(1);
1057                 cn_count++;
1058             }
1059             cnHash[k].copies(v);
1060         });
1061
1062         cnList = [];
1063         angular.forEach(cnHash, function (v, k) {
1064             cnList.push(v);
1065         });
1066
1067         if (cnList.length == 0) return;
1068
1069         var flags = {};
1070         if (vols && copies) flags.force_delete_copies = 1;
1071
1072         egConfirmDialog.open(
1073             egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES,
1074             egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES_MESSAGE,
1075             {copies : cp_count, volumes : cn_count}
1076         ).result.then(function() {
1077             egCore.net.request(
1078                 'open-ils.cat',
1079                 'open-ils.cat.asset.volume.fleshed.batch.update.override',
1080                 egCore.auth.token(), cnList, 1, flags
1081             ).then(function(update_count) {
1082                 holdingsSvcInst.fetchAgain().then(function() {
1083                     $scope.holdingsGridDataProvider.refresh();
1084                 });
1085             });
1086         });
1087     }
1088     $scope.selectedHoldingsCopyDelete = function () { $scope.selectedHoldingsDelete(false,true) }
1089     $scope.selectedHoldingsVolCopyDelete = function () { $scope.selectedHoldingsDelete(true,true) }
1090     $scope.selectedHoldingsEmptyVolCopyDelete = function () { $scope.selectedHoldingsDelete(true,false) }
1091
1092     spawnHoldingsAdd = function (vols,copies){
1093         var raw = [];
1094         if (copies) { // just a copy on existing volumes
1095             angular.forEach(gatherSelectedVolumeIds(), function (v) {
1096                 raw.push( {callnumber : v} );
1097             });
1098         } else if (vols) {
1099             if (typeof $scope.holdingsGridControls.selectedItems == "function" &&
1100                 $scope.holdingsGridControls.selectedItems().length > 0) {
1101                 angular.forEach($scope.holdingsGridControls.selectedItems(),
1102                     function (item) {
1103                         raw.push({
1104                             owner : item.owner_id,
1105                             label : item.call_number.label
1106                         });
1107                     });
1108             } else {
1109                 raw.push({
1110                     owner : egCore.auth.user().ws_ou()
1111                 });
1112             }
1113         }
1114
1115         if (raw.length == 0) raw.push({});
1116
1117         egCore.net.request(
1118             'open-ils.actor',
1119             'open-ils.actor.anon_cache.set_value',
1120             null, 'edit-these-copies', {
1121                 record_id: $scope.record_id,
1122                 raw: raw,
1123                 hide_vols : false,
1124                 hide_copies : false
1125             }
1126         ).then(function(key) {
1127             if (key) {
1128                 var url = egCore.env.basePath + 'cat/volcopy/' + key;
1129                 $timeout(function() { $window.open(url, '_blank') });
1130             } else {
1131                 alert('Could not create anonymous cache key!');
1132             }
1133         });
1134     }
1135     $scope.selectedHoldingsVolCopyAdd = function () { spawnHoldingsAdd(true,false) }
1136     $scope.selectedHoldingsCopyAdd = function () { spawnHoldingsAdd(false,true) }
1137
1138     spawnHoldingsEdit = function (hide_vols,hide_copies){
1139         egCore.net.request(
1140             'open-ils.actor',
1141             'open-ils.actor.anon_cache.set_value',
1142             null, 'edit-these-copies', {
1143                 record_id: $scope.record_id,
1144                 copies: gatherSelectedHoldingsIds(),
1145                 raw: gatherSelectedEmptyVolumeIds().map(
1146                     function(v){ return { callnumber : v } }
1147                 ),
1148                 hide_vols : hide_vols,
1149                 hide_copies : hide_copies
1150             }
1151         ).then(function(key) {
1152             if (key) {
1153                 var url = egCore.env.basePath + 'cat/volcopy/' + key;
1154                 $timeout(function() { $window.open(url, '_blank') });
1155             } else {
1156                 alert('Could not create anonymous cache key!');
1157             }
1158         });
1159     }
1160     $scope.selectedHoldingsVolCopyEdit = function () { spawnHoldingsEdit(false,false) }
1161     $scope.selectedHoldingsVolEdit = function () { spawnHoldingsEdit(false,true) }
1162     $scope.selectedHoldingsCopyEdit = function () { spawnHoldingsEdit(true,false) }
1163
1164     $scope.selectedHoldingsItemStatus = function (){
1165         var url = egCore.env.basePath + 'cat/item/search/' + gatherSelectedHoldingsIds().join(',')
1166         $timeout(function() { $window.open(url, '_blank') });
1167     }
1168
1169     $scope.markVolAsItemTarget = function() {
1170         if ($scope.holdingsGridControls.selectedItems()[0].call_number.id) { // cn.id missing when vols are collapsed
1171             egCore.hatch.setLocalItem(
1172                 'eg.cat.item_transfer_target',
1173                 $scope.holdingsGridControls.selectedItems()[0].call_number.id
1174             );
1175             ngToast.create(egCore.strings.MARK_ITEM_TARGET);
1176         }
1177     }
1178
1179     $scope.markLibAsVolTarget = function() {
1180         return $uibModal.open({
1181             templateUrl: './cat/catalog/t_choose_vol_target_lib',
1182             animation: true,
1183             controller:
1184                    ['$scope','$uibModalInstance',
1185             function($scope , $uibModalInstance) {
1186
1187                 var orgId = egCore.hatch.getLocalItem('eg.cat.volume_transfer_target') || 1;
1188                 $scope.org = egCore.org.get(orgId);
1189                 $scope.cant_have_vols = function (id) { return !egCore.org.CanHaveVolumes(id); };
1190                 $scope.ok = function(org) {
1191                     egCore.hatch.setLocalItem(
1192                         'eg.cat.volume_transfer_target',
1193                         org.id()
1194                     );
1195                     $uibModalInstance.close();
1196                 }
1197                 $scope.cancel = function($event) {
1198                     $uibModalInstance.dismiss();
1199                     $event.preventDefault();
1200                 }
1201             }]
1202         });
1203     }
1204     $scope.markLibFromSelectedAsVolTarget = function() {
1205         egCore.hatch.setLocalItem(
1206             'eg.cat.volume_transfer_target',
1207             $scope.holdingsGridControls.selectedItems()[0].owner_id
1208         );
1209         ngToast.create(egCore.strings.MARK_VOL_TARGET);
1210     }
1211
1212     $scope.selectedHoldingsItemStatusDetail = function (){
1213         angular.forEach(
1214             gatherSelectedHoldingsIds(),
1215             function (cid) {
1216                 var url = egCore.env.basePath +
1217                           'cat/item/' + cid;
1218                 $timeout(function() { $window.open(url, '_blank') });
1219             }
1220         );
1221     }
1222
1223     $scope.transferVolumesToRecord = function (){
1224         var target_record = egCore.hatch.getLocalItem('eg.cat.marked_volume_transfer_record');
1225         if (!target_record) return;
1226         if ($scope.record_id == target_record) return;
1227         var items = $scope.holdingsGridControls.selectedItems();
1228         if (!items.length) return;
1229
1230         var vols_to_move   = {};
1231         angular.forEach(items, function(item) {
1232             if (!(item.call_number.owning_lib in vols_to_move)) {
1233                 vols_to_move[item.call_number.owning_lib] = new Array;
1234             }
1235             vols_to_move[item.call_number.owning_lib].push(item.call_number.id);
1236         });
1237
1238         var promises = [];        
1239         angular.forEach(vols_to_move, function(vols, owning_lib) {
1240             promises.push(egCore.net.request(
1241                 'open-ils.cat',
1242                 'open-ils.cat.asset.volume.batch.transfer.override',
1243                 egCore.auth.token(), {
1244                     docid   : target_record,
1245                     lib     : owning_lib,
1246                     volumes : vols
1247                 }
1248             ));
1249         });
1250         $q.all(promises).then(function(success) {
1251             if (success) {
1252                 ngToast.create(egCore.strings.VOLS_TRANSFERED);
1253                 holdingsSvcInst.fetchAgain().then(function() {
1254                     $scope.holdingsGridDataProvider.refresh();
1255                 });
1256             } else {
1257                 alert('Could not transfer volumes!');
1258             }
1259         });
1260     }
1261
1262     function transferVolumes(new_record){
1263         var xfer_target = egCore.hatch.getLocalItem('eg.cat.volume_transfer_target');
1264
1265         if (xfer_target) {
1266             egCore.net.request(
1267                 'open-ils.cat',
1268                 'open-ils.cat.asset.volume.batch.transfer.override',
1269                 egCore.auth.token(), {
1270                     docid   : (new_record ? new_record : $scope.record_id),
1271                     lib     : xfer_target,
1272                     volumes : gatherSelectedVolumeIds()
1273                 }
1274             ).then(function(success) {
1275                 if (success) {
1276                     ngToast.create(egCore.strings.VOLS_TRANSFERED);
1277                     holdingsSvcInst.fetchAgain().then(function() {
1278                         $scope.holdingsGridDataProvider.refresh();
1279                     });
1280                 } else {
1281                     alert('Could not transfer volumes!');
1282                 }
1283             });
1284         }
1285         
1286     }
1287
1288     $scope.transferVolumesToLibrary = function() {
1289         transferVolumes();
1290     }
1291
1292     $scope.transferVolumesToRecordAndLibrary = function() {
1293         var target_record = egCore.hatch.getLocalItem('eg.cat.marked_volume_transfer_record');
1294         if (!target_record) return;
1295         transferVolumes(target_record);
1296     }
1297
1298     // this "transfers" selected copies to a new owning library,
1299     // auto-creating volumes and deleting unused volumes as required.
1300     $scope.changeItemOwningLib = function() {
1301         var xfer_target = egCore.hatch.getLocalItem('eg.cat.volume_transfer_target');
1302         var items = $scope.holdingsGridControls.selectedItems();
1303         if (!xfer_target || !items.length) {
1304             return;
1305         }
1306         var vols_to_move   = {};
1307         var copies_to_move = {};
1308         angular.forEach(items, function(item) {
1309             if (item.call_number.owning_lib != xfer_target) {
1310                 if (item.call_number.id in vols_to_move) {
1311                     copies_to_move[item.call_number.id].push(item.id);
1312                 } else {
1313                     vols_to_move[item.call_number.id] = item.call_number;
1314                     copies_to_move[item.call_number.id] = new Array;
1315                     copies_to_move[item.call_number.id].push(item.id);
1316                 }
1317             }
1318         });
1319     
1320         var promises = [];
1321         angular.forEach(vols_to_move, function(vol) {
1322             promises.push(egCore.net.request(
1323                 'open-ils.cat',
1324                 'open-ils.cat.call_number.find_or_create',
1325                 egCore.auth.token(),
1326                 vol.label,
1327                 vol.record,
1328                 xfer_target,
1329                 vol.prefix.id,
1330                 vol.suffix.id,
1331                 vol.label_class
1332             ).then(function(resp) {
1333                 var evt = egCore.evt.parse(resp);
1334                 if (evt) return;
1335                 return egCore.net.request(
1336                     'open-ils.cat',
1337                     'open-ils.cat.transfer_copies_to_volume',
1338                     egCore.auth.token(),
1339                     resp.acn_id,
1340                     copies_to_move[vol.id]
1341                 );
1342             }));
1343         });
1344         $q.all(promises).then(function() {
1345             ngToast.create(egCore.strings.ITEMS_TRANSFERED);
1346             holdingsSvcInst.fetchAgain().then(function() {
1347                 $scope.holdingsGridDataProvider.refresh();
1348             });
1349         });
1350     }
1351
1352     $scope.transferItems = function (){
1353         var xfer_target = egCore.hatch.getLocalItem('eg.cat.item_transfer_target');
1354         var copy_ids = gatherSelectedHoldingsIds();
1355         if (xfer_target && copy_ids.length > 0) {
1356             egCore.net.request(
1357                 'open-ils.cat',
1358                 'open-ils.cat.transfer_copies_to_volume',
1359                 egCore.auth.token(),
1360                 xfer_target,
1361                 copy_ids
1362             ).then(
1363                 function(resp) { // oncomplete
1364                     var evt = egCore.evt.parse(resp);
1365                     if (evt) {
1366                         egConfirmDialog.open(
1367                             egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_TITLE,
1368                             egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_BODY,
1369                             {'evt_desc': evt.desc}
1370                         ).result.then(function() {
1371                             egCore.net.request(
1372                                 'open-ils.cat',
1373                                 'open-ils.cat.transfer_copies_to_volume.override',
1374                                 egCore.auth.token(),
1375                                 xfer_target,
1376                                 copy_ids,
1377                                 { events: ['TITLE_LAST_COPY', 'COPY_DELETE_WARNING'] }
1378                             ).then(function(resp) {
1379                                 holdingsSvcInst.fetchAgain().then(function() {
1380                                     $scope.holdingsGridDataProvider.refresh();
1381                                 });
1382                             });
1383                         });
1384                     } else {
1385                         ngToast.create(egCore.strings.ITEMS_TRANSFERED);
1386                         holdingsSvcInst.fetchAgain().then(function() {
1387                             $scope.holdingsGridDataProvider.refresh();
1388                         });
1389                     }
1390                 },
1391                 null, // onerror
1392                 null // onprogress
1393             )
1394         }
1395     }
1396
1397     $scope.selectedHoldingsItemStatusTgrEvt = function (){
1398         angular.forEach(
1399             gatherSelectedHoldingsIds(),
1400             function (cid) {
1401                 var url = egCore.env.basePath +
1402                           'cat/item/' + cid + '/triggered_events';
1403                 $timeout(function() { $window.open(url, '_blank') });
1404             }
1405         );
1406     }
1407
1408     $scope.selectedHoldingsItemStatusHolds = function (){
1409         angular.forEach(
1410             gatherSelectedHoldingsIds(),
1411             function (cid) {
1412                 var url = egCore.env.basePath +
1413                           'cat/item/' + cid + '/holds';
1414                 $timeout(function() { $window.open(url, '_blank') });
1415             }
1416         );
1417     }
1418
1419     $scope.selectedHoldingsDamaged = function () {
1420         egCirc.mark_damaged(gatherSelectedHoldingsIds()).then(function() {
1421             holdingsSvcInst.fetchAgain().then(function() {
1422                 $scope.holdingsGridDataProvider.refresh();
1423             });
1424         });
1425     }
1426
1427     $scope.selectedHoldingsMissing = function () {
1428         egCirc.mark_missing(gatherSelectedHoldingsIds()).then(function() {
1429             holdingsSvcInst.fetchAgain().then(function() {
1430                 $scope.holdingsGridDataProvider.refresh();
1431             });
1432         });
1433     }
1434
1435     $scope.attach_to_peer_bib = function() {
1436         var copy_list = gatherSelectedHoldingsIds();
1437         if (copy_list.length == 0) return;
1438
1439         egCore.hatch.getItem('eg.cat.marked_conjoined_record').then(function(target_record) {
1440             if (!target_record) return;
1441
1442             return $uibModal.open({
1443                 templateUrl: './cat/catalog/t_conjoined_selector',
1444                 animation: true,
1445                 controller:
1446                        ['$scope','$uibModalInstance',
1447                 function($scope , $uibModalInstance) {
1448                     $scope.update = false;
1449
1450                     $scope.peer_type = null;
1451                     $scope.peer_type_list = [];
1452                     conjoinedSvc.get_peer_types().then(function(list){
1453                         $scope.peer_type_list = list;
1454                     });
1455     
1456                     $scope.ok = function(type) {
1457                         var promises = [];
1458     
1459                         angular.forEach(copy_list, function (cp) {
1460                             var n = new egCore.idl.bpbcm();
1461                             n.isnew(true);
1462                             n.peer_record(target_record);
1463                             n.target_copy(cp);
1464                             n.peer_type(type);
1465                             promises.push(egCore.pcrud.create(n));
1466                         });
1467     
1468                         return $q.all(promises).then(function(){$uibModalInstance.close()});
1469                     }
1470     
1471                     $scope.cancel = function($event) {
1472                         $uibModalInstance.dismiss();
1473                         $event.preventDefault();
1474                     }
1475                 }]
1476             });
1477         });
1478     }
1479
1480
1481     // ------------------------------------------------------------------
1482     // Holds 
1483     var provider = egGridDataProvider.instance({});
1484     $scope.hold_grid_data_provider = provider;
1485     $scope.grid_actions = egHoldGridActions;
1486     $scope.grid_actions.refresh = function () { provider.refresh() };
1487     $scope.hold_grid_controls = {};
1488
1489     var hold_ids = []; // current list of holds
1490     function fetchHolds(offset, count) {
1491         var ids = hold_ids.slice(offset, offset + count);
1492
1493         return egHolds.fetch_holds(ids).then(null, null,
1494             function(hold_data) { 
1495                 return hold_data;
1496             }
1497         );
1498     }
1499
1500     provider.get = function(offset, count) {
1501         if ($scope.record_tab != 'holds') return $q.when();
1502         var deferred = $q.defer();
1503         hold_ids = []; // no caching ATM
1504
1505         // open a determinate progress dialog, max value set below.
1506         egProgressDialog.open({max : 1, value : 0});
1507
1508         // fetch the IDs
1509         egCore.net.request(
1510             'open-ils.circ',
1511             'open-ils.circ.holds.retrieve_all_from_title',
1512             egCore.auth.token(), $scope.record_id, 
1513             {pickup_lib : egCore.org.descendants($scope.pickup_ou.id(), true)}
1514         ).then(
1515             function(hold_data) {
1516                 hold_ids = []; // clear the list of ids, hack to avoid dups
1517                 // TODO: fix the underlying problem, which is that
1518                 // this gets called twice when switching to the holds
1519                 // tab; once explicitly, and once via the change handler
1520                 // on the OU selector
1521                 angular.forEach(hold_data, function(list, type) {
1522                     hold_ids = hold_ids.concat(list);
1523                 });
1524
1525                 // Set the max value of the progress bar to the lesser of
1526                 // the total number of holds to fetch or the page size
1527                 // of the grid.
1528                 egProgressDialog.update(
1529                     {max : Math.min(hold_ids.length, count)});
1530
1531                 var holds_fetched = 0;
1532                 fetchHolds(offset, count)
1533                 .then(deferred.resolve, null, 
1534                     function(hold_data) {
1535                         holds_fetched++;
1536                         deferred.notify(hold_data);
1537                         egProgressDialog.increment();
1538                     }
1539                 )['finally'](egProgressDialog.close);
1540             }
1541         );
1542
1543         return deferred.promise;
1544     }
1545
1546     $scope.detail_view = function(action, user_data, items) {
1547         if (h = items[0]) {
1548             $scope.detail_hold_id = h.hold.id();
1549         }
1550     }
1551
1552     $scope.list_view = function(items) {
1553          $scope.detail_hold_id = null;
1554     }
1555
1556     // refresh the list of record holds when the pickup lib is changed.
1557     $scope.pickup_ou = egCore.org.get(egCore.auth.user().ws_ou());
1558     $scope.pickup_ou_changed = function(org) {
1559         $scope.pickup_ou = org;
1560         provider.refresh();
1561     }
1562
1563     $scope.print_holds = function() {
1564         var holds = [];
1565         angular.forEach($scope.hold_grid_controls.allItems(), function(item) {
1566             holds.push({
1567                 hold : egCore.idl.toHash(item.hold),
1568                 patron_last : item.patron_last,
1569                 patron_alias : item.patron_alias,
1570                 patron_barcode : item.patron_barcode,
1571                 copy : egCore.idl.toHash(item.copy),
1572                 volume : egCore.idl.toHash(item.volume),
1573                 title : item.mvr.title(),
1574                 author : item.mvr.author()
1575             });
1576         });
1577
1578         egCore.print.print({
1579             context : 'receipt', 
1580             template : 'holds_for_bib', 
1581             scope : {holds : holds}
1582         });
1583     }
1584
1585     $scope.current_hold_transfer_dest = egCore.hatch.getLocalItem ('eg.circ.hold.title_transfer_target');
1586
1587     $scope.mark_hold_transfer_dest = function() {
1588         $scope.current_hold_transfer_dest = $scope.record_id;
1589         egCore.hatch.setLocalItem(
1590             'eg.circ.hold.title_transfer_target', $scope.record_id);
1591         ngToast.create(egCore.strings.HOLD_TRANSFER_DEST_MARKED);
1592     }
1593
1594     // UI presents this option as "all holds"
1595     $scope.transfer_holds_to_marked = function() {
1596         var hold_ids = $scope.hold_grid_controls.allItems().map(
1597             function(hold_data) {return hold_data.hold.id()});
1598         egHolds.transfer_to_marked_title(hold_ids);
1599     }
1600
1601     // ------------------------------------------------------------------
1602     // Initialize the selected tab
1603
1604     function init_cat_url() {
1605         // Set the initial catalog URL.  This only happens once.
1606         // The URL is otherwise generated through user navigation.
1607         if ($scope.catalog_url) return; 
1608
1609         var url = $location.absUrl().replace(/\/staff.*/, '/opac/advanced');
1610
1611         // A record ID in the path indicates a request for the record-
1612         // specific page.
1613         if ($routeParams.record_id) {
1614             url = url.replace(/advanced/, '/record/' + $scope.record_id);
1615         }
1616
1617         // Jumping directly to the results page by passing a search
1618         // query via the URL.  Copy all URL params to the iframe url.
1619         if ($location.path().match(/catalog\/results/)) {
1620             url = url.replace(/advanced/, '/results?');
1621             var first = true;
1622             angular.forEach($location.search(), function(val, key) {
1623                 if (!first) url += '&';
1624                 first = false;
1625                 url += encodeURIComponent(key) 
1626                     + '=' + encodeURIComponent(val);
1627             });
1628         }
1629
1630         // if we're displaying the advanced search form, select
1631         // whatever default pane the user has chosen via workstation
1632         // preference
1633         if (url.match(/\/opac\/advanced$/)) {
1634             var adv_pane = egCore.hatch.getLocalItem('eg.search.adv_pane');
1635             if (adv_pane) {
1636                 url += '?pane=' + encodeURIComponent(adv_pane);
1637             }
1638         }
1639
1640         $scope.catalog_url = url;
1641     }
1642
1643     function init_parts_url() {
1644         $scope.parts_url = $location
1645             .absUrl()
1646             .replace(
1647                 /\/staff.*/,
1648                 '/conify/global/biblio/monograph_part?r='+$scope.record_id
1649             );
1650     }
1651
1652     $scope.set_record_tab = function(tab) {
1653         $scope.record_tab = tab;
1654
1655         switch(tab) {
1656
1657             case 'monoparts':
1658                 init_parts_url();
1659                 break;
1660
1661             case 'catalog':
1662                 init_cat_url();
1663                 break;
1664
1665             case 'holds':
1666                 $scope.detail_hold_record_id = $scope.record_id; 
1667                 // refresh the holds grid
1668                 provider.refresh();
1669
1670                 break;
1671         }
1672     }
1673
1674     $scope.set_default_record_tab = function() {
1675         egCore.hatch.setLocalItem(
1676             'eg.cat.default_record_tab', $scope.record_tab);
1677         $timeout(function(){$scope.default_tab = $scope.record_tab});
1678     }
1679
1680     var tab;
1681     if ($scope.record_id) {
1682         $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
1683         tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
1684
1685     } else {
1686         tab = $routeParams.record_tab || 'catalog';
1687     }
1688     $scope.set_record_tab(tab);
1689
1690 }])
1691
1692 .controller('AuthorityCtrl',
1693        ['$scope','$routeParams','$location','$window','$q','egCore',
1694 function($scope , $routeParams , $location , $window , $q , egCore) {
1695
1696     // set record ID on page load if available...
1697     $scope.authority_id = $routeParams.authority_id;
1698
1699     if ($routeParams.authority_id) $scope.from_route = true;
1700     else $scope.from_route = false;
1701
1702     $scope.stop_unload = false;
1703 }])
1704
1705 .controller('URLVerifyCtrl',
1706        ['$scope','$location',
1707 function($scope , $location) {
1708     $scope.verifyurls_url = $location.absUrl().replace(/\/staff.*/, '/url_verify/sessions');
1709 }])
1710
1711 .controller('VandelayCtrl',
1712        ['$scope','$location',
1713 function($scope , $location) {
1714     $scope.vandelay_url = $location.absUrl().replace(/\/staff\/cat\/catalog\/vandelay/, '/vandelay/vandelay');
1715 }])
1716
1717 .controller('ManageAuthoritiesCtrl',
1718        ['$scope','$location',
1719 function($scope , $location) {
1720     $scope.manageauthorities_url = $location.absUrl().replace(/\/staff.*/, '/cat/authority/list');
1721 }])
1722
1723 .controller('BatchEditCtrl',
1724        ['$scope','$location','$routeParams',
1725 function($scope , $location , $routeParams) {
1726     $scope.batchedit_url = $location.absUrl().replace(/\/eg.*/, '/opac/extras/merge_template');
1727     if ($routeParams.container_type) {
1728         switch ($routeParams.container_type) {
1729             case 'bucket':
1730                 $scope.batchedit_url += '?recordSource=b&containerid=' + $routeParams.container_id;
1731                 break;
1732             case 'record':
1733                 $scope.batchedit_url += '?recordSource=r&recid=' + $routeParams.container_id;
1734                 break;
1735         };
1736     }
1737 }])
1738
1739  
1740 .filter('boolText', function(){
1741     return function (v) {
1742         return v == 't';
1743     }
1744 })
1745
1746 .factory('conjoinedSvc', 
1747        ['egCore','$q',
1748 function(egCore , $q) {
1749
1750     var service = {
1751         items : [], // record search results
1752         index : 0, // search grid index
1753         rid : null
1754     };
1755
1756     service.flesh = {   
1757         flesh : 4, 
1758         flesh_fields : {
1759             bpbcm : ['target_copy','peer_type'],
1760             acp : ['call_number'],
1761             acn : ['record'],
1762             bre : ['simple_record']
1763         },
1764         // avoid fetching the MARC blob by specifying which
1765         // fields on the bre to select.  More may be needed.
1766         // note that fleshed fields are explicitly selected.
1767         select : { bre : ['id'] },
1768         order_by : { bpbcm : ['id'] },
1769     }
1770
1771     // resolved with the last received copy
1772     service.fetch = function(rid) {
1773         if (!rid && !service.rid) return $q.when();
1774
1775         if (rid) service.rid = rid;
1776         service.items = [];
1777         service.index = 0;
1778
1779         return egCore.pcrud.search(
1780             'bpbcm',
1781             {peer_record : service.rid},
1782             service.flesh,
1783             {atomic : true}
1784         ).then( function(list) { // finished
1785             service.items = list;
1786             return service.items;
1787         });
1788     }
1789
1790     // returns a promise resolved with the list of peer bib types
1791     service.get_peer_types = function() {
1792         if (egCore.env.bpt)
1793             return $q.when(egCore.env.bpt.list);
1794
1795         return egCore.pcrud.retrieveAll('bpt', null, {atomic : true})
1796         .then(function(list) {
1797             egCore.env.absorbList(list, 'bpt');
1798             return list;
1799         });
1800     };
1801
1802     return service;
1803 }])
1804
1805