LP#1840332 Item Status Input Field Acessibility
[Evergreen.git] / Open-ILS / web / js / ui / default / staff / cat / services / holdings.js
1 angular.module('egHoldingsMod', ['egCoreMod','egGridMod'])
2
3 .factory('holdingsSvc', 
4        ['egCore','$q',
5 function(egCore , $q) {
6
7     var service = function() {
8         this.ongoing = false;
9         this.copies = []; // record search results
10         this.index = 0; // search grid index
11         this.org = null;
12         this.rid = null;
13     };
14
15     service.prototype.flesh = {   
16         flesh : 3,
17         flesh_fields : {
18             acp : ['status','location','circ_lib','parts','age_protect','copy_alerts', 'latest_inventory'],
19             acn : ['prefix','suffix','copies','label_class','record'],
20             bre : ['simple_record'],
21             alci : ['inventory_workstation']
22         }
23     }
24
25     service.prototype.fetchAgain = function() {
26         return this.fetch({
27             rid: this.rid,
28             org: this.org,
29             copy: this.copy,
30             vol: this.vol,
31             empty: this.empty,
32             empty_org: this.empty_org
33         })
34     };
35
36     // resolved with the last received copy
37     service.prototype.fetch = function(opts) {
38         var svc = this;
39
40         if (svc.ongoing && svc.p) {
41             svc.p.cancel = true;
42             console.log('Canceling fetch for org '+ svc.org.id());
43             if (svc.p.reject) svc.p.reject();
44         }
45
46         var rid = opts.rid;
47         var empty_org = opts.empty_org;
48         var org = opts.org;
49         var copy = opts.copy;
50         var vol = opts.vol;
51         var empty = opts.empty;
52
53         if (!rid) return $q.when();
54         if (!org) return $q.when();
55
56         svc.ongoing = true;
57
58         svc.rid = rid;
59         svc.empty_org = opts.empty_org;
60         svc.org = org;
61         svc.copy = opts.copy;
62         svc.vol = opts.vol;
63         svc.empty = opts.empty;
64
65         svc.copies = [];
66         svc.index = 0;
67
68         var org_list = egCore.org.descendants(org.id(), true);
69         console.log('Holdings fetch with: rid='+rid+' org='+org_list+' copy='+copy+' vol='+vol+' empty='+empty);
70
71         svc.org_use_map = {};
72         org_list.map(function(o){svc.org_use_map[''+o]=0;})
73
74         var p = egCore.pcrud.search(
75             'acn',
76             {record : rid, owning_lib : org_list, deleted : 'f', label : {'!=' : '##URI##'}},
77             svc.flesh
78         ).then(
79             function() { // finished
80                 if (p.cancel) return;
81
82                 // create virtual field for displaying active parts
83                 angular.forEach(svc.copies, function (cp) {
84                     cp.monograph_parts = '';
85                     if (cp.parts && cp.parts.length > 0) {
86                         cp.monograph_parts = cp.parts.map(function(obj) { return obj.label; }).join(', ');
87                         cp.monograph_parts_sortkeys = cp.parts.map(function(obj) { return obj.label_sortkey; }).join();
88                     }
89                 });
90
91                 svc.copies = svc.copies.sort(
92                     function (a, b) {
93                         function compare_array (x, y, i) {
94                             if (x[i] && y[i]) { // both have values
95                                 if (x[i] == y[i]) { // need to look deeper
96                                     return compare_array(x, y, ++i);
97                                 }
98
99                                 if (x[i] < y[i]) { // x is first
100                                     return -1;
101                                 } else if (x[i] > y[i]) { // y is first
102                                     return 1;
103                                 }
104
105                             } else { // no orgs to compare ...
106                                 if (x[i]) return -1;
107                                 if (y[i]) return 1;
108                             }
109                             return 0;
110                         }
111
112                         var owner_order = compare_array(a.owner_list, b.owner_list, 0);
113                         if (!owner_order) {
114                             // now compare on CN label
115                             if (a.call_number.label < b.call_number.label) return -1;
116                             if (a.call_number.label > b.call_number.label) return 1;
117
118                             // also parts sortkeys combined string
119                             if (a.monograph_parts_sortkeys < b.monograph_parts_sortkeys) return -1;
120                             if (a.monograph_parts_sortkeys > b.monograph_parts_sortkeys) return 1;
121
122                             // try copy number
123                             if (a.copy_number < b.copy_number) return -1;
124                             if (a.copy_number > b.copy_number) return 1;
125
126                             // finally, barcode
127                             if (a.barcode < b.barcode) return -1;
128                             if (a.barcode > b.barcode) return 1;
129                         }
130                         return owner_order;
131                     }
132                 );
133
134                 // create virtual fields for copy alert count and most recent circ
135                 angular.forEach(svc.copies, function (cp) {
136                     if (cp.copy_alerts) {
137                         cp.copy_alert_count = cp.copy_alerts.filter(function(aca) { return aca.ack_time == null ;}).length;
138                     }
139                     else cp.copy_alert_count = 0;
140                 });
141
142                 // Grab the open circulation (i.e. checkin_time=null) for
143                 // all of the copies we're rendering so we can display
144                 // due date info.  There should only ever be one circulation
145                 // at most with checkin_time=null for any copy.
146                 var copyIds = svc.copies.map(function(cp) {return cp.id})
147                     .filter(function(id) {return Boolean(id)}); // avoid nulls
148
149                 egCore.pcrud.search('circ', 
150                     {target_copy: copyIds, checkin_time: null}
151                 ).then(
152                     null, // complete
153                     null, // error
154                     function(circ) {
155                         var cp = svc.copies.filter(function(c) { 
156                             return c.id == circ.target_copy() })[0];
157                         cp._circ = egCore.idl.toHash(circ, true);
158                         cp._circ_lib = circ.circ_lib();
159                         cp._duration = circ.duration();
160                     }
161                 );
162
163                 // create a label using just the unique part of the owner list
164                 var index = 0;
165                 var prev_owner_list;
166                 angular.forEach(svc.copies, function (cp) {
167                     if (!prev_owner_list) {
168                         cp.owner_label = cp.owner_list.join(' ... ');
169                     } else {
170                         var current_owner_list = cp.owner_list.slice();
171                         while (current_owner_list[1] && prev_owner_list[1] && current_owner_list[0] == prev_owner_list[0]) {
172                             current_owner_list.shift();
173                             prev_owner_list.shift();
174                         }
175                         cp.owner_label = current_owner_list.join(' ... ');
176                     }
177
178                     cp.index = index++;
179                     prev_owner_list = cp.owner_list.slice();
180                 });
181
182                 var new_list = svc.copies;
183                 if (!copy || !vol) { // collapse copy rows, supply a count instead
184
185                     index = 0;
186                     var cp_list = [];
187                     var prev_key;
188                     var current_blob = { copy_count : 0 };
189                     angular.forEach(new_list, function (cp) {
190                         if (!prev_key) {
191                             prev_key = cp.owner_list.join('') + cp.call_number.label;
192                             if (cp.barcode) current_blob.copy_count = 1;
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.call_number = cp.call_number;
197                             current_blob.owner_list = cp.owner_list;
198                             current_blob.owner_label = cp.owner_label;
199                             current_blob.owner_id = cp.owner_id;
200                         } else {
201                             var current_key = cp.owner_list.join('') + cp.call_number.label;
202                             if (prev_key == current_key) { // collapse into current_blob
203                                 current_blob.copy_count++;
204                                 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
205                                 current_blob.raw = current_blob.raw.concat(cp.raw);
206                             } else {
207                                 current_blob.barcode = current_blob.copy_count;
208                                 cp_list.push(current_blob);
209                                 prev_key = current_key;
210                                 current_blob = { copy_count : 0 };
211                                 if (cp.barcode) current_blob.copy_count = 1;
212                                 current_blob.index = index++;
213                                 current_blob.id_list = cp.id_list;
214                                 if (cp.raw) current_blob.raw = cp.raw;
215                                 current_blob.owner_label = cp.owner_label;
216                                 current_blob.owner_id = cp.owner_id;
217                                 current_blob.call_number = cp.call_number;
218                                 current_blob.owner_list = cp.owner_list;
219                             }
220                         }
221                     });
222
223                     current_blob.barcode = current_blob.copy_count;
224                     cp_list.push(current_blob);
225                     new_list = cp_list;
226
227                     if (!vol) { // do the same for vol rows
228
229                         index = 0;
230                         var cn_list = [];
231                         prev_key = '';
232                         current_blob = { copy_count : 0 };
233                         angular.forEach(cp_list, function (cp) {
234                             if (!prev_key) {
235                                 prev_key = cp.owner_list.join('');
236                                 current_blob.index = index++;
237                                 current_blob.id_list = cp.id_list;
238                                 if (cp.raw) current_blob.raw = cp.raw;
239                                 current_blob.cn_count = 1;
240                                 current_blob.copy_count = cp.copy_count;
241                                 current_blob.owner_list = cp.owner_list;
242                                 current_blob.owner_label = cp.owner_label;
243                                 current_blob.owner_id = cp.owner_id;
244                             } else {
245                                 var current_key = cp.owner_list.join('');
246                                 if (prev_key == current_key) { // collapse into current_blob
247                                     current_blob.cn_count++;
248                                     current_blob.copy_count += cp.copy_count;
249                                     current_blob.id_list = current_blob.id_list.concat(cp.id_list);
250                                     if (cp.raw) {
251                                         if (current_blob.raw) current_blob.raw = current_blob.raw.concat(cp.raw);
252                                         else current_blob.raw = cp.raw;
253                                     }
254                                 } else {
255                                     current_blob.barcode = current_blob.copy_count;
256                                     current_blob.call_number = { label : current_blob.cn_count };
257                                     cn_list.push(current_blob);
258                                     prev_key = current_key;
259                                     current_blob = { copy_count : 0 };
260                                     current_blob.index = index++;
261                                     current_blob.id_list = cp.id_list;
262                                     if (cp.raw) current_blob.raw = cp.raw;
263                                     current_blob.owner_label = cp.owner_label;
264                                     current_blob.owner_id = cp.owner_id;
265                                     current_blob.cn_count = 1;
266                                     current_blob.copy_count = cp.copy_count;
267                                     current_blob.owner_list = cp.owner_list;
268                                 }
269                             }
270                         });
271     
272                         current_blob.barcode = current_blob.copy_count;
273                         current_blob.call_number = { label : current_blob.cn_count };
274                         cn_list.push(current_blob);
275                         new_list = cn_list;
276     
277                     }
278                 }
279
280                 if (empty_org) {
281
282                     var empty_org_list = [];
283                     angular.forEach(svc.org_use_map,function(v,k){
284                         if (v == 0) empty_org_list.push(k);
285                     });
286
287                     angular.forEach(empty_org_list, function (oid) {
288                         var owner = egCore.org.get(oid);
289                         if (owner.ou_type().can_have_vols() != 't') return;
290
291                         var owner_list = [];
292                         while (owner.parent_ou()) { // we're going to skip the top of the tree...
293                             owner_list.unshift(owner.shortname());
294                             owner = egCore.org.get(owner.parent_ou());
295                         }
296
297                         var owner_label = owner_list.join(' ... ');
298
299                         new_list.push({
300                             index      : index++,
301                             id_list    : [],
302                             call_number: { label : '' },
303                             barcode    : '',
304                             owner_id   : oid,
305                             owner_list : owner_list,
306                             owner_label: owner_label,
307                             copy_count : 0,
308                             cn_count   : 0,
309                             copy_alert_count : 0
310                         });
311                     });
312                 }
313
314                 svc.copies = new_list;
315                 svc.ongoing = false;
316             },
317
318             null, // error
319
320             // notify reads the stream of copies, one at a time.
321             function(cn) {
322                 if (p.cancel) return;
323
324                 var copies = cn.copies().filter(function(cp){ return cp.deleted() == 'f' });
325                 cn.copies([]);
326
327                 angular.forEach(copies, function (cp) {
328                     cp.call_number(cn);
329                 });
330
331                 var owner_id = cn.owning_lib();
332                 var owner = egCore.org.get(owner_id);
333                 svc.org_use_map[''+owner_id] += 1;
334
335                 var owner_name_list = [];
336                 while (owner.parent_ou()) { // we're going to skip the top of the tree...
337                     owner_name_list.unshift(owner.shortname());
338                     owner = egCore.org.get(owner.parent_ou());
339                 }
340
341                 if (copies[0]) {
342                     var flat = [];
343                     angular.forEach(copies, function (cp) {
344                         var flat_cp = egCore.idl.toHash(cp);
345                         flat_cp.owner_id = owner_id;
346                         flat_cp.owner_list = owner_name_list;
347                         flat_cp.id_list = [flat_cp.id];
348                         flat_cp.raw = [cp];
349                         flat.push(flat_cp);
350                     });
351
352                     svc.copies = svc.copies.concat(flat);
353                 } else if (empty) {
354                     svc.copies.push({
355                         id_list    : [],
356                         owner_id   : owner_id,
357                         owner_list : owner_name_list,
358                         call_number: egCore.idl.toHash(cn),
359                         raw_call_number: cn
360                     });
361                 }
362
363                 return cn;
364             }
365         );
366
367         return svc.p = p;
368     };
369
370     return service;
371 }])
372 .directive("egVolumeList", function () {
373     return {
374         restrict:   'AE',
375         scope: {
376             recordId : '=',
377             editVolumes : '@',
378             editCopies  : '@'
379         },
380         templateUrl: './cat/share/t_volume_list',
381         controller:
382                    ['$scope','holdingsSvc','egCore','egGridDataProvider','$uibModal',
383             function($scope , holdingsSvc , egCore , egGridDataProvider,  $uibModal) {
384                 var holdingsSvcInst = new holdingsSvc();
385
386                 $scope.holdingsGridControls = {};
387                 $scope.holdingsGridDataProvider = egGridDataProvider.instance({
388                     get : function(offset, count) {
389                         return this.arrayNotifier(holdingsSvcInst.copies, offset, count);
390                     }
391                 });
392
393                 function gatherHoldingsIds () {
394                     var cp_id_list = [];
395                     angular.forEach(
396                         $scope.holdingsGridControls.allItems(),
397                         function (item) { cp_id_list = cp_id_list.concat(item.id_list) }
398                     );
399                     return cp_id_list;
400                 }
401
402                 var spawn_volume_editor = function (copies_too) {
403                     egCore.net.request(
404                         'open-ils.actor',
405                         'open-ils.actor.anon_cache.set_value',
406                         null, 'edit-these-copies', {
407                             record_id: $scope.recordId,
408                             copies: gatherHoldingsIds(),
409                             hide_vols : false,
410                             hide_copies : ((copies_too) ? false : true)
411                         }
412                     ).then(function(key) {
413                         if (key) {
414                             $uibModal.open({
415                                 templateUrl: './cat/share/t_embedded_volcopy',
416                                 backdrop: 'static',
417                                 size: 'lg',
418                                 windowClass: 'eg-wide-modal',
419                                 controller:
420                                     ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
421                                     $scope.volcopy_url = 
422                                         egCore.env.basePath + 'cat/volcopy/' + key + '/embedded';
423                                     $scope.ok = function(args) { $uibModalInstance.close(args) }
424                                     $scope.cancel = function () { $uibModalInstance.dismiss() }
425                                 }]
426                             }).result.then(function() {
427                                 load_holdings();
428                             });
429                         }
430                     });
431                 }
432                 $scope.edit_volumes = function() {
433                     spawn_volume_editor(false);
434                 }
435                 $scope.edit_copies = function() {
436                     spawn_volume_editor(true);
437                 }
438
439                 function load_holdings() {
440                     holdingsSvcInst.fetch({
441                         rid   : $scope.recordId,
442                         org   : egCore.org.root(),
443                         copy  : false,
444                         vol   : true,
445                         empty : true
446                     }).then(function() {
447                         $scope.holdingsGridDataProvider.refresh();
448                     });
449                 };
450                 $scope.$watch('recordId',
451                     function(newVal, oldVal) {
452                         if (newVal && newVal !== oldVal) {
453                             load_holdings();
454                         }
455                     }
456                 );
457                 load_holdings();
458             }]
459     }
460 })
461 .filter('string_pick', function() { return function(i){ return arguments[i] || ''; }; })
462 ;