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.when('/cat/catalog/authority/:authority_id/marc_edit', {
82 templateUrl: './cat/catalog/t_authority',
83 controller: 'AuthorityCtrl',
87 $routeProvider.otherwise({redirectTo : '/cat/catalog/index'});
93 .controller('CatalogRecordRetrieve',
94 ['$scope','$routeParams','$location','$q','egCore',
95 function($scope , $routeParams , $location , $q , egCore ) {
97 $scope.focusMe = true;
99 // jump to the patron checkout UI
100 function loadRecord(record_id) {
102 .path('/cat/catalog/record/' + record_id);
105 $scope.submitId = function(args) {
106 $scope.recordNotFound = null;
107 if (!args.record_id) return;
109 // blur so next time it's set to true it will re-apply select()
110 $scope.selectMe = false;
112 return loadRecord(args.record_id);
115 $scope.submitTCN = function(args) {
116 $scope.recordNotFound = null;
117 $scope.moreRecordsFound = null;
118 if (!args.record_tcn) return;
120 // blur so next time it's set to true it will re-apply select()
121 $scope.selectMe = false;
126 'open-ils.search.biblio.tcn',
129 .then(function(resp) { // get_barcodes
131 if (evt = egCore.evt.parse(resp)) {
137 $scope.recordNotFound = args.record_tcn;
138 $scope.selectMe = true;
142 if (resp.count > 1) {
143 $scope.moreRecordsFound = args.record_tcn;
144 $scope.selectMe = true;
148 var record_id = resp.ids[0];
149 return loadRecord(record_id);
155 .controller('CatalogCtrl',
156 ['$scope','$routeParams','$location','$window','$q','egCore','egHolds','egCirc',
157 'egGridDataProvider','egHoldGridActions','$timeout','holdingsSvc',
158 function($scope , $routeParams , $location , $window , $q , egCore , egHolds , egCirc,
159 egGridDataProvider , egHoldGridActions , $timeout , holdingsSvc) {
161 // set record ID on page load if available...
162 $scope.record_id = $routeParams.record_id;
164 if ($routeParams.record_id) $scope.from_route = true;
165 else $scope.from_route = false;
167 // will hold a ref to the opac iframe
168 $scope.opac_iframe = null;
169 $scope.parts_iframe = null;
171 $scope.in_opac_call = false;
172 $scope.opac_call = function (opac_frame_function, force_opac_tab) {
173 if ($scope.opac_iframe) {
174 if (force_opac_tab) $scope.record_tab = 'catalog';
175 $scope.in_opac_call = true;
176 $scope.opac_iframe.dom.contentWindow[opac_frame_function]();
180 $scope.stop_unload = false;
181 $scope.$watch('stop_unload',
182 function(newVal, oldVal) {
183 if (newVal && newVal != oldVal && $scope.opac_iframe) {
184 $($scope.opac_iframe.dom.contentWindow).on('beforeunload', function(){
185 return 'There is unsaved data in this record.'
188 if ($scope.opac_iframe)
189 $($scope.opac_iframe.dom.contentWindow).off('beforeunload');
194 // Set the "last bib" cookie, if we have that
195 if ($scope.record_id)
196 egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
198 // also set it when the iframe changes to a new record
199 $scope.handle_page = function(url) {
201 if (!url || url == 'about:blank') {
202 // nothing loaded. If we already have a record ID, leave it.
206 var match = url.match(/\/+opac\/+record\/+(\d+)/);
208 $scope.record_id = match[1];
209 egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
210 $scope.holdings_record_id_changed($scope.record_id);
213 delete $scope.record_id;
214 $scope.from_route = false;
217 // child scope is executing this function, so our digest doesn't fire ... thus,
220 if (!$scope.in_opac_call) {
221 if ($scope.record_id) {
222 $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
223 tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
225 tab = $routeParams.record_tab || 'catalog';
227 $scope.set_record_tab(tab);
229 $scope.in_opac_call = false;
233 // xulG catalog handlers
234 $scope.handlers = { }
236 // ------------------------------------------------------------------
239 $scope.holdingsGridControls = {};
240 $scope.holdingsGridDataProvider = egGridDataProvider.instance({
241 get : function(offset, count) {
242 return this.arrayNotifier(holdingsSvc.copies, offset, count);
246 // refresh the list of holdings when the record_id is changed.
247 $scope.holdings_record_id_changed = function(id) {
248 if ($scope.record_id != id) $scope.record_id = id;
249 console.log('record id changed to ' + id + ', loading new holdings');
251 rid : $scope.record_id,
252 org : $scope.holdings_ou,
253 copy: $scope.holdings_show_copies,
254 vol : $scope.holdings_show_vols,
255 empty: $scope.holdings_show_empty
257 $scope.holdingsGridDataProvider.refresh();
261 // refresh the list of holdings when the filter lib is changed.
262 $scope.holdings_ou = egCore.org.get(egCore.auth.user().ws_ou());
263 $scope.holdings_ou_changed = function(org) {
264 $scope.holdings_ou = org;
266 rid : $scope.record_id,
267 org : $scope.holdings_ou,
268 copy: $scope.holdings_show_copies,
269 vol : $scope.holdings_show_vols,
270 empty: $scope.holdings_show_empty
272 $scope.holdingsGridDataProvider.refresh();
276 $scope.holdings_cb_changed = function(cb,newVal,norefresh) {
278 egCore.hatch.setItem('cat.' + cb, newVal);
279 if (!norefresh) holdingsSvc.fetch({
280 rid : $scope.record_id,
281 org : $scope.holdings_ou,
282 copy: $scope.holdings_show_copies,
283 vol : $scope.holdings_show_vols,
284 empty: $scope.holdings_show_empty
286 $scope.holdingsGridDataProvider.refresh();
290 egCore.hatch.getItem('cat.holdings_show_vols').then(function(x){
291 if (typeof x == 'undefined') x = true;
292 $scope.holdings_cb_changed('holdings_show_vols',x,true);
293 $('#holdings_show_vols').prop('checked', x);
295 egCore.hatch.getItem('cat.holdings_show_copies').then(function(x){
296 if (typeof x == 'undefined') x = true;
297 $scope.holdings_cb_changed('holdings_show_copies',x,true);
298 $('#holdings_show_copies').prop('checked', x);
300 egCore.hatch.getItem('cat.holdings_show_empty').then(function(x){
301 if (typeof x == 'undefined') x = true;
302 $scope.holdings_cb_changed('holdings_show_empty',x);
303 $('#holdings_show_empty').prop('checked', x);
308 $scope.holdings_checkbox_handler = function (item) {
309 $scope.holdings_cb_changed(item.checkbox,item.checked);
312 function gatherSelectedHoldingsIds () {
315 $scope.holdingsGridControls.selectedItems(),
316 function (item) { cp_id_list = cp_id_list.concat(item.id_list) }
321 spawnHoldingsEdit = function (hide_vols,hide_copies){
324 'open-ils.actor.anon_cache.set_value',
325 null, 'edit-these-copies', {
326 record_id: $scope.record_id,
327 copies: gatherSelectedHoldingsIds(),
328 hide_vols : hide_vols,
329 hide_copies : hide_copies
331 ).then(function(key) {
333 var url = egCore.env.basePath + 'cat/volcopy/' + key;
334 $timeout(function() { $window.open(url, '_blank') });
336 alert('Could not create anonymous cache key!');
340 $scope.selectedHoldingsVolCopyEdit = function () { spawnHoldingsEdit(false,false) }
341 $scope.selectedHoldingsVolEdit = function () { spawnHoldingsEdit(false,true) }
342 $scope.selectedHoldingsCopyEdit = function () { spawnHoldingsEdit(true,false) }
344 $scope.selectedHoldingsItemStatus = function (){
345 var url = egCore.env.basePath + 'cat/item/search/' + gatherSelectedHoldingsIds().join(',')
346 $timeout(function() { $window.open(url, '_blank') });
349 $scope.selectedHoldingsItemStatusDetail = function (){
351 gatherSelectedHoldingsIds(),
353 var url = egCore.env.basePath +
355 $timeout(function() { $window.open(url, '_blank') });
360 $scope.selectedHoldingsItemStatusTgrEvt = function (){
362 gatherSelectedHoldingsIds(),
364 var url = egCore.env.basePath +
365 'cat/item/' + cid + '/triggered_events';
366 $timeout(function() { $window.open(url, '_blank') });
371 $scope.selectedHoldingsItemStatusHolds = function (){
373 gatherSelectedHoldingsIds(),
375 var url = egCore.env.basePath +
376 'cat/item/' + cid + '/holds';
377 $timeout(function() { $window.open(url, '_blank') });
382 $scope.selectedHoldingsDamaged = function () {
383 egCirc.mark_damaged(gatherSelectedHoldingsIds()).then(function() {
385 rid : $scope.record_id,
386 org : $scope.holdings_ou,
387 copy: $scope.holdings_show_copies,
388 vol : $scope.holdings_show_vols,
389 empty: $scope.holdings_show_empty
391 $scope.holdingsGridDataProvider.refresh();
396 $scope.selectedHoldingsMissing = function () {
397 egCirc.mark_missing(gatherSelectedHoldingsIds()).then(function() {
399 rid : $scope.record_id,
400 org : $scope.holdings_ou,
401 copy: $scope.holdings_show_copies,
402 vol : $scope.holdings_show_vols,
403 empty: $scope.holdings_show_empty
405 $scope.holdingsGridDataProvider.refresh();
411 // ------------------------------------------------------------------
413 var provider = egGridDataProvider.instance({});
414 $scope.hold_grid_data_provider = provider;
415 $scope.grid_actions = egHoldGridActions;
416 $scope.grid_actions.refresh = function () { provider.refresh() };
417 $scope.hold_grid_controls = {};
419 var hold_ids = []; // current list of holds
420 function fetchHolds(offset, count) {
421 var ids = hold_ids.slice(offset, offset + count);
422 return egHolds.fetch_holds(ids).then(null, null,
423 function(hold_data) {
429 provider.get = function(offset, count) {
430 if ($scope.record_tab != 'holds') return $q.when();
431 var deferred = $q.defer();
432 hold_ids = []; // no caching ATM
437 'open-ils.circ.holds.retrieve_all_from_title',
438 egCore.auth.token(), $scope.record_id,
439 {pickup_lib : egCore.org.descendants($scope.pickup_ou.id(), true)}
441 function(hold_data) {
442 angular.forEach(hold_data, function(list, type) {
443 hold_ids = hold_ids.concat(list);
445 fetchHolds(offset, count).then(
446 deferred.resolve, null, deferred.notify);
450 return deferred.promise;
453 $scope.detail_view = function(action, user_data, items) {
455 $scope.detail_hold_id = h.hold.id();
459 $scope.list_view = function(items) {
460 $scope.detail_hold_id = null;
463 // refresh the list of record holds when the pickup lib is changed.
464 $scope.pickup_ou = egCore.org.get(egCore.auth.user().ws_ou());
465 $scope.pickup_ou_changed = function(org) {
466 $scope.pickup_ou = org;
470 $scope.print_holds = function() {
472 angular.forEach($scope.hold_grid_controls.allItems(), function(item) {
474 hold : egCore.idl.toHash(item.hold),
475 patron_last : item.patron_last,
476 patron_alias : item.patron_alias,
477 patron_barcode : item.patron_barcode,
478 copy : egCore.idl.toHash(item.copy),
479 volume : egCore.idl.toHash(item.volume),
480 title : item.mvr.title(),
481 author : item.mvr.author()
487 template : 'holds_for_bib',
488 scope : {holds : holds}
492 $scope.mark_hold_transfer_dest = function() {
493 egCore.hatch.setLocalItem(
494 'eg.circ.hold.title_transfer_target', $scope.record_id);
497 // UI presents this option as "all holds"
498 $scope.transfer_holds_to_marked = function() {
499 var hold_ids = $scope.hold_grid_controls.allItems().map(
500 function(hold_data) {return hold_data.hold.id()});
501 egHolds.transfer_to_marked_title(hold_ids);
504 // ------------------------------------------------------------------
505 // Initialize the selected tab
507 function init_cat_url() {
508 // Set the initial catalog URL. This only happens once.
509 // The URL is otherwise generated through user navigation.
510 if ($scope.catalog_url) return;
512 var url = $location.absUrl().replace(/\/staff.*/, '/opac/advanced');
514 // A record ID in the path indicates a request for the record-
516 if ($routeParams.record_id) {
517 url = url.replace(/advanced/, '/record/' + $scope.record_id);
520 $scope.catalog_url = url;
523 function init_parts_url() {
524 $scope.parts_url = $location
528 '/conify/global/biblio/monograph_part?r='+$scope.record_id
532 $scope.set_record_tab = function(tab) {
533 $scope.record_tab = tab;
546 $scope.detail_hold_record_id = $scope.record_id;
547 // refresh the holds grid
553 $scope.set_default_record_tab = function() {
554 egCore.hatch.setLocalItem(
555 'eg.cat.default_record_tab', $scope.record_tab);
556 $timeout(function(){$scope.default_tab = $scope.record_tab});
560 if ($scope.record_id) {
561 $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
562 tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
565 tab = $routeParams.record_tab || 'catalog';
567 $scope.set_record_tab(tab);
571 .controller('AuthorityCtrl',
572 ['$scope','$routeParams','$location','$window','$q','egCore',
573 function($scope , $routeParams , $location , $window , $q , egCore) {
575 // set record ID on page load if available...
576 $scope.authority_id = $routeParams.authority_id;
578 if ($routeParams.authority_id) $scope.from_route = true;
579 else $scope.from_route = false;
581 $scope.stop_unload = false;
584 .controller('URLVerifyCtrl',
585 ['$scope','$location',
586 function($scope , $location) {
587 $scope.verifyurls_url = $location.absUrl().replace(/\/staff.*/, '/url_verify/sessions');
590 .controller('VandelayCtrl',
591 ['$scope','$location',
592 function($scope , $location) {
593 $scope.vandelay_url = $location.absUrl().replace(/\/staff.*/, '/vandelay/vandelay');
596 .controller('ManageAuthoritiesCtrl',
597 ['$scope','$location',
598 function($scope , $location) {
599 $scope.manageauthorities_url = $location.absUrl().replace(/\/staff.*/, '/cat/authority/list');
602 .controller('BatchEditCtrl',
603 ['$scope','$location','$routeParams',
604 function($scope , $location , $routeParams) {
605 $scope.batchedit_url = $location.absUrl().replace(/\/eg.*/, '/opac/extras/merge_template');
606 if ($routeParams.container_type) {
607 switch ($routeParams.container_type) {
609 $scope.batchedit_url += '?recordSource=b&containerid=' + $routeParams.container_id;
612 $scope.batchedit_url += '?recordSource=r&recid=' + $routeParams.container_id;
619 .filter('boolText', function(){
620 return function (v) {
625 .factory('holdingsSvc',
627 function(egCore , $q) {
631 copies : [], // record search results
632 index : 0, // search grid index
640 acp : ['status','location'],
641 acn : ['prefix','suffix','copies']
645 // resolved with the last received copy
646 service.fetch = function(opts) {
647 if (service.ongoing) {
648 console.log('Skipping fetch, ongoing = true');
654 var copy = opts.copy;
656 var empty = opts.empty;
658 if (!rid) return $q.when();
659 if (!org) return $q.when();
661 service.ongoing = true;
668 var org_list = egCore.org.descendants(org.id(), true);
669 console.log('Holdings fetch with: rid='+rid+' org='+org_list+' copy='+copy+' vol='+vol+' empty='+empty);
671 return egCore.pcrud.search(
673 {record : rid, owning_lib : org_list, deleted : 'f'},
676 function() { // finished
677 service.copies = service.copies.sort(
679 function compare_array (x, y, i) {
680 if (x[i] && y[i]) { // both have values
681 if (x[i] == y[i]) { // need to look deeper
682 return compare_array(x, y, ++i);
685 if (x[i] < y[i]) { // x is first
687 } else if (x[i] > y[i]) { // y is first
691 } else { // no orgs to compare ...
698 var owner_order = compare_array(a.owner_list, b.owner_list, 0);
700 // now compare on CN label
701 if (a.call_number.label < b.call_number.label) return -1;
702 if (a.call_number.label > b.call_number.label) return 1;
705 if (a.copy_number < b.copy_number) return -1;
706 if (a.copy_number > b.copy_number) return 1;
709 if (a.barcode < b.barcode) return -1;
710 if (a.barcode > b.barcode) return 1;
716 // create a label using just the unique part of the owner list
719 angular.forEach(service.copies, function (cp) {
720 if (!prev_owner_list) {
721 cp.owner_label = cp.owner_list.join(' ... ');
723 var current_owner_list = cp.owner_list.slice();
724 while (current_owner_list[1] && prev_owner_list[1] && current_owner_list[0] == prev_owner_list[0]) {
725 current_owner_list.shift();
726 prev_owner_list.shift();
728 cp.owner_label = current_owner_list.join(' ... ');
732 prev_owner_list = cp.owner_list.slice();
735 var new_list = service.copies;
736 if (!copy || !vol) { // collapse copy rows, supply a count instead
741 var current_blob = {};
742 angular.forEach(new_list, function (cp) {
744 prev_key = cp.owner_list.join('') + cp.call_number.label;
745 if (cp.barcode) current_blob.copy_count = 1;
746 current_blob.index = index++;
747 current_blob.id_list = cp.id_list;
748 current_blob.call_number = cp.call_number;
749 current_blob.owner_list = cp.owner_list;
750 current_blob.owner_label = cp.owner_label;
752 var current_key = cp.owner_list.join('') + cp.call_number.label;
753 if (prev_key == current_key) { // collapse into current_blob
754 current_blob.copy_count++;
755 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
757 current_blob.barcode = current_blob.copy_count;
758 cp_list.push(current_blob);
759 prev_key = current_key;
761 if (cp.barcode) current_blob.copy_count = 1;
762 current_blob.index = index++;
763 current_blob.id_list = cp.id_list;
764 current_blob.owner_label = cp.owner_label;
765 current_blob.call_number = cp.call_number;
766 current_blob.owner_list = cp.owner_list;
771 current_blob.barcode = current_blob.copy_count;
772 cp_list.push(current_blob);
775 if (!vol) { // do the same for vol rows
780 var current_blob = {};
781 angular.forEach(cp_list, function (cp) {
783 prev_key = cp.owner_list.join('');
784 current_blob.index = index++;
785 current_blob.id_list = cp.id_list;
786 current_blob.cn_count = 1;
787 current_blob.copy_count = cp.copy_count;
788 current_blob.owner_list = cp.owner_list;
789 current_blob.owner_label = cp.owner_label;
791 var current_key = cp.owner_list.join('');
792 if (prev_key == current_key) { // collapse into current_blob
793 current_blob.cn_count++;
794 current_blob.copy_count += cp.copy_count;
795 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
797 current_blob.barcode = current_blob.copy_count;
798 current_blob.call_number = { label : current_blob.cn_count };
799 cn_list.push(current_blob);
800 prev_key = current_key;
802 current_blob.index = index++;
803 current_blob.id_list = cp.id_list;
804 current_blob.owner_label = cp.owner_label;
805 current_blob.cn_count = 1;
806 current_blob.copy_count = cp.copy_count;
807 current_blob.owner_list = cp.owner_list;
812 current_blob.barcode = current_blob.copy_count;
813 current_blob.call_number = { label : current_blob.cn_count };
814 cn_list.push(current_blob);
820 service.copies = new_list;
821 service.ongoing = false;
826 // notify reads the stream of copies, one at a time.
829 var copies = cn.copies();
832 angular.forEach(copies, function (cp) {
836 var flat = egCore.idl.toHash(copies);
838 var owner = egCore.org.get(flat[0].call_number.owning_lib);
840 var owner_name_list = [];
841 while (owner.parent_ou()) { // we're going to skip the top of the tree...
842 owner_name_list.unshift(owner.name());
843 owner = egCore.org.get(owner.parent_ou());
846 angular.forEach(flat, function (cp) {
847 cp.owner_list = owner_name_list;
848 cp.id_list = [cp.id];
851 service.copies = service.copies.concat(flat);
853 if (empty && flat.length == 0) {
854 service.copies.push({
855 owner_list : owner_name_list,
856 call_number: egCore.idl.toHash(cn)