]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/catalog/app.js
webstaff: Transfer volume and item actions
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / cat / catalog / app.js
1 /**
2  * TPAC Frame App
3  *
4  * currently, this app doesn't use routes for each sub-ui, because 
5  * reloading the catalog each time is sloooow.  better so far to 
6  * swap out divs w/ ng-if / ng-show / ng-hide as needed.
7  *
8  */
9
10 angular.module('egCatalogApp', ['ui.bootstrap','ngRoute','egCoreMod','egGridMod', 'egMarcMod'])
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', function(egCore,  egStartup) {
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     // create some catalog page-specific mappings
49     $routeProvider.when('/cat/catalog/record/:record_id', {
50         templateUrl: './cat/catalog/t_catalog',
51         controller: 'CatalogCtrl',
52         resolve : resolver
53     });
54
55     // create some catalog page-specific mappings
56     $routeProvider.when('/cat/catalog/record/:record_id/:record_tab', {
57         templateUrl: './cat/catalog/t_catalog',
58         controller: 'CatalogCtrl',
59         resolve : resolver
60     });
61
62     $routeProvider.when('/cat/catalog/batchEdit', {
63         templateUrl: './cat/catalog/t_batchedit',
64         controller: 'BatchEditCtrl',
65         resolve : resolver
66     });
67
68     $routeProvider.when('/cat/catalog/batchEdit/:container_type/:container_id', {
69         templateUrl: './cat/catalog/t_batchedit',
70         controller: 'BatchEditCtrl',
71         resolve : resolver
72     });
73
74     $routeProvider.when('/cat/catalog/vandelay', {
75         templateUrl: './cat/catalog/t_vandelay',
76         controller: 'VandelayCtrl',
77         resolve : resolver
78     });
79
80     $routeProvider.when('/cat/catalog/verifyURLs', {
81         templateUrl: './cat/catalog/t_verifyurls',
82         controller: 'URLVerifyCtrl',
83         resolve : resolver
84     });
85
86     $routeProvider.when('/cat/catalog/manageAuthorities', {
87         templateUrl: './cat/catalog/t_manageauthorities',
88         controller: 'ManageAuthoritiesCtrl',
89         resolve : resolver
90     });
91
92     $routeProvider.when('/cat/catalog/authority/:authority_id/marc_edit', {
93         templateUrl: './cat/catalog/t_authority',
94         controller: 'AuthorityCtrl',
95         resolve : resolver
96     });
97
98     $routeProvider.otherwise({redirectTo : '/cat/catalog/index'});
99 })
100
101
102 /**
103  * */
104 .controller('CatalogRecordRetrieve',
105        ['$scope','$routeParams','$location','$q','egCore',
106 function($scope , $routeParams , $location , $q , egCore ) {
107
108     $scope.focusMe = true;
109
110     // jump to the patron checkout UI
111     function loadRecord(record_id) {
112         $location
113         .path('/cat/catalog/record/' + record_id);
114     }
115
116     $scope.submitId = function(args) {
117         $scope.recordNotFound = null;
118         if (!args.record_id) return;
119
120         // blur so next time it's set to true it will re-apply select()
121         $scope.selectMe = false;
122
123         return loadRecord(args.record_id);
124     }
125
126     $scope.submitTCN = function(args) {
127         $scope.recordNotFound = null;
128         $scope.moreRecordsFound = null;
129         if (!args.record_tcn) return;
130
131         // blur so next time it's set to true it will re-apply select()
132         $scope.selectMe = false;
133
134         // lookup TCN
135         egCore.net.request(
136             'open-ils.search',
137             'open-ils.search.biblio.tcn',
138             args.record_tcn)
139
140         .then(function(resp) { // get_barcodes
141
142             if (evt = egCore.evt.parse(resp)) {
143                 alert(evt); // FIXME
144                 return;
145             }
146
147             if (!resp.count) {
148                 $scope.recordNotFound = args.record_tcn;
149                 $scope.selectMe = true;
150                 return;
151             }
152
153             if (resp.count > 1) {
154                 $scope.moreRecordsFound = args.record_tcn;
155                 $scope.selectMe = true;
156                 return;
157             }
158
159             var record_id = resp.ids[0];
160             return loadRecord(record_id);
161         });
162     }
163
164 }])
165
166 .controller('CatalogCtrl',
167        ['$scope','$routeParams','$location','$window','$q','egCore','egHolds','egCirc',
168         'egGridDataProvider','egHoldGridActions','$timeout','holdingsSvc',
169 function($scope , $routeParams , $location , $window , $q , egCore , egHolds , egCirc, 
170          egGridDataProvider , egHoldGridActions , $timeout , holdingsSvc) {
171
172     // set record ID on page load if available...
173     $scope.record_id = $routeParams.record_id;
174
175     if ($routeParams.record_id) $scope.from_route = true;
176     else $scope.from_route = false;
177
178     // will hold a ref to the opac iframe
179     $scope.opac_iframe = null;
180     $scope.parts_iframe = null;
181
182     $scope.in_opac_call = false;
183     $scope.opac_call = function (opac_frame_function, force_opac_tab) {
184         if ($scope.opac_iframe) {
185             if (force_opac_tab) $scope.record_tab = 'catalog';
186             $scope.in_opac_call = true;
187             $scope.opac_iframe.dom.contentWindow[opac_frame_function]();
188         }
189     }
190
191     $scope.stop_unload = false;
192     $scope.$watch('stop_unload',
193         function(newVal, oldVal) {
194             if (newVal && newVal != oldVal && $scope.opac_iframe) {
195                 $($scope.opac_iframe.dom.contentWindow).on('beforeunload', function(){
196                     return 'There is unsaved data in this record.'
197                 });
198             } else {
199                 if ($scope.opac_iframe)
200                     $($scope.opac_iframe.dom.contentWindow).off('beforeunload');
201             }
202         }
203     );
204
205     // Set the "last bib" cookie, if we have that
206     if ($scope.record_id)
207         egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
208
209     // also set it when the iframe changes to a new record
210     $scope.handle_page = function(url) {
211
212         if (!url || url == 'about:blank') {
213             // nothing loaded.  If we already have a record ID, leave it.
214             return;
215         }
216
217         var match = url.match(/\/+opac\/+record\/+(\d+)/);
218         if (match) {
219             $scope.record_id = match[1];
220             egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
221             $scope.holdings_record_id_changed($scope.record_id);
222             init_parts_url();
223         } else {
224             delete $scope.record_id;
225             $scope.from_route = false;
226         }
227
228         // child scope is executing this function, so our digest doesn't fire ... thus,
229         $scope.$apply();
230
231         if (!$scope.in_opac_call) {
232             if ($scope.record_id) {
233                 $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
234                 tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
235             } else {
236                 tab = $routeParams.record_tab || 'catalog';
237             }
238             $scope.set_record_tab(tab);
239         } else {
240             $scope.in_opac_call = false;
241         }
242     }
243
244     // xulG catalog handlers
245     $scope.handlers = { }
246
247     // ------------------------------------------------------------------
248     // Holdings
249
250     $scope.holdingsGridControls = {};
251     $scope.holdingsGridDataProvider = egGridDataProvider.instance({
252         get : function(offset, count) {
253             return this.arrayNotifier(holdingsSvc.copies, offset, count);
254         }
255     });
256
257     // refresh the list of holdings when the record_id is changed.
258     $scope.holdings_record_id_changed = function(id) {
259         if ($scope.record_id != id) $scope.record_id = id;
260         console.log('record id changed to ' + id + ', loading new holdings');
261         holdingsSvc.fetch({
262             rid : $scope.record_id,
263             org : $scope.holdings_ou,
264             copy: $scope.holdings_show_copies,
265             vol : $scope.holdings_show_vols,
266             empty: $scope.holdings_show_empty
267         }).then(function() {
268             $scope.holdingsGridDataProvider.refresh();
269         });
270     }
271
272     // refresh the list of holdings when the filter lib is changed.
273     $scope.holdings_ou = egCore.org.get(egCore.auth.user().ws_ou());
274     $scope.holdings_ou_changed = function(org) {
275         $scope.holdings_ou = org;
276         holdingsSvc.fetch({
277             rid : $scope.record_id,
278             org : $scope.holdings_ou,
279             copy: $scope.holdings_show_copies,
280             vol : $scope.holdings_show_vols,
281             empty: $scope.holdings_show_empty
282         }).then(function() {
283             $scope.holdingsGridDataProvider.refresh();
284         });
285     }
286
287     $scope.holdings_cb_changed = function(cb,newVal,norefresh) {
288         $scope[cb] = newVal;
289         egCore.hatch.setItem('cat.' + cb, newVal);
290         if (!norefresh) holdingsSvc.fetch({
291             rid : $scope.record_id,
292             org : $scope.holdings_ou,
293             copy: $scope.holdings_show_copies,
294             vol : $scope.holdings_show_vols,
295             empty: $scope.holdings_show_empty
296         }).then(function() {
297             $scope.holdingsGridDataProvider.refresh();
298         });
299     }
300
301     egCore.hatch.getItem('cat.holdings_show_vols').then(function(x){
302         if (typeof x ==  'undefined') x = true;
303         $scope.holdings_cb_changed('holdings_show_vols',x,true);
304         $('#holdings_show_vols').prop('checked', x);
305     }).then(function(){
306         egCore.hatch.getItem('cat.holdings_show_copies').then(function(x){
307             if (typeof x ==  'undefined') x = true;
308             $scope.holdings_cb_changed('holdings_show_copies',x,true);
309             $('#holdings_show_copies').prop('checked', x);
310         }).then(function(){
311             egCore.hatch.getItem('cat.holdings_show_empty').then(function(x){
312                 if (typeof x ==  'undefined') x = true;
313                 $scope.holdings_cb_changed('holdings_show_empty',x);
314                 $('#holdings_show_empty').prop('checked', x);
315             })
316         })
317     });
318
319     $scope.vols_not_shown = function () {
320         return !$scope.holdings_show_vols;
321     }
322
323     $scope.holdings_checkbox_handler = function (item) {
324         $scope.holdings_cb_changed(item.checkbox,item.checked);
325     }
326
327     function gatherSelectedHoldingsIds () {
328         var cp_id_list = [];
329         angular.forEach(
330             $scope.holdingsGridControls.selectedItems(),
331             function (item) { cp_id_list = cp_id_list.concat(item.id_list) }
332         );
333         return cp_id_list;
334     }
335
336     function gatherSelectedRawCopies () {
337         var cp_list = [];
338         angular.forEach(
339             $scope.holdingsGridControls.selectedItems(),
340             function (item) { cp_list = cp_list.concat(item.raw) }
341         );
342         return cp_list;
343     }
344
345     function gatherSelectedVolumeIds () {
346         var cn_id_list = [];
347         angular.forEach(
348             $scope.holdingsGridControls.selectedItems(),
349             function (item) {
350                 if (cn_id_list.indexOf(item.call_number.id) == -1)
351                     cn_id_list.push(item.call_number.id)
352             }
353         );
354         return cn_id_list;
355     }
356
357     spawnHoldingsEdit = function (hide_vols,hide_copies){
358         egCore.net.request(
359             'open-ils.actor',
360             'open-ils.actor.anon_cache.set_value',
361             null, 'edit-these-copies', {
362                 record_id: $scope.record_id,
363                 copies: gatherSelectedHoldingsIds(),
364                 hide_vols : hide_vols,
365                 hide_copies : hide_copies
366             }
367         ).then(function(key) {
368             if (key) {
369                 var url = egCore.env.basePath + 'cat/volcopy/' + key;
370                 $timeout(function() { $window.open(url, '_blank') });
371             } else {
372                 alert('Could not create anonymous cache key!');
373             }
374         });
375     }
376     $scope.selectedHoldingsVolCopyEdit = function () { spawnHoldingsEdit(false,false) }
377     $scope.selectedHoldingsVolEdit = function () { spawnHoldingsEdit(false,true) }
378     $scope.selectedHoldingsCopyEdit = function () { spawnHoldingsEdit(true,false) }
379
380     $scope.selectedHoldingsItemStatus = function (){
381         var url = egCore.env.basePath + 'cat/item/search/' + gatherSelectedHoldingsIds().join(',')
382         $timeout(function() { $window.open(url, '_blank') });
383     }
384
385     $scope.markVolAsItemTarget = function() {
386         if ($scope.holdingsGridControls.selectedItems()[0].call_number.id) { // cn.id missing when vols are collapsed
387             egCore.hatch.setLocalItem(
388                 'eg.cat.item_transfer_target',
389                 $scope.holdingsGridControls.selectedItems()[0].call_number.id
390             );
391             console.log('item_transfer_dest: '+$scope.holdingsGridControls.selectedItems()[0].call_number.id);
392         }
393     }
394
395     $scope.markLibAsVolTarget = function() {
396         egCore.hatch.setLocalItem(
397             'eg.cat.volume_transfer_target',
398             $scope.holdingsGridControls.selectedItems()[0].owner_id
399         );
400         console.log('vol_transfer_dest: '+$scope.holdingsGridControls.selectedItems()[0].owner_id);
401     }
402
403     $scope.selectedHoldingsItemStatusDetail = function (){
404         angular.forEach(
405             gatherSelectedHoldingsIds(),
406             function (cid) {
407                 var url = egCore.env.basePath +
408                           'cat/item/' + cid;
409                 $timeout(function() { $window.open(url, '_blank') });
410             }
411         );
412     }
413
414     $scope.transferVolumes = function (){
415         var xfer_target = egCore.hatch.getLocalItem('eg.cat.volume_transfer_target');
416
417         if (xfer_target) {
418             egCore.net.request(
419                 'open-ils.cat',
420                 'open-ils.open-ils.cat.asset.volume.batch.transfer.override',
421                 egCore.auth.token(), {
422                     docid   : $scope.record_id,
423                     lib     : xfer_target,
424                     volumes : gatherSelectedVolumeIds()
425                 }
426             ).then(function(success) {
427                 if (success) {
428                     holdingsSvc.fetch({
429                         rid : $scope.record_id,
430                         org : $scope.holdings_ou,
431                         copy: $scope.holdings_show_copies,
432                         vol : $scope.holdings_show_vols,
433                         empty: $scope.holdings_show_empty
434                     }).then(function() {
435                         $scope.holdingsGridDataProvider.refresh();
436                     });
437                 } else {
438                     alert('Could not transfer volumes!');
439                 }
440             });
441         }
442         
443     }
444
445     $scope.transferItems = function (){
446         var xfer_target = egCore.hatch.getLocalItem('eg.cat.item_transfer_target');
447         if (xfer_target) {
448             var copy_list = gatherSelectedRawCopies();
449
450             angular.forEach(copy_list, function (cp) {
451                 cp.call_number(xfer_target);
452             });
453
454             egCore.pcrud.update(
455                 copy_list
456             ).then(function(success) {
457                 if (success) {
458                     holdingsSvc.fetch({
459                         rid : $scope.record_id,
460                         org : $scope.holdings_ou,
461                         copy: $scope.holdings_show_copies,
462                         vol : $scope.holdings_show_vols,
463                         empty: $scope.holdings_show_empty
464                     }).then(function() {
465                         $scope.holdingsGridDataProvider.refresh();
466                     });
467                 } else {
468                     alert('Could not transfer items!');
469                 }
470             });
471         }
472         
473     }
474
475     $scope.selectedHoldingsItemStatusTgrEvt = function (){
476         angular.forEach(
477             gatherSelectedHoldingsIds(),
478             function (cid) {
479                 var url = egCore.env.basePath +
480                           'cat/item/' + cid + '/triggered_events';
481                 $timeout(function() { $window.open(url, '_blank') });
482             }
483         );
484     }
485
486     $scope.selectedHoldingsItemStatusHolds = function (){
487         angular.forEach(
488             gatherSelectedHoldingsIds(),
489             function (cid) {
490                 var url = egCore.env.basePath +
491                           'cat/item/' + cid + '/holds';
492                 $timeout(function() { $window.open(url, '_blank') });
493             }
494         );
495     }
496
497     $scope.selectedHoldingsDamaged = function () {
498         egCirc.mark_damaged(gatherSelectedHoldingsIds()).then(function() {
499             holdingsSvc.fetch({
500                 rid : $scope.record_id,
501                 org : $scope.holdings_ou,
502                 copy: $scope.holdings_show_copies,
503                 vol : $scope.holdings_show_vols,
504                 empty: $scope.holdings_show_empty
505             }).then(function() {
506                 $scope.holdingsGridDataProvider.refresh();
507             });
508         });
509     }
510
511     $scope.selectedHoldingsMissing = function () {
512         egCirc.mark_missing(gatherSelectedHoldingsIds()).then(function() {
513             holdingsSvc.fetch({
514                 rid : $scope.record_id,
515                 org : $scope.holdings_ou,
516                 copy: $scope.holdings_show_copies,
517                 vol : $scope.holdings_show_vols,
518                 empty: $scope.holdings_show_empty
519             }).then(function() {
520                 $scope.holdingsGridDataProvider.refresh();
521             });
522         });
523     }
524
525
526     // ------------------------------------------------------------------
527     // Holds 
528     var provider = egGridDataProvider.instance({});
529     $scope.hold_grid_data_provider = provider;
530     $scope.grid_actions = egHoldGridActions;
531     $scope.grid_actions.refresh = function () { provider.refresh() };
532     $scope.hold_grid_controls = {};
533
534     var hold_ids = []; // current list of holds
535     function fetchHolds(offset, count) {
536         var ids = hold_ids.slice(offset, offset + count);
537         return egHolds.fetch_holds(ids).then(null, null,
538             function(hold_data) { 
539                 return hold_data;
540             }
541         );
542     }
543
544     provider.get = function(offset, count) {
545         if ($scope.record_tab != 'holds') return $q.when();
546         var deferred = $q.defer();
547         hold_ids = []; // no caching ATM
548
549         // fetch the IDs
550         egCore.net.request(
551             'open-ils.circ',
552             'open-ils.circ.holds.retrieve_all_from_title',
553             egCore.auth.token(), $scope.record_id, 
554             {pickup_lib : egCore.org.descendants($scope.pickup_ou.id(), true)}
555         ).then(
556             function(hold_data) {
557                 angular.forEach(hold_data, function(list, type) {
558                     hold_ids = hold_ids.concat(list);
559                 });
560                 fetchHolds(offset, count).then(
561                     deferred.resolve, null, deferred.notify);
562             }
563         );
564
565         return deferred.promise;
566     }
567
568     $scope.detail_view = function(action, user_data, items) {
569         if (h = items[0]) {
570             $scope.detail_hold_id = h.hold.id();
571         }
572     }
573
574     $scope.list_view = function(items) {
575          $scope.detail_hold_id = null;
576     }
577
578     // refresh the list of record holds when the pickup lib is changed.
579     $scope.pickup_ou = egCore.org.get(egCore.auth.user().ws_ou());
580     $scope.pickup_ou_changed = function(org) {
581         $scope.pickup_ou = org;
582         provider.refresh();
583     }
584
585     $scope.print_holds = function() {
586         var holds = [];
587         angular.forEach($scope.hold_grid_controls.allItems(), function(item) {
588             holds.push({
589                 hold : egCore.idl.toHash(item.hold),
590                 patron_last : item.patron_last,
591                 patron_alias : item.patron_alias,
592                 patron_barcode : item.patron_barcode,
593                 copy : egCore.idl.toHash(item.copy),
594                 volume : egCore.idl.toHash(item.volume),
595                 title : item.mvr.title(),
596                 author : item.mvr.author()
597             });
598         });
599
600         egCore.print.print({
601             context : 'receipt', 
602             template : 'holds_for_bib', 
603             scope : {holds : holds}
604         });
605     }
606
607     $scope.mark_hold_transfer_dest = function() {
608         egCore.hatch.setLocalItem(
609             'eg.circ.hold.title_transfer_target', $scope.record_id);
610     }
611
612     // UI presents this option as "all holds"
613     $scope.transfer_holds_to_marked = function() {
614         var hold_ids = $scope.hold_grid_controls.allItems().map(
615             function(hold_data) {return hold_data.hold.id()});
616         egHolds.transfer_to_marked_title(hold_ids);
617     }
618
619     // ------------------------------------------------------------------
620     // Initialize the selected tab
621
622     function init_cat_url() {
623         // Set the initial catalog URL.  This only happens once.
624         // The URL is otherwise generated through user navigation.
625         if ($scope.catalog_url) return; 
626
627         var url = $location.absUrl().replace(/\/staff.*/, '/opac/advanced');
628
629         // A record ID in the path indicates a request for the record-
630         // specific page.
631         if ($routeParams.record_id) {
632             url = url.replace(/advanced/, '/record/' + $scope.record_id);
633         }
634
635         $scope.catalog_url = url;
636     }
637
638     function init_parts_url() {
639         $scope.parts_url = $location
640             .absUrl()
641             .replace(
642                 /\/staff.*/,
643                 '/conify/global/biblio/monograph_part?r='+$scope.record_id
644             );
645     }
646
647     $scope.set_record_tab = function(tab) {
648         $scope.record_tab = tab;
649
650         switch(tab) {
651
652             case 'monoparts':
653                 init_parts_url();
654                 break;
655
656             case 'catalog':
657                 init_cat_url();
658                 break;
659
660             case 'holds':
661                 $scope.detail_hold_record_id = $scope.record_id; 
662                 // refresh the holds grid
663                 provider.refresh();
664                 break;
665         }
666     }
667
668     $scope.set_default_record_tab = function() {
669         egCore.hatch.setLocalItem(
670             'eg.cat.default_record_tab', $scope.record_tab);
671         $timeout(function(){$scope.default_tab = $scope.record_tab});
672     }
673
674     var tab;
675     if ($scope.record_id) {
676         $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
677         tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
678
679     } else {
680         tab = $routeParams.record_tab || 'catalog';
681     }
682     $scope.set_record_tab(tab);
683
684 }])
685
686 .controller('AuthorityCtrl',
687        ['$scope','$routeParams','$location','$window','$q','egCore',
688 function($scope , $routeParams , $location , $window , $q , egCore) {
689
690     // set record ID on page load if available...
691     $scope.authority_id = $routeParams.authority_id;
692
693     if ($routeParams.authority_id) $scope.from_route = true;
694     else $scope.from_route = false;
695
696     $scope.stop_unload = false;
697 }])
698
699 .controller('URLVerifyCtrl',
700        ['$scope','$location',
701 function($scope , $location) {
702     $scope.verifyurls_url = $location.absUrl().replace(/\/staff.*/, '/url_verify/sessions');
703 }])
704
705 .controller('VandelayCtrl',
706        ['$scope','$location',
707 function($scope , $location) {
708     $scope.vandelay_url = $location.absUrl().replace(/\/staff.*/, '/vandelay/vandelay');
709 }])
710
711 .controller('ManageAuthoritiesCtrl',
712        ['$scope','$location',
713 function($scope , $location) {
714     $scope.manageauthorities_url = $location.absUrl().replace(/\/staff.*/, '/cat/authority/list');
715 }])
716
717 .controller('BatchEditCtrl',
718        ['$scope','$location','$routeParams',
719 function($scope , $location , $routeParams) {
720     $scope.batchedit_url = $location.absUrl().replace(/\/eg.*/, '/opac/extras/merge_template');
721     if ($routeParams.container_type) {
722         switch ($routeParams.container_type) {
723             case 'bucket':
724                 $scope.batchedit_url += '?recordSource=b&containerid=' + $routeParams.container_id;
725                 break;
726             case 'record':
727                 $scope.batchedit_url += '?recordSource=r&recid=' + $routeParams.container_id;
728                 break;
729         };
730     }
731 }])
732
733  
734 .filter('boolText', function(){
735     return function (v) {
736         return v == 't';
737     }
738 })
739
740 .factory('holdingsSvc', 
741        ['egCore','$q',
742 function(egCore , $q) {
743
744     var service = {
745         ongoing : false,
746         copies : [], // record search results
747         index : 0, // search grid index
748         org : null,
749         rid : null
750     };
751
752     service.flesh = {   
753         flesh : 2, 
754         flesh_fields : {
755             acp : ['status','location'],
756             acn : ['prefix','suffix','copies']
757         }
758     }
759
760     // resolved with the last received copy
761     service.fetch = function(opts) {
762         if (service.ongoing) {
763             console.log('Skipping fetch, ongoing = true');
764             return $q.when();
765         }
766
767         var rid = opts.rid;
768         var org = opts.org;
769         var copy = opts.copy;
770         var vol = opts.vol;
771         var empty = opts.empty;
772
773         if (!rid) return $q.when();
774         if (!org) return $q.when();
775
776         service.ongoing = true;
777
778         service.rid = rid;
779         service.org = org;
780         service.copies = [];
781         service.index = 0;
782
783         var org_list = egCore.org.descendants(org.id(), true);
784         console.log('Holdings fetch with: rid='+rid+' org='+org_list+' copy='+copy+' vol='+vol+' empty='+empty);
785
786         return egCore.pcrud.search(
787             'acn',
788             {record : rid, owning_lib : org_list, deleted : 'f'},
789             service.flesh
790         ).then(
791             function() { // finished
792                 service.copies = service.copies.sort(
793                     function (a, b) {
794                         function compare_array (x, y, i) {
795                             if (x[i] && y[i]) { // both have values
796                                 if (x[i] == y[i]) { // need to look deeper
797                                     return compare_array(x, y, ++i);
798                                 }
799
800                                 if (x[i] < y[i]) { // x is first
801                                     return -1;
802                                 } else if (x[i] > y[i]) { // y is first
803                                     return 1;
804                                 }
805
806                             } else { // no orgs to compare ...
807                                 if (x[i]) return -1;
808                                 if (y[i]) return 1;
809                             }
810                             return 0;
811                         }
812
813                         var owner_order = compare_array(a.owner_list, b.owner_list, 0);
814                         if (!owner_order) {
815                             // now compare on CN label
816                             if (a.call_number.label < b.call_number.label) return -1;
817                             if (a.call_number.label > b.call_number.label) return 1;
818
819                             // try copy number
820                             if (a.copy_number < b.copy_number) return -1;
821                             if (a.copy_number > b.copy_number) return 1;
822
823                             // finally, barcode
824                             if (a.barcode < b.barcode) return -1;
825                             if (a.barcode > b.barcode) return 1;
826                         }
827                         return owner_order;
828                     }
829                 );
830
831                 // create a label using just the unique part of the owner list
832                 var index = 0;
833                 var prev_owner_list;
834                 angular.forEach(service.copies, function (cp) {
835                     if (!prev_owner_list) {
836                         cp.owner_label = cp.owner_list.join(' ... ');
837                     } else {
838                         var current_owner_list = cp.owner_list.slice();
839                         while (current_owner_list[1] && prev_owner_list[1] && current_owner_list[0] == prev_owner_list[0]) {
840                             current_owner_list.shift();
841                             prev_owner_list.shift();
842                         }
843                         cp.owner_label = current_owner_list.join(' ... ');
844                     }
845
846                     cp.index = index++;
847                     prev_owner_list = cp.owner_list.slice();
848                 });
849
850                 var new_list = service.copies;
851                 if (!copy || !vol) { // collapse copy rows, supply a count instead
852
853                     index = 0;
854                     var cp_list = [];
855                     var prev_key;
856                     var current_blob = {};
857                     angular.forEach(new_list, function (cp) {
858                         if (!prev_key) {
859                             prev_key = cp.owner_list.join('') + cp.call_number.label;
860                             if (cp.barcode) current_blob.copy_count = 1;
861                             current_blob.index = index++;
862                             current_blob.id_list = cp.id_list;
863                             current_blob.raw = cp.raw;
864                             current_blob.call_number = cp.call_number;
865                             current_blob.owner_list = cp.owner_list;
866                             current_blob.owner_label = cp.owner_label;
867                         } else {
868                             var current_key = cp.owner_list.join('') + cp.call_number.label;
869                             if (prev_key == current_key) { // collapse into current_blob
870                                 current_blob.copy_count++;
871                                 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
872                                 current_blob.raw = current_blob.raw.concat(cp.raw);
873                             } else {
874                                 current_blob.barcode = current_blob.copy_count;
875                                 cp_list.push(current_blob);
876                                 prev_key = current_key;
877                                 current_blob = {};
878                                 if (cp.barcode) current_blob.copy_count = 1;
879                                 current_blob.index = index++;
880                                 current_blob.id_list = cp.id_list;
881                                 current_blob.raw = cp.raw;
882                                 current_blob.owner_label = cp.owner_label;
883                                 current_blob.call_number = cp.call_number;
884                                 current_blob.owner_list = cp.owner_list;
885                             }
886                         }
887                     });
888
889                     current_blob.barcode = current_blob.copy_count;
890                     cp_list.push(current_blob);
891                     new_list = cp_list;
892
893                     if (!vol) { // do the same for vol rows
894
895                         index = 0;
896                         var cn_list = [];
897                         prev_key = '';
898                         var current_blob = {};
899                         angular.forEach(cp_list, function (cp) {
900                             if (!prev_key) {
901                                 prev_key = cp.owner_list.join('');
902                                 current_blob.index = index++;
903                                 current_blob.id_list = cp.id_list;
904                                 current_blob.raw = cp.raw;
905                                 current_blob.cn_count = 1;
906                                 current_blob.copy_count = cp.copy_count;
907                                 current_blob.owner_list = cp.owner_list;
908                                 current_blob.owner_label = cp.owner_label;
909                             } else {
910                                 var current_key = cp.owner_list.join('');
911                                 if (prev_key == current_key) { // collapse into current_blob
912                                     current_blob.cn_count++;
913                                     current_blob.copy_count += cp.copy_count;
914                                     current_blob.id_list = current_blob.id_list.concat(cp.id_list);
915                                     current_blob.raw = current_blob.raw.concat(cp.raw);
916                                 } else {
917                                     current_blob.barcode = current_blob.copy_count;
918                                     current_blob.call_number = { label : current_blob.cn_count };
919                                     cn_list.push(current_blob);
920                                     prev_key = current_key;
921                                     current_blob = {};
922                                     current_blob.index = index++;
923                                     current_blob.id_list = cp.id_list;
924                                     current_blob.raw = cp.raw;
925                                     current_blob.owner_label = cp.owner_label;
926                                     current_blob.cn_count = 1;
927                                     current_blob.copy_count = cp.copy_count;
928                                     current_blob.owner_list = cp.owner_list;
929                                 }
930                             }
931                         });
932     
933                         current_blob.barcode = current_blob.copy_count;
934                         current_blob.call_number = { label : current_blob.cn_count };
935                         cn_list.push(current_blob);
936                         new_list = cn_list;
937     
938                     }
939                 }
940
941                 service.copies = new_list;
942                 service.ongoing = false;
943             },
944
945             null, // error
946
947             // notify reads the stream of copies, one at a time.
948             function(cn) {
949
950                 var copies = cn.copies();
951                 cn.copies([]);
952
953                 angular.forEach(copies, function (cp) {
954                     cp.call_number(cn);
955                 });
956
957                 var owner_id = cn.owning_lib();
958                 var owner = egCore.org.get(owner_id);
959
960                 var owner_name_list = [];
961                 while (owner.parent_ou()) { // we're going to skip the top of the tree...
962                     owner_name_list.unshift(owner.name());
963                     owner = egCore.org.get(owner.parent_ou());
964                 }
965
966                 if (copies[0]) {
967                     var flat = [];
968                     angular.forEach(copies, function (cp) {
969                         var flat_cp = egCore.idl.toHash(cp);
970                         flat_cp.owner_id = owner_id;
971                         flat_cp.owner_list = owner_name_list;
972                         flat_cp.id_list = [flat_cp.id];
973                         flat_cp.raw = [cp];
974                         flat.push(flat_cp);
975                     });
976
977                     service.copies = service.copies.concat(flat);
978                 } else if (empty) {
979                     service.copies.push({
980                         owner_id   : owner_id,
981                         owner_list : owner_name_list,
982                         call_number: egCore.idl.toHash(cn)
983                     });
984                 }
985
986                 return cn;
987             }
988         );
989     }
990
991     return service;
992 }])
993
994