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'],
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 virtual field for displaying active parts
110 angular.forEach(svc.copies, function (cp) {
111 cp.monograph_parts = '';
112 if (cp.parts && cp.parts.length > 0) {
113 cp.monograph_parts = cp.parts.map(function(obj) { return obj.label; }).join();
117 // create a label using just the unique part of the owner list
120 angular.forEach(svc.copies, function (cp) {
121 if (!prev_owner_list) {
122 cp.owner_label = cp.owner_list.join(' ... ');
124 var current_owner_list = cp.owner_list.slice();
125 while (current_owner_list[1] && prev_owner_list[1] && current_owner_list[0] == prev_owner_list[0]) {
126 current_owner_list.shift();
127 prev_owner_list.shift();
129 cp.owner_label = current_owner_list.join(' ... ');
133 prev_owner_list = cp.owner_list.slice();
136 var new_list = svc.copies;
137 if (!copy || !vol) { // collapse copy rows, supply a count instead
142 var current_blob = { copy_count : 0 };
143 angular.forEach(new_list, function (cp) {
145 prev_key = cp.owner_list.join('') + cp.call_number.label;
146 if (cp.barcode) current_blob.copy_count = 1;
147 current_blob.index = index++;
148 current_blob.id_list = cp.id_list;
149 if (cp.raw) current_blob.raw = cp.raw;
150 current_blob.call_number = cp.call_number;
151 current_blob.owner_list = cp.owner_list;
152 current_blob.owner_label = cp.owner_label;
153 current_blob.owner_id = cp.owner_id;
155 var current_key = cp.owner_list.join('') + cp.call_number.label;
156 if (prev_key == current_key) { // collapse into current_blob
157 current_blob.copy_count++;
158 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
159 current_blob.raw = current_blob.raw.concat(cp.raw);
161 current_blob.barcode = current_blob.copy_count;
162 cp_list.push(current_blob);
163 prev_key = current_key;
164 current_blob = { copy_count : 0 };
165 if (cp.barcode) current_blob.copy_count = 1;
166 current_blob.index = index++;
167 current_blob.id_list = cp.id_list;
168 if (cp.raw) current_blob.raw = cp.raw;
169 current_blob.owner_label = cp.owner_label;
170 current_blob.owner_id = cp.owner_id;
171 current_blob.call_number = cp.call_number;
172 current_blob.owner_list = cp.owner_list;
177 current_blob.barcode = current_blob.copy_count;
178 cp_list.push(current_blob);
181 if (!vol) { // do the same for vol rows
186 current_blob = { copy_count : 0 };
187 angular.forEach(cp_list, function (cp) {
189 prev_key = cp.owner_list.join('');
190 current_blob.index = index++;
191 current_blob.id_list = cp.id_list;
192 if (cp.raw) current_blob.raw = cp.raw;
193 current_blob.cn_count = 1;
194 current_blob.copy_count = cp.copy_count;
195 current_blob.owner_list = cp.owner_list;
196 current_blob.owner_label = cp.owner_label;
197 current_blob.owner_id = cp.owner_id;
199 var current_key = cp.owner_list.join('');
200 if (prev_key == current_key) { // collapse into current_blob
201 current_blob.cn_count++;
202 current_blob.copy_count += cp.copy_count;
203 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
204 if (cp.raw) current_blob.raw = current_blob.raw.concat(cp.raw);
206 current_blob.barcode = current_blob.copy_count;
207 current_blob.call_number = { label : current_blob.cn_count };
208 cn_list.push(current_blob);
209 prev_key = current_key;
210 current_blob = { copy_count : 0 };
211 current_blob.index = index++;
212 current_blob.id_list = cp.id_list;
213 if (cp.raw) current_blob.raw = cp.raw;
214 current_blob.owner_label = cp.owner_label;
215 current_blob.owner_id = cp.owner_id;
216 current_blob.cn_count = 1;
217 current_blob.copy_count = cp.copy_count;
218 current_blob.owner_list = cp.owner_list;
223 current_blob.barcode = current_blob.copy_count;
224 current_blob.call_number = { label : current_blob.cn_count };
225 cn_list.push(current_blob);
231 svc.copies = new_list;
237 // notify reads the stream of copies, one at a time.
240 var copies = cn.copies().filter(function(cp){ return cp.deleted() == 'f' });
243 angular.forEach(copies, function (cp) {
247 var owner_id = cn.owning_lib();
248 var owner = egCore.org.get(owner_id);
250 var owner_name_list = [];
251 while (owner.parent_ou()) { // we're going to skip the top of the tree...
252 owner_name_list.unshift(owner.name());
253 owner = egCore.org.get(owner.parent_ou());
258 angular.forEach(copies, function (cp) {
259 var flat_cp = egCore.idl.toHash(cp);
260 flat_cp.owner_id = owner_id;
261 flat_cp.owner_list = owner_name_list;
262 flat_cp.id_list = [flat_cp.id];
267 svc.copies = svc.copies.concat(flat);
272 owner_list : owner_name_list,
273 call_number: egCore.idl.toHash(cn),
285 .directive("egVolumeList", function () {
293 templateUrl: './cat/share/t_volume_list',
295 ['$scope','holdingsSvc','egCore','egGridDataProvider','$uibModal',
296 function($scope , holdingsSvc , egCore , egGridDataProvider, $uibModal) {
297 var holdingsSvcInst = new holdingsSvc();
299 $scope.holdingsGridControls = {};
300 $scope.holdingsGridDataProvider = egGridDataProvider.instance({
301 get : function(offset, count) {
302 return this.arrayNotifier(holdingsSvcInst.copies, offset, count);
306 function gatherHoldingsIds () {
309 $scope.holdingsGridControls.allItems(),
310 function (item) { cp_id_list = cp_id_list.concat(item.id_list) }
315 var spawn_volume_editor = function (copies_too) {
318 'open-ils.actor.anon_cache.set_value',
319 null, 'edit-these-copies', {
320 record_id: $scope.recordId,
321 copies: gatherHoldingsIds(),
323 hide_copies : ((copies_too) ? false : true)
325 ).then(function(key) {
328 templateUrl: './cat/share/t_embedded_volcopy',
330 windowClass: 'eg-wide-modal',
332 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
334 egCore.env.basePath + 'cat/volcopy/' + key + '/embedded';
335 $scope.ok = function(args) { $uibModalInstance.close(args) }
336 $scope.cancel = function () { $uibModalInstance.dismiss() }
338 }).result.then(function() {
344 $scope.edit_volumes = function() {
345 spawn_volume_editor(false);
347 $scope.edit_copies = function() {
348 spawn_volume_editor(true);
351 function load_holdings() {
352 holdingsSvcInst.fetch({
353 rid : $scope.recordId,
354 org : egCore.org.root(),
359 $scope.holdingsGridDataProvider.refresh();
362 $scope.$watch('recordId',
363 function(newVal, oldVal) {
364 if (newVal && newVal !== oldVal) {