]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/catalog/app.js
webstaff: fix saving copy price in vol/copy editor
[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 gatherSelectedVolumeIds () {
800         var cn_id_list = [];
801         angular.forEach(
802             $scope.holdingsGridControls.selectedItems(),
803             function (item) {
804                 if (cn_id_list.indexOf(item.call_number.id) == -1)
805                     cn_id_list.push(item.call_number.id)
806             }
807         );
808         return cn_id_list;
809     }
810
811     $scope.selectedHoldingsDelete = function (vols, copies) {
812
813         var cnHash = {};
814         var perCnCopies = {};
815
816         var cn_count = 0;
817         var cp_count = 0;
818
819         angular.forEach(
820             $scope.holdingsGridControls.selectedItems(),
821             function (item) {
822                 if (vols && item.raw_call_number) {
823                     cnHash[item.call_number.id] = egCore.idl.Clone(item.raw_call_number);
824                     cnHash[item.call_number.id].isdeleted(1);
825                     cn_count++;
826                 } else if (copies) {
827                     angular.forEach(egCore.idl.Clone(item.raw), function (cp) {
828                         cp.isdeleted(1);
829                         cp_count++;
830                         var cn_id = cp.call_number().id();
831                         if (!cnHash[cn_id]) {
832                             cnHash[cn_id] = cp.call_number();
833                             perCnCopies[cn_id] = [cp];
834                         } else {
835                             perCnCopies[cn_id].push(cp);
836                         }
837                         cp.call_number(cn_id); // prevent loops in JSON-ification
838                     });
839
840                 }
841             }
842         );
843
844         angular.forEach(perCnCopies, function (v, k) {
845             if (vols) {
846                 cnHash[k].isdeleted(1);
847                 cn_count++;
848             }
849             cnHash[k].copies(v);
850         });
851
852         cnList = [];
853         angular.forEach(cnHash, function (v, k) {
854             cnList.push(v);
855         });
856
857         if (cnList.length == 0) return;
858
859         egConfirmDialog.open(
860             egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES,
861             egCore.strings.CONFIRM_DELETE_COPIES_VOLUMES_MESSAGE,
862             {copies : cp_count, volumes : cn_count}
863         ).result.then(function() {
864             egCore.net.request(
865                 'open-ils.cat',
866                 'open-ils.cat.asset.volume.fleshed.batch.update.override',
867                 egCore.auth.token(), cnList, 1, {}
868             ).then(function(update_count) {
869                 $scope.holdingsGridDataProvider.refresh();
870             });
871         });
872     }
873     $scope.selectedHoldingsCopyDelete = function () { $scope.selectedHoldingsDelete(false,true) }
874     $scope.selectedHoldingsVolCopyDelete = function () { $scope.selectedHoldingsDelete(true,true) }
875     $scope.selectedHoldingsEmptyVolCopyDelete = function () { $scope.selectedHoldingsDelete(true,false) }
876
877     spawnHoldingsAdd = function (vols,copies){
878         var raw = [];
879         if (copies) { // just a copy on existing volumes
880             angular.forEach(gatherSelectedVolumeIds(), function (v) {
881                 raw.push( {callnumber : v} );
882             });
883         } else if (vols) {
884             angular.forEach(
885                 $scope.holdingsGridControls.selectedItems(),
886                 function (item) {
887                     raw.push({owner : item.owner_id});
888                 }
889             );
890         }
891
892         if (raw.length == 0) raw.push({});
893
894         egCore.net.request(
895             'open-ils.actor',
896             'open-ils.actor.anon_cache.set_value',
897             null, 'edit-these-copies', {
898                 record_id: $scope.record_id,
899                 raw: raw,
900                 hide_vols : false,
901                 hide_copies : false
902             }
903         ).then(function(key) {
904             if (key) {
905                 var url = egCore.env.basePath + 'cat/volcopy/' + key;
906                 $timeout(function() { $window.open(url, '_blank') });
907             } else {
908                 alert('Could not create anonymous cache key!');
909             }
910         });
911     }
912     $scope.selectedHoldingsVolCopyAdd = function () { spawnHoldingsAdd(true,false) }
913     $scope.selectedHoldingsCopyAdd = function () { spawnHoldingsAdd(false,true) }
914
915     spawnHoldingsEdit = function (hide_vols,hide_copies){
916         egCore.net.request(
917             'open-ils.actor',
918             'open-ils.actor.anon_cache.set_value',
919             null, 'edit-these-copies', {
920                 record_id: $scope.record_id,
921                 copies: gatherSelectedHoldingsIds(),
922                 hide_vols : hide_vols,
923                 hide_copies : hide_copies
924             }
925         ).then(function(key) {
926             if (key) {
927                 var url = egCore.env.basePath + 'cat/volcopy/' + key;
928                 $timeout(function() { $window.open(url, '_blank') });
929             } else {
930                 alert('Could not create anonymous cache key!');
931             }
932         });
933     }
934     $scope.selectedHoldingsVolCopyEdit = function () { spawnHoldingsEdit(false,false) }
935     $scope.selectedHoldingsVolEdit = function () { spawnHoldingsEdit(false,true) }
936     $scope.selectedHoldingsCopyEdit = function () { spawnHoldingsEdit(true,false) }
937
938     $scope.selectedHoldingsItemStatus = function (){
939         var url = egCore.env.basePath + 'cat/item/search/' + gatherSelectedHoldingsIds().join(',')
940         $timeout(function() { $window.open(url, '_blank') });
941     }
942
943     $scope.markVolAsItemTarget = function() {
944         if ($scope.holdingsGridControls.selectedItems()[0].call_number.id) { // cn.id missing when vols are collapsed
945             egCore.hatch.setLocalItem(
946                 'eg.cat.item_transfer_target',
947                 $scope.holdingsGridControls.selectedItems()[0].call_number.id
948             );
949         }
950     }
951
952     $scope.markLibAsVolTarget = function() {
953         egCore.hatch.setLocalItem(
954             'eg.cat.volume_transfer_target',
955             $scope.holdingsGridControls.selectedItems()[0].owner_id
956         );
957     }
958
959     $scope.selectedHoldingsItemStatusDetail = function (){
960         angular.forEach(
961             gatherSelectedHoldingsIds(),
962             function (cid) {
963                 var url = egCore.env.basePath +
964                           'cat/item/' + cid;
965                 $timeout(function() { $window.open(url, '_blank') });
966             }
967         );
968     }
969
970     $scope.transferVolumes = function (){
971         var xfer_target = egCore.hatch.getLocalItem('eg.cat.volume_transfer_target');
972
973         if (xfer_target) {
974             egCore.net.request(
975                 'open-ils.cat',
976                 'open-ils.open-ils.cat.asset.volume.batch.transfer.override',
977                 egCore.auth.token(), {
978                     docid   : $scope.record_id,
979                     lib     : xfer_target,
980                     volumes : gatherSelectedVolumeIds()
981                 }
982             ).then(function(success) {
983                 if (success) {
984                     holdingsSvcInst.fetchAgain().then(function() {
985                         $scope.holdingsGridDataProvider.refresh();
986                     });
987                 } else {
988                     alert('Could not transfer volumes!');
989                 }
990             });
991         }
992         
993     }
994
995     $scope.transferItems = function (){
996         var xfer_target = egCore.hatch.getLocalItem('eg.cat.item_transfer_target');
997         var copy_ids = gatherSelectedHoldingsIds();
998         if (xfer_target && copy_ids.length > 0) {
999             egCore.net.request(
1000                 'open-ils.cat',
1001                 'open-ils.cat.transfer_copies_to_volume',
1002                 egCore.auth.token(),
1003                 xfer_target,
1004                 copy_ids
1005             ).then(
1006                 function(resp) { // oncomplete
1007                     var evt = egCore.evt.parse(resp);
1008                     if (evt) {
1009                         egConfirmDialog.open(
1010                             egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_TITLE,
1011                             egCore.strings.OVERRIDE_TRANSFER_COPIES_TO_MARKED_VOLUME_BODY,
1012                             {'evt_desc': evt.desc}
1013                         ).result.then(function() {
1014                             egCore.net.request(
1015                                 'open-ils.cat',
1016                                 'open-ils.cat.transfer_copies_to_volume.override',
1017                                 egCore.auth.token(),
1018                                 xfer_target,
1019                                 copy_ids,
1020                                 { events: ['TITLE_LAST_COPY', 'COPY_DELETE_WARNING'] }
1021                             ).then(function(resp) {
1022                                 holdingsSvcInst.fetchAgain().then(function() {
1023                                     $scope.holdingsGridDataProvider.refresh();
1024                                 });
1025                             });
1026                         });
1027                     } else {
1028                         holdingsSvcInst.fetchAgain().then(function() {
1029                             $scope.holdingsGridDataProvider.refresh();
1030                         });
1031                     }
1032                 },
1033                 null, // onerror
1034                 null // onprogress
1035             )
1036         }
1037     }
1038
1039     $scope.selectedHoldingsItemStatusTgrEvt = function (){
1040         angular.forEach(
1041             gatherSelectedHoldingsIds(),
1042             function (cid) {
1043                 var url = egCore.env.basePath +
1044                           'cat/item/' + cid + '/triggered_events';
1045                 $timeout(function() { $window.open(url, '_blank') });
1046             }
1047         );
1048     }
1049
1050     $scope.selectedHoldingsItemStatusHolds = function (){
1051         angular.forEach(
1052             gatherSelectedHoldingsIds(),
1053             function (cid) {
1054                 var url = egCore.env.basePath +
1055                           'cat/item/' + cid + '/holds';
1056                 $timeout(function() { $window.open(url, '_blank') });
1057             }
1058         );
1059     }
1060
1061     $scope.selectedHoldingsDamaged = function () {
1062         egCirc.mark_damaged(gatherSelectedHoldingsIds()).then(function() {
1063             holdingsSvcInst.fetchAgain().then(function() {
1064                 $scope.holdingsGridDataProvider.refresh();
1065             });
1066         });
1067     }
1068
1069     $scope.selectedHoldingsMissing = function () {
1070         egCirc.mark_missing(gatherSelectedHoldingsIds()).then(function() {
1071             holdingsSvcInst.fetchAgain().then(function() {
1072                 $scope.holdingsGridDataProvider.refresh();
1073             });
1074         });
1075     }
1076
1077     $scope.attach_to_peer_bib = function() {
1078         var copy_list = gatherSelectedHoldingsIds();
1079         if (copy_list.length == 0) return;
1080
1081         egCore.hatch.getItem('eg.cat.marked_conjoined_record').then(function(target_record) {
1082             if (!target_record) return;
1083
1084             return $modal.open({
1085                 templateUrl: './cat/catalog/t_conjoined_selector',
1086                 animation: true,
1087                 controller:
1088                        ['$scope','$modalInstance',
1089                 function($scope , $modalInstance) {
1090                     $scope.update = false;
1091
1092                     $scope.peer_type = null;
1093                     $scope.peer_type_list = [];
1094                     conjoinedSvc.get_peer_types().then(function(list){
1095                         $scope.peer_type_list = list;
1096                     });
1097     
1098                     $scope.ok = function(type) {
1099                         var promises = [];
1100     
1101                         angular.forEach(copy_list, function (cp) {
1102                             var n = new egCore.idl.bpbcm();
1103                             n.isnew(true);
1104                             n.peer_record(target_record);
1105                             n.target_copy(cp);
1106                             n.peer_type(type);
1107                             promises.push(egCore.pcrud.create(n));
1108                         });
1109     
1110                         return $q.all(promises).then(function(){$modalInstance.close()});
1111                     }
1112     
1113                     $scope.cancel = function($event) {
1114                         $modalInstance.dismiss();
1115                         $event.preventDefault();
1116                     }
1117                 }]
1118             });
1119         });
1120     }
1121
1122
1123     // ------------------------------------------------------------------
1124     // Holds 
1125     var provider = egGridDataProvider.instance({});
1126     $scope.hold_grid_data_provider = provider;
1127     $scope.grid_actions = egHoldGridActions;
1128     $scope.grid_actions.refresh = function () { provider.refresh() };
1129     $scope.hold_grid_controls = {};
1130
1131     var hold_ids = []; // current list of holds
1132     function fetchHolds(offset, count) {
1133         var ids = hold_ids.slice(offset, offset + count);
1134         return egHolds.fetch_holds(ids).then(null, null,
1135             function(hold_data) { 
1136                 return hold_data;
1137             }
1138         );
1139     }
1140
1141     provider.get = function(offset, count) {
1142         if ($scope.record_tab != 'holds') return $q.when();
1143         var deferred = $q.defer();
1144         hold_ids = []; // no caching ATM
1145
1146         // fetch the IDs
1147         egCore.net.request(
1148             'open-ils.circ',
1149             'open-ils.circ.holds.retrieve_all_from_title',
1150             egCore.auth.token(), $scope.record_id, 
1151             {pickup_lib : egCore.org.descendants($scope.pickup_ou.id(), true)}
1152         ).then(
1153             function(hold_data) {
1154                 angular.forEach(hold_data, function(list, type) {
1155                     hold_ids = hold_ids.concat(list);
1156                 });
1157                 fetchHolds(offset, count).then(
1158                     deferred.resolve, null, deferred.notify);
1159             }
1160         );
1161
1162         return deferred.promise;
1163     }
1164
1165     $scope.detail_view = function(action, user_data, items) {
1166         if (h = items[0]) {
1167             $scope.detail_hold_id = h.hold.id();
1168         }
1169     }
1170
1171     $scope.list_view = function(items) {
1172          $scope.detail_hold_id = null;
1173     }
1174
1175     // refresh the list of record holds when the pickup lib is changed.
1176     $scope.pickup_ou = egCore.org.get(egCore.auth.user().ws_ou());
1177     $scope.pickup_ou_changed = function(org) {
1178         $scope.pickup_ou = org;
1179         provider.refresh();
1180     }
1181
1182     $scope.print_holds = function() {
1183         var holds = [];
1184         angular.forEach($scope.hold_grid_controls.allItems(), function(item) {
1185             holds.push({
1186                 hold : egCore.idl.toHash(item.hold),
1187                 patron_last : item.patron_last,
1188                 patron_alias : item.patron_alias,
1189                 patron_barcode : item.patron_barcode,
1190                 copy : egCore.idl.toHash(item.copy),
1191                 volume : egCore.idl.toHash(item.volume),
1192                 title : item.mvr.title(),
1193                 author : item.mvr.author()
1194             });
1195         });
1196
1197         egCore.print.print({
1198             context : 'receipt', 
1199             template : 'holds_for_bib', 
1200             scope : {holds : holds}
1201         });
1202     }
1203
1204     $scope.mark_hold_transfer_dest = function() {
1205         egCore.hatch.setLocalItem(
1206             'eg.circ.hold.title_transfer_target', $scope.record_id);
1207     }
1208
1209     // UI presents this option as "all holds"
1210     $scope.transfer_holds_to_marked = function() {
1211         var hold_ids = $scope.hold_grid_controls.allItems().map(
1212             function(hold_data) {return hold_data.hold.id()});
1213         egHolds.transfer_to_marked_title(hold_ids);
1214     }
1215
1216     // ------------------------------------------------------------------
1217     // Initialize the selected tab
1218
1219     function init_cat_url() {
1220         // Set the initial catalog URL.  This only happens once.
1221         // The URL is otherwise generated through user navigation.
1222         if ($scope.catalog_url) return; 
1223
1224         var url = $location.absUrl().replace(/\/staff.*/, '/opac/advanced');
1225
1226         // A record ID in the path indicates a request for the record-
1227         // specific page.
1228         if ($routeParams.record_id) {
1229             url = url.replace(/advanced/, '/record/' + $scope.record_id);
1230         }
1231
1232         $scope.catalog_url = url;
1233     }
1234
1235     function init_parts_url() {
1236         $scope.parts_url = $location
1237             .absUrl()
1238             .replace(
1239                 /\/staff.*/,
1240                 '/conify/global/biblio/monograph_part?r='+$scope.record_id
1241             );
1242     }
1243
1244     $scope.set_record_tab = function(tab) {
1245         $scope.record_tab = tab;
1246
1247         switch(tab) {
1248
1249             case 'monoparts':
1250                 init_parts_url();
1251                 break;
1252
1253             case 'catalog':
1254                 init_cat_url();
1255                 break;
1256
1257             case 'holds':
1258                 $scope.detail_hold_record_id = $scope.record_id; 
1259                 // refresh the holds grid
1260                 provider.refresh();
1261                 break;
1262         }
1263     }
1264
1265     $scope.set_default_record_tab = function() {
1266         egCore.hatch.setLocalItem(
1267             'eg.cat.default_record_tab', $scope.record_tab);
1268         $timeout(function(){$scope.default_tab = $scope.record_tab});
1269     }
1270
1271     var tab;
1272     if ($scope.record_id) {
1273         $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
1274         tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
1275
1276     } else {
1277         tab = $routeParams.record_tab || 'catalog';
1278     }
1279     $scope.set_record_tab(tab);
1280
1281 }])
1282
1283 .controller('AuthorityCtrl',
1284        ['$scope','$routeParams','$location','$window','$q','egCore',
1285 function($scope , $routeParams , $location , $window , $q , egCore) {
1286
1287     // set record ID on page load if available...
1288     $scope.authority_id = $routeParams.authority_id;
1289
1290     if ($routeParams.authority_id) $scope.from_route = true;
1291     else $scope.from_route = false;
1292
1293     $scope.stop_unload = false;
1294 }])
1295
1296 .controller('URLVerifyCtrl',
1297        ['$scope','$location',
1298 function($scope , $location) {
1299     $scope.verifyurls_url = $location.absUrl().replace(/\/staff.*/, '/url_verify/sessions');
1300 }])
1301
1302 .controller('VandelayCtrl',
1303        ['$scope','$location',
1304 function($scope , $location) {
1305     $scope.vandelay_url = $location.absUrl().replace(/\/staff.*/, '/vandelay/vandelay');
1306 }])
1307
1308 .controller('ManageAuthoritiesCtrl',
1309        ['$scope','$location',
1310 function($scope , $location) {
1311     $scope.manageauthorities_url = $location.absUrl().replace(/\/staff.*/, '/cat/authority/list');
1312 }])
1313
1314 .controller('BatchEditCtrl',
1315        ['$scope','$location','$routeParams',
1316 function($scope , $location , $routeParams) {
1317     $scope.batchedit_url = $location.absUrl().replace(/\/eg.*/, '/opac/extras/merge_template');
1318     if ($routeParams.container_type) {
1319         switch ($routeParams.container_type) {
1320             case 'bucket':
1321                 $scope.batchedit_url += '?recordSource=b&containerid=' + $routeParams.container_id;
1322                 break;
1323             case 'record':
1324                 $scope.batchedit_url += '?recordSource=r&recid=' + $routeParams.container_id;
1325                 break;
1326         };
1327     }
1328 }])
1329
1330  
1331 .filter('boolText', function(){
1332     return function (v) {
1333         return v == 't';
1334     }
1335 })
1336
1337 .factory('conjoinedSvc', 
1338        ['egCore','$q',
1339 function(egCore , $q) {
1340
1341     var service = {
1342         items : [], // record search results
1343         index : 0, // search grid index
1344         rid : null
1345     };
1346
1347     service.flesh = {   
1348         flesh : 4, 
1349         flesh_fields : {
1350             bpbcm : ['target_copy','peer_type'],
1351             acp : ['call_number'],
1352             acn : ['record'],
1353             bre : ['simple_record']
1354         },
1355         // avoid fetching the MARC blob by specifying which
1356         // fields on the bre to select.  More may be needed.
1357         // note that fleshed fields are explicitly selected.
1358         select : { bre : ['id'] },
1359         order_by : { bpbcm : ['id'] },
1360     }
1361
1362     // resolved with the last received copy
1363     service.fetch = function(rid) {
1364         if (!rid && !service.rid) return $q.when();
1365
1366         if (rid) service.rid = rid;
1367         service.items = [];
1368         service.index = 0;
1369
1370         return egCore.pcrud.search(
1371             'bpbcm',
1372             {peer_record : service.rid},
1373             service.flesh,
1374             {atomic : true}
1375         ).then( function(list) { // finished
1376             service.items = list;
1377             return service.items;
1378         });
1379     }
1380
1381     // returns a promise resolved with the list of peer bib types
1382     service.get_peer_types = function() {
1383         if (egCore.env.bpt)
1384             return $q.when(egCore.env.bpt.list);
1385
1386         return egCore.pcrud.retrieveAll('bpt', null, {atomic : true})
1387         .then(function(list) {
1388             egCore.env.absorbList(list, 'bpt');
1389             return list;
1390         });
1391     };
1392
1393     return service;
1394 }])
1395
1396