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