]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/catalog/app.js
webstaff: break holdingsSvc out of catalog/app.js
[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'])
11
12 .config(function($routeProvider, $locationProvider, $compileProvider) {
13     $locationProvider.html5Mode(true);
14     $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|blob):/); // grid export
15
16     var resolver = {delay : ['egCore','egStartup','egUser', function(egCore, egStartup, egUser) {
17         egCore.env.classLoaders.aous = function() {
18             return egCore.org.settings([
19                 'cat.marc_control_number_identifier'
20             ]).then(function(settings) {
21                 // local settings are cached within egOrg.  Caching them
22                 // again in egEnv just simplifies the syntax for access.
23                 egCore.env.aous = settings;
24             });
25         }
26         egCore.env.loadClasses.push('aous');
27         return egStartup.go()
28     }]};
29
30     $routeProvider.when('/cat/catalog/index', {
31         templateUrl: './cat/catalog/t_catalog',
32         controller: 'CatalogCtrl',
33         resolve : resolver
34     });
35
36     $routeProvider.when('/cat/catalog/retrieve_by_id', {
37         templateUrl: './cat/catalog/t_retrieve_by_id',
38         controller: 'CatalogRecordRetrieve',
39         resolve : resolver
40     });
41
42     $routeProvider.when('/cat/catalog/retrieve_by_tcn', {
43         templateUrl: './cat/catalog/t_retrieve_by_tcn',
44         controller: 'CatalogRecordRetrieve',
45         resolve : resolver
46     });
47
48     $routeProvider.when('/cat/catalog/new_bib', {
49         templateUrl: './cat/catalog/t_new_bib',
50         controller: 'NewBibCtrl',
51         resolve : resolver
52     });
53
54     // create some catalog page-specific mappings
55     $routeProvider.when('/cat/catalog/record/:record_id', {
56         templateUrl: './cat/catalog/t_catalog',
57         controller: 'CatalogCtrl',
58         resolve : resolver
59     });
60
61     // create some catalog page-specific mappings
62     $routeProvider.when('/cat/catalog/record/:record_id/:record_tab', {
63         templateUrl: './cat/catalog/t_catalog',
64         controller: 'CatalogCtrl',
65         resolve : resolver
66     });
67
68     $routeProvider.when('/cat/catalog/batchEdit', {
69         templateUrl: './cat/catalog/t_batchedit',
70         controller: 'BatchEditCtrl',
71         resolve : resolver
72     });
73
74     $routeProvider.when('/cat/catalog/batchEdit/:container_type/:container_id', {
75         templateUrl: './cat/catalog/t_batchedit',
76         controller: 'BatchEditCtrl',
77         resolve : resolver
78     });
79
80     $routeProvider.when('/cat/catalog/vandelay', {
81         templateUrl: './cat/catalog/t_vandelay',
82         controller: 'VandelayCtrl',
83         resolve : resolver
84     });
85
86     $routeProvider.when('/cat/catalog/verifyURLs', {
87         templateUrl: './cat/catalog/t_verifyurls',
88         controller: 'URLVerifyCtrl',
89         resolve : resolver
90     });
91
92     $routeProvider.when('/cat/catalog/manageAuthorities', {
93         templateUrl: './cat/catalog/t_manageauthorities',
94         controller: 'ManageAuthoritiesCtrl',
95         resolve : resolver
96     });
97
98     $routeProvider.when('/cat/catalog/authority/:authority_id/marc_edit', {
99         templateUrl: './cat/catalog/t_authority',
100         controller: 'AuthorityCtrl',
101         resolve : resolver
102     });
103
104     $routeProvider.otherwise({redirectTo : '/cat/catalog/index'});
105 })
106
107
108 /**
109  * */
110 .controller('CatalogRecordRetrieve',
111        ['$scope','$routeParams','$location','$q','egCore',
112 function($scope , $routeParams , $location , $q , egCore ) {
113
114     $scope.focusMe = true;
115
116     // jump to the patron checkout UI
117     function loadRecord(record_id) {
118         $location
119         .path('/cat/catalog/record/' + record_id);
120     }
121
122     $scope.submitId = function(args) {
123         $scope.recordNotFound = null;
124         if (!args.record_id) return;
125
126         // blur so next time it's set to true it will re-apply select()
127         $scope.selectMe = false;
128
129         return loadRecord(args.record_id);
130     }
131
132     $scope.submitTCN = function(args) {
133         $scope.recordNotFound = null;
134         $scope.moreRecordsFound = null;
135         if (!args.record_tcn) return;
136
137         // blur so next time it's set to true it will re-apply select()
138         $scope.selectMe = false;
139
140         // lookup TCN
141         egCore.net.request(
142             'open-ils.search',
143             'open-ils.search.biblio.tcn',
144             args.record_tcn)
145
146         .then(function(resp) { // get_barcodes
147
148             if (evt = egCore.evt.parse(resp)) {
149                 alert(evt); // FIXME
150                 return;
151             }
152
153             if (!resp.count) {
154                 $scope.recordNotFound = args.record_tcn;
155                 $scope.selectMe = true;
156                 return;
157             }
158
159             if (resp.count > 1) {
160                 $scope.moreRecordsFound = args.record_tcn;
161                 $scope.selectMe = true;
162                 return;
163             }
164
165             var record_id = resp.ids[0];
166             return loadRecord(record_id);
167         });
168     }
169
170 }])
171
172 .controller('NewBibCtrl',
173        ['$scope','$routeParams','$location','$window','$q','egCore',
174         'egGridDataProvider','egHoldGridActions','$timeout','holdingsSvc',
175 function($scope , $routeParams , $location , $window , $q , egCore) {
176
177     $scope.have_template = false;
178     $scope.marc_template = '';
179     $scope.stop_unload = false;
180     $scope.template_list = [];
181     $scope.template_name = '';
182     $scope.new_bib_id = 0;
183
184     egCore.net.request(
185         'open-ils.cat',
186         'open-ils.cat.marc_template.types.retrieve'
187     ).then(function(resp) {
188         angular.forEach(resp, function(name) {
189             $scope.template_list.push(name);
190         });
191         $scope.template_list.sort();
192     });
193     egCore.hatch.getItem('cat.default_bib_marc_template').then(function(template) {
194         $scope.template_name = template;
195     });
196
197     $scope.loadTemplate = function() {
198         if ($scope.template_name) {
199             egCore.net.request(
200                 'open-ils.cat',
201                 'open-ils.cat.biblio.marc_template.retrieve',
202                 $scope.template_name
203             ).then(function(template) {
204                 $scope.marc_template = template;
205                 $scope.have_template = true;
206             });
207         }
208     }
209
210     $scope.setDefaultTemplate = function() {
211         var hatch_key = "cat.default_bib_marc_template";
212         if ($scope.template_name) {
213             egCore.hatch.setItem(hatch_key, $scope.template_name);
214         } else {
215             egCore.hatch.removeItem(hatch_key);
216         }
217     }
218
219     $scope.$watch('new_bib_id', function(newVal, oldVal) {
220         if (newVal) {
221             $location.path('/cat/catalog/record/' + $scope.new_bib_id);
222         }
223     });
224     
225
226 }])
227
228 .controller('CatalogCtrl',
229        ['$scope','$routeParams','$location','$window','$q','egCore','egHolds','egCirc','egConfirmDialog',
230         'egGridDataProvider','egHoldGridActions','$timeout','$modal','holdingsSvc','egUser','conjoinedSvc',
231 function($scope , $routeParams , $location , $window , $q , egCore , egHolds , egCirc,  egConfirmDialog,
232          egGridDataProvider , egHoldGridActions , $timeout , $modal , holdingsSvc , egUser , conjoinedSvc) {
233
234     // set record ID on page load if available...
235     $scope.record_id = $routeParams.record_id;
236     $scope.summary_pane_record;
237
238     if ($routeParams.record_id) $scope.from_route = true;
239     else $scope.from_route = false;
240
241     // will hold a ref to the opac iframe
242     $scope.opac_iframe = null;
243     $scope.parts_iframe = null;
244
245     $scope.in_opac_call = false;
246     $scope.opac_call = function (opac_frame_function, force_opac_tab) {
247         if ($scope.opac_iframe) {
248             if (force_opac_tab) $scope.record_tab = 'catalog';
249             $scope.in_opac_call = true;
250             $scope.opac_iframe.dom.contentWindow[opac_frame_function]();
251             if (opac_frame_function == 'rdetailBackToResults') {
252                 $location.update_path('/cat/catalog/index');
253             }
254         }
255     }
256
257     $scope.add_to_record_bucket = function() {
258         var recId = $scope.record_id;
259         return $modal.open({
260             templateUrl: './cat/catalog/t_add_to_bucket',
261             animation: true,
262             size: 'md',
263             controller:
264                    ['$scope','$modalInstance',
265             function($scope , $modalInstance) {
266
267                 $scope.bucket_id = 0;
268                 $scope.newBucketName = '';
269                 $scope.allBuckets = [];
270                 egCore.net.request(
271                     'open-ils.actor',
272                     'open-ils.actor.container.retrieve_by_class.authoritative',
273                     egCore.auth.token(), egCore.auth.user().id(),
274                     'biblio', 'staff_client'
275                 ).then(function(buckets) { $scope.allBuckets = buckets; });
276
277                 $scope.add_to_bucket = function() {
278                     var item = new egCore.idl.cbrebi();
279                     item.bucket($scope.bucket_id);
280                     item.target_biblio_record_entry(recId);
281                     egCore.net.request(
282                         'open-ils.actor',
283                         'open-ils.actor.container.item.create',
284                         egCore.auth.token(), 'biblio', item
285                     ).then(function(resp) {
286                         $modalInstance.close();
287                     });
288                 }
289
290                 $scope.add_to_new_bucket = function() {
291                     var bucket = new egCore.idl.cbreb();
292                     bucket.owner(egCore.auth.user().id());
293                     bucket.name($scope.newBucketName);
294                     bucket.description('');
295                     bucket.btype('staff_client');
296
297                     egCore.net.request(
298                         'open-ils.actor',
299                         'open-ils.actor.container.create',
300                         egCore.auth.token(), 'biblio', bucket
301                     ).then(function(bucket) {
302                         $scope.bucket_id = bucket;
303                         $scope.add_to_bucket();
304                     });
305                 }
306
307                 $scope.cancel = function() {
308                     $modalInstance.dismiss();
309                 }
310             }]
311         });
312     }
313
314     $scope.current_overlay_target     = egCore.hatch.getLocalItem('eg.cat.marked_overlay_record');
315     $scope.current_voltransfer_target = egCore.hatch.getLocalItem('eg.cat.marked_volume_transfer_record');
316     $scope.current_conjoined_target   = egCore.hatch.getLocalItem('eg.cat.marked_conjoined_record');
317
318     $scope.markConjoined = function () {
319         $scope.current_conjoined_target = $scope.record_id;
320         egCore.hatch.setLocalItem('eg.cat.marked_conjoined_record',$scope.record_id);
321     };
322
323     $scope.markVolTransfer = function () {
324         $scope.current_voltransfer_target = $scope.record_id;
325         egCore.hatch.setLocalItem('eg.cat.marked_volume_transfer_record',$scope.record_id);
326     };
327
328     $scope.markOverlay = function () {
329         $scope.current_overlay_target = $scope.record_id;
330         egCore.hatch.setLocalItem('eg.cat.marked_overlay_record',$scope.record_id);
331     };
332
333     $scope.clearRecordMarks = function () {
334         $scope.current_overlay_target     = null;
335         $scope.current_voltransfer_target = null;
336         $scope.current_conjoined_target   = null;
337         egCore.hatch.removeLocalItem('eg.cat.marked_volume_transfer_record');
338         egCore.hatch.removeLocalItem('eg.cat.marked_conjoined_record');
339         egCore.hatch.removeLocalItem('eg.cat.marked_overlay_record');
340     }
341
342     $scope.stop_unload = false;
343     $scope.$watch('stop_unload',
344         function(newVal, oldVal) {
345             if (newVal && newVal != oldVal && $scope.opac_iframe) {
346                 $($scope.opac_iframe.dom.contentWindow).on('beforeunload', function(){
347                     return 'There is unsaved data in this record.'
348                 });
349             } else {
350                 if ($scope.opac_iframe)
351                     $($scope.opac_iframe.dom.contentWindow).off('beforeunload');
352             }
353         }
354     );
355
356     // Set the "last bib" cookie, if we have that
357     if ($scope.record_id)
358         egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
359
360     $scope.refresh_record_callback = function (record_id) {
361         egCore.pcrud.retrieve('bre', record_id, {
362             flesh : 1,
363             flesh_fields : {
364                 bre : ['simple_record','creator','editor']
365             }
366         }).then(function(rec) {
367             rec.owner(egCore.org.get(rec.owner()));
368             $scope.summary_pane_record = rec;
369         });
370
371         return record_id;
372     }
373
374     // also set it when the iframe changes to a new record
375     $scope.handle_page = function(url) {
376
377         if (!url || url == 'about:blank') {
378             // nothing loaded.  If we already have a record ID, leave it.
379             return;
380         }
381
382         var match = url.match(/\/+opac\/+record\/+(\d+)/);
383         if (match) {
384             $scope.record_id = match[1];
385             egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
386             $scope.holdings_record_id_changed($scope.record_id);
387             conjoinedSvc.fetch($scope.record_id).then(function(){
388                 $scope.conjoinedGridDataProvider.refresh();
389             });
390             init_parts_url();
391             $location.update_path('/cat/catalog/record/' + $scope.record_id);
392         } else {
393             delete $scope.record_id;
394             $scope.from_route = false;
395         }
396
397         // child scope is executing this function, so our digest doesn't fire ... thus,
398         $scope.$apply();
399
400         if (!$scope.in_opac_call) {
401             if ($scope.record_id) {
402                 $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
403                 tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
404             } else {
405                 tab = $routeParams.record_tab || 'catalog';
406             }
407             $scope.set_record_tab(tab);
408         } else {
409             $scope.in_opac_call = false;
410         }
411     }
412
413     // xulG catalog handlers
414     $scope.handlers = { }
415
416     // ------------------------------------------------------------------
417     // Conjoined items
418
419     $scope.conjoinedGridControls = {};
420     $scope.conjoinedGridDataProvider = egGridDataProvider.instance({
421         get : function(offset, count) {
422             return this.arrayNotifier(conjoinedSvc.items, offset, count);
423         }
424     });
425
426     $scope.changeConjoinedType = function () {
427         var peers = egCore.idl.Clone($scope.conjoinedGridControls.selectedItems());
428         angular.forEach(peers, function (p) {
429             p.target_copy(p.target_copy().id());
430             p.peer_type(p.peer_type().id());
431         });
432
433         var conjoinedGridDataProviderRef = $scope.conjoinedGridDataProvider;
434
435         return $modal.open({
436             templateUrl: './cat/catalog/t_conjoined_selector',
437             animation: true,
438             controller:
439                    ['$scope','$modalInstance',
440             function($scope , $modalInstance) {
441                 $scope.update = true;
442
443                 $scope.peer_type = null;
444                 $scope.peer_type_list = [];
445                 conjoinedSvc.get_peer_types().then(function(list){
446                     $scope.peer_type_list = list;
447                 });
448     
449                 $scope.ok = function(type) {
450                     var promises = [];
451     
452                     angular.forEach(peers, function (p) {
453                         p.ischanged(1);
454                         p.peer_type(type);
455                         promises.push(egCore.pcrud.update(p));
456                     });
457     
458                     return $q.all(promises)
459                         .then(function(){$modalInstance.close()})
460                         .then(function(){return conjoinedSvc.fetch()})
461                         .then(function(){conjoinedGridDataProviderRef.refresh()});
462                 }
463     
464                 $scope.cancel = function($event) {
465                     $modalInstance.dismiss();
466                     $event.preventDefault();
467                 }
468             }]
469         });
470         
471     }
472
473     $scope.refreshConjoined = function () {
474         conjoinedSvc.fetch($scope.record_id)
475         .then(function(){$scope.conjoinedGridDataProvider.refresh();});
476     }
477
478     $scope.deleteSelectedConjoined = function () {
479         var peers = $scope.conjoinedGridControls.selectedItems();
480
481         if (peers.length > 0) {
482             egConfirmDialog.open(
483                 egCore.strings.CONFIRM_DELETE_PEERS,
484                 egCore.strings.CONFIRM_DELETE_PEERS_MESSAGE,
485                 {peers : peers.length}
486             ).result.then(function() {
487                 angular.forEach(peers, function (p) {
488                     p.isdeleted(1);
489                 });
490
491                 egCore.pcrud.remove(peers).then(function() {
492                     return conjoinedSvc.fetch();
493                 }).then(function() {
494                     $scope.conjoinedGridDataProvider.refresh();
495                 });
496             });
497         }
498     }
499     if ($scope.record_id)
500         conjoinedSvc.fetch($scope.record_id);
501
502     // ------------------------------------------------------------------
503     // Holdings
504
505     $scope.holdingsGridControls = {};
506     $scope.holdingsGridDataProvider = egGridDataProvider.instance({
507         get : function(offset, count) {
508             return this.arrayNotifier(holdingsSvc.copies, offset, count);
509         }
510     });
511
512     $scope.add_copies_to_bucket = function() {
513         var copy_list = gatherSelectedHoldingsIds();
514         if (copy_list.length == 0) return;
515
516         return $modal.open({
517             templateUrl: './cat/catalog/t_add_to_bucket',
518             animation: true,
519             size: 'md',
520             controller:
521                    ['$scope','$modalInstance',
522             function($scope , $modalInstance) {
523
524                 $scope.bucket_id = 0;
525                 $scope.newBucketName = '';
526                 $scope.allBuckets = [];
527
528                 egCore.net.request(
529                     'open-ils.actor',
530                     'open-ils.actor.container.retrieve_by_class.authoritative',
531                     egCore.auth.token(), egCore.auth.user().id(),
532                     'copy', 'staff_client'
533                 ).then(function(buckets) { $scope.allBuckets = buckets; });
534
535                 $scope.add_to_bucket = function() {
536                     var promises = [];
537                     angular.forEach(copy_list, function (cp) {
538                         var item = new egCore.idl.ccbi()
539                         item.bucket($scope.bucket_id);
540                         item.target_copy(cp);
541                         promises.push(
542                             egCore.net.request(
543                                 'open-ils.actor',
544                                 'open-ils.actor.container.item.create',
545                                 egCore.auth.token(), 'copy', item
546                             )
547                         );
548
549                         return $q.all(promises).then(function() {
550                             $modalInstance.close();
551                         });
552                     });
553                 }
554
555                 $scope.add_to_new_bucket = function() {
556                     var bucket = new egCore.idl.ccb();
557                     bucket.owner(egCore.auth.user().id());
558                     bucket.name($scope.newBucketName);
559                     bucket.description('');
560                     bucket.btype('staff_client');
561
562                     return egCore.net.request(
563                         'open-ils.actor',
564                         'open-ils.actor.container.create',
565                         egCore.auth.token(), 'copy', bucket
566                     ).then(function(bucket) {
567                         $scope.bucket_id = bucket;
568                         $scope.add_to_bucket();
569                     });
570                 }
571
572                 $scope.cancel = function() {
573                     $modalInstance.dismiss();
574                 }
575             }]
576         });
577     }
578
579     $scope.requestItems = function() {
580         var copy_list = gatherSelectedHoldingsIds();
581         if (copy_list.length == 0) return;
582
583         return $modal.open({
584             templateUrl: './cat/catalog/t_request_items',
585             animation: true,
586             controller:
587                    ['$scope','$modalInstance',
588             function($scope , $modalInstance) {
589                 $scope.user = null;
590                 $scope.first_user_fetch = true;
591
592                 $scope.hold_data = {
593                     hold_type : 'C',
594                     copy_list : copy_list,
595                     pickup_lib: egCore.org.get(egCore.auth.user().ws_ou()),
596                     user      : egCore.auth.user().id()
597                 };
598
599                 egUser.get( $scope.hold_data.user ).then(function(u) {
600                     $scope.user = u;
601                     $scope.barcode = u.card().barcode();
602                     $scope.user_name = egUser.format_name(u);
603                     $scope.hold_data.user = u.id();
604                 });
605
606                 $scope.user_name = '';
607                 $scope.barcode = '';
608                 $scope.$watch('barcode', function (n) {
609                     if (!$scope.first_user_fetch) {
610                         egUser.getByBarcode(n).then(function(u) {
611                             $scope.user = u;
612                             $scope.user_name = egUser.format_name(u);
613                             $scope.hold_data.user = u.id();
614                         }, function() {
615                             $scope.user = null;
616                             $scope.user_name = '';
617                             delete $scope.hold_data.user;
618                         });
619                     }
620                     $scope.first_user_fetch = false;
621                 });
622
623                 $scope.ok = function(h) {
624                     var args = {
625                         patronid  : h.user,
626                         hold_type : h.hold_type,
627                         pickup_lib: h.pickup_lib.id(),
628                         depth     : 0
629                     };
630
631                     egCore.net.request(
632                         'open-ils.circ',
633                         'open-ils.circ.holds.test_and_create.batch.override',
634                         egCore.auth.token(), args, h.copy_list
635                     );
636
637                     $modalInstance.close();
638                 }
639
640                 $scope.cancel = function($event) {
641                     $modalInstance.dismiss();
642                     $event.preventDefault();
643                 }
644             }]
645         });
646     }
647
648     $scope.replaceBarcodes = function() {
649         var copy_list = gatherSelectedRawCopies();
650         if (copy_list.length == 0) return;
651
652         var holdingsGridDataProviderRef = $scope.holdingsGridDataProvider;
653
654         angular.forEach(copy_list, function (cp) {
655             $modal.open({
656                 templateUrl: './cat/share/t_replace_barcode',
657                 animation: true,
658                 controller:
659                            ['$scope','$modalInstance',
660                     function($scope , $modalInstance) {
661                         $scope.isModal = true;
662                         $scope.focusBarcode = false;
663                         $scope.focusBarcode2 = true;
664                         $scope.barcode1 = cp.barcode();
665
666                         $scope.updateBarcode = function() {
667                             $scope.copyNotFound = false;
668                             $scope.updateOK = false;
669                 
670                             egCore.pcrud.search('acp',
671                                 {deleted : 'f', barcode : $scope.barcode1})
672                             .then(function(copy) {
673                 
674                                 if (!copy) {
675                                     $scope.focusBarcode = true;
676                                     $scope.copyNotFound = true;
677                                     return;
678                                 }
679                 
680                                 $scope.copyId = copy.id();
681                                 copy.barcode($scope.barcode2);
682                 
683                                 egCore.pcrud.update(copy).then(function(stat) {
684                                     $scope.updateOK = stat;
685                                     $scope.focusBarcode = true;
686                                     holdingsSvc.fetchAgain().then(function (){
687                                         holdingsGridDataProviderRef.refresh();
688                                     });
689                                 });
690
691                             });
692                             $modalInstance.close();
693                         }
694
695                         $scope.cancel = function($event) {
696                             $modalInstance.dismiss();
697                             $event.preventDefault();
698                         }
699                     }
700                 ]
701             });
702         });
703     }
704
705     // refresh the list of holdings when the record_id is changed.
706     $scope.holdings_record_id_changed = function(id) {
707         if ($scope.record_id != id) $scope.record_id = id;
708         console.log('record id changed to ' + id + ', loading new holdings');
709         holdingsSvc.fetch({
710             rid : $scope.record_id,
711             org : $scope.holdings_ou,
712             copy: $scope.holdings_show_copies,
713             vol : $scope.holdings_show_vols,
714             empty: $scope.holdings_show_empty
715         }).then(function() {
716             $scope.holdingsGridDataProvider.refresh();
717         });
718     }
719
720     // refresh the list of holdings when the filter lib is changed.
721     $scope.holdings_ou = egCore.org.get(egCore.auth.user().ws_ou());
722     $scope.holdings_ou_changed = function(org) {
723         $scope.holdings_ou = org;
724         holdingsSvc.fetch({
725             rid : $scope.record_id,
726             org : $scope.holdings_ou,
727             copy: $scope.holdings_show_copies,
728             vol : $scope.holdings_show_vols,
729             empty: $scope.holdings_show_empty
730         }).then(function() {
731             $scope.holdingsGridDataProvider.refresh();
732         });
733     }
734
735     $scope.holdings_cb_changed = function(cb,newVal,norefresh) {
736         $scope[cb] = newVal;
737         egCore.hatch.setItem('cat.' + cb, newVal);
738         if (!norefresh) holdingsSvc.fetch({
739             rid : $scope.record_id,
740             org : $scope.holdings_ou,
741             copy: $scope.holdings_show_copies,
742             vol : $scope.holdings_show_vols,
743             empty: $scope.holdings_show_empty
744         }).then(function() {
745             $scope.holdingsGridDataProvider.refresh();
746         });
747     }
748
749     egCore.hatch.getItem('cat.holdings_show_vols').then(function(x){
750         if (typeof x ==  'undefined') x = true;
751         $scope.holdings_cb_changed('holdings_show_vols',x,true);
752         $('#holdings_show_vols').prop('checked', x);
753     }).then(function(){
754         egCore.hatch.getItem('cat.holdings_show_copies').then(function(x){
755             if (typeof x ==  'undefined') x = true;
756             $scope.holdings_cb_changed('holdings_show_copies',x,true);
757             $('#holdings_show_copies').prop('checked', x);
758         }).then(function(){
759             egCore.hatch.getItem('cat.holdings_show_empty').then(function(x){
760                 if (typeof x ==  'undefined') x = true;
761                 $scope.holdings_cb_changed('holdings_show_empty',x);
762                 $('#holdings_show_empty').prop('checked', x);
763             })
764         })
765     });
766
767     $scope.vols_not_shown = function () {
768         return !$scope.holdings_show_vols;
769     }
770
771     $scope.copies_not_shown = function () {
772         return !$scope.holdings_show_copies;
773     }
774
775     $scope.holdings_checkbox_handler = function (item) {
776         $scope.holdings_cb_changed(item.checkbox,item.checked);
777     }
778
779     function gatherSelectedHoldingsIds () {
780         var cp_id_list = [];
781         angular.forEach(
782             $scope.holdingsGridControls.selectedItems(),
783             function (item) { cp_id_list = cp_id_list.concat(item.id_list) }
784         );
785         return cp_id_list;
786     }
787
788     function gatherSelectedRawCopies () {
789         var cp_list = [];
790         angular.forEach(
791             $scope.holdingsGridControls.selectedItems(),
792             function (item) { if (item.raw) cp_list = cp_list.concat(item.raw) }
793         );
794         return cp_list;
795     }
796
797     function gatherSelectedVolumeIds () {
798         var cn_id_list = [];
799         angular.forEach(
800             $scope.holdingsGridControls.selectedItems(),
801             function (item) {
802                 if (cn_id_list.indexOf(item.call_number.id) == -1)
803                     cn_id_list.push(item.call_number.id)
804             }
805         );
806         return cn_id_list;
807     }
808
809     $scope.selectedHoldingsDelete = function (vols, copies) {
810
811         var cnHash = {};
812         var perCnCopies = {};
813
814         var cn_count = 0;
815         var cp_count = 0;
816
817         angular.forEach(
818             $scope.holdingsGridControls.selectedItems(),
819             function (item) {
820                 if (vols && item.raw_call_number) {
821                     cnHash[item.call_number.id] = egCore.idl.Clone(item.raw_call_number);
822                     cnHash[item.call_number.id].isdeleted(1);
823                     cn_count++;
824                 } else if (copies) {
825                     angular.forEach(egCore.idl.Clone(item.raw), function (cp) {
826                         cp.isdeleted(1);
827                         cp_count++;
828                         var cn_id = cp.call_number().id();
829                         if (!cnHash[cn_id]) {
830                             cnHash[cn_id] = cp.call_number();
831                             perCnCopies[cn_id] = [cp];
832                         } else {
833                             perCnCopies[cn_id].push(cp);
834                         }
835                         cp.call_number(cn_id); // prevent loops in JSON-ification
836                     });
837
838                 }
839             }
840         );
841
842         angular.forEach(perCnCopies, function (v, k) {
843             if (vols) {
844                 cnHash[k].isdeleted(1);
845                 cn_count++;
846             }
847             cnHash[k].copies(v);
848         });
849
850         cnList = [];
851         angular.forEach(cnHash, function (v, k) {
852             cnList.push(v);
853         });
854
855         if (cnList.length == 0) return;
856
857         egConfirmDialog.open(
858             egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES,
859             egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES_MESSAGE,
860             {copies : cp_count, volumes : cn_count}
861         ).result.then(function() {
862             egCore.net.request(
863                 'open-ils.cat',
864                 'open-ils.cat.asset.volume.fleshed.batch.update.override',
865                 egCore.auth.token(), cnList, 1, {}
866             ).then(function(update_count) {
867                 $scope.holdingsGridDataProvider.refresh();
868             });
869         });
870     }
871     $scope.selectedHoldingsCopyDelete = function () { $scope.selectedHoldingsDelete(false,true) }
872     $scope.selectedHoldingsVolCopyDelete = function () { $scope.selectedHoldingsDelete(true,true) }
873     $scope.selectedHoldingsEmptyVolCopyDelete = function () { $scope.selectedHoldingsDelete(true,false) }
874
875     spawnHoldingsAdd = function (vols,copies){
876         var raw = [];
877         if (copies) { // just a copy on existing volumes
878             angular.forEach(gatherSelectedVolumeIds(), function (v) {
879                 raw.push( {callnumber : v} );
880             });
881         } else if (vols) {
882             angular.forEach(
883                 $scope.holdingsGridControls.selectedItems(),
884                 function (item) {
885                     raw.push({owner : item.owner_id});
886                 }
887             );
888         }
889
890         if (raw.length == 0) raw.push({});
891
892         egCore.net.request(
893             'open-ils.actor',
894             'open-ils.actor.anon_cache.set_value',
895             null, 'edit-these-copies', {
896                 record_id: $scope.record_id,
897                 raw: raw,
898                 hide_vols : false,
899                 hide_copies : false
900             }
901         ).then(function(key) {
902             if (key) {
903                 var url = egCore.env.basePath + 'cat/volcopy/' + key;
904                 $timeout(function() { $window.open(url, '_blank') });
905             } else {
906                 alert('Could not create anonymous cache key!');
907             }
908         });
909     }
910     $scope.selectedHoldingsVolCopyAdd = function () { spawnHoldingsAdd(true,false) }
911     $scope.selectedHoldingsCopyAdd = function () { spawnHoldingsAdd(false,true) }
912
913     spawnHoldingsEdit = function (hide_vols,hide_copies){
914         egCore.net.request(
915             'open-ils.actor',
916             'open-ils.actor.anon_cache.set_value',
917             null, 'edit-these-copies', {
918                 record_id: $scope.record_id,
919                 copies: gatherSelectedHoldingsIds(),
920                 hide_vols : hide_vols,
921                 hide_copies : hide_copies
922             }
923         ).then(function(key) {
924             if (key) {
925                 var url = egCore.env.basePath + 'cat/volcopy/' + key;
926                 $timeout(function() { $window.open(url, '_blank') });
927             } else {
928                 alert('Could not create anonymous cache key!');
929             }
930         });
931     }
932     $scope.selectedHoldingsVolCopyEdit = function () { spawnHoldingsEdit(false,false) }
933     $scope.selectedHoldingsVolEdit = function () { spawnHoldingsEdit(false,true) }
934     $scope.selectedHoldingsCopyEdit = function () { spawnHoldingsEdit(true,false) }
935
936     $scope.selectedHoldingsItemStatus = function (){
937         var url = egCore.env.basePath + 'cat/item/search/' + gatherSelectedHoldingsIds().join(',')
938         $timeout(function() { $window.open(url, '_blank') });
939     }
940
941     $scope.markVolAsItemTarget = function() {
942         if ($scope.holdingsGridControls.selectedItems()[0].call_number.id) { // cn.id missing when vols are collapsed
943             egCore.hatch.setLocalItem(
944                 'eg.cat.item_transfer_target',
945                 $scope.holdingsGridControls.selectedItems()[0].call_number.id
946             );
947         }
948     }
949
950     $scope.markLibAsVolTarget = function() {
951         egCore.hatch.setLocalItem(
952             'eg.cat.volume_transfer_target',
953             $scope.holdingsGridControls.selectedItems()[0].owner_id
954         );
955     }
956
957     $scope.selectedHoldingsItemStatusDetail = function (){
958         angular.forEach(
959             gatherSelectedHoldingsIds(),
960             function (cid) {
961                 var url = egCore.env.basePath +
962                           'cat/item/' + cid;
963                 $timeout(function() { $window.open(url, '_blank') });
964             }
965         );
966     }
967
968     $scope.transferVolumes = function (){
969         var xfer_target = egCore.hatch.getLocalItem('eg.cat.volume_transfer_target');
970
971         if (xfer_target) {
972             egCore.net.request(
973                 'open-ils.cat',
974                 'open-ils.open-ils.cat.asset.volume.batch.transfer.override',
975                 egCore.auth.token(), {
976                     docid   : $scope.record_id,
977                     lib     : xfer_target,
978                     volumes : gatherSelectedVolumeIds()
979                 }
980             ).then(function(success) {
981                 if (success) {
982                     holdingsSvc.fetchAgain().then(function() {
983                         $scope.holdingsGridDataProvider.refresh();
984                     });
985                 } else {
986                     alert('Could not transfer volumes!');
987                 }
988             });
989         }
990         
991     }
992
993     $scope.transferItems = function (){
994         var xfer_target = egCore.hatch.getLocalItem('eg.cat.item_transfer_target');
995         var copy_ids = gatherSelectedHoldingsIds();
996         if (xfer_target && copy_ids.length > 0) {
997             egCore.net.request(
998                 'open-ils.cat',
999                 'open-ils.cat.transfer_copies_to_volume',
1000                 egCore.auth.token(),
1001                 xfer_target,
1002                 copy_ids
1003             ).then(
1004                 function(resp) { // oncomplete
1005                     var evt = egCore.evt.parse(resp);
1006                     if (evt) {
1007                         egConfirmDialog.open(
1008                             egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_TITLE,
1009                             egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_BODY,
1010                             {'evt_desc': evt.desc}
1011                         ).result.then(function() {
1012                             egCore.net.request(
1013                                 'open-ils.cat',
1014                                 'open-ils.cat.transfer_copies_to_volume.override',
1015                                 egCore.auth.token(),
1016                                 xfer_target,
1017                                 copy_ids,
1018                                 { events: ['TITLE_LAST_COPY', 'COPY_DELETE_WARNING'] }
1019                             ).then(function(resp) {
1020                                 holdingsSvc.fetchAgain().then(function() {
1021                                     $scope.holdingsGridDataProvider.refresh();
1022                                 });
1023                             });
1024                         });
1025                     } else {
1026                         holdingsSvc.fetchAgain().then(function() {
1027                             $scope.holdingsGridDataProvider.refresh();
1028                         });
1029                     }
1030                 },
1031                 null, // onerror
1032                 null // onprogress
1033             )
1034         }
1035     }
1036
1037     $scope.selectedHoldingsItemStatusTgrEvt = function (){
1038         angular.forEach(
1039             gatherSelectedHoldingsIds(),
1040             function (cid) {
1041                 var url = egCore.env.basePath +
1042                           'cat/item/' + cid + '/triggered_events';
1043                 $timeout(function() { $window.open(url, '_blank') });
1044             }
1045         );
1046     }
1047
1048     $scope.selectedHoldingsItemStatusHolds = function (){
1049         angular.forEach(
1050             gatherSelectedHoldingsIds(),
1051             function (cid) {
1052                 var url = egCore.env.basePath +
1053                           'cat/item/' + cid + '/holds';
1054                 $timeout(function() { $window.open(url, '_blank') });
1055             }
1056         );
1057     }
1058
1059     $scope.selectedHoldingsDamaged = function () {
1060         egCirc.mark_damaged(gatherSelectedHoldingsIds()).then(function() {
1061             holdingsSvc.fetchAgain().then(function() {
1062                 $scope.holdingsGridDataProvider.refresh();
1063             });
1064         });
1065     }
1066
1067     $scope.selectedHoldingsMissing = function () {
1068         egCirc.mark_missing(gatherSelectedHoldingsIds()).then(function() {
1069             holdingsSvc.fetchAgain().then(function() {
1070                 $scope.holdingsGridDataProvider.refresh();
1071             });
1072         });
1073     }
1074
1075     $scope.attach_to_peer_bib = function() {
1076         var copy_list = gatherSelectedHoldingsIds();
1077         if (copy_list.length == 0) return;
1078
1079         egCore.hatch.getItem('eg.cat.marked_conjoined_record').then(function(target_record) {
1080             if (!target_record) return;
1081
1082             return $modal.open({
1083                 templateUrl: './cat/catalog/t_conjoined_selector',
1084                 animation: true,
1085                 controller:
1086                        ['$scope','$modalInstance',
1087                 function($scope , $modalInstance) {
1088                     $scope.update = false;
1089
1090                     $scope.peer_type = null;
1091                     $scope.peer_type_list = [];
1092                     conjoinedSvc.get_peer_types().then(function(list){
1093                         $scope.peer_type_list = list;
1094                     });
1095     
1096                     $scope.ok = function(type) {
1097                         var promises = [];
1098     
1099                         angular.forEach(copy_list, function (cp) {
1100                             var n = new egCore.idl.bpbcm();
1101                             n.isnew(true);
1102                             n.peer_record(target_record);
1103                             n.target_copy(cp);
1104                             n.peer_type(type);
1105                             promises.push(egCore.pcrud.create(n));
1106                         });
1107     
1108                         return $q.all(promises).then(function(){$modalInstance.close()});
1109                     }
1110     
1111                     $scope.cancel = function($event) {
1112                         $modalInstance.dismiss();
1113                         $event.preventDefault();
1114                     }
1115                 }]
1116             });
1117         });
1118     }
1119
1120
1121     // ------------------------------------------------------------------
1122     // Holds 
1123     var provider = egGridDataProvider.instance({});
1124     $scope.hold_grid_data_provider = provider;
1125     $scope.grid_actions = egHoldGridActions;
1126     $scope.grid_actions.refresh = function () { provider.refresh() };
1127     $scope.hold_grid_controls = {};
1128
1129     var hold_ids = []; // current list of holds
1130     function fetchHolds(offset, count) {
1131         var ids = hold_ids.slice(offset, offset + count);
1132         return egHolds.fetch_holds(ids).then(null, null,
1133             function(hold_data) { 
1134                 return hold_data;
1135             }
1136         );
1137     }
1138
1139     provider.get = function(offset, count) {
1140         if ($scope.record_tab != 'holds') return $q.when();
1141         var deferred = $q.defer();
1142         hold_ids = []; // no caching ATM
1143
1144         // fetch the IDs
1145         egCore.net.request(
1146             'open-ils.circ',
1147             'open-ils.circ.holds.retrieve_all_from_title',
1148             egCore.auth.token(), $scope.record_id, 
1149             {pickup_lib : egCore.org.descendants($scope.pickup_ou.id(), true)}
1150         ).then(
1151             function(hold_data) {
1152                 angular.forEach(hold_data, function(list, type) {
1153                     hold_ids = hold_ids.concat(list);
1154                 });
1155                 fetchHolds(offset, count).then(
1156                     deferred.resolve, null, deferred.notify);
1157             }
1158         );
1159
1160         return deferred.promise;
1161     }
1162
1163     $scope.detail_view = function(action, user_data, items) {
1164         if (h = items[0]) {
1165             $scope.detail_hold_id = h.hold.id();
1166         }
1167     }
1168
1169     $scope.list_view = function(items) {
1170          $scope.detail_hold_id = null;
1171     }
1172
1173     // refresh the list of record holds when the pickup lib is changed.
1174     $scope.pickup_ou = egCore.org.get(egCore.auth.user().ws_ou());
1175     $scope.pickup_ou_changed = function(org) {
1176         $scope.pickup_ou = org;
1177         provider.refresh();
1178     }
1179
1180     $scope.print_holds = function() {
1181         var holds = [];
1182         angular.forEach($scope.hold_grid_controls.allItems(), function(item) {
1183             holds.push({
1184                 hold : egCore.idl.toHash(item.hold),
1185                 patron_last : item.patron_last,
1186                 patron_alias : item.patron_alias,
1187                 patron_barcode : item.patron_barcode,
1188                 copy : egCore.idl.toHash(item.copy),
1189                 volume : egCore.idl.toHash(item.volume),
1190                 title : item.mvr.title(),
1191                 author : item.mvr.author()
1192             });
1193         });
1194
1195         egCore.print.print({
1196             context : 'receipt', 
1197             template : 'holds_for_bib', 
1198             scope : {holds : holds}
1199         });
1200     }
1201
1202     $scope.mark_hold_transfer_dest = function() {
1203         egCore.hatch.setLocalItem(
1204             'eg.circ.hold.title_transfer_target', $scope.record_id);
1205     }
1206
1207     // UI presents this option as "all holds"
1208     $scope.transfer_holds_to_marked = function() {
1209         var hold_ids = $scope.hold_grid_controls.allItems().map(
1210             function(hold_data) {return hold_data.hold.id()});
1211         egHolds.transfer_to_marked_title(hold_ids);
1212     }
1213
1214     // ------------------------------------------------------------------
1215     // Initialize the selected tab
1216
1217     function init_cat_url() {
1218         // Set the initial catalog URL.  This only happens once.
1219         // The URL is otherwise generated through user navigation.
1220         if ($scope.catalog_url) return; 
1221
1222         var url = $location.absUrl().replace(/\/staff.*/, '/opac/advanced');
1223
1224         // A record ID in the path indicates a request for the record-
1225         // specific page.
1226         if ($routeParams.record_id) {
1227             url = url.replace(/advanced/, '/record/' + $scope.record_id);
1228         }
1229
1230         $scope.catalog_url = url;
1231     }
1232
1233     function init_parts_url() {
1234         $scope.parts_url = $location
1235             .absUrl()
1236             .replace(
1237                 /\/staff.*/,
1238                 '/conify/global/biblio/monograph_part?r='+$scope.record_id
1239             );
1240     }
1241
1242     $scope.set_record_tab = function(tab) {
1243         $scope.record_tab = tab;
1244
1245         switch(tab) {
1246
1247             case 'monoparts':
1248                 init_parts_url();
1249                 break;
1250
1251             case 'catalog':
1252                 init_cat_url();
1253                 break;
1254
1255             case 'holds':
1256                 $scope.detail_hold_record_id = $scope.record_id; 
1257                 // refresh the holds grid
1258                 provider.refresh();
1259                 break;
1260         }
1261     }
1262
1263     $scope.set_default_record_tab = function() {
1264         egCore.hatch.setLocalItem(
1265             'eg.cat.default_record_tab', $scope.record_tab);
1266         $timeout(function(){$scope.default_tab = $scope.record_tab});
1267     }
1268
1269     var tab;
1270     if ($scope.record_id) {
1271         $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
1272         tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
1273
1274     } else {
1275         tab = $routeParams.record_tab || 'catalog';
1276     }
1277     $scope.set_record_tab(tab);
1278
1279 }])
1280
1281 .controller('AuthorityCtrl',
1282        ['$scope','$routeParams','$location','$window','$q','egCore',
1283 function($scope , $routeParams , $location , $window , $q , egCore) {
1284
1285     // set record ID on page load if available...
1286     $scope.authority_id = $routeParams.authority_id;
1287
1288     if ($routeParams.authority_id) $scope.from_route = true;
1289     else $scope.from_route = false;
1290
1291     $scope.stop_unload = false;
1292 }])
1293
1294 .controller('URLVerifyCtrl',
1295        ['$scope','$location',
1296 function($scope , $location) {
1297     $scope.verifyurls_url = $location.absUrl().replace(/\/staff.*/, '/url_verify/sessions');
1298 }])
1299
1300 .controller('VandelayCtrl',
1301        ['$scope','$location',
1302 function($scope , $location) {
1303     $scope.vandelay_url = $location.absUrl().replace(/\/staff.*/, '/vandelay/vandelay');
1304 }])
1305
1306 .controller('ManageAuthoritiesCtrl',
1307        ['$scope','$location',
1308 function($scope , $location) {
1309     $scope.manageauthorities_url = $location.absUrl().replace(/\/staff.*/, '/cat/authority/list');
1310 }])
1311
1312 .controller('BatchEditCtrl',
1313        ['$scope','$location','$routeParams',
1314 function($scope , $location , $routeParams) {
1315     $scope.batchedit_url = $location.absUrl().replace(/\/eg.*/, '/opac/extras/merge_template');
1316     if ($routeParams.container_type) {
1317         switch ($routeParams.container_type) {
1318             case 'bucket':
1319                 $scope.batchedit_url += '?recordSource=b&containerid=' + $routeParams.container_id;
1320                 break;
1321             case 'record':
1322                 $scope.batchedit_url += '?recordSource=r&recid=' + $routeParams.container_id;
1323                 break;
1324         };
1325     }
1326 }])
1327
1328  
1329 .filter('boolText', function(){
1330     return function (v) {
1331         return v == 't';
1332     }
1333 })
1334
1335 .factory('conjoinedSvc', 
1336        ['egCore','$q',
1337 function(egCore , $q) {
1338
1339     var service = {
1340         items : [], // record search results
1341         index : 0, // search grid index
1342         rid : null
1343     };
1344
1345     service.flesh = {   
1346         flesh : 4, 
1347         flesh_fields : {
1348             bpbcm : ['target_copy','peer_type'],
1349             acp : ['call_number'],
1350             acn : ['record'],
1351             bre : ['simple_record']
1352         },
1353         // avoid fetching the MARC blob by specifying which
1354         // fields on the bre to select.  More may be needed.
1355         // note that fleshed fields are explicitly selected.
1356         select : { bre : ['id'] },
1357         order_by : { bpbcm : ['id'] },
1358     }
1359
1360     // resolved with the last received copy
1361     service.fetch = function(rid) {
1362         if (!rid && !service.rid) return $q.when();
1363
1364         if (rid) service.rid = rid;
1365         service.items = [];
1366         service.index = 0;
1367
1368         return egCore.pcrud.search(
1369             'bpbcm',
1370             {peer_record : service.rid},
1371             service.flesh,
1372             {atomic : true}
1373         ).then( function(list) { // finished
1374             service.items = list;
1375             return service.items;
1376         });
1377     }
1378
1379     // returns a promise resolved with the list of peer bib types
1380     service.get_peer_types = function() {
1381         if (egCore.env.bpt)
1382             return $q.when(egCore.env.bpt.list);
1383
1384         return egCore.pcrud.retrieveAll('bpt', null, {atomic : true})
1385         .then(function(list) {
1386             egCore.env.absorbList(list, 'bpt');
1387             return list;
1388         });
1389     };
1390
1391     return service;
1392 }])
1393
1394