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'],
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 a label using just the unique part of the owner list
123 angular.forEach(svc.copies, function (cp) {
124 if (!prev_owner_list) {
125 cp.owner_label = cp.owner_list.join(' ... ');
127 var current_owner_list = cp.owner_list.slice();
128 while (current_owner_list[1] && prev_owner_list[1] && current_owner_list[0] == prev_owner_list[0]) {
129 current_owner_list.shift();
130 prev_owner_list.shift();
132 cp.owner_label = current_owner_list.join(' ... ');
136 prev_owner_list = cp.owner_list.slice();
139 var new_list = svc.copies;
140 if (!copy || !vol) { // collapse copy rows, supply a count instead
145 var current_blob = { copy_count : 0 };
146 angular.forEach(new_list, function (cp) {
148 prev_key = cp.owner_list.join('') + cp.call_number.label;
149 if (cp.barcode) current_blob.copy_count = 1;
150 current_blob.index = index++;
151 current_blob.id_list = cp.id_list;
152 if (cp.raw) current_blob.raw = cp.raw;
153 current_blob.call_number = cp.call_number;
154 current_blob.owner_list = cp.owner_list;
155 current_blob.owner_label = cp.owner_label;
156 current_blob.owner_id = cp.owner_id;
158 var current_key = cp.owner_list.join('') + cp.call_number.label;
159 if (prev_key == current_key) { // collapse into current_blob
160 current_blob.copy_count++;
161 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
162 current_blob.raw = current_blob.raw.concat(cp.raw);
164 current_blob.barcode = current_blob.copy_count;
165 cp_list.push(current_blob);
166 prev_key = current_key;
167 current_blob = { copy_count : 0 };
168 if (cp.barcode) current_blob.copy_count = 1;
169 current_blob.index = index++;
170 current_blob.id_list = cp.id_list;
171 if (cp.raw) current_blob.raw = cp.raw;
172 current_blob.owner_label = cp.owner_label;
173 current_blob.owner_id = cp.owner_id;
174 current_blob.call_number = cp.call_number;
175 current_blob.owner_list = cp.owner_list;
180 current_blob.barcode = current_blob.copy_count;
181 cp_list.push(current_blob);
184 if (!vol) { // do the same for vol rows
189 current_blob = { copy_count : 0 };
190 angular.forEach(cp_list, function (cp) {
192 prev_key = cp.owner_list.join('');
193 current_blob.index = index++;
194 current_blob.id_list = cp.id_list;
195 if (cp.raw) current_blob.raw = cp.raw;
196 current_blob.cn_count = 1;
197 current_blob.copy_count = cp.copy_count;
198 current_blob.owner_list = cp.owner_list;
199 current_blob.owner_label = cp.owner_label;
200 current_blob.owner_id = cp.owner_id;
202 var current_key = cp.owner_list.join('');
203 if (prev_key == current_key) { // collapse into current_blob
204 current_blob.cn_count++;
205 current_blob.copy_count += cp.copy_count;
206 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
207 if (cp.raw) current_blob.raw = current_blob.raw.concat(cp.raw);
209 current_blob.barcode = current_blob.copy_count;
210 current_blob.call_number = { label : current_blob.cn_count };
211 cn_list.push(current_blob);
212 prev_key = current_key;
213 current_blob = { copy_count : 0 };
214 current_blob.index = index++;
215 current_blob.id_list = cp.id_list;
216 if (cp.raw) current_blob.raw = cp.raw;
217 current_blob.owner_label = cp.owner_label;
218 current_blob.owner_id = cp.owner_id;
219 current_blob.cn_count = 1;
220 current_blob.copy_count = cp.copy_count;
221 current_blob.owner_list = cp.owner_list;
226 current_blob.barcode = current_blob.copy_count;
227 current_blob.call_number = { label : current_blob.cn_count };
228 cn_list.push(current_blob);
234 svc.copies = new_list;
240 // notify reads the stream of copies, one at a time.
242 if (p.cancel) return;
244 var copies = cn.copies().filter(function(cp){ return cp.deleted() == 'f' });
247 angular.forEach(copies, function (cp) {
251 var owner_id = cn.owning_lib();
252 var owner = egCore.org.get(owner_id);
254 var owner_name_list = [];
255 while (owner.parent_ou()) { // we're going to skip the top of the tree...
256 owner_name_list.unshift(owner.name());
257 owner = egCore.org.get(owner.parent_ou());
262 angular.forEach(copies, function (cp) {
263 var flat_cp = egCore.idl.toHash(cp);
264 flat_cp.owner_id = owner_id;
265 flat_cp.owner_list = owner_name_list;
266 flat_cp.id_list = [flat_cp.id];
271 svc.copies = svc.copies.concat(flat);
276 owner_list : owner_name_list,
277 call_number: egCore.idl.toHash(cn),
291 .directive("egVolumeList", function () {
299 templateUrl: './cat/share/t_volume_list',
301 ['$scope','holdingsSvc','egCore','egGridDataProvider','$uibModal',
302 function($scope , holdingsSvc , egCore , egGridDataProvider, $uibModal) {
303 var holdingsSvcInst = new holdingsSvc();
305 $scope.holdingsGridControls = {};
306 $scope.holdingsGridDataProvider = egGridDataProvider.instance({
307 get : function(offset, count) {
308 return this.arrayNotifier(holdingsSvcInst.copies, offset, count);
312 function gatherHoldingsIds () {
315 $scope.holdingsGridControls.allItems(),
316 function (item) { cp_id_list = cp_id_list.concat(item.id_list) }
321 var spawn_volume_editor = function (copies_too) {
324 'open-ils.actor.anon_cache.set_value',
325 null, 'edit-these-copies', {
326 record_id: $scope.recordId,
327 copies: gatherHoldingsIds(),
329 hide_copies : ((copies_too) ? false : true)
331 ).then(function(key) {
334 templateUrl: './cat/share/t_embedded_volcopy',
336 windowClass: 'eg-wide-modal',
338 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
340 egCore.env.basePath + 'cat/volcopy/' + key + '/embedded';
341 $scope.ok = function(args) { $uibModalInstance.close(args) }
342 $scope.cancel = function () { $uibModalInstance.dismiss() }
344 }).result.then(function() {
350 $scope.edit_volumes = function() {
351 spawn_volume_editor(false);
353 $scope.edit_copies = function() {
354 spawn_volume_editor(true);
357 function load_holdings() {
358 holdingsSvcInst.fetch({
359 rid : $scope.recordId,
360 org : egCore.org.root(),
365 $scope.holdingsGridDataProvider.refresh();
368 $scope.$watch('recordId',
369 function(newVal, oldVal) {
370 if (newVal && newVal !== oldVal) {