$routeProvider.otherwise({redirectTo : '/cat/item/search'});
})
+.factory('itemSvc',
+ ['egCore',
+function(egCore) {
+
+ var service = {
+ copies : [], // copy barcode search results
+ index : 0 // search grid index
+ };
+
+ service.flesh = {
+ flesh : 3,
+ flesh_fields : {
+ acp : ['call_number','location','status','location'],
+ acn : ['record'],
+ bre : ['simple_record','creator','editor']
+ },
+ select : {
+ // avoid fleshing MARC on the bre
+ // note: don't add simple_record.. not sure why
+ bre : ['id','tcn_value','creator','editor'],
+ }
+ }
+
+ service.fetch = function(barcode, id) {
+ var promise;
+
+ if (barcode) {
+ promise = egCore.pcrud.search('acp',
+ {barcode : barcode, deleted : 'f'}, service.flesh);
+ } else {
+ promise = egCore.pcrud.retrieve('acp', id, service.flesh);
+ }
+
+ return promise.then(function(copy) {
+ if (!copy) return null;
+
+ var flatCopy = egCore.idl.toHash(copy, true);
+ flatCopy.index = service.index++;
+ service.copies.unshift(flatCopy);
+
+ return {
+ copy : copy,
+ index : flatCopy.index
+ };
+ });
+ }
+
+ return service;
+}])
+
/**
* Search bar along the top of the page.
* Parent scope for list and detail views
*/
.controller('SearchCtrl',
- ['$scope','$location','egCore','egGridDataProvider',
-function($scope , $location , egCore , egGridDataProvider) {
-
+ ['$scope','$location','egCore','egGridDataProvider','itemSvc',
+function($scope , $location , egCore , egGridDataProvider , itemSvc) {
$scope.focusBarcode = true;
- var copies = [];
- var provider = egGridDataProvider.instance({});
+ // sub-scopes (search / detail-view) apply their version
+ // of retrieval function to $scope.context.search
+ // and display toggling via $scope.context.toggleDisplay
+ $scope.context = {};
+
+ $scope.toggleView = function($event) {
+ $scope.context.toggleDisplay();
+ $event.preventDefault(); // avoid form submission
+ }
+
+ $scope.args = {};
+}])
+
+/**
+ * List view - grid stuff
+ */
+.controller('ListCtrl',
+ ['$scope','$location','$timeout','egCore','egGridDataProvider','itemSvc',
+function($scope , $location , $timeout , egCore , egGridDataProvider , itemSvc) {
+ $scope.context.page = 'list';
+
+ var provider = egGridDataProvider.instance();
provider.get = function(offset, count) {
- return provider.arrayNotifier(copies, offset, count);
+ return provider.arrayNotifier(itemSvc.copies, offset, count);
}
+
provider.itemFieldValue = provider.flatItemFieldValue;
$scope.gridDataProvider = provider;
- $scope.searchCopy = function(args) {
- $scope.fetchCopy(args.barcode)
- .then(function(copy) {
- if (copy) {
- copies.unshift(egCore.idl.toHash(copy, true));
+ $scope.context.search = function(args) {
+ itemSvc.fetch(args.barcode).then(function(res) {
+ if (res) {
provider.increment();
- } else {
- $scope.copyNotFound = true;
+ provider.selectOneItem(res.index);
}
- });
+ })
}
- $scope.fetchCopy(barcode, id) {
- var flesh = {
- flesh : 3,
- flesh_fields : {
- acp : ['call_number','location','status','location'],
- acn : ['record'],
- bre : ['simple_record','creator','editor']
- },
- select : {
- // avoid fleshing MARC on the bre
- // note: don't add simple_record.. not sure why
- bre : ['id','tcn_value','creator','editor'],
- }
- };
-
- var promise;
-
- if (barcode) {
- promise = egCore.pcrud.search('acp',
- {barcode : barcode, deleted : 'f'}, flesh);
- } else {
- promise = egCore.pcrud.retrieve('acp', id, flesh);
- }
-
- promise.then(function(copy) {
- if (!copy) {
- $scope.copyNotFound = true;
- return;
- }
- return egCore.idl.toHash(copy, true)
+ if (itemSvc.copy) {
+ // If a copy was just displayed in the detail view, ensure it's
+ // focused in the list view. However, give the grid a chance to
+ // instantiate before attempting to select any items.
+ $timeout(function() {
+ var flatCopy = itemSvc.copies.filter(
+ function(c) { return c.id == itemSvc.copy.id() })[0];
+ provider.selectOneItem(flatCopy.index);
});
}
-}])
-/**
- * List view - grid stuff
- */
-.controller('ListCtrl',
- ['$scope','$location','egCore',
-function($scope , $location , egCore) {
- // grid stuff
+ $scope.context.toggleDisplay = function() {
+ var item = provider.getSelectedItems()[0];
+ if (item)
+ $location.path('/cat/item/view/' + item.id + '/summary');
+ }
}])
* Detail view -- shows one copy
*/
.controller('ViewCtrl',
- ['$scope','$location','$routeParams','egCore',
-function($scope , $location , $routeParams , egCore) {
+ ['$scope','$location','$routeParams','egCore','itemSvc',
+function($scope , $location , $routeParams , egCore , itemSvc) {
var copyId = $routeParams.id;
$scope.tab = $routeParams.tab;
+ $scope.context.page = 'detail';
- // TODO: cache data per copy
-
- function loadCopy() {
- $scope.fetchCopy(null, copyId)
-
- }).then(function(copy) {
- if (!copy) return;
+ function loadCopy(barcode) {
+ delete $scope.copy;
+ delete itemSvc.copy;
- // TODO: FLATTEN EVERYTHING
+ return itemSvc.fetch(barcode, copyId).then(function(res) {
+ if (!res) return;
+ var copy = res.copy;
+ copyId = copy.id();
+ $scope.args.barcode = copy.barcode();
+ itemSvc.copy = copy;
// locally flesh org units
copy.circ_lib(egCore.org.get(copy.circ_lib()));
}
function loadCurrentCirc() {
-
+ delete $scope.circ;
+ delete $scope.circ_summary;
+
egCore.pcrud.search('circ',
{target_copy : copyId},
{ flesh : 1,
function loadCircCounts() {
+ delete $scope.circ_counts;
+ $scope.total_circs = 0;
+ $scope.total_circs_this_year = 0;
+ $scope.total_circs_prev_year = 0;
+
egCore.pcrud.search('circbyyr',
{copy : copyId}, null, {atomic : true})
.then(function(counts) {
$scope.circ_counts = counts;
- $scope.total_circs = 0;
angular.forEach(counts, function(count) {
$scope.total_circs += Number(count.count());
function loadPrevCirc() {
}
- loadCopy();
- switch($scope.tab) {
- case 'summary':
- loadCurrentCirc();
- loadCircCounts();
- break;
+ // we don't need all data on all tabs, so fetch what's needed when needed.
+ function loadTabData() {
+ switch($scope.tab) {
+ case 'summary':
+ loadCurrentCirc();
+ loadCircCounts();
+ break;
+
+ case 'circs':
+ loadCurrentCirc();
+ loadPrevCirc();
+ break;
+ }
+ }
- case 'circs':
- loadCurrentCirc();
- loadPrevCirc();
- break;
+ // handle the barcode scan box, which will replace our current copy
+ $scope.context.search = function(args) {
+ // when searching by barcode, we have to wait for the
+ // copy to arrive (to collect the copyID) before
+ // fetching the remaining tab data.
+ loadCopy(args.barcode).then(loadTabData);
+ }
+ $scope.context.toggleDisplay = function() {
+ $location.path('/cat/item/search');
}
+ loadCopy();
+ loadTabData();
}])