1 angular.module('egHoldingsMod', ['egCoreMod','egGridMod'])
3 .factory('holdingsSvc',
5 function(egCore , $q) {
7 var service = function() {
9 this.copies = []; // record search results
10 this.index = 0; // search grid index
15 service.prototype.flesh = {
18 acp : ['status','location','circ_lib','parts','age_protect','copy_alerts'],
19 acn : ['prefix','suffix','copies']
23 service.prototype.fetchAgain = function() {
33 // resolved with the last received copy
34 service.prototype.fetch = function(opts) {
37 if (svc.ongoing && svc.p) {
39 console.log('Canceling fetch for org '+ svc.org.id());
40 if (svc.p.reject) svc.p.reject();
47 var empty = opts.empty;
49 if (!rid) return $q.when();
50 if (!org) return $q.when();
58 svc.empty = opts.empty;
63 var org_list = egCore.org.descendants(org.id(), true);
64 console.log('Holdings fetch with: rid='+rid+' org='+org_list+' copy='+copy+' vol='+vol+' empty='+empty);
66 var p = egCore.pcrud.search(
68 {record : rid, owning_lib : org_list, deleted : 'f'},
71 function() { // finished
73 svc.copies = svc.copies.sort(
75 function compare_array (x, y, i) {
76 if (x[i] && y[i]) { // both have values
77 if (x[i] == y[i]) { // need to look deeper
78 return compare_array(x, y, ++i);
81 if (x[i] < y[i]) { // x is first
83 } else if (x[i] > y[i]) { // y is first
87 } else { // no orgs to compare ...
94 var owner_order = compare_array(a.owner_list, b.owner_list, 0);
96 // now compare on CN label
97 if (a.call_number.label < b.call_number.label) return -1;
98 if (a.call_number.label > b.call_number.label) return 1;
101 if (a.copy_number < b.copy_number) return -1;
102 if (a.copy_number > b.copy_number) return 1;
105 if (a.barcode < b.barcode) return -1;
106 if (a.barcode > b.barcode) return 1;
112 // create virtual field for displaying active parts
113 angular.forEach(svc.copies, function (cp) {
114 cp.monograph_parts = '';
115 if (cp.parts && cp.parts.length > 0) {
116 cp.monograph_parts = cp.parts.map(function(obj) { return obj.label; }).join();
120 // create virtual field for copy alert count
121 angular.forEach(svc.copies, function (cp) {
122 if (cp.copy_alerts) cp.copy_alert_count = cp.copy_alerts.length;
123 else cp.copy_alert_count = 0;
126 // create a label using just the unique part of the owner list
129 angular.forEach(svc.copies, function (cp) {
130 if (!prev_owner_list) {
131 cp.owner_label = cp.owner_list.join(' ... ');
133 var current_owner_list = cp.owner_list.slice();
134 while (current_owner_list[1] && prev_owner_list[1] && current_owner_list[0] == prev_owner_list[0]) {
135 current_owner_list.shift();
136 prev_owner_list.shift();
138 cp.owner_label = current_owner_list.join(' ... ');
142 prev_owner_list = cp.owner_list.slice();
145 var new_list = svc.copies;
146 if (!copy || !vol) { // collapse copy rows, supply a count instead
151 var current_blob = { copy_count : 0 };
152 angular.forEach(new_list, function (cp) {
154 prev_key = cp.owner_list.join('') + cp.call_number.label;
155 if (cp.barcode) current_blob.copy_count = 1;
156 current_blob.index = index++;
157 current_blob.id_list = cp.id_list;
158 if (cp.raw) current_blob.raw = cp.raw;
159 current_blob.call_number = cp.call_number;
160 current_blob.owner_list = cp.owner_list;
161 current_blob.owner_label = cp.owner_label;
162 current_blob.owner_id = cp.owner_id;
164 var current_key = cp.owner_list.join('') + cp.call_number.label;
165 if (prev_key == current_key) { // collapse into current_blob
166 current_blob.copy_count++;
167 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
168 current_blob.raw = current_blob.raw.concat(cp.raw);
170 current_blob.barcode = current_blob.copy_count;
171 cp_list.push(current_blob);
172 prev_key = current_key;
173 current_blob = { copy_count : 0 };
174 if (cp.barcode) current_blob.copy_count = 1;
175 current_blob.index = index++;
176 current_blob.id_list = cp.id_list;
177 if (cp.raw) current_blob.raw = cp.raw;
178 current_blob.owner_label = cp.owner_label;
179 current_blob.owner_id = cp.owner_id;
180 current_blob.call_number = cp.call_number;
181 current_blob.owner_list = cp.owner_list;
186 current_blob.barcode = current_blob.copy_count;
187 cp_list.push(current_blob);
190 if (!vol) { // do the same for vol rows
195 current_blob = { copy_count : 0 };
196 angular.forEach(cp_list, function (cp) {
198 prev_key = cp.owner_list.join('');
199 current_blob.index = index++;
200 current_blob.id_list = cp.id_list;
201 if (cp.raw) current_blob.raw = cp.raw;
202 current_blob.cn_count = 1;
203 current_blob.copy_count = cp.copy_count;
204 current_blob.owner_list = cp.owner_list;
205 current_blob.owner_label = cp.owner_label;
206 current_blob.owner_id = cp.owner_id;
208 var current_key = cp.owner_list.join('');
209 if (prev_key == current_key) { // collapse into current_blob
210 current_blob.cn_count++;
211 current_blob.copy_count += cp.copy_count;
212 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
213 if (cp.raw) current_blob.raw = current_blob.raw.concat(cp.raw);
215 current_blob.barcode = current_blob.copy_count;
216 current_blob.call_number = { label : current_blob.cn_count };
217 cn_list.push(current_blob);
218 prev_key = current_key;
219 current_blob = { copy_count : 0 };
220 current_blob.index = index++;
221 current_blob.id_list = cp.id_list;
222 if (cp.raw) current_blob.raw = cp.raw;
223 current_blob.owner_label = cp.owner_label;
224 current_blob.owner_id = cp.owner_id;
225 current_blob.cn_count = 1;
226 current_blob.copy_count = cp.copy_count;
227 current_blob.owner_list = cp.owner_list;
232 current_blob.barcode = current_blob.copy_count;
233 current_blob.call_number = { label : current_blob.cn_count };
234 cn_list.push(current_blob);
240 svc.copies = new_list;
246 // notify reads the stream of copies, one at a time.
248 if (p.cancel) return;
250 var copies = cn.copies().filter(function(cp){ return cp.deleted() == 'f' });
253 angular.forEach(copies, function (cp) {
257 var owner_id = cn.owning_lib();
258 var owner = egCore.org.get(owner_id);
260 var owner_name_list = [];
261 while (owner.parent_ou()) { // we're going to skip the top of the tree...
262 owner_name_list.unshift(owner.shortname());
263 owner = egCore.org.get(owner.parent_ou());
268 angular.forEach(copies, function (cp) {
269 var flat_cp = egCore.idl.toHash(cp);
270 flat_cp.owner_id = owner_id;
271 flat_cp.owner_list = owner_name_list;
272 flat_cp.id_list = [flat_cp.id];
277 svc.copies = svc.copies.concat(flat);
282 owner_list : owner_name_list,
283 call_number: egCore.idl.toHash(cn),
297 .directive("egVolumeList", function () {
305 templateUrl: './cat/share/t_volume_list',
307 ['$scope','holdingsSvc','egCore','egGridDataProvider','$uibModal',
308 function($scope , holdingsSvc , egCore , egGridDataProvider, $uibModal) {
309 var holdingsSvcInst = new holdingsSvc();
311 $scope.holdingsGridControls = {};
312 $scope.holdingsGridDataProvider = egGridDataProvider.instance({
313 get : function(offset, count) {
314 return this.arrayNotifier(holdingsSvcInst.copies, offset, count);
318 function gatherHoldingsIds () {
321 $scope.holdingsGridControls.allItems(),
322 function (item) { cp_id_list = cp_id_list.concat(item.id_list) }
327 var spawn_volume_editor = function (copies_too) {
330 'open-ils.actor.anon_cache.set_value',
331 null, 'edit-these-copies', {
332 record_id: $scope.recordId,
333 copies: gatherHoldingsIds(),
335 hide_copies : ((copies_too) ? false : true)
337 ).then(function(key) {
340 templateUrl: './cat/share/t_embedded_volcopy',
343 windowClass: 'eg-wide-modal',
345 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
347 egCore.env.basePath + 'cat/volcopy/' + key + '/embedded';
348 $scope.ok = function(args) { $uibModalInstance.close(args) }
349 $scope.cancel = function () { $uibModalInstance.dismiss() }
351 }).result.then(function() {
357 $scope.edit_volumes = function() {
358 spawn_volume_editor(false);
360 $scope.edit_copies = function() {
361 spawn_volume_editor(true);
364 function load_holdings() {
365 holdingsSvcInst.fetch({
366 rid : $scope.recordId,
367 org : egCore.org.root(),
372 $scope.holdingsGridDataProvider.refresh();
375 $scope.$watch('recordId',
376 function(newVal, oldVal) {
377 if (newVal && newVal !== oldVal) {