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