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.
10 angular.module('egCatalogApp', ['ui.bootstrap','ngRoute','egCoreMod','egGridMod', 'egMarcMod'])
12 .config(function($routeProvider, $locationProvider, $compileProvider) {
13 $locationProvider.html5Mode(true);
14 $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|blob):/); // grid export
16 var resolver = {delay :
17 ['egStartup', function(egStartup) {return egStartup.go()}]}
19 $routeProvider.when('/cat/catalog/index', {
20 templateUrl: './cat/catalog/t_catalog',
21 controller: 'CatalogCtrl',
25 $routeProvider.when('/cat/catalog/retrieve_by_id', {
26 templateUrl: './cat/catalog/t_retrieve_by_id',
27 controller: 'CatalogRecordRetrieve',
31 $routeProvider.when('/cat/catalog/retrieve_by_tcn', {
32 templateUrl: './cat/catalog/t_retrieve_by_tcn',
33 controller: 'CatalogRecordRetrieve',
37 // create some catalog page-specific mappings
38 $routeProvider.when('/cat/catalog/record/:record_id', {
39 templateUrl: './cat/catalog/t_catalog',
40 controller: 'CatalogCtrl',
44 // create some catalog page-specific mappings
45 $routeProvider.when('/cat/catalog/record/:record_id/:record_tab', {
46 templateUrl: './cat/catalog/t_catalog',
47 controller: 'CatalogCtrl',
51 $routeProvider.when('/cat/catalog/batchEdit', {
52 templateUrl: './cat/catalog/t_batchedit',
53 controller: 'BatchEditCtrl',
57 $routeProvider.when('/cat/catalog/batchEdit/:container_type/:container_id', {
58 templateUrl: './cat/catalog/t_batchedit',
59 controller: 'BatchEditCtrl',
63 $routeProvider.when('/cat/catalog/vandelay', {
64 templateUrl: './cat/catalog/t_vandelay',
65 controller: 'VandelayCtrl',
69 $routeProvider.when('/cat/catalog/verifyURLs', {
70 templateUrl: './cat/catalog/t_verifyurls',
71 controller: 'URLVerifyCtrl',
75 $routeProvider.when('/cat/catalog/manageAuthorities', {
76 templateUrl: './cat/catalog/t_manageauthorities',
77 controller: 'ManageAuthoritiesCtrl',
81 $routeProvider.otherwise({redirectTo : '/cat/catalog/index'});
87 .controller('CatalogRecordRetrieve',
88 ['$scope','$routeParams','$location','$q','egCore',
89 function($scope , $routeParams , $location , $q , egCore ) {
91 $scope.focusMe = true;
93 // jump to the patron checkout UI
94 function loadRecord(record_id) {
96 .path('/cat/catalog/record/' + record_id);
99 $scope.submitId = function(args) {
100 $scope.recordNotFound = null;
101 if (!args.record_id) return;
103 // blur so next time it's set to true it will re-apply select()
104 $scope.selectMe = false;
106 return loadRecord(args.record_id);
109 $scope.submitTCN = function(args) {
110 $scope.recordNotFound = null;
111 $scope.moreRecordsFound = null;
112 if (!args.record_tcn) return;
114 // blur so next time it's set to true it will re-apply select()
115 $scope.selectMe = false;
120 'open-ils.search.biblio.tcn',
123 .then(function(resp) { // get_barcodes
125 if (evt = egCore.evt.parse(resp)) {
131 $scope.recordNotFound = args.record_tcn;
132 $scope.selectMe = true;
136 if (resp.count > 1) {
137 $scope.moreRecordsFound = args.record_tcn;
138 $scope.selectMe = true;
142 var record_id = resp.ids[0];
143 return loadRecord(record_id);
149 .controller('CatalogCtrl',
150 ['$scope','$routeParams','$location','$window','$q','egCore','egHolds','egCirc',
151 'egGridDataProvider','egHoldGridActions','$timeout','holdingsSvc',
152 function($scope , $routeParams , $location , $window , $q , egCore , egHolds , egCirc,
153 egGridDataProvider , egHoldGridActions , $timeout , holdingsSvc) {
155 // set record ID on page load if available...
156 $scope.record_id = $routeParams.record_id;
158 if ($routeParams.record_id) $scope.from_route = true;
159 else $scope.from_route = false;
161 // will hold a ref to the opac iframe
162 $scope.opac_iframe = null;
163 $scope.parts_iframe = null;
165 $scope.in_opac_call = false;
166 $scope.opac_call = function (opac_frame_function, force_opac_tab) {
167 if ($scope.opac_iframe) {
168 if (force_opac_tab) $scope.record_tab = 'catalog';
169 $scope.in_opac_call = true;
170 $scope.opac_iframe.dom.contentWindow[opac_frame_function]();
174 $scope.stop_unload = false;
175 $scope.$watch('stop_unload',
176 function(newVal, oldVal) {
177 if (newVal && newVal != oldVal && $scope.opac_iframe) {
178 $($scope.opac_iframe.dom.contentWindow).on('beforeunload', function(){
179 return 'There is unsaved data in this record.'
182 if ($scope.opac_iframe)
183 $($scope.opac_iframe.dom.contentWindow).off('beforeunload');
188 // Set the "last bib" cookie, if we have that
189 if ($scope.record_id)
190 egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
192 // also set it when the iframe changes to a new record
193 $scope.handle_page = function(url) {
195 if (!url || url == 'about:blank') {
196 // nothing loaded. If we already have a record ID, leave it.
200 var match = url.match(/\/+opac\/+record\/+(\d+)/);
202 $scope.record_id = match[1];
203 egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
206 delete $scope.record_id;
207 $scope.from_route = false;
210 // child scope is executing this function, so our digest doesn't fire ... thus,
213 if (!$scope.in_opac_call) {
214 if ($scope.record_id) {
215 $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
216 tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
218 tab = $routeParams.record_tab || 'catalog';
220 $scope.set_record_tab(tab);
222 $scope.in_opac_call = false;
226 // xulG catalog handlers
227 $scope.handlers = { }
229 // ------------------------------------------------------------------
232 $scope.holdingsGridControls = {};
233 $scope.holdingsGridDataProvider = egGridDataProvider.instance({
234 get : function(offset, count) {
235 return this.arrayNotifier(holdingsSvc.copies, offset, count);
239 // refresh the list of holdings when the filter lib is changed.
240 $scope.holdings_ou = egCore.org.get(egCore.auth.user().ws_ou());
241 $scope.holdings_ou_changed = function(org) {
242 $scope.holdings_ou = org;
244 rid : $scope.record_id,
245 org : $scope.holdings_ou,
246 copy: $scope.holdings_show_copies,
247 vol : $scope.holdings_show_vols,
248 empty: $scope.holdings_show_empty
250 $scope.holdingsGridDataProvider.refresh();
254 $scope.holdings_cb_changed = function(cb,newVal,norefresh) {
256 egCore.hatch.setItem('cat.' + cb, newVal);
257 if (!norefresh) holdingsSvc.fetch({
258 rid : $scope.record_id,
259 org : $scope.holdings_ou,
260 copy: $scope.holdings_show_copies,
261 vol : $scope.holdings_show_vols,
262 empty: $scope.holdings_show_empty
264 $scope.holdingsGridDataProvider.refresh();
268 egCore.hatch.getItem('cat.holdings_show_vols').then(function(x){
269 if (typeof x == 'undefined') x = true;
270 $scope.holdings_cb_changed('holdings_show_vols',x,true);
271 $('#holdings_show_vols').prop('checked', x);
273 egCore.hatch.getItem('cat.holdings_show_copies').then(function(x){
274 if (typeof x == 'undefined') x = true;
275 $scope.holdings_cb_changed('holdings_show_copies',x,true);
276 $('#holdings_show_copies').prop('checked', x);
278 egCore.hatch.getItem('cat.holdings_show_empty').then(function(x){
279 if (typeof x == 'undefined') x = true;
280 $scope.holdings_cb_changed('holdings_show_empty',x);
281 $('#holdings_show_empty').prop('checked', x);
286 $scope.holdings_checkbox_handler = function (item) {
287 $scope.holdings_cb_changed(item.checkbox,item.checked);
290 function gatherSelectedHoldingsIds () {
293 $scope.holdingsGridControls.selectedItems(),
294 function (item) { cp_id_list = cp_id_list.concat(item.id_list) }
299 $scope.selectedHoldingsItemStatus = function (){
300 var url = egCore.env.basePath + 'cat/item/search/' + gatherSelectedHoldingsIds().join(',')
301 $timeout(function() { $window.open(url, '_blank') });
304 $scope.selectedHoldingsItemStatusDetail = function (){
306 gatherSelectedHoldingsIds(),
308 var url = egCore.env.basePath +
310 $timeout(function() { $window.open(url, '_blank') });
315 $scope.selectedHoldingsItemStatusTgrEvt = function (){
317 gatherSelectedHoldingsIds(),
319 var url = egCore.env.basePath +
320 'cat/item/' + cid + '/triggered_events';
321 $timeout(function() { $window.open(url, '_blank') });
326 $scope.selectedHoldingsItemStatusHolds = function (){
328 gatherSelectedHoldingsIds(),
330 var url = egCore.env.basePath +
331 'cat/item/' + cid + '/holds';
332 $timeout(function() { $window.open(url, '_blank') });
337 $scope.selectedHoldingsDamaged = function () {
338 egCirc.mark_damaged(gatherSelectedHoldingsIds()).then(function() {
340 rid : $scope.record_id,
341 org : $scope.holdings_ou,
342 copy: $scope.holdings_show_copies,
343 vol : $scope.holdings_show_vols,
344 empty: $scope.holdings_show_empty
346 $scope.holdingsGridDataProvider.refresh();
351 $scope.selectedHoldingsMissing = function () {
352 egCirc.mark_missing(gatherSelectedHoldingsIds()).then(function() {
354 rid : $scope.record_id,
355 org : $scope.holdings_ou,
356 copy: $scope.holdings_show_copies,
357 vol : $scope.holdings_show_vols,
358 empty: $scope.holdings_show_empty
360 $scope.holdingsGridDataProvider.refresh();
366 // ------------------------------------------------------------------
368 var provider = egGridDataProvider.instance({});
369 $scope.hold_grid_data_provider = provider;
370 $scope.grid_actions = egHoldGridActions;
371 $scope.grid_actions.refresh = function () { provider.refresh() };
372 $scope.hold_grid_controls = {};
374 var hold_ids = []; // current list of holds
375 function fetchHolds(offset, count) {
376 var ids = hold_ids.slice(offset, offset + count);
377 return egHolds.fetch_holds(ids).then(null, null,
378 function(hold_data) {
384 provider.get = function(offset, count) {
385 if ($scope.record_tab != 'holds') return $q.when();
386 var deferred = $q.defer();
387 hold_ids = []; // no caching ATM
392 'open-ils.circ.holds.retrieve_all_from_title',
393 egCore.auth.token(), $scope.record_id,
394 {pickup_lib : egCore.org.descendants($scope.pickup_ou.id(), true)}
396 function(hold_data) {
397 angular.forEach(hold_data, function(list, type) {
398 hold_ids = hold_ids.concat(list);
400 fetchHolds(offset, count).then(
401 deferred.resolve, null, deferred.notify);
405 return deferred.promise;
408 $scope.detail_view = function(action, user_data, items) {
410 $scope.detail_hold_id = h.hold.id();
414 $scope.list_view = function(items) {
415 $scope.detail_hold_id = null;
418 // refresh the list of record holds when the pickup lib is changed.
419 $scope.pickup_ou = egCore.org.get(egCore.auth.user().ws_ou());
420 $scope.pickup_ou_changed = function(org) {
421 $scope.pickup_ou = org;
425 $scope.print_holds = function() {
427 angular.forEach($scope.hold_grid_controls.allItems(), function(item) {
429 hold : egCore.idl.toHash(item.hold),
430 patron_last : item.patron_last,
431 patron_alias : item.patron_alias,
432 patron_barcode : item.patron_barcode,
433 copy : egCore.idl.toHash(item.copy),
434 volume : egCore.idl.toHash(item.volume),
435 title : item.mvr.title(),
436 author : item.mvr.author()
442 template : 'holds_for_bib',
443 scope : {holds : holds}
447 $scope.mark_hold_transfer_dest = function() {
448 egCore.hatch.setLocalItem(
449 'eg.circ.hold.title_transfer_target', $scope.record_id);
452 // UI presents this option as "all holds"
453 $scope.transfer_holds_to_marked = function() {
454 var hold_ids = $scope.hold_grid_controls.allItems().map(
455 function(hold_data) {return hold_data.hold.id()});
456 egHolds.transfer_to_marked_title(hold_ids);
459 // ------------------------------------------------------------------
460 // Initialize the selected tab
462 function init_cat_url() {
463 // Set the initial catalog URL. This only happens once.
464 // The URL is otherwise generated through user navigation.
465 if ($scope.catalog_url) return;
467 var url = $location.absUrl().replace(/\/staff.*/, '/opac/advanced');
469 // A record ID in the path indicates a request for the record-
471 if ($routeParams.record_id) {
472 url = url.replace(/advanced/, '/record/' + $scope.record_id);
475 $scope.catalog_url = url;
478 function init_parts_url() {
479 $scope.parts_url = $location
483 '/conify/global/biblio/monograph_part?r='+$scope.record_id
487 $scope.set_record_tab = function(tab) {
488 $scope.record_tab = tab;
501 $scope.detail_hold_record_id = $scope.record_id;
502 // refresh the holds grid
508 $scope.set_default_record_tab = function() {
509 egCore.hatch.setLocalItem(
510 'eg.cat.default_record_tab', $scope.record_tab);
511 $timeout(function(){$scope.default_tab = $scope.record_tab});
515 if ($scope.record_id) {
516 $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
517 tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
520 tab = $routeParams.record_tab || 'catalog';
522 $scope.set_record_tab(tab);
526 .controller('URLVerifyCtrl',
527 ['$scope','$location',
528 function($scope , $location) {
529 $scope.verifyurls_url = $location.absUrl().replace(/\/staff.*/, '/url_verify/sessions');
532 .controller('VandelayCtrl',
533 ['$scope','$location',
534 function($scope , $location) {
535 $scope.vandelay_url = $location.absUrl().replace(/\/staff.*/, '/vandelay/vandelay');
538 .controller('ManageAuthoritiesCtrl',
539 ['$scope','$location',
540 function($scope , $location) {
541 $scope.manageauthorities_url = $location.absUrl().replace(/\/staff.*/, '/cat/authority/list');
544 .controller('BatchEditCtrl',
545 ['$scope','$location','$routeParams',
546 function($scope , $location , $routeParams) {
547 $scope.batchedit_url = $location.absUrl().replace(/\/eg.*/, '/opac/extras/merge_template');
548 if ($routeParams.container_type) {
549 switch ($routeParams.container_type) {
551 $scope.batchedit_url += '?recordSource=b&containerid=' + $routeParams.container_id;
554 $scope.batchedit_url += '?recordSource=r&recid=' + $routeParams.container_id;
561 .filter('boolText', function(){
562 return function (v) {
567 .factory('holdingsSvc',
569 function(egCore , $q) {
573 copies : [], // record search results
574 index : 0, // search grid index
582 acp : ['status','location'],
583 acn : ['prefix','suffix','copies']
587 // resolved with the last received copy
588 service.fetch = function(opts) {
589 if (service.ongoing) {
590 console.log('Skipping fetch, ongoing = true');
596 var copy = opts.copy;
598 var empty = opts.empty;
600 if (!rid) return $q.when();
601 if (!org) return $q.when();
603 service.ongoing = true;
610 var org_list = egCore.org.descendants(org.id(), true);
611 console.log('Holdings fetch with: rid='+rid+' org='+org_list+' copy='+copy+' vol='+vol+' empty='+empty);
613 return egCore.pcrud.search(
615 {record : rid, owning_lib : org_list, deleted : 'f'},
618 function() { // finished
619 service.copies = service.copies.sort(
621 function compare_array (x, y, i) {
622 if (x[i] && y[i]) { // both have values
623 if (x[i] == y[i]) { // need to look deeper
624 return compare_array(x, y, ++i);
627 if (x[i] < y[i]) { // x is first
629 } else if (x[i] > y[i]) { // y is first
633 } else { // no orgs to compare ...
640 var owner_order = compare_array(a.owner_list, b.owner_list, 0);
642 // now compare on CN label
643 if (a.call_number.label < b.call_number.label) return -1;
644 if (a.call_number.label > b.call_number.label) return 1;
647 if (a.copy_number < b.copy_number) return -1;
648 if (a.copy_number > b.copy_number) return 1;
651 if (a.barcode < b.barcode) return -1;
652 if (a.barcode > b.barcode) return 1;
658 // create a label using just the unique part of the owner list
661 angular.forEach(service.copies, function (cp) {
662 if (!prev_owner_list) {
663 cp.owner_label = cp.owner_list.join(' ... ');
665 var current_owner_list = cp.owner_list.slice();
666 while (current_owner_list[1] && prev_owner_list[1] && current_owner_list[0] == prev_owner_list[0]) {
667 current_owner_list.shift();
668 prev_owner_list.shift();
670 cp.owner_label = current_owner_list.join(' ... ');
674 prev_owner_list = cp.owner_list.slice();
677 var new_list = service.copies;
678 if (!copy || !vol) { // collapse copy rows, supply a count instead
683 var current_blob = {};
684 angular.forEach(new_list, function (cp) {
686 prev_key = cp.owner_list.join('') + cp.call_number.label;
687 if (cp.barcode) current_blob.copy_count = 1;
688 current_blob.index = index++;
689 current_blob.id_list = cp.id_list;
690 current_blob.call_number = cp.call_number;
691 current_blob.owner_list = cp.owner_list;
692 current_blob.owner_label = cp.owner_label;
694 var current_key = cp.owner_list.join('') + cp.call_number.label;
695 if (prev_key == current_key) { // collapse into current_blob
696 current_blob.copy_count++;
697 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
699 current_blob.barcode = current_blob.copy_count;
700 cp_list.push(current_blob);
701 prev_key = current_key;
703 if (cp.barcode) current_blob.copy_count = 1;
704 current_blob.index = index++;
705 current_blob.id_list = cp.id_list;
706 current_blob.owner_label = cp.owner_label;
707 current_blob.call_number = cp.call_number;
708 current_blob.owner_list = cp.owner_list;
713 current_blob.barcode = current_blob.copy_count;
714 cp_list.push(current_blob);
717 if (!vol) { // do the same for vol rows
722 var current_blob = {};
723 angular.forEach(cp_list, function (cp) {
725 prev_key = cp.owner_list.join('');
726 current_blob.index = index++;
727 current_blob.id_list = cp.id_list;
728 current_blob.cn_count = 1;
729 current_blob.copy_count = cp.copy_count;
730 current_blob.owner_list = cp.owner_list;
731 current_blob.owner_label = cp.owner_label;
733 var current_key = cp.owner_list.join('');
734 if (prev_key == current_key) { // collapse into current_blob
735 current_blob.cn_count++;
736 current_blob.copy_count += cp.copy_count;
737 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
739 current_blob.barcode = current_blob.copy_count;
740 current_blob.call_number = { label : current_blob.cn_count };
741 cn_list.push(current_blob);
742 prev_key = current_key;
744 current_blob.index = index++;
745 current_blob.id_list = cp.id_list;
746 current_blob.owner_label = cp.owner_label;
747 current_blob.cn_count = 1;
748 current_blob.copy_count = cp.copy_count;
749 current_blob.owner_list = cp.owner_list;
754 current_blob.barcode = current_blob.copy_count;
755 current_blob.call_number = { label : current_blob.cn_count };
756 cn_list.push(current_blob);
762 service.copies = new_list;
763 service.ongoing = false;
768 // notify reads the stream of copies, one at a time.
771 var copies = cn.copies();
774 angular.forEach(copies, function (cp) {
778 var flat = egCore.idl.toHash(copies);
779 var owner = egCore.org.get(flat[0].call_number.owning_lib);
781 var owner_name_list = [];
782 while (owner.parent_ou()) { // we're going to skip the top of the tree...
783 owner_name_list.unshift(owner.name());
784 owner = egCore.org.get(owner.parent_ou());
787 angular.forEach(flat, function (cp) {
788 cp.owner_list = owner_name_list;
789 cp.id_list = [cp.id];
792 service.copies = service.copies.concat(flat);
794 if (empty && flat.length == 0) {
795 service.copies.push({
796 owner_list : owner_name_list,
797 call_number: egCore.idl.toHash(cn)