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'],
19 acn : ['prefix','suffix','copies']
23 service.prototype.fetchAgain = function() {
33 // resolved with the last received copy
34 service.prototype.fetch = function(opts) {
37 console.log('Skipping fetch, ongoing = true');
45 var empty = opts.empty;
47 if (!rid) return $q.when();
48 if (!org) return $q.when();
56 svc.empty = opts.empty;
61 var org_list = egCore.org.descendants(org.id(), true);
62 console.log('Holdings fetch with: rid='+rid+' org='+org_list+' copy='+copy+' vol='+vol+' empty='+empty);
64 return egCore.pcrud.search(
66 {record : rid, owning_lib : org_list, deleted : 'f'},
69 function() { // finished
70 svc.copies = svc.copies.sort(
72 function compare_array (x, y, i) {
73 if (x[i] && y[i]) { // both have values
74 if (x[i] == y[i]) { // need to look deeper
75 return compare_array(x, y, ++i);
78 if (x[i] < y[i]) { // x is first
80 } else if (x[i] > y[i]) { // y is first
84 } else { // no orgs to compare ...
91 var owner_order = compare_array(a.owner_list, b.owner_list, 0);
93 // now compare on CN label
94 if (a.call_number.label < b.call_number.label) return -1;
95 if (a.call_number.label > b.call_number.label) return 1;
98 if (a.copy_number < b.copy_number) return -1;
99 if (a.copy_number > b.copy_number) return 1;
102 if (a.barcode < b.barcode) return -1;
103 if (a.barcode > b.barcode) return 1;
109 // create a label using just the unique part of the owner list
112 angular.forEach(svc.copies, function (cp) {
113 if (!prev_owner_list) {
114 cp.owner_label = cp.owner_list.join(' ... ');
116 var current_owner_list = cp.owner_list.slice();
117 while (current_owner_list[1] && prev_owner_list[1] && current_owner_list[0] == prev_owner_list[0]) {
118 current_owner_list.shift();
119 prev_owner_list.shift();
121 cp.owner_label = current_owner_list.join(' ... ');
125 prev_owner_list = cp.owner_list.slice();
128 var new_list = svc.copies;
129 if (!copy || !vol) { // collapse copy rows, supply a count instead
134 var current_blob = { copy_count : 0 };
135 angular.forEach(new_list, function (cp) {
137 prev_key = cp.owner_list.join('') + cp.call_number.label;
138 if (cp.barcode) current_blob.copy_count = 1;
139 current_blob.index = index++;
140 current_blob.id_list = cp.id_list;
141 if (cp.raw) current_blob.raw = cp.raw;
142 current_blob.call_number = cp.call_number;
143 current_blob.owner_list = cp.owner_list;
144 current_blob.owner_label = cp.owner_label;
145 current_blob.owner_id = cp.owner_id;
147 var current_key = cp.owner_list.join('') + cp.call_number.label;
148 if (prev_key == current_key) { // collapse into current_blob
149 current_blob.copy_count++;
150 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
151 current_blob.raw = current_blob.raw.concat(cp.raw);
153 current_blob.barcode = current_blob.copy_count;
154 cp_list.push(current_blob);
155 prev_key = current_key;
156 current_blob = { copy_count : 0 };
157 if (cp.barcode) current_blob.copy_count = 1;
158 current_blob.index = index++;
159 current_blob.id_list = cp.id_list;
160 if (cp.raw) current_blob.raw = cp.raw;
161 current_blob.owner_label = cp.owner_label;
162 current_blob.owner_id = cp.owner_id;
163 current_blob.call_number = cp.call_number;
164 current_blob.owner_list = cp.owner_list;
169 current_blob.barcode = current_blob.copy_count;
170 cp_list.push(current_blob);
173 if (!vol) { // do the same for vol rows
178 current_blob = { copy_count : 0 };
179 angular.forEach(cp_list, function (cp) {
181 prev_key = cp.owner_list.join('');
182 current_blob.index = index++;
183 current_blob.id_list = cp.id_list;
184 if (cp.raw) current_blob.raw = cp.raw;
185 current_blob.cn_count = 1;
186 current_blob.copy_count = cp.copy_count;
187 current_blob.owner_list = cp.owner_list;
188 current_blob.owner_label = cp.owner_label;
189 current_blob.owner_id = cp.owner_id;
191 var current_key = cp.owner_list.join('');
192 if (prev_key == current_key) { // collapse into current_blob
193 current_blob.cn_count++;
194 current_blob.copy_count += cp.copy_count;
195 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
196 if (cp.raw) current_blob.raw = current_blob.raw.concat(cp.raw);
198 current_blob.barcode = current_blob.copy_count;
199 current_blob.call_number = { label : current_blob.cn_count };
200 cn_list.push(current_blob);
201 prev_key = current_key;
202 current_blob = { copy_count : 0 };
203 current_blob.index = index++;
204 current_blob.id_list = cp.id_list;
205 if (cp.raw) current_blob.raw = cp.raw;
206 current_blob.owner_label = cp.owner_label;
207 current_blob.owner_id = cp.owner_id;
208 current_blob.cn_count = 1;
209 current_blob.copy_count = cp.copy_count;
210 current_blob.owner_list = cp.owner_list;
215 current_blob.barcode = current_blob.copy_count;
216 current_blob.call_number = { label : current_blob.cn_count };
217 cn_list.push(current_blob);
223 svc.copies = new_list;
229 // notify reads the stream of copies, one at a time.
232 var copies = cn.copies().filter(function(cp){ return cp.deleted() == 'f' });
235 angular.forEach(copies, function (cp) {
239 var owner_id = cn.owning_lib();
240 var owner = egCore.org.get(owner_id);
242 var owner_name_list = [];
243 while (owner.parent_ou()) { // we're going to skip the top of the tree...
244 owner_name_list.unshift(owner.name());
245 owner = egCore.org.get(owner.parent_ou());
250 angular.forEach(copies, function (cp) {
251 var flat_cp = egCore.idl.toHash(cp);
252 flat_cp.owner_id = owner_id;
253 flat_cp.owner_list = owner_name_list;
254 flat_cp.id_list = [flat_cp.id];
259 svc.copies = svc.copies.concat(flat);
264 owner_list : owner_name_list,
265 call_number: egCore.idl.toHash(cn),
277 .directive("egVolumeList", function () {
285 templateUrl: './cat/share/t_volume_list',
287 ['$scope','holdingsSvc','egCore','egGridDataProvider','$modal',
288 function($scope , holdingsSvc , egCore , egGridDataProvider, $modal) {
289 var holdingsSvcInst = new holdingsSvc();
291 $scope.holdingsGridControls = {};
292 $scope.holdingsGridDataProvider = egGridDataProvider.instance({
293 get : function(offset, count) {
294 return this.arrayNotifier(holdingsSvcInst.copies, offset, count);
298 function gatherHoldingsIds () {
301 $scope.holdingsGridControls.allItems(),
302 function (item) { cp_id_list = cp_id_list.concat(item.id_list) }
307 $scope.edit_volumes = function (copies_too) {
310 'open-ils.actor.anon_cache.set_value',
311 null, 'edit-these-copies', {
312 record_id: $scope.recordId,
313 copies: gatherHoldingsIds(),
315 hide_copies : (copies_too) ? false : true
317 ).then(function(key) {
320 templateUrl: './cat/share/t_embedded_volcopy',
323 ['$scope', '$modalInstance', function($scope, $modalInstance) {
325 egCore.env.basePath + 'cat/volcopy/' + key + '/embedded';
326 $scope.ok = function(args) { $modalInstance.close(args) }
327 $scope.cancel = function () { $modalInstance.dismiss() }
329 }).result.then(function() {
335 $scope.edit_copies = function() {
336 $scope.edit_volumes(true);
339 function load_holdings() {
340 holdingsSvcInst.fetch({
341 rid : $scope.recordId,
342 org : egCore.org.root(),
347 $scope.holdingsGridDataProvider.refresh();
350 $scope.$watch('recordId',
351 function(newVal, oldVal) {
352 if (newVal && newVal !== oldVal) {