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() {
30 empty_org: this.empty_org
34 // resolved with the last received copy
35 service.prototype.fetch = function(opts) {
38 if (svc.ongoing && svc.p) {
40 console.log('Canceling fetch for org '+ svc.org.id());
41 if (svc.p.reject) svc.p.reject();
45 var empty_org = opts.empty_org;
49 var empty = opts.empty;
51 if (!rid) return $q.when();
52 if (!org) return $q.when();
57 svc.empty_org = opts.empty_org;
61 svc.empty = opts.empty;
66 var org_list = egCore.org.descendants(org.id(), true);
67 console.log('Holdings fetch with: rid='+rid+' org='+org_list+' copy='+copy+' vol='+vol+' empty='+empty);
70 org_list.map(function(o){svc.org_use_map[''+o]=0;})
72 var p = egCore.pcrud.search(
74 {record : rid, owning_lib : org_list, deleted : 'f'},
77 function() { // finished
80 svc.copies = svc.copies.sort(
82 function compare_array (x, y, i) {
83 if (x[i] && y[i]) { // both have values
84 if (x[i] == y[i]) { // need to look deeper
85 return compare_array(x, y, ++i);
88 if (x[i] < y[i]) { // x is first
90 } else if (x[i] > y[i]) { // y is first
94 } else { // no orgs to compare ...
101 var owner_order = compare_array(a.owner_list, b.owner_list, 0);
103 // now compare on CN label
104 if (a.call_number.label < b.call_number.label) return -1;
105 if (a.call_number.label > b.call_number.label) return 1;
108 if (a.copy_number < b.copy_number) return -1;
109 if (a.copy_number > b.copy_number) return 1;
112 if (a.barcode < b.barcode) return -1;
113 if (a.barcode > b.barcode) return 1;
119 // create virtual field for displaying active parts
120 angular.forEach(svc.copies, function (cp) {
121 cp.monograph_parts = '';
122 if (cp.parts && cp.parts.length > 0) {
123 cp.monograph_parts = cp.parts.map(function(obj) { return obj.label; }).join();
127 // create virtual field for copy alert count
128 angular.forEach(svc.copies, function (cp) {
129 if (cp.copy_alerts) cp.copy_alert_count = cp.copy_alerts.length;
130 else cp.copy_alert_count = 0;
133 // create a label using just the unique part of the owner list
136 angular.forEach(svc.copies, function (cp) {
137 if (!prev_owner_list) {
138 cp.owner_label = cp.owner_list.join(' ... ');
140 var current_owner_list = cp.owner_list.slice();
141 while (current_owner_list[1] && prev_owner_list[1] && current_owner_list[0] == prev_owner_list[0]) {
142 current_owner_list.shift();
143 prev_owner_list.shift();
145 cp.owner_label = current_owner_list.join(' ... ');
149 prev_owner_list = cp.owner_list.slice();
152 var new_list = svc.copies;
153 if (!copy || !vol) { // collapse copy rows, supply a count instead
158 var current_blob = { copy_count : 0 };
159 angular.forEach(new_list, function (cp) {
161 prev_key = cp.owner_list.join('') + cp.call_number.label;
162 if (cp.barcode) current_blob.copy_count = 1;
163 current_blob.index = index++;
164 current_blob.id_list = cp.id_list;
165 if (cp.raw) current_blob.raw = cp.raw;
166 current_blob.call_number = cp.call_number;
167 current_blob.owner_list = cp.owner_list;
168 current_blob.owner_label = cp.owner_label;
169 current_blob.owner_id = cp.owner_id;
171 var current_key = cp.owner_list.join('') + cp.call_number.label;
172 if (prev_key == current_key) { // collapse into current_blob
173 current_blob.copy_count++;
174 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
175 current_blob.raw = current_blob.raw.concat(cp.raw);
177 current_blob.barcode = current_blob.copy_count;
178 cp_list.push(current_blob);
179 prev_key = current_key;
180 current_blob = { copy_count : 0 };
181 if (cp.barcode) current_blob.copy_count = 1;
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.owner_label = cp.owner_label;
186 current_blob.owner_id = cp.owner_id;
187 current_blob.call_number = cp.call_number;
188 current_blob.owner_list = cp.owner_list;
193 current_blob.barcode = current_blob.copy_count;
194 cp_list.push(current_blob);
197 if (!vol) { // do the same for vol rows
202 current_blob = { copy_count : 0 };
203 angular.forEach(cp_list, function (cp) {
205 prev_key = cp.owner_list.join('');
206 current_blob.index = index++;
207 current_blob.id_list = cp.id_list;
208 if (cp.raw) current_blob.raw = cp.raw;
209 current_blob.cn_count = 1;
210 current_blob.copy_count = cp.copy_count;
211 current_blob.owner_list = cp.owner_list;
212 current_blob.owner_label = cp.owner_label;
213 current_blob.owner_id = cp.owner_id;
215 var current_key = cp.owner_list.join('');
216 if (prev_key == current_key) { // collapse into current_blob
217 current_blob.cn_count++;
218 current_blob.copy_count += cp.copy_count;
219 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
221 if (current_blob.raw) current_blob.raw = current_blob.raw.concat(cp.raw);
222 else current_blob.raw = cp.raw;
225 current_blob.barcode = current_blob.copy_count;
226 current_blob.call_number = { label : current_blob.cn_count };
227 cn_list.push(current_blob);
228 prev_key = current_key;
229 current_blob = { copy_count : 0 };
230 current_blob.index = index++;
231 current_blob.id_list = cp.id_list;
232 if (cp.raw) current_blob.raw = cp.raw;
233 current_blob.owner_label = cp.owner_label;
234 current_blob.owner_id = cp.owner_id;
235 current_blob.cn_count = 1;
236 current_blob.copy_count = cp.copy_count;
237 current_blob.owner_list = cp.owner_list;
242 current_blob.barcode = current_blob.copy_count;
243 current_blob.call_number = { label : current_blob.cn_count };
244 cn_list.push(current_blob);
252 var empty_org_list = [];
253 angular.forEach(svc.org_use_map,function(v,k){
254 if (v == 0) empty_org_list.push(k);
257 angular.forEach(empty_org_list, function (oid) {
258 var owner = egCore.org.get(oid);
259 if (owner.ou_type().can_have_vols() != 't') return;
262 while (owner.parent_ou()) { // we're going to skip the top of the tree...
263 owner_list.unshift(owner.shortname());
264 owner = egCore.org.get(owner.parent_ou());
267 var owner_label = owner_list.join(' ... ');
272 call_number: { label : '' },
275 owner_list : owner_list,
276 owner_label: owner_label,
284 svc.copies = new_list;
290 // notify reads the stream of copies, one at a time.
292 if (p.cancel) return;
294 var copies = cn.copies().filter(function(cp){ return cp.deleted() == 'f' });
297 angular.forEach(copies, function (cp) {
301 var owner_id = cn.owning_lib();
302 var owner = egCore.org.get(owner_id);
303 svc.org_use_map[''+owner_id] += 1;
305 var owner_name_list = [];
306 while (owner.parent_ou()) { // we're going to skip the top of the tree...
307 owner_name_list.unshift(owner.shortname());
308 owner = egCore.org.get(owner.parent_ou());
313 angular.forEach(copies, function (cp) {
314 var flat_cp = egCore.idl.toHash(cp);
315 flat_cp.owner_id = owner_id;
316 flat_cp.owner_list = owner_name_list;
317 flat_cp.id_list = [flat_cp.id];
322 svc.copies = svc.copies.concat(flat);
327 owner_list : owner_name_list,
328 call_number: egCore.idl.toHash(cn),
342 .directive("egVolumeList", function () {
350 templateUrl: './cat/share/t_volume_list',
352 ['$scope','holdingsSvc','egCore','egGridDataProvider','$uibModal',
353 function($scope , holdingsSvc , egCore , egGridDataProvider, $uibModal) {
354 var holdingsSvcInst = new holdingsSvc();
356 $scope.holdingsGridControls = {};
357 $scope.holdingsGridDataProvider = egGridDataProvider.instance({
358 get : function(offset, count) {
359 return this.arrayNotifier(holdingsSvcInst.copies, offset, count);
363 function gatherHoldingsIds () {
366 $scope.holdingsGridControls.allItems(),
367 function (item) { cp_id_list = cp_id_list.concat(item.id_list) }
372 var spawn_volume_editor = function (copies_too) {
375 'open-ils.actor.anon_cache.set_value',
376 null, 'edit-these-copies', {
377 record_id: $scope.recordId,
378 copies: gatherHoldingsIds(),
380 hide_copies : ((copies_too) ? false : true)
382 ).then(function(key) {
385 templateUrl: './cat/share/t_embedded_volcopy',
388 windowClass: 'eg-wide-modal',
390 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
392 egCore.env.basePath + 'cat/volcopy/' + key + '/embedded';
393 $scope.ok = function(args) { $uibModalInstance.close(args) }
394 $scope.cancel = function () { $uibModalInstance.dismiss() }
396 }).result.then(function() {
402 $scope.edit_volumes = function() {
403 spawn_volume_editor(false);
405 $scope.edit_copies = function() {
406 spawn_volume_editor(true);
409 function load_holdings() {
410 holdingsSvcInst.fetch({
411 rid : $scope.recordId,
412 org : egCore.org.root(),
417 $scope.holdingsGridDataProvider.refresh();
420 $scope.$watch('recordId',
421 function(newVal, oldVal) {
422 if (newVal && newVal !== oldVal) {