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','$q','egCore','egHolds',
151 'egGridDataProvider','egHoldGridActions','$timeout','holdingsSvc',
152 function($scope , $routeParams , $location , $q , egCore , egHolds,
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.holdings_ou = null;
175 $scope.$watch('holdings_show_copies',
176 function(newVal, oldVal) {
177 if (newVal != oldVal) {
178 egCore.hatch.setItem('cat.holdings.show_copies', newVal);
180 rid : $scope.record_id,
181 org : $scope.holdings_ou,
183 vol : $scope.holdings_show_vols,
184 empty: $scope.holdings_show_empty
186 $scope.holdingsGridDataProvider.refresh();
192 $scope.$watch('holdings_show_vols',
193 function(newVal, oldVal) {
194 if (newVal != oldVal) {
195 egCore.hatch.setItem('cat.holdings.show_vols', newVal);
197 rid : $scope.record_id,
198 org : $scope.holdings_ou,
199 copy: $scope.holdings_show_copies,
201 empty: $scope.holdings_show_empty
203 $scope.holdingsGridDataProvider.refresh();
209 $scope.$watch('holdings_show_empty',
210 function(newVal, oldVal) {
211 if (newVal != oldVal) {
212 egCore.hatch.setItem('cat.holdings.show_empty', newVal);
214 rid : $scope.record_id,
215 org : $scope.holdings_ou,
216 copy: $scope.holdings_show_copies,
217 vol : $scope.holdings_show_vols,
220 $scope.holdingsGridDataProvider.refresh();
226 egCore.hatch.getItem('cat.holdings.show_copies').then(function(x){
227 if (x === null) x = true;
228 $scope.holdings_show_copies = x;
231 egCore.hatch.getItem('cat.holdings.show_vols').then(function(x){
232 if (x === null) x = true;
233 $scope.holdings_show_vols = x;
236 egCore.hatch.getItem('cat.holdings.show_emtpy').then(function(x){
237 if (x === null) x = false;
238 $scope.holdings_show_empty = x;
241 $scope.holdings_checkbox_handler = function (item) { $scope[item.checkbox] = item.checked; }
243 $scope.stop_unload = false;
244 $scope.$watch('stop_unload',
245 function(newVal, oldVal) {
246 if (newVal && newVal != oldVal && $scope.opac_iframe) {
247 $($scope.opac_iframe.dom.contentWindow).on('beforeunload', function(){
248 return 'There is unsaved data in this record.'
251 if ($scope.opac_iframe)
252 $($scope.opac_iframe.dom.contentWindow).off('beforeunload');
257 // Set the "last bib" cookie, if we have that
258 if ($scope.record_id)
259 egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
261 // also set it when the iframe changes to a new record
262 $scope.handle_page = function(url) {
264 if (!url || url == 'about:blank') {
265 // nothing loaded. If we already have a record ID, leave it.
269 var match = url.match(/\/+opac\/+record\/+(\d+)/);
271 $scope.record_id = match[1];
272 egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
275 delete $scope.record_id;
276 $scope.from_route = false;
279 // child scope is executing this function, so our digest doesn't fire ... thus,
282 if (!$scope.in_opac_call) {
283 if ($scope.record_id) {
284 $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
285 tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
287 tab = $routeParams.record_tab || 'catalog';
289 $scope.set_record_tab(tab);
291 $scope.in_opac_call = false;
295 // xulG catalog handlers
296 $scope.handlers = { }
298 // ------------------------------------------------------------------
301 $scope.holdingsGridControls = {};
302 $scope.holdingsGridDataProvider = egGridDataProvider.instance({
303 get : function(offset, count) {
304 return this.arrayNotifier(holdingsSvc.copies, offset, count);
308 // refresh the list of holdings when the filter lib is changed.
309 $scope.holdings_ou = egCore.org.get(egCore.auth.user().ws_ou());
310 $scope.holdings_ou_changed = function(org) {
311 $scope.holdings_ou = org;
313 rid : $scope.record_id,
314 org : $scope.holdings_ou,
315 copy: $scope.holdings_show_copies,
316 vol : $scope.holdings_show_vols,
317 empty: $scope.holdings_show_empty
319 $scope.holdingsGridDataProvider.refresh();
324 // ------------------------------------------------------------------
326 var provider = egGridDataProvider.instance({});
327 $scope.hold_grid_data_provider = provider;
328 $scope.grid_actions = egHoldGridActions;
329 $scope.grid_actions.refresh = function () { provider.refresh() };
330 $scope.hold_grid_controls = {};
332 var hold_ids = []; // current list of holds
333 function fetchHolds(offset, count) {
334 var ids = hold_ids.slice(offset, offset + count);
335 return egHolds.fetch_holds(ids).then(null, null,
336 function(hold_data) {
342 provider.get = function(offset, count) {
343 if ($scope.record_tab != 'holds') return $q.when();
344 var deferred = $q.defer();
345 hold_ids = []; // no caching ATM
350 'open-ils.circ.holds.retrieve_all_from_title',
351 egCore.auth.token(), $scope.record_id,
352 {pickup_lib : egCore.org.descendants($scope.pickup_ou.id(), true)}
354 function(hold_data) {
355 angular.forEach(hold_data, function(list, type) {
356 hold_ids = hold_ids.concat(list);
358 fetchHolds(offset, count).then(
359 deferred.resolve, null, deferred.notify);
363 return deferred.promise;
366 $scope.detail_view = function(action, user_data, items) {
368 $scope.detail_hold_id = h.hold.id();
372 $scope.list_view = function(items) {
373 $scope.detail_hold_id = null;
376 // refresh the list of record holds when the pickup lib is changed.
377 $scope.pickup_ou = egCore.org.get(egCore.auth.user().ws_ou());
378 $scope.pickup_ou_changed = function(org) {
379 $scope.pickup_ou = org;
383 $scope.print_holds = function() {
385 angular.forEach($scope.hold_grid_controls.allItems(), function(item) {
387 hold : egCore.idl.toHash(item.hold),
388 patron_last : item.patron_last,
389 patron_alias : item.patron_alias,
390 patron_barcode : item.patron_barcode,
391 copy : egCore.idl.toHash(item.copy),
392 volume : egCore.idl.toHash(item.volume),
393 title : item.mvr.title(),
394 author : item.mvr.author()
400 template : 'holds_for_bib',
401 scope : {holds : holds}
405 $scope.mark_hold_transfer_dest = function() {
406 egCore.hatch.setLocalItem(
407 'eg.circ.hold.title_transfer_target', $scope.record_id);
410 // UI presents this option as "all holds"
411 $scope.transfer_holds_to_marked = function() {
412 var hold_ids = $scope.hold_grid_controls.allItems().map(
413 function(hold_data) {return hold_data.hold.id()});
414 egHolds.transfer_to_marked_title(hold_ids);
417 // ------------------------------------------------------------------
418 // Initialize the selected tab
420 function init_cat_url() {
421 // Set the initial catalog URL. This only happens once.
422 // The URL is otherwise generated through user navigation.
423 if ($scope.catalog_url) return;
425 var url = $location.absUrl().replace(/\/staff.*/, '/opac/advanced');
427 // A record ID in the path indicates a request for the record-
429 if ($routeParams.record_id) {
430 url = url.replace(/advanced/, '/record/' + $scope.record_id);
433 $scope.catalog_url = url;
436 function init_parts_url() {
437 $scope.parts_url = $location
441 '/conify/global/biblio/monograph_part?r='+$scope.record_id
445 $scope.set_record_tab = function(tab) {
446 $scope.record_tab = tab;
459 $scope.detail_hold_record_id = $scope.record_id;
460 // refresh the holds grid
466 $scope.set_default_record_tab = function() {
467 egCore.hatch.setLocalItem(
468 'eg.cat.default_record_tab', $scope.record_tab);
469 $timeout(function(){$scope.default_tab = $scope.record_tab});
473 if ($scope.record_id) {
474 $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
475 tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
480 rid : $scope.record_id,
481 org : $scope.holdings_ou,
482 copy: $scope.holdings_show_copies,
483 vol : $scope.holdings_show_vols,
484 empty: $scope.holdings_show_empty
486 $scope.holdingsGridDataProvider.refresh();
491 tab = $routeParams.record_tab || 'catalog';
493 $scope.set_record_tab(tab);
497 .controller('URLVerifyCtrl',
498 ['$scope','$location',
499 function($scope , $location) {
500 $scope.verifyurls_url = $location.absUrl().replace(/\/staff.*/, '/url_verify/sessions');
503 .controller('VandelayCtrl',
504 ['$scope','$location',
505 function($scope , $location) {
506 $scope.vandelay_url = $location.absUrl().replace(/\/staff.*/, '/vandelay/vandelay');
509 .controller('ManageAuthoritiesCtrl',
510 ['$scope','$location',
511 function($scope , $location) {
512 $scope.manageauthorities_url = $location.absUrl().replace(/\/staff.*/, '/cat/authority/list');
515 .controller('BatchEditCtrl',
516 ['$scope','$location','$routeParams',
517 function($scope , $location , $routeParams) {
518 $scope.batchedit_url = $location.absUrl().replace(/\/eg.*/, '/opac/extras/merge_template');
519 if ($routeParams.container_type) {
520 switch ($routeParams.container_type) {
522 $scope.batchedit_url += '?recordSource=b&containerid=' + $routeParams.container_id;
525 $scope.batchedit_url += '?recordSource=r&recid=' + $routeParams.container_id;
532 .filter('boolText', function(){
533 return function (v) {
538 .factory('holdingsSvc',
540 function(egCore , $q) {
543 copies : [], // record search results
544 index : 0, // search grid index
552 acp : ['status','location'],
553 acn : ['prefix','suffix','copies']
557 // resolved with the last received copy
558 service.fetch = function(opts) {
562 var copy = opts.copy;
564 var empty = opts.empty;
566 if (!rid) return $q.when();
567 if (!org) return $q.when();
574 var org_list = egCore.org.descendants(org.id(), true);
576 return egCore.pcrud.search(
578 {record : rid, owning_lib : org_list, deleted : 'f'},
581 function() { // finished
582 service.copies = service.copies.sort(
584 function compare_array (x, y, i) {
585 if (x[i] && y[i]) { // both have values
586 if (x[i] == y[i]) { // need to look deeper
587 return compare_array(x, y, ++i);
590 if (x[i] < y[i]) { // x is first
592 } else if (x[i] > y[i]) { // y is first
596 } else { // no orgs to compare ...
603 var owner_order = compare_array(a.owner_list, b.owner_list, 0);
605 // now compare on CN label
606 if (a.call_number.label < b.call_number.label) return -1;
607 if (a.call_number.label > b.call_number.label) return 1;
610 if (a.copy_number < b.copy_number) return -1;
611 if (a.copy_number > b.copy_number) return 1;
614 if (a.barcode < b.barcode) return -1;
615 if (a.barcode > b.barcode) return 1;
621 // create a label using just the unique part of the owner list
624 angular.forEach(service.copies, function (cp) {
625 if (!prev_owner_list) {
626 cp.owner_label = cp.owner_list.join(' ... ');
628 var current_owner_list = cp.owner_list.slice();
629 while (current_owner_list[1] && prev_owner_list[1] && current_owner_list[0] == prev_owner_list[0]) {
630 current_owner_list.shift();
631 prev_owner_list.shift();
633 cp.owner_label = current_owner_list.join(' ... ');
637 prev_owner_list = cp.owner_list.slice();
640 var new_list = service.copies;
641 if (!copy || !vol) { // collapse copy rows, supply a count instead
646 var current_blob = {};
647 angular.forEach(new_list, function (cp) {
649 prev_key = cp.owner_list.join('') + cp.call_number.label;
650 if (cp.barcode) current_blob.copy_count = 1;
651 current_blob.index = ++index;
652 current_blob.call_number = cp.call_number;
653 current_blob.owner_list = cp.owner_list;
654 current_blob.owner_label = cp.owner_label;
656 var current_key = cp.owner_list.join('') + cp.call_number.label;
657 if (prev_key == current_key) { // collapse into current_blob
658 current_blob.copy_count++;
660 cp_list.push(current_blob);
661 prev_key = current_key;
663 if (cp.barcode) current_blob.copy_count = 1;
664 current_blob.index = ++index;
665 current_blob.owner_label = cp.owner_label;
666 current_blob.call_number = cp.call_number;
667 current_blob.owner_list = cp.owner_list;
672 cp_list.push(current_blob);
680 var current_blob = {};
681 angular.forEach(cp_list, function (cp) {
683 prev_key = cp.owner_list.join('');
684 current_blob.index = ++index;
685 current_blob.cn_count = 1;
686 current_blob.copy_count = cp.copy_count;
687 current_blob.owner_list = cp.owner_list;
688 current_blob.owner_label = cp.owner_label;
690 var current_key = cp.owner_list.join('');
691 if (prev_key == current_key) { // collapse into current_blob
692 current_blob.cn_count++;
693 current_blob.copy_count += cp.copy_count;
695 cn_list.push(current_blob);
696 prev_key = current_key;
698 current_blob.index = ++index;
699 current_blob.owner_label = cp.owner_label;
700 current_blob.cn_count = 1;
701 current_blob.copy_count = cp.copy_count;
702 current_blob.owner_list = cp.owner_list;
707 cn_list.push(current_blob);
713 service.copies = new_list;
719 // notify reads the stream of copies, one at a time.
722 var copies = cn.copies();
725 angular.forEach(copies, function (cp) {
729 var flat = egCore.idl.toHash(copies);
730 var owner = egCore.org.get(flat[0].call_number.owning_lib);
732 var owner_name_list = [];
733 while (owner.parent_ou()) { // we're going to skip the top of the tree...
734 owner_name_list.unshift(owner.name());
735 owner = egCore.org.get(owner.parent_ou());
738 angular.forEach(flat, function (cp) {
739 cp.owner_list = owner_name_list;
742 service.copies = service.copies.concat(flat);
744 if (empty && flat.length == 0) {
745 service.copies.push({
746 owner_list : owner_name_list,
747 call_number: egCore.idl.toHash(cn)