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);
204 $scope.holdings_record_id_changed($scope.record_id);
207 delete $scope.record_id;
208 $scope.from_route = false;
211 // child scope is executing this function, so our digest doesn't fire ... thus,
214 if (!$scope.in_opac_call) {
215 if ($scope.record_id) {
216 $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
217 tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
219 tab = $routeParams.record_tab || 'catalog';
221 $scope.set_record_tab(tab);
223 $scope.in_opac_call = false;
227 // xulG catalog handlers
228 $scope.handlers = { }
230 // ------------------------------------------------------------------
233 $scope.holdingsGridControls = {};
234 $scope.holdingsGridDataProvider = egGridDataProvider.instance({
235 get : function(offset, count) {
236 return this.arrayNotifier(holdingsSvc.copies, offset, count);
240 // refresh the list of holdings when the record_id is changed.
241 $scope.holdings_record_id_changed = function(id) {
242 if ($scope.record_id != id) $scope.record_id = id;
243 console.log('record id changed to ' + id + ', loading new holdings');
245 rid : $scope.record_id,
246 org : $scope.holdings_ou,
247 copy: $scope.holdings_show_copies,
248 vol : $scope.holdings_show_vols,
249 empty: $scope.holdings_show_empty
251 $scope.holdingsGridDataProvider.refresh();
255 // refresh the list of holdings when the filter lib is changed.
256 $scope.holdings_ou = egCore.org.get(egCore.auth.user().ws_ou());
257 $scope.holdings_ou_changed = function(org) {
258 $scope.holdings_ou = org;
260 rid : $scope.record_id,
261 org : $scope.holdings_ou,
262 copy: $scope.holdings_show_copies,
263 vol : $scope.holdings_show_vols,
264 empty: $scope.holdings_show_empty
266 $scope.holdingsGridDataProvider.refresh();
270 $scope.holdings_cb_changed = function(cb,newVal,norefresh) {
272 egCore.hatch.setItem('cat.' + cb, newVal);
273 if (!norefresh) holdingsSvc.fetch({
274 rid : $scope.record_id,
275 org : $scope.holdings_ou,
276 copy: $scope.holdings_show_copies,
277 vol : $scope.holdings_show_vols,
278 empty: $scope.holdings_show_empty
280 $scope.holdingsGridDataProvider.refresh();
284 egCore.hatch.getItem('cat.holdings_show_vols').then(function(x){
285 if (typeof x == 'undefined') x = true;
286 $scope.holdings_cb_changed('holdings_show_vols',x,true);
287 $('#holdings_show_vols').prop('checked', x);
289 egCore.hatch.getItem('cat.holdings_show_copies').then(function(x){
290 if (typeof x == 'undefined') x = true;
291 $scope.holdings_cb_changed('holdings_show_copies',x,true);
292 $('#holdings_show_copies').prop('checked', x);
294 egCore.hatch.getItem('cat.holdings_show_empty').then(function(x){
295 if (typeof x == 'undefined') x = true;
296 $scope.holdings_cb_changed('holdings_show_empty',x);
297 $('#holdings_show_empty').prop('checked', x);
302 $scope.holdings_checkbox_handler = function (item) {
303 $scope.holdings_cb_changed(item.checkbox,item.checked);
306 function gatherSelectedHoldingsIds () {
309 $scope.holdingsGridControls.selectedItems(),
310 function (item) { cp_id_list = cp_id_list.concat(item.id_list) }
315 $scope.selectedHoldingsItemStatus = function (){
316 var url = egCore.env.basePath + 'cat/item/search/' + gatherSelectedHoldingsIds().join(',')
317 $timeout(function() { $window.open(url, '_blank') });
320 $scope.selectedHoldingsItemStatusDetail = function (){
322 gatherSelectedHoldingsIds(),
324 var url = egCore.env.basePath +
326 $timeout(function() { $window.open(url, '_blank') });
331 $scope.selectedHoldingsItemStatusTgrEvt = function (){
333 gatherSelectedHoldingsIds(),
335 var url = egCore.env.basePath +
336 'cat/item/' + cid + '/triggered_events';
337 $timeout(function() { $window.open(url, '_blank') });
342 $scope.selectedHoldingsItemStatusHolds = function (){
344 gatherSelectedHoldingsIds(),
346 var url = egCore.env.basePath +
347 'cat/item/' + cid + '/holds';
348 $timeout(function() { $window.open(url, '_blank') });
353 $scope.selectedHoldingsDamaged = function () {
354 egCirc.mark_damaged(gatherSelectedHoldingsIds()).then(function() {
356 rid : $scope.record_id,
357 org : $scope.holdings_ou,
358 copy: $scope.holdings_show_copies,
359 vol : $scope.holdings_show_vols,
360 empty: $scope.holdings_show_empty
362 $scope.holdingsGridDataProvider.refresh();
367 $scope.selectedHoldingsMissing = function () {
368 egCirc.mark_missing(gatherSelectedHoldingsIds()).then(function() {
370 rid : $scope.record_id,
371 org : $scope.holdings_ou,
372 copy: $scope.holdings_show_copies,
373 vol : $scope.holdings_show_vols,
374 empty: $scope.holdings_show_empty
376 $scope.holdingsGridDataProvider.refresh();
382 // ------------------------------------------------------------------
384 var provider = egGridDataProvider.instance({});
385 $scope.hold_grid_data_provider = provider;
386 $scope.grid_actions = egHoldGridActions;
387 $scope.grid_actions.refresh = function () { provider.refresh() };
388 $scope.hold_grid_controls = {};
390 var hold_ids = []; // current list of holds
391 function fetchHolds(offset, count) {
392 var ids = hold_ids.slice(offset, offset + count);
393 return egHolds.fetch_holds(ids).then(null, null,
394 function(hold_data) {
400 provider.get = function(offset, count) {
401 if ($scope.record_tab != 'holds') return $q.when();
402 var deferred = $q.defer();
403 hold_ids = []; // no caching ATM
408 'open-ils.circ.holds.retrieve_all_from_title',
409 egCore.auth.token(), $scope.record_id,
410 {pickup_lib : egCore.org.descendants($scope.pickup_ou.id(), true)}
412 function(hold_data) {
413 angular.forEach(hold_data, function(list, type) {
414 hold_ids = hold_ids.concat(list);
416 fetchHolds(offset, count).then(
417 deferred.resolve, null, deferred.notify);
421 return deferred.promise;
424 $scope.detail_view = function(action, user_data, items) {
426 $scope.detail_hold_id = h.hold.id();
430 $scope.list_view = function(items) {
431 $scope.detail_hold_id = null;
434 // refresh the list of record holds when the pickup lib is changed.
435 $scope.pickup_ou = egCore.org.get(egCore.auth.user().ws_ou());
436 $scope.pickup_ou_changed = function(org) {
437 $scope.pickup_ou = org;
441 $scope.print_holds = function() {
443 angular.forEach($scope.hold_grid_controls.allItems(), function(item) {
445 hold : egCore.idl.toHash(item.hold),
446 patron_last : item.patron_last,
447 patron_alias : item.patron_alias,
448 patron_barcode : item.patron_barcode,
449 copy : egCore.idl.toHash(item.copy),
450 volume : egCore.idl.toHash(item.volume),
451 title : item.mvr.title(),
452 author : item.mvr.author()
458 template : 'holds_for_bib',
459 scope : {holds : holds}
463 $scope.mark_hold_transfer_dest = function() {
464 egCore.hatch.setLocalItem(
465 'eg.circ.hold.title_transfer_target', $scope.record_id);
468 // UI presents this option as "all holds"
469 $scope.transfer_holds_to_marked = function() {
470 var hold_ids = $scope.hold_grid_controls.allItems().map(
471 function(hold_data) {return hold_data.hold.id()});
472 egHolds.transfer_to_marked_title(hold_ids);
475 // ------------------------------------------------------------------
476 // Initialize the selected tab
478 function init_cat_url() {
479 // Set the initial catalog URL. This only happens once.
480 // The URL is otherwise generated through user navigation.
481 if ($scope.catalog_url) return;
483 var url = $location.absUrl().replace(/\/staff.*/, '/opac/advanced');
485 // A record ID in the path indicates a request for the record-
487 if ($routeParams.record_id) {
488 url = url.replace(/advanced/, '/record/' + $scope.record_id);
491 $scope.catalog_url = url;
494 function init_parts_url() {
495 $scope.parts_url = $location
499 '/conify/global/biblio/monograph_part?r='+$scope.record_id
503 $scope.set_record_tab = function(tab) {
504 $scope.record_tab = tab;
517 $scope.detail_hold_record_id = $scope.record_id;
518 // refresh the holds grid
524 $scope.set_default_record_tab = function() {
525 egCore.hatch.setLocalItem(
526 'eg.cat.default_record_tab', $scope.record_tab);
527 $timeout(function(){$scope.default_tab = $scope.record_tab});
531 if ($scope.record_id) {
532 $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
533 tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
536 tab = $routeParams.record_tab || 'catalog';
538 $scope.set_record_tab(tab);
542 .controller('URLVerifyCtrl',
543 ['$scope','$location',
544 function($scope , $location) {
545 $scope.verifyurls_url = $location.absUrl().replace(/\/staff.*/, '/url_verify/sessions');
548 .controller('VandelayCtrl',
549 ['$scope','$location',
550 function($scope , $location) {
551 $scope.vandelay_url = $location.absUrl().replace(/\/staff.*/, '/vandelay/vandelay');
554 .controller('ManageAuthoritiesCtrl',
555 ['$scope','$location',
556 function($scope , $location) {
557 $scope.manageauthorities_url = $location.absUrl().replace(/\/staff.*/, '/cat/authority/list');
560 .controller('BatchEditCtrl',
561 ['$scope','$location','$routeParams',
562 function($scope , $location , $routeParams) {
563 $scope.batchedit_url = $location.absUrl().replace(/\/eg.*/, '/opac/extras/merge_template');
564 if ($routeParams.container_type) {
565 switch ($routeParams.container_type) {
567 $scope.batchedit_url += '?recordSource=b&containerid=' + $routeParams.container_id;
570 $scope.batchedit_url += '?recordSource=r&recid=' + $routeParams.container_id;
577 .filter('boolText', function(){
578 return function (v) {
583 .factory('holdingsSvc',
585 function(egCore , $q) {
589 copies : [], // record search results
590 index : 0, // search grid index
598 acp : ['status','location'],
599 acn : ['prefix','suffix','copies']
603 // resolved with the last received copy
604 service.fetch = function(opts) {
605 if (service.ongoing) {
606 console.log('Skipping fetch, ongoing = true');
612 var copy = opts.copy;
614 var empty = opts.empty;
616 if (!rid) return $q.when();
617 if (!org) return $q.when();
619 service.ongoing = true;
626 var org_list = egCore.org.descendants(org.id(), true);
627 console.log('Holdings fetch with: rid='+rid+' org='+org_list+' copy='+copy+' vol='+vol+' empty='+empty);
629 return egCore.pcrud.search(
631 {record : rid, owning_lib : org_list, deleted : 'f'},
634 function() { // finished
635 service.copies = service.copies.sort(
637 function compare_array (x, y, i) {
638 if (x[i] && y[i]) { // both have values
639 if (x[i] == y[i]) { // need to look deeper
640 return compare_array(x, y, ++i);
643 if (x[i] < y[i]) { // x is first
645 } else if (x[i] > y[i]) { // y is first
649 } else { // no orgs to compare ...
656 var owner_order = compare_array(a.owner_list, b.owner_list, 0);
658 // now compare on CN label
659 if (a.call_number.label < b.call_number.label) return -1;
660 if (a.call_number.label > b.call_number.label) return 1;
663 if (a.copy_number < b.copy_number) return -1;
664 if (a.copy_number > b.copy_number) return 1;
667 if (a.barcode < b.barcode) return -1;
668 if (a.barcode > b.barcode) return 1;
674 // create a label using just the unique part of the owner list
677 angular.forEach(service.copies, function (cp) {
678 if (!prev_owner_list) {
679 cp.owner_label = cp.owner_list.join(' ... ');
681 var current_owner_list = cp.owner_list.slice();
682 while (current_owner_list[1] && prev_owner_list[1] && current_owner_list[0] == prev_owner_list[0]) {
683 current_owner_list.shift();
684 prev_owner_list.shift();
686 cp.owner_label = current_owner_list.join(' ... ');
690 prev_owner_list = cp.owner_list.slice();
693 var new_list = service.copies;
694 if (!copy || !vol) { // collapse copy rows, supply a count instead
699 var current_blob = {};
700 angular.forEach(new_list, function (cp) {
702 prev_key = cp.owner_list.join('') + cp.call_number.label;
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.call_number = cp.call_number;
707 current_blob.owner_list = cp.owner_list;
708 current_blob.owner_label = cp.owner_label;
710 var current_key = cp.owner_list.join('') + cp.call_number.label;
711 if (prev_key == current_key) { // collapse into current_blob
712 current_blob.copy_count++;
713 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
715 current_blob.barcode = current_blob.copy_count;
716 cp_list.push(current_blob);
717 prev_key = current_key;
719 if (cp.barcode) current_blob.copy_count = 1;
720 current_blob.index = index++;
721 current_blob.id_list = cp.id_list;
722 current_blob.owner_label = cp.owner_label;
723 current_blob.call_number = cp.call_number;
724 current_blob.owner_list = cp.owner_list;
729 current_blob.barcode = current_blob.copy_count;
730 cp_list.push(current_blob);
733 if (!vol) { // do the same for vol rows
738 var current_blob = {};
739 angular.forEach(cp_list, function (cp) {
741 prev_key = cp.owner_list.join('');
742 current_blob.index = index++;
743 current_blob.id_list = cp.id_list;
744 current_blob.cn_count = 1;
745 current_blob.copy_count = cp.copy_count;
746 current_blob.owner_list = cp.owner_list;
747 current_blob.owner_label = cp.owner_label;
749 var current_key = cp.owner_list.join('');
750 if (prev_key == current_key) { // collapse into current_blob
751 current_blob.cn_count++;
752 current_blob.copy_count += cp.copy_count;
753 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
755 current_blob.barcode = current_blob.copy_count;
756 current_blob.call_number = { label : current_blob.cn_count };
757 cn_list.push(current_blob);
758 prev_key = current_key;
760 current_blob.index = index++;
761 current_blob.id_list = cp.id_list;
762 current_blob.owner_label = cp.owner_label;
763 current_blob.cn_count = 1;
764 current_blob.copy_count = cp.copy_count;
765 current_blob.owner_list = cp.owner_list;
770 current_blob.barcode = current_blob.copy_count;
771 current_blob.call_number = { label : current_blob.cn_count };
772 cn_list.push(current_blob);
778 service.copies = new_list;
779 service.ongoing = false;
784 // notify reads the stream of copies, one at a time.
787 var copies = cn.copies();
790 angular.forEach(copies, function (cp) {
794 var flat = egCore.idl.toHash(copies);
796 var owner = egCore.org.get(flat[0].call_number.owning_lib);
798 var owner_name_list = [];
799 while (owner.parent_ou()) { // we're going to skip the top of the tree...
800 owner_name_list.unshift(owner.name());
801 owner = egCore.org.get(owner.parent_ou());
804 angular.forEach(flat, function (cp) {
805 cp.owner_list = owner_name_list;
806 cp.id_list = [cp.id];
809 service.copies = service.copies.concat(flat);
811 if (empty && flat.length == 0) {
812 service.copies.push({
813 owner_list : owner_name_list,
814 call_number: egCore.idl.toHash(cn)