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 : ['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;
26 egCore.env.loadClasses.push('aous');
30 $routeProvider.when('/cat/catalog/index', {
31 templateUrl: './cat/catalog/t_catalog',
32 controller: 'CatalogCtrl',
36 $routeProvider.when('/cat/catalog/retrieve_by_id', {
37 templateUrl: './cat/catalog/t_retrieve_by_id',
38 controller: 'CatalogRecordRetrieve',
42 $routeProvider.when('/cat/catalog/retrieve_by_tcn', {
43 templateUrl: './cat/catalog/t_retrieve_by_tcn',
44 controller: 'CatalogRecordRetrieve',
48 // create some catalog page-specific mappings
49 $routeProvider.when('/cat/catalog/record/:record_id', {
50 templateUrl: './cat/catalog/t_catalog',
51 controller: 'CatalogCtrl',
55 // create some catalog page-specific mappings
56 $routeProvider.when('/cat/catalog/record/:record_id/:record_tab', {
57 templateUrl: './cat/catalog/t_catalog',
58 controller: 'CatalogCtrl',
62 $routeProvider.when('/cat/catalog/batchEdit', {
63 templateUrl: './cat/catalog/t_batchedit',
64 controller: 'BatchEditCtrl',
68 $routeProvider.when('/cat/catalog/batchEdit/:container_type/:container_id', {
69 templateUrl: './cat/catalog/t_batchedit',
70 controller: 'BatchEditCtrl',
74 $routeProvider.when('/cat/catalog/vandelay', {
75 templateUrl: './cat/catalog/t_vandelay',
76 controller: 'VandelayCtrl',
80 $routeProvider.when('/cat/catalog/verifyURLs', {
81 templateUrl: './cat/catalog/t_verifyurls',
82 controller: 'URLVerifyCtrl',
86 $routeProvider.when('/cat/catalog/manageAuthorities', {
87 templateUrl: './cat/catalog/t_manageauthorities',
88 controller: 'ManageAuthoritiesCtrl',
92 $routeProvider.when('/cat/catalog/authority/:authority_id/marc_edit', {
93 templateUrl: './cat/catalog/t_authority',
94 controller: 'AuthorityCtrl',
98 $routeProvider.otherwise({redirectTo : '/cat/catalog/index'});
104 .controller('CatalogRecordRetrieve',
105 ['$scope','$routeParams','$location','$q','egCore',
106 function($scope , $routeParams , $location , $q , egCore ) {
108 $scope.focusMe = true;
110 // jump to the patron checkout UI
111 function loadRecord(record_id) {
113 .path('/cat/catalog/record/' + record_id);
116 $scope.submitId = function(args) {
117 $scope.recordNotFound = null;
118 if (!args.record_id) return;
120 // blur so next time it's set to true it will re-apply select()
121 $scope.selectMe = false;
123 return loadRecord(args.record_id);
126 $scope.submitTCN = function(args) {
127 $scope.recordNotFound = null;
128 $scope.moreRecordsFound = null;
129 if (!args.record_tcn) return;
131 // blur so next time it's set to true it will re-apply select()
132 $scope.selectMe = false;
137 'open-ils.search.biblio.tcn',
140 .then(function(resp) { // get_barcodes
142 if (evt = egCore.evt.parse(resp)) {
148 $scope.recordNotFound = args.record_tcn;
149 $scope.selectMe = true;
153 if (resp.count > 1) {
154 $scope.moreRecordsFound = args.record_tcn;
155 $scope.selectMe = true;
159 var record_id = resp.ids[0];
160 return loadRecord(record_id);
166 .controller('CatalogCtrl',
167 ['$scope','$routeParams','$location','$window','$q','egCore','egHolds','egCirc',
168 'egGridDataProvider','egHoldGridActions','$timeout','holdingsSvc',
169 function($scope , $routeParams , $location , $window , $q , egCore , egHolds , egCirc,
170 egGridDataProvider , egHoldGridActions , $timeout , holdingsSvc) {
172 // set record ID on page load if available...
173 $scope.record_id = $routeParams.record_id;
175 if ($routeParams.record_id) $scope.from_route = true;
176 else $scope.from_route = false;
178 // will hold a ref to the opac iframe
179 $scope.opac_iframe = null;
180 $scope.parts_iframe = null;
182 $scope.in_opac_call = false;
183 $scope.opac_call = function (opac_frame_function, force_opac_tab) {
184 if ($scope.opac_iframe) {
185 if (force_opac_tab) $scope.record_tab = 'catalog';
186 $scope.in_opac_call = true;
187 $scope.opac_iframe.dom.contentWindow[opac_frame_function]();
191 $scope.stop_unload = false;
192 $scope.$watch('stop_unload',
193 function(newVal, oldVal) {
194 if (newVal && newVal != oldVal && $scope.opac_iframe) {
195 $($scope.opac_iframe.dom.contentWindow).on('beforeunload', function(){
196 return 'There is unsaved data in this record.'
199 if ($scope.opac_iframe)
200 $($scope.opac_iframe.dom.contentWindow).off('beforeunload');
205 // Set the "last bib" cookie, if we have that
206 if ($scope.record_id)
207 egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
209 // also set it when the iframe changes to a new record
210 $scope.handle_page = function(url) {
212 if (!url || url == 'about:blank') {
213 // nothing loaded. If we already have a record ID, leave it.
217 var match = url.match(/\/+opac\/+record\/+(\d+)/);
219 $scope.record_id = match[1];
220 egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
221 $scope.holdings_record_id_changed($scope.record_id);
224 delete $scope.record_id;
225 $scope.from_route = false;
228 // child scope is executing this function, so our digest doesn't fire ... thus,
231 if (!$scope.in_opac_call) {
232 if ($scope.record_id) {
233 $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
234 tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
236 tab = $routeParams.record_tab || 'catalog';
238 $scope.set_record_tab(tab);
240 $scope.in_opac_call = false;
244 // xulG catalog handlers
245 $scope.handlers = { }
247 // ------------------------------------------------------------------
250 $scope.holdingsGridControls = {};
251 $scope.holdingsGridDataProvider = egGridDataProvider.instance({
252 get : function(offset, count) {
253 return this.arrayNotifier(holdingsSvc.copies, offset, count);
257 // refresh the list of holdings when the record_id is changed.
258 $scope.holdings_record_id_changed = function(id) {
259 if ($scope.record_id != id) $scope.record_id = id;
260 console.log('record id changed to ' + id + ', loading new holdings');
262 rid : $scope.record_id,
263 org : $scope.holdings_ou,
264 copy: $scope.holdings_show_copies,
265 vol : $scope.holdings_show_vols,
266 empty: $scope.holdings_show_empty
268 $scope.holdingsGridDataProvider.refresh();
272 // refresh the list of holdings when the filter lib is changed.
273 $scope.holdings_ou = egCore.org.get(egCore.auth.user().ws_ou());
274 $scope.holdings_ou_changed = function(org) {
275 $scope.holdings_ou = org;
277 rid : $scope.record_id,
278 org : $scope.holdings_ou,
279 copy: $scope.holdings_show_copies,
280 vol : $scope.holdings_show_vols,
281 empty: $scope.holdings_show_empty
283 $scope.holdingsGridDataProvider.refresh();
287 $scope.holdings_cb_changed = function(cb,newVal,norefresh) {
289 egCore.hatch.setItem('cat.' + cb, newVal);
290 if (!norefresh) holdingsSvc.fetch({
291 rid : $scope.record_id,
292 org : $scope.holdings_ou,
293 copy: $scope.holdings_show_copies,
294 vol : $scope.holdings_show_vols,
295 empty: $scope.holdings_show_empty
297 $scope.holdingsGridDataProvider.refresh();
301 egCore.hatch.getItem('cat.holdings_show_vols').then(function(x){
302 if (typeof x == 'undefined') x = true;
303 $scope.holdings_cb_changed('holdings_show_vols',x,true);
304 $('#holdings_show_vols').prop('checked', x);
306 egCore.hatch.getItem('cat.holdings_show_copies').then(function(x){
307 if (typeof x == 'undefined') x = true;
308 $scope.holdings_cb_changed('holdings_show_copies',x,true);
309 $('#holdings_show_copies').prop('checked', x);
311 egCore.hatch.getItem('cat.holdings_show_empty').then(function(x){
312 if (typeof x == 'undefined') x = true;
313 $scope.holdings_cb_changed('holdings_show_empty',x);
314 $('#holdings_show_empty').prop('checked', x);
319 $scope.holdings_checkbox_handler = function (item) {
320 $scope.holdings_cb_changed(item.checkbox,item.checked);
323 function gatherSelectedHoldingsIds () {
326 $scope.holdingsGridControls.selectedItems(),
327 function (item) { cp_id_list = cp_id_list.concat(item.id_list) }
332 spawnHoldingsEdit = function (hide_vols,hide_copies){
335 'open-ils.actor.anon_cache.set_value',
336 null, 'edit-these-copies', {
337 record_id: $scope.record_id,
338 copies: gatherSelectedHoldingsIds(),
339 hide_vols : hide_vols,
340 hide_copies : hide_copies
342 ).then(function(key) {
344 var url = egCore.env.basePath + 'cat/volcopy/' + key;
345 $timeout(function() { $window.open(url, '_blank') });
347 alert('Could not create anonymous cache key!');
351 $scope.selectedHoldingsVolCopyEdit = function () { spawnHoldingsEdit(false,false) }
352 $scope.selectedHoldingsVolEdit = function () { spawnHoldingsEdit(false,true) }
353 $scope.selectedHoldingsCopyEdit = function () { spawnHoldingsEdit(true,false) }
355 $scope.selectedHoldingsItemStatus = function (){
356 var url = egCore.env.basePath + 'cat/item/search/' + gatherSelectedHoldingsIds().join(',')
357 $timeout(function() { $window.open(url, '_blank') });
360 $scope.selectedHoldingsItemStatusDetail = function (){
362 gatherSelectedHoldingsIds(),
364 var url = egCore.env.basePath +
366 $timeout(function() { $window.open(url, '_blank') });
371 $scope.selectedHoldingsItemStatusTgrEvt = function (){
373 gatherSelectedHoldingsIds(),
375 var url = egCore.env.basePath +
376 'cat/item/' + cid + '/triggered_events';
377 $timeout(function() { $window.open(url, '_blank') });
382 $scope.selectedHoldingsItemStatusHolds = function (){
384 gatherSelectedHoldingsIds(),
386 var url = egCore.env.basePath +
387 'cat/item/' + cid + '/holds';
388 $timeout(function() { $window.open(url, '_blank') });
393 $scope.selectedHoldingsDamaged = function () {
394 egCirc.mark_damaged(gatherSelectedHoldingsIds()).then(function() {
396 rid : $scope.record_id,
397 org : $scope.holdings_ou,
398 copy: $scope.holdings_show_copies,
399 vol : $scope.holdings_show_vols,
400 empty: $scope.holdings_show_empty
402 $scope.holdingsGridDataProvider.refresh();
407 $scope.selectedHoldingsMissing = function () {
408 egCirc.mark_missing(gatherSelectedHoldingsIds()).then(function() {
410 rid : $scope.record_id,
411 org : $scope.holdings_ou,
412 copy: $scope.holdings_show_copies,
413 vol : $scope.holdings_show_vols,
414 empty: $scope.holdings_show_empty
416 $scope.holdingsGridDataProvider.refresh();
422 // ------------------------------------------------------------------
424 var provider = egGridDataProvider.instance({});
425 $scope.hold_grid_data_provider = provider;
426 $scope.grid_actions = egHoldGridActions;
427 $scope.grid_actions.refresh = function () { provider.refresh() };
428 $scope.hold_grid_controls = {};
430 var hold_ids = []; // current list of holds
431 function fetchHolds(offset, count) {
432 var ids = hold_ids.slice(offset, offset + count);
433 return egHolds.fetch_holds(ids).then(null, null,
434 function(hold_data) {
440 provider.get = function(offset, count) {
441 if ($scope.record_tab != 'holds') return $q.when();
442 var deferred = $q.defer();
443 hold_ids = []; // no caching ATM
448 'open-ils.circ.holds.retrieve_all_from_title',
449 egCore.auth.token(), $scope.record_id,
450 {pickup_lib : egCore.org.descendants($scope.pickup_ou.id(), true)}
452 function(hold_data) {
453 angular.forEach(hold_data, function(list, type) {
454 hold_ids = hold_ids.concat(list);
456 fetchHolds(offset, count).then(
457 deferred.resolve, null, deferred.notify);
461 return deferred.promise;
464 $scope.detail_view = function(action, user_data, items) {
466 $scope.detail_hold_id = h.hold.id();
470 $scope.list_view = function(items) {
471 $scope.detail_hold_id = null;
474 // refresh the list of record holds when the pickup lib is changed.
475 $scope.pickup_ou = egCore.org.get(egCore.auth.user().ws_ou());
476 $scope.pickup_ou_changed = function(org) {
477 $scope.pickup_ou = org;
481 $scope.print_holds = function() {
483 angular.forEach($scope.hold_grid_controls.allItems(), function(item) {
485 hold : egCore.idl.toHash(item.hold),
486 patron_last : item.patron_last,
487 patron_alias : item.patron_alias,
488 patron_barcode : item.patron_barcode,
489 copy : egCore.idl.toHash(item.copy),
490 volume : egCore.idl.toHash(item.volume),
491 title : item.mvr.title(),
492 author : item.mvr.author()
498 template : 'holds_for_bib',
499 scope : {holds : holds}
503 $scope.mark_hold_transfer_dest = function() {
504 egCore.hatch.setLocalItem(
505 'eg.circ.hold.title_transfer_target', $scope.record_id);
508 // UI presents this option as "all holds"
509 $scope.transfer_holds_to_marked = function() {
510 var hold_ids = $scope.hold_grid_controls.allItems().map(
511 function(hold_data) {return hold_data.hold.id()});
512 egHolds.transfer_to_marked_title(hold_ids);
515 // ------------------------------------------------------------------
516 // Initialize the selected tab
518 function init_cat_url() {
519 // Set the initial catalog URL. This only happens once.
520 // The URL is otherwise generated through user navigation.
521 if ($scope.catalog_url) return;
523 var url = $location.absUrl().replace(/\/staff.*/, '/opac/advanced');
525 // A record ID in the path indicates a request for the record-
527 if ($routeParams.record_id) {
528 url = url.replace(/advanced/, '/record/' + $scope.record_id);
531 $scope.catalog_url = url;
534 function init_parts_url() {
535 $scope.parts_url = $location
539 '/conify/global/biblio/monograph_part?r='+$scope.record_id
543 $scope.set_record_tab = function(tab) {
544 $scope.record_tab = tab;
557 $scope.detail_hold_record_id = $scope.record_id;
558 // refresh the holds grid
564 $scope.set_default_record_tab = function() {
565 egCore.hatch.setLocalItem(
566 'eg.cat.default_record_tab', $scope.record_tab);
567 $timeout(function(){$scope.default_tab = $scope.record_tab});
571 if ($scope.record_id) {
572 $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
573 tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
576 tab = $routeParams.record_tab || 'catalog';
578 $scope.set_record_tab(tab);
582 .controller('AuthorityCtrl',
583 ['$scope','$routeParams','$location','$window','$q','egCore',
584 function($scope , $routeParams , $location , $window , $q , egCore) {
586 // set record ID on page load if available...
587 $scope.authority_id = $routeParams.authority_id;
589 if ($routeParams.authority_id) $scope.from_route = true;
590 else $scope.from_route = false;
592 $scope.stop_unload = false;
595 .controller('URLVerifyCtrl',
596 ['$scope','$location',
597 function($scope , $location) {
598 $scope.verifyurls_url = $location.absUrl().replace(/\/staff.*/, '/url_verify/sessions');
601 .controller('VandelayCtrl',
602 ['$scope','$location',
603 function($scope , $location) {
604 $scope.vandelay_url = $location.absUrl().replace(/\/staff.*/, '/vandelay/vandelay');
607 .controller('ManageAuthoritiesCtrl',
608 ['$scope','$location',
609 function($scope , $location) {
610 $scope.manageauthorities_url = $location.absUrl().replace(/\/staff.*/, '/cat/authority/list');
613 .controller('BatchEditCtrl',
614 ['$scope','$location','$routeParams',
615 function($scope , $location , $routeParams) {
616 $scope.batchedit_url = $location.absUrl().replace(/\/eg.*/, '/opac/extras/merge_template');
617 if ($routeParams.container_type) {
618 switch ($routeParams.container_type) {
620 $scope.batchedit_url += '?recordSource=b&containerid=' + $routeParams.container_id;
623 $scope.batchedit_url += '?recordSource=r&recid=' + $routeParams.container_id;
630 .filter('boolText', function(){
631 return function (v) {
636 .factory('holdingsSvc',
638 function(egCore , $q) {
642 copies : [], // record search results
643 index : 0, // search grid index
651 acp : ['status','location'],
652 acn : ['prefix','suffix','copies']
656 // resolved with the last received copy
657 service.fetch = function(opts) {
658 if (service.ongoing) {
659 console.log('Skipping fetch, ongoing = true');
665 var copy = opts.copy;
667 var empty = opts.empty;
669 if (!rid) return $q.when();
670 if (!org) return $q.when();
672 service.ongoing = true;
679 var org_list = egCore.org.descendants(org.id(), true);
680 console.log('Holdings fetch with: rid='+rid+' org='+org_list+' copy='+copy+' vol='+vol+' empty='+empty);
682 return egCore.pcrud.search(
684 {record : rid, owning_lib : org_list, deleted : 'f'},
687 function() { // finished
688 service.copies = service.copies.sort(
690 function compare_array (x, y, i) {
691 if (x[i] && y[i]) { // both have values
692 if (x[i] == y[i]) { // need to look deeper
693 return compare_array(x, y, ++i);
696 if (x[i] < y[i]) { // x is first
698 } else if (x[i] > y[i]) { // y is first
702 } else { // no orgs to compare ...
709 var owner_order = compare_array(a.owner_list, b.owner_list, 0);
711 // now compare on CN label
712 if (a.call_number.label < b.call_number.label) return -1;
713 if (a.call_number.label > b.call_number.label) return 1;
716 if (a.copy_number < b.copy_number) return -1;
717 if (a.copy_number > b.copy_number) return 1;
720 if (a.barcode < b.barcode) return -1;
721 if (a.barcode > b.barcode) return 1;
727 // create a label using just the unique part of the owner list
730 angular.forEach(service.copies, function (cp) {
731 if (!prev_owner_list) {
732 cp.owner_label = cp.owner_list.join(' ... ');
734 var current_owner_list = cp.owner_list.slice();
735 while (current_owner_list[1] && prev_owner_list[1] && current_owner_list[0] == prev_owner_list[0]) {
736 current_owner_list.shift();
737 prev_owner_list.shift();
739 cp.owner_label = current_owner_list.join(' ... ');
743 prev_owner_list = cp.owner_list.slice();
746 var new_list = service.copies;
747 if (!copy || !vol) { // collapse copy rows, supply a count instead
752 var current_blob = {};
753 angular.forEach(new_list, function (cp) {
755 prev_key = cp.owner_list.join('') + cp.call_number.label;
756 if (cp.barcode) current_blob.copy_count = 1;
757 current_blob.index = index++;
758 current_blob.id_list = cp.id_list;
759 current_blob.call_number = cp.call_number;
760 current_blob.owner_list = cp.owner_list;
761 current_blob.owner_label = cp.owner_label;
763 var current_key = cp.owner_list.join('') + cp.call_number.label;
764 if (prev_key == current_key) { // collapse into current_blob
765 current_blob.copy_count++;
766 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
768 current_blob.barcode = current_blob.copy_count;
769 cp_list.push(current_blob);
770 prev_key = current_key;
772 if (cp.barcode) current_blob.copy_count = 1;
773 current_blob.index = index++;
774 current_blob.id_list = cp.id_list;
775 current_blob.owner_label = cp.owner_label;
776 current_blob.call_number = cp.call_number;
777 current_blob.owner_list = cp.owner_list;
782 current_blob.barcode = current_blob.copy_count;
783 cp_list.push(current_blob);
786 if (!vol) { // do the same for vol rows
791 var current_blob = {};
792 angular.forEach(cp_list, function (cp) {
794 prev_key = cp.owner_list.join('');
795 current_blob.index = index++;
796 current_blob.id_list = cp.id_list;
797 current_blob.cn_count = 1;
798 current_blob.copy_count = cp.copy_count;
799 current_blob.owner_list = cp.owner_list;
800 current_blob.owner_label = cp.owner_label;
802 var current_key = cp.owner_list.join('');
803 if (prev_key == current_key) { // collapse into current_blob
804 current_blob.cn_count++;
805 current_blob.copy_count += cp.copy_count;
806 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
808 current_blob.barcode = current_blob.copy_count;
809 current_blob.call_number = { label : current_blob.cn_count };
810 cn_list.push(current_blob);
811 prev_key = current_key;
813 current_blob.index = index++;
814 current_blob.id_list = cp.id_list;
815 current_blob.owner_label = cp.owner_label;
816 current_blob.cn_count = 1;
817 current_blob.copy_count = cp.copy_count;
818 current_blob.owner_list = cp.owner_list;
823 current_blob.barcode = current_blob.copy_count;
824 current_blob.call_number = { label : current_blob.cn_count };
825 cn_list.push(current_blob);
831 service.copies = new_list;
832 service.ongoing = false;
837 // notify reads the stream of copies, one at a time.
840 var copies = cn.copies();
843 angular.forEach(copies, function (cp) {
847 var flat = egCore.idl.toHash(copies);
849 var owner = egCore.org.get(flat[0].call_number.owning_lib);
851 var owner_name_list = [];
852 while (owner.parent_ou()) { // we're going to skip the top of the tree...
853 owner_name_list.unshift(owner.name());
854 owner = egCore.org.get(owner.parent_ou());
857 angular.forEach(flat, function (cp) {
858 cp.owner_list = owner_name_list;
859 cp.id_list = [cp.id];
862 service.copies = service.copies.concat(flat);
864 if (empty && flat.length == 0) {
865 service.copies.push({
866 owner_list : owner_name_list,
867 call_number: egCore.idl.toHash(cn)