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