2608418a1e871bb45dfc3190d042731951e17be1
[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     $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',
230         'egGridDataProvider','egHoldGridActions','$timeout','holdingsSvc',
231 function($scope , $routeParams , $location , $window , $q , egCore , egHolds , egCirc, 
232          egGridDataProvider , egHoldGridActions , $timeout , holdingsSvc) {
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.stop_unload = false;
254     $scope.$watch('stop_unload',
255         function(newVal, oldVal) {
256             if (newVal && newVal != oldVal && $scope.opac_iframe) {
257                 $($scope.opac_iframe.dom.contentWindow).on('beforeunload', function(){
258                     return 'There is unsaved data in this record.'
259                 });
260             } else {
261                 if ($scope.opac_iframe)
262                     $($scope.opac_iframe.dom.contentWindow).off('beforeunload');
263             }
264         }
265     );
266
267     // Set the "last bib" cookie, if we have that
268     if ($scope.record_id)
269         egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
270
271     // also set it when the iframe changes to a new record
272     $scope.handle_page = function(url) {
273
274         if (!url || url == 'about:blank') {
275             // nothing loaded.  If we already have a record ID, leave it.
276             return;
277         }
278
279         var match = url.match(/\/+opac\/+record\/+(\d+)/);
280         if (match) {
281             $scope.record_id = match[1];
282             egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
283             $scope.holdings_record_id_changed($scope.record_id);
284             init_parts_url();
285         } else {
286             delete $scope.record_id;
287             $scope.from_route = false;
288         }
289
290         // child scope is executing this function, so our digest doesn't fire ... thus,
291         $scope.$apply();
292
293         if (!$scope.in_opac_call) {
294             if ($scope.record_id) {
295                 $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
296                 tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
297             } else {
298                 tab = $routeParams.record_tab || 'catalog';
299             }
300             $scope.set_record_tab(tab);
301         } else {
302             $scope.in_opac_call = false;
303         }
304     }
305
306     // xulG catalog handlers
307     $scope.handlers = { }
308
309     // ------------------------------------------------------------------
310     // Holdings
311
312     $scope.holdingsGridControls = {};
313     $scope.holdingsGridDataProvider = egGridDataProvider.instance({
314         get : function(offset, count) {
315             return this.arrayNotifier(holdingsSvc.copies, offset, count);
316         }
317     });
318
319     // refresh the list of holdings when the record_id is changed.
320     $scope.holdings_record_id_changed = function(id) {
321         if ($scope.record_id != id) $scope.record_id = id;
322         console.log('record id changed to ' + id + ', loading new holdings');
323         holdingsSvc.fetch({
324             rid : $scope.record_id,
325             org : $scope.holdings_ou,
326             copy: $scope.holdings_show_copies,
327             vol : $scope.holdings_show_vols,
328             empty: $scope.holdings_show_empty
329         }).then(function() {
330             $scope.holdingsGridDataProvider.refresh();
331         });
332     }
333
334     // refresh the list of holdings when the filter lib is changed.
335     $scope.holdings_ou = egCore.org.get(egCore.auth.user().ws_ou());
336     $scope.holdings_ou_changed = function(org) {
337         $scope.holdings_ou = org;
338         holdingsSvc.fetch({
339             rid : $scope.record_id,
340             org : $scope.holdings_ou,
341             copy: $scope.holdings_show_copies,
342             vol : $scope.holdings_show_vols,
343             empty: $scope.holdings_show_empty
344         }).then(function() {
345             $scope.holdingsGridDataProvider.refresh();
346         });
347     }
348
349     $scope.holdings_cb_changed = function(cb,newVal,norefresh) {
350         $scope[cb] = newVal;
351         egCore.hatch.setItem('cat.' + cb, newVal);
352         if (!norefresh) holdingsSvc.fetch({
353             rid : $scope.record_id,
354             org : $scope.holdings_ou,
355             copy: $scope.holdings_show_copies,
356             vol : $scope.holdings_show_vols,
357             empty: $scope.holdings_show_empty
358         }).then(function() {
359             $scope.holdingsGridDataProvider.refresh();
360         });
361     }
362
363     egCore.hatch.getItem('cat.holdings_show_vols').then(function(x){
364         if (typeof x ==  'undefined') x = true;
365         $scope.holdings_cb_changed('holdings_show_vols',x,true);
366         $('#holdings_show_vols').prop('checked', x);
367     }).then(function(){
368         egCore.hatch.getItem('cat.holdings_show_copies').then(function(x){
369             if (typeof x ==  'undefined') x = true;
370             $scope.holdings_cb_changed('holdings_show_copies',x,true);
371             $('#holdings_show_copies').prop('checked', x);
372         }).then(function(){
373             egCore.hatch.getItem('cat.holdings_show_empty').then(function(x){
374                 if (typeof x ==  'undefined') x = true;
375                 $scope.holdings_cb_changed('holdings_show_empty',x);
376                 $('#holdings_show_empty').prop('checked', x);
377             })
378         })
379     });
380
381     $scope.vols_not_shown = function () {
382         return !$scope.holdings_show_vols;
383     }
384
385     $scope.holdings_checkbox_handler = function (item) {
386         $scope.holdings_cb_changed(item.checkbox,item.checked);
387     }
388
389     function gatherSelectedHoldingsIds () {
390         var cp_id_list = [];
391         angular.forEach(
392             $scope.holdingsGridControls.selectedItems(),
393             function (item) { cp_id_list = cp_id_list.concat(item.id_list) }
394         );
395         return cp_id_list;
396     }
397
398     function gatherSelectedRawCopies () {
399         var cp_list = [];
400         angular.forEach(
401             $scope.holdingsGridControls.selectedItems(),
402             function (item) { cp_list = cp_list.concat(item.raw) }
403         );
404         return cp_list;
405     }
406
407     function gatherSelectedVolumeIds () {
408         var cn_id_list = [];
409         angular.forEach(
410             $scope.holdingsGridControls.selectedItems(),
411             function (item) {
412                 if (cn_id_list.indexOf(item.call_number.id) == -1)
413                     cn_id_list.push(item.call_number.id)
414             }
415         );
416         return cn_id_list;
417     }
418
419     spawnHoldingsAdd = function (hide_vols,hide_copies){
420         var raw = [];
421         if (hide_vols) { // just a copy on existing volumes
422             angular.forEach(gatherSelectedVolumeIds(), function (v) {
423                 raw.push( {callnumber : v} );
424             });
425         } else {
426             angular.forEach(
427                 $scope.holdingsGridControls.selectedItems(),
428                 function (item) {
429                     raw.push({owner : item.owner_id});
430                 }
431             );
432         }
433
434         egCore.net.request(
435             'open-ils.actor',
436             'open-ils.actor.anon_cache.set_value',
437             null, 'edit-these-copies', {
438                 record_id: $scope.record_id,
439                 raw: raw,
440                 hide_vols : hide_vols,
441                 hide_copies : hide_copies
442             }
443         ).then(function(key) {
444             if (key) {
445                 var url = egCore.env.basePath + 'cat/volcopy/' + key;
446                 $timeout(function() { $window.open(url, '_blank') });
447             } else {
448                 alert('Could not create anonymous cache key!');
449             }
450         });
451     }
452     $scope.selectedHoldingsVolCopyAdd = function () { spawnHoldingsAdd(false,false) }
453     $scope.selectedHoldingsCopyAdd = function () { spawnHoldingsAdd(true,false) }
454
455     spawnHoldingsEdit = function (hide_vols,hide_copies){
456         egCore.net.request(
457             'open-ils.actor',
458             'open-ils.actor.anon_cache.set_value',
459             null, 'edit-these-copies', {
460                 record_id: $scope.record_id,
461                 copies: gatherSelectedHoldingsIds(),
462                 hide_vols : hide_vols,
463                 hide_copies : hide_copies
464             }
465         ).then(function(key) {
466             if (key) {
467                 var url = egCore.env.basePath + 'cat/volcopy/' + key;
468                 $timeout(function() { $window.open(url, '_blank') });
469             } else {
470                 alert('Could not create anonymous cache key!');
471             }
472         });
473     }
474     $scope.selectedHoldingsVolCopyEdit = function () { spawnHoldingsEdit(false,false) }
475     $scope.selectedHoldingsVolEdit = function () { spawnHoldingsEdit(false,true) }
476     $scope.selectedHoldingsCopyEdit = function () { spawnHoldingsEdit(true,false) }
477
478     $scope.selectedHoldingsItemStatus = function (){
479         var url = egCore.env.basePath + 'cat/item/search/' + gatherSelectedHoldingsIds().join(',')
480         $timeout(function() { $window.open(url, '_blank') });
481     }
482
483     $scope.markVolAsItemTarget = function() {
484         if ($scope.holdingsGridControls.selectedItems()[0].call_number.id) { // cn.id missing when vols are collapsed
485             egCore.hatch.setLocalItem(
486                 'eg.cat.item_transfer_target',
487                 $scope.holdingsGridControls.selectedItems()[0].call_number.id
488             );
489             console.log('item_transfer_dest: '+$scope.holdingsGridControls.selectedItems()[0].call_number.id);
490         }
491     }
492
493     $scope.markLibAsVolTarget = function() {
494         egCore.hatch.setLocalItem(
495             'eg.cat.volume_transfer_target',
496             $scope.holdingsGridControls.selectedItems()[0].owner_id
497         );
498         console.log('vol_transfer_dest: '+$scope.holdingsGridControls.selectedItems()[0].owner_id);
499     }
500
501     $scope.selectedHoldingsItemStatusDetail = function (){
502         angular.forEach(
503             gatherSelectedHoldingsIds(),
504             function (cid) {
505                 var url = egCore.env.basePath +
506                           'cat/item/' + cid;
507                 $timeout(function() { $window.open(url, '_blank') });
508             }
509         );
510     }
511
512     $scope.transferVolumes = function (){
513         var xfer_target = egCore.hatch.getLocalItem('eg.cat.volume_transfer_target');
514
515         if (xfer_target) {
516             egCore.net.request(
517                 'open-ils.cat',
518                 'open-ils.open-ils.cat.asset.volume.batch.transfer.override',
519                 egCore.auth.token(), {
520                     docid   : $scope.record_id,
521                     lib     : xfer_target,
522                     volumes : gatherSelectedVolumeIds()
523                 }
524             ).then(function(success) {
525                 if (success) {
526                     holdingsSvc.fetch({
527                         rid : $scope.record_id,
528                         org : $scope.holdings_ou,
529                         copy: $scope.holdings_show_copies,
530                         vol : $scope.holdings_show_vols,
531                         empty: $scope.holdings_show_empty
532                     }).then(function() {
533                         $scope.holdingsGridDataProvider.refresh();
534                     });
535                 } else {
536                     alert('Could not transfer volumes!');
537                 }
538             });
539         }
540         
541     }
542
543     $scope.transferItems = function (){
544         var xfer_target = egCore.hatch.getLocalItem('eg.cat.item_transfer_target');
545         if (xfer_target) {
546             var copy_list = gatherSelectedRawCopies();
547
548             angular.forEach(copy_list, function (cp) {
549                 cp.call_number(xfer_target);
550             });
551
552             egCore.pcrud.update(
553                 copy_list
554             ).then(function(success) {
555                 if (success) {
556                     holdingsSvc.fetch({
557                         rid : $scope.record_id,
558                         org : $scope.holdings_ou,
559                         copy: $scope.holdings_show_copies,
560                         vol : $scope.holdings_show_vols,
561                         empty: $scope.holdings_show_empty
562                     }).then(function() {
563                         $scope.holdingsGridDataProvider.refresh();
564                     });
565                 } else {
566                     alert('Could not transfer items!');
567                 }
568             });
569         }
570         
571     }
572
573     $scope.selectedHoldingsItemStatusTgrEvt = function (){
574         angular.forEach(
575             gatherSelectedHoldingsIds(),
576             function (cid) {
577                 var url = egCore.env.basePath +
578                           'cat/item/' + cid + '/triggered_events';
579                 $timeout(function() { $window.open(url, '_blank') });
580             }
581         );
582     }
583
584     $scope.selectedHoldingsItemStatusHolds = function (){
585         angular.forEach(
586             gatherSelectedHoldingsIds(),
587             function (cid) {
588                 var url = egCore.env.basePath +
589                           'cat/item/' + cid + '/holds';
590                 $timeout(function() { $window.open(url, '_blank') });
591             }
592         );
593     }
594
595     $scope.selectedHoldingsDamaged = function () {
596         egCirc.mark_damaged(gatherSelectedHoldingsIds()).then(function() {
597             holdingsSvc.fetch({
598                 rid : $scope.record_id,
599                 org : $scope.holdings_ou,
600                 copy: $scope.holdings_show_copies,
601                 vol : $scope.holdings_show_vols,
602                 empty: $scope.holdings_show_empty
603             }).then(function() {
604                 $scope.holdingsGridDataProvider.refresh();
605             });
606         });
607     }
608
609     $scope.selectedHoldingsMissing = function () {
610         egCirc.mark_missing(gatherSelectedHoldingsIds()).then(function() {
611             holdingsSvc.fetch({
612                 rid : $scope.record_id,
613                 org : $scope.holdings_ou,
614                 copy: $scope.holdings_show_copies,
615                 vol : $scope.holdings_show_vols,
616                 empty: $scope.holdings_show_empty
617             }).then(function() {
618                 $scope.holdingsGridDataProvider.refresh();
619             });
620         });
621     }
622
623
624     // ------------------------------------------------------------------
625     // Holds 
626     var provider = egGridDataProvider.instance({});
627     $scope.hold_grid_data_provider = provider;
628     $scope.grid_actions = egHoldGridActions;
629     $scope.grid_actions.refresh = function () { provider.refresh() };
630     $scope.hold_grid_controls = {};
631
632     var hold_ids = []; // current list of holds
633     function fetchHolds(offset, count) {
634         var ids = hold_ids.slice(offset, offset + count);
635         return egHolds.fetch_holds(ids).then(null, null,
636             function(hold_data) { 
637                 return hold_data;
638             }
639         );
640     }
641
642     provider.get = function(offset, count) {
643         if ($scope.record_tab != 'holds') return $q.when();
644         var deferred = $q.defer();
645         hold_ids = []; // no caching ATM
646
647         // fetch the IDs
648         egCore.net.request(
649             'open-ils.circ',
650             'open-ils.circ.holds.retrieve_all_from_title',
651             egCore.auth.token(), $scope.record_id, 
652             {pickup_lib : egCore.org.descendants($scope.pickup_ou.id(), true)}
653         ).then(
654             function(hold_data) {
655                 angular.forEach(hold_data, function(list, type) {
656                     hold_ids = hold_ids.concat(list);
657                 });
658                 fetchHolds(offset, count).then(
659                     deferred.resolve, null, deferred.notify);
660             }
661         );
662
663         return deferred.promise;
664     }
665
666     $scope.detail_view = function(action, user_data, items) {
667         if (h = items[0]) {
668             $scope.detail_hold_id = h.hold.id();
669         }
670     }
671
672     $scope.list_view = function(items) {
673          $scope.detail_hold_id = null;
674     }
675
676     // refresh the list of record holds when the pickup lib is changed.
677     $scope.pickup_ou = egCore.org.get(egCore.auth.user().ws_ou());
678     $scope.pickup_ou_changed = function(org) {
679         $scope.pickup_ou = org;
680         provider.refresh();
681     }
682
683     $scope.print_holds = function() {
684         var holds = [];
685         angular.forEach($scope.hold_grid_controls.allItems(), function(item) {
686             holds.push({
687                 hold : egCore.idl.toHash(item.hold),
688                 patron_last : item.patron_last,
689                 patron_alias : item.patron_alias,
690                 patron_barcode : item.patron_barcode,
691                 copy : egCore.idl.toHash(item.copy),
692                 volume : egCore.idl.toHash(item.volume),
693                 title : item.mvr.title(),
694                 author : item.mvr.author()
695             });
696         });
697
698         egCore.print.print({
699             context : 'receipt', 
700             template : 'holds_for_bib', 
701             scope : {holds : holds}
702         });
703     }
704
705     $scope.mark_hold_transfer_dest = function() {
706         egCore.hatch.setLocalItem(
707             'eg.circ.hold.title_transfer_target', $scope.record_id);
708     }
709
710     // UI presents this option as "all holds"
711     $scope.transfer_holds_to_marked = function() {
712         var hold_ids = $scope.hold_grid_controls.allItems().map(
713             function(hold_data) {return hold_data.hold.id()});
714         egHolds.transfer_to_marked_title(hold_ids);
715     }
716
717     // ------------------------------------------------------------------
718     // Initialize the selected tab
719
720     function init_cat_url() {
721         // Set the initial catalog URL.  This only happens once.
722         // The URL is otherwise generated through user navigation.
723         if ($scope.catalog_url) return; 
724
725         var url = $location.absUrl().replace(/\/staff.*/, '/opac/advanced');
726
727         // A record ID in the path indicates a request for the record-
728         // specific page.
729         if ($routeParams.record_id) {
730             url = url.replace(/advanced/, '/record/' + $scope.record_id);
731         }
732
733         $scope.catalog_url = url;
734     }
735
736     function init_parts_url() {
737         $scope.parts_url = $location
738             .absUrl()
739             .replace(
740                 /\/staff.*/,
741                 '/conify/global/biblio/monograph_part?r='+$scope.record_id
742             );
743     }
744
745     $scope.set_record_tab = function(tab) {
746         $scope.record_tab = tab;
747
748         switch(tab) {
749
750             case 'monoparts':
751                 init_parts_url();
752                 break;
753
754             case 'catalog':
755                 init_cat_url();
756                 break;
757
758             case 'holds':
759                 $scope.detail_hold_record_id = $scope.record_id; 
760                 // refresh the holds grid
761                 provider.refresh();
762                 break;
763         }
764     }
765
766     $scope.set_default_record_tab = function() {
767         egCore.hatch.setLocalItem(
768             'eg.cat.default_record_tab', $scope.record_tab);
769         $timeout(function(){$scope.default_tab = $scope.record_tab});
770     }
771
772     var tab;
773     if ($scope.record_id) {
774         $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
775         tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
776
777     } else {
778         tab = $routeParams.record_tab || 'catalog';
779     }
780     $scope.set_record_tab(tab);
781
782 }])
783
784 .controller('AuthorityCtrl',
785        ['$scope','$routeParams','$location','$window','$q','egCore',
786 function($scope , $routeParams , $location , $window , $q , egCore) {
787
788     // set record ID on page load if available...
789     $scope.authority_id = $routeParams.authority_id;
790
791     if ($routeParams.authority_id) $scope.from_route = true;
792     else $scope.from_route = false;
793
794     $scope.stop_unload = false;
795 }])
796
797 .controller('URLVerifyCtrl',
798        ['$scope','$location',
799 function($scope , $location) {
800     $scope.verifyurls_url = $location.absUrl().replace(/\/staff.*/, '/url_verify/sessions');
801 }])
802
803 .controller('VandelayCtrl',
804        ['$scope','$location',
805 function($scope , $location) {
806     $scope.vandelay_url = $location.absUrl().replace(/\/staff.*/, '/vandelay/vandelay');
807 }])
808
809 .controller('ManageAuthoritiesCtrl',
810        ['$scope','$location',
811 function($scope , $location) {
812     $scope.manageauthorities_url = $location.absUrl().replace(/\/staff.*/, '/cat/authority/list');
813 }])
814
815 .controller('BatchEditCtrl',
816        ['$scope','$location','$routeParams',
817 function($scope , $location , $routeParams) {
818     $scope.batchedit_url = $location.absUrl().replace(/\/eg.*/, '/opac/extras/merge_template');
819     if ($routeParams.container_type) {
820         switch ($routeParams.container_type) {
821             case 'bucket':
822                 $scope.batchedit_url += '?recordSource=b&containerid=' + $routeParams.container_id;
823                 break;
824             case 'record':
825                 $scope.batchedit_url += '?recordSource=r&recid=' + $routeParams.container_id;
826                 break;
827         };
828     }
829 }])
830
831  
832 .filter('boolText', function(){
833     return function (v) {
834         return v == 't';
835     }
836 })
837
838 .factory('holdingsSvc', 
839        ['egCore','$q',
840 function(egCore , $q) {
841
842     var service = {
843         ongoing : false,
844         copies : [], // record search results
845         index : 0, // search grid index
846         org : null,
847         rid : null
848     };
849
850     service.flesh = {   
851         flesh : 2, 
852         flesh_fields : {
853             acp : ['status','location'],
854             acn : ['prefix','suffix','copies']
855         }
856     }
857
858     // resolved with the last received copy
859     service.fetch = function(opts) {
860         if (service.ongoing) {
861             console.log('Skipping fetch, ongoing = true');
862             return $q.when();
863         }
864
865         var rid = opts.rid;
866         var org = opts.org;
867         var copy = opts.copy;
868         var vol = opts.vol;
869         var empty = opts.empty;
870
871         if (!rid) return $q.when();
872         if (!org) return $q.when();
873
874         service.ongoing = true;
875
876         service.rid = rid;
877         service.org = org;
878         service.copies = [];
879         service.index = 0;
880
881         var org_list = egCore.org.descendants(org.id(), true);
882         console.log('Holdings fetch with: rid='+rid+' org='+org_list+' copy='+copy+' vol='+vol+' empty='+empty);
883
884         return egCore.pcrud.search(
885             'acn',
886             {record : rid, owning_lib : org_list, deleted : 'f'},
887             service.flesh
888         ).then(
889             function() { // finished
890                 service.copies = service.copies.sort(
891                     function (a, b) {
892                         function compare_array (x, y, i) {
893                             if (x[i] && y[i]) { // both have values
894                                 if (x[i] == y[i]) { // need to look deeper
895                                     return compare_array(x, y, ++i);
896                                 }
897
898                                 if (x[i] < y[i]) { // x is first
899                                     return -1;
900                                 } else if (x[i] > y[i]) { // y is first
901                                     return 1;
902                                 }
903
904                             } else { // no orgs to compare ...
905                                 if (x[i]) return -1;
906                                 if (y[i]) return 1;
907                             }
908                             return 0;
909                         }
910
911                         var owner_order = compare_array(a.owner_list, b.owner_list, 0);
912                         if (!owner_order) {
913                             // now compare on CN label
914                             if (a.call_number.label < b.call_number.label) return -1;
915                             if (a.call_number.label > b.call_number.label) return 1;
916
917                             // try copy number
918                             if (a.copy_number < b.copy_number) return -1;
919                             if (a.copy_number > b.copy_number) return 1;
920
921                             // finally, barcode
922                             if (a.barcode < b.barcode) return -1;
923                             if (a.barcode > b.barcode) return 1;
924                         }
925                         return owner_order;
926                     }
927                 );
928
929                 // create a label using just the unique part of the owner list
930                 var index = 0;
931                 var prev_owner_list;
932                 angular.forEach(service.copies, function (cp) {
933                     if (!prev_owner_list) {
934                         cp.owner_label = cp.owner_list.join(' ... ');
935                     } else {
936                         var current_owner_list = cp.owner_list.slice();
937                         while (current_owner_list[1] && prev_owner_list[1] && current_owner_list[0] == prev_owner_list[0]) {
938                             current_owner_list.shift();
939                             prev_owner_list.shift();
940                         }
941                         cp.owner_label = current_owner_list.join(' ... ');
942                     }
943
944                     cp.index = index++;
945                     prev_owner_list = cp.owner_list.slice();
946                 });
947
948                 var new_list = service.copies;
949                 if (!copy || !vol) { // collapse copy rows, supply a count instead
950
951                     index = 0;
952                     var cp_list = [];
953                     var prev_key;
954                     var current_blob = {};
955                     angular.forEach(new_list, function (cp) {
956                         if (!prev_key) {
957                             prev_key = cp.owner_list.join('') + cp.call_number.label;
958                             if (cp.barcode) current_blob.copy_count = 1;
959                             current_blob.index = index++;
960                             current_blob.id_list = cp.id_list;
961                             current_blob.raw = cp.raw;
962                             current_blob.call_number = cp.call_number;
963                             current_blob.owner_list = cp.owner_list;
964                             current_blob.owner_label = cp.owner_label;
965                             current_blob.owner_id = cp.owner_id;
966                         } else {
967                             var current_key = cp.owner_list.join('') + cp.call_number.label;
968                             if (prev_key == current_key) { // collapse into current_blob
969                                 current_blob.copy_count++;
970                                 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
971                                 current_blob.raw = current_blob.raw.concat(cp.raw);
972                             } else {
973                                 current_blob.barcode = current_blob.copy_count;
974                                 cp_list.push(current_blob);
975                                 prev_key = current_key;
976                                 current_blob = {};
977                                 if (cp.barcode) current_blob.copy_count = 1;
978                                 current_blob.index = index++;
979                                 current_blob.id_list = cp.id_list;
980                                 current_blob.raw = cp.raw;
981                                 current_blob.owner_label = cp.owner_label;
982                                 current_blob.owner_id = cp.owner_id;
983                                 current_blob.call_number = cp.call_number;
984                                 current_blob.owner_list = cp.owner_list;
985                             }
986                         }
987                     });
988
989                     current_blob.barcode = current_blob.copy_count;
990                     cp_list.push(current_blob);
991                     new_list = cp_list;
992
993                     if (!vol) { // do the same for vol rows
994
995                         index = 0;
996                         var cn_list = [];
997                         prev_key = '';
998                         var current_blob = {};
999                         angular.forEach(cp_list, function (cp) {
1000                             if (!prev_key) {
1001                                 prev_key = cp.owner_list.join('');
1002                                 current_blob.index = index++;
1003                                 current_blob.id_list = cp.id_list;
1004                                 current_blob.raw = cp.raw;
1005                                 current_blob.cn_count = 1;
1006                                 current_blob.copy_count = cp.copy_count;
1007                                 current_blob.owner_list = cp.owner_list;
1008                                 current_blob.owner_label = cp.owner_label;
1009                                 current_blob.owner_id = cp.owner_id;
1010                             } else {
1011                                 var current_key = cp.owner_list.join('');
1012                                 if (prev_key == current_key) { // collapse into current_blob
1013                                     current_blob.cn_count++;
1014                                     current_blob.copy_count += cp.copy_count;
1015                                     current_blob.id_list = current_blob.id_list.concat(cp.id_list);
1016                                     current_blob.raw = current_blob.raw.concat(cp.raw);
1017                                 } else {
1018                                     current_blob.barcode = current_blob.copy_count;
1019                                     current_blob.call_number = { label : current_blob.cn_count };
1020                                     cn_list.push(current_blob);
1021                                     prev_key = current_key;
1022                                     current_blob = {};
1023                                     current_blob.index = index++;
1024                                     current_blob.id_list = cp.id_list;
1025                                     current_blob.raw = cp.raw;
1026                                     current_blob.owner_label = cp.owner_label;
1027                                     current_blob.owner_id = cp.owner_id;
1028                                     current_blob.cn_count = 1;
1029                                     current_blob.copy_count = cp.copy_count;
1030                                     current_blob.owner_list = cp.owner_list;
1031                                 }
1032                             }
1033                         });
1034     
1035                         current_blob.barcode = current_blob.copy_count;
1036                         current_blob.call_number = { label : current_blob.cn_count };
1037                         cn_list.push(current_blob);
1038                         new_list = cn_list;
1039     
1040                     }
1041                 }
1042
1043                 service.copies = new_list;
1044                 service.ongoing = false;
1045             },
1046
1047             null, // error
1048
1049             // notify reads the stream of copies, one at a time.
1050             function(cn) {
1051
1052                 var copies = cn.copies();
1053                 cn.copies([]);
1054
1055                 angular.forEach(copies, function (cp) {
1056                     cp.call_number(cn);
1057                 });
1058
1059                 var owner_id = cn.owning_lib();
1060                 var owner = egCore.org.get(owner_id);
1061
1062                 var owner_name_list = [];
1063                 while (owner.parent_ou()) { // we're going to skip the top of the tree...
1064                     owner_name_list.unshift(owner.name());
1065                     owner = egCore.org.get(owner.parent_ou());
1066                 }
1067
1068                 if (copies[0]) {
1069                     var flat = [];
1070                     angular.forEach(copies, function (cp) {
1071                         var flat_cp = egCore.idl.toHash(cp);
1072                         flat_cp.owner_id = owner_id;
1073                         flat_cp.owner_list = owner_name_list;
1074                         flat_cp.id_list = [flat_cp.id];
1075                         flat_cp.raw = [cp];
1076                         flat.push(flat_cp);
1077                     });
1078
1079                     service.copies = service.copies.concat(flat);
1080                 } else if (empty) {
1081                     service.copies.push({
1082                         owner_id   : owner_id,
1083                         owner_list : owner_name_list,
1084                         call_number: egCore.idl.toHash(cn)
1085                     });
1086                 }
1087
1088                 return cn;
1089             }
1090         );
1091     }
1092
1093     return service;
1094 }])
1095
1096