]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/volcopy/app.js
webstaff: Copy editing!
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / cat / volcopy / app.js
1 /**
2  * Vol/Copy Editor
3  */
4
5 angular.module('egVolCopy',
6     ['ngRoute', 'ui.bootstrap', 'egCoreMod', 'egUiMod', 'egGridMod'])
7
8 .filter('boolText', function(){
9     return function (v) {
10         return v == 't';
11     }
12 })
13
14 .config(function($routeProvider, $locationProvider, $compileProvider) {
15     $locationProvider.html5Mode(true);
16     $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|blob):/); // grid export
17
18     var resolver = {
19         delay : ['egStartup', function(egStartup) { return egStartup.go(); }]
20     };
21
22     $routeProvider.when('/cat/volcopy/:dataKey', {
23         templateUrl: './cat/volcopy/t_view',
24         controller: 'EditCtrl',
25         resolve : resolver
26     });
27
28 })
29
30 .factory('itemSvc', 
31        ['egCore','$q',
32 function(egCore , $q) {
33
34     var service = {
35         tree : {}, // holds lib->cn->copy hash stack
36         copies : [] // raw copy list
37     };
38
39     // returns a promise resolved with the list of circ mods
40     service.get_classifications = function() {
41         if (egCore.env.acnc)
42             return $q.when(egCore.env.acnc.list);
43
44         return egCore.pcrud.retrieveAll('acnc', null, {atomic : true})
45         .then(function(list) {
46             egCore.env.absorbList(list, 'acnc');
47             return list;
48         });
49     };
50
51     service.get_prefixes = function(org) {
52         if (egCore.env.acnp)
53             return $q.when(egCore.env.acnp.list);
54
55         return egCore.pcrud.search('acnp',
56             {owning_lib : egCore.org.fullPath(org, true)},
57             null, {atomic : true}
58         ).then(function(list) {
59             egCore.env.absorbList(list, 'acnp');
60             return list;
61         });
62
63     };
64
65     service.get_locations = function(orgs) {
66         return egCore.pcrud.search('acpl',
67             {owning_lib : orgs},
68             null, {atomic : true}
69         );
70     };
71
72     service.get_suffixes = function(org) {
73         if (egCore.env.acns)
74             return $q.when(egCore.env.acns.list);
75
76         return egCore.pcrud.search('acns',
77             {owning_lib : egCore.org.fullPath(org, true)},
78             null, {atomic : true}
79         ).then(function(list) {
80             egCore.env.absorbList(list, 'acns');
81             return list;
82         });
83
84     };
85
86     service.get_statuses = function() {
87         if (egCore.env.ccs)
88             return $q.when(egCore.env.ccs.list);
89
90         return egCore.pcrud.retrieveAll('ccs', {}, {atomic : true}).then(
91             function(list) {
92                 egCore.env.absorbList(list, 'ccs');
93                 return list;
94             }
95         );
96
97     };
98
99     service.get_circ_mods = function() {
100         if (egCore.env.ccm)
101             return $q.when(egCore.env.ccm.list);
102
103         return egCore.pcrud.retrieveAll('ccm', {}, {atomic : true}).then(
104             function(list) {
105                 egCore.env.absorbList(list, 'ccm');
106                 return list;
107             }
108         );
109
110     };
111
112     service.get_circ_types = function() {
113         if (egCore.env.citm)
114             return $q.when(egCore.env.citm.list);
115
116         return egCore.pcrud.retrieveAll('citm', {}, {atomic : true}).then(
117             function(list) {
118                 egCore.env.absorbList(list, 'citm');
119                 return list;
120             }
121         );
122
123     };
124
125     service.get_age_protects = function() {
126         if (egCore.env.crahp)
127             return $q.when(egCore.env.crahp.list);
128
129         return egCore.pcrud.retrieveAll('crahp', {}, {atomic : true}).then(
130             function(list) {
131                 egCore.env.absorbList(list, 'crahp');
132                 return list;
133             }
134         );
135
136     };
137
138     service.bmp_parts = {};
139     service.get_parts = function(rec) {
140         if (service.bmp_parts[rec])
141             return $q.when(service.bmp_parts[rec]);
142
143         return egCore.pcrud.search('bmp',
144             {record : rec},
145             null, {atomic : true}
146         ).then(function(list) {
147             service.bmp_parts[rec] = list;
148             return list;
149         });
150
151     };
152
153     service.flesh = {   
154         flesh : 3, 
155         flesh_fields : {
156             acp : ['call_number','parts'],
157             acn : ['label_class','prefix','suffix']
158         }
159     }
160
161     service.addCopy = function (cp) {
162
163         if (!cp.parts()) cp.parts([]); // just in case...
164
165         var lib = cp.call_number().owning_lib();
166         var cn = cp.call_number().id();
167
168         if (!service.tree[lib]) service.tree[lib] = {};
169         if (!service.tree[lib][cn]) service.tree[lib][cn] = [];
170
171         service.tree[lib][cn].push(cp);
172         service.copies.push(cp);
173     }
174
175     service.fetchIds = function(idList) {
176         service.tree = {}; // clear the tree on fetch
177         service.copies = []; // clear the copy list on fetch
178         return egCore.pcrud.search('acp', { 'id' : idList }, service.flesh).then(null,null,
179             function(copy) {
180                 service.addCopy(copy);
181             }
182         );
183     }
184
185     return service;
186 }])
187
188 .directive("egVolCopyEdit", function () {
189     return {
190         restrict: 'E',
191         replace: true,
192         template:
193             '<div class="row">'+
194                 '<div class="col-xs-6"><input type="text" ng-model="barcode" ng-change="updateBarcode()"/></div>'+
195                 '<div class="col-xs-2"><input type="number" ng-model="copy_number" ng-change="updateCopyNo()"/></div>'+
196                 '<div class="col-xs-4"><eg-basic-combo-box list="parts" selected="part"></eg-basic-combo-box></div>'+
197             '</div>',
198
199         scope: { copy: "=", callNumber: "=" },
200         controller : ['$scope','itemSvc',
201             function ( $scope , itemSvc ) {
202                 $scope.new_part_id = 0;
203
204                 $scope.updateBarcode = function () { $scope.copy.barcode($scope.barcode); $scope.copy.ischanged(1); };
205                 $scope.updateCopyNo = function () { $scope.copy.copy_number($scope.copy_number); $scope.copy.ischanged(1); };
206                 $scope.updatePart = function () {
207                     var p = angular.filter($scope.part_list, function (x) {
208                         return x.label() == $scope.part
209                     });
210                     if (p.length > 0) { // preexisting part
211                         $scope.copy.parts(p)
212                     } else { // create one...
213                         var part = new egCore.idl.bmp();
214                         part.id( --$scope.new_part_id );
215                         part.isnew( true );
216                         part.label( $scope.part );
217                         part.record( $scope.callNumber.owning_lib() );
218                         $scope.copy.parts([part]);
219                         $scope.copy.ischanged(1);
220                     }
221                 }
222
223                 $scope.barcode = $scope.copy.barcode();
224                 $scope.copy_number = $scope.copy.copy_number();
225
226                 if ($scope.copy.parts()) {
227                     $scope.part = $scope.copy.parts()[0];
228                     if ($scope.part) $scope.part = $scope.part.label();
229                 };
230
231                 $scope.parts = [];
232                 $scope.part_list = [];
233
234                 itemSvc.get_parts($scope.callNumber.record()).then(function(list){
235                     $scope.part_list = list;
236                     angular.forEach(list, function(p){ $scope.parts.push(p.label()) });
237                 });
238
239             }
240         ]
241
242     }
243 })
244
245 .directive("egVolRow", function () {
246     return {
247         restrict: 'E',
248         replace: true,
249         transclude: true,
250         template:
251             '<div class="row">'+
252                 '<div class="col-xs-1">'+
253                     '<select ng-model="classification" ng-options="cl.name() for cl in classification_list track by idTracker(cl)"/>'+
254                 '</div>'+
255                 '<div class="col-xs-1">'+
256                     '<select ng-model="prefix" ng-change="updatePrefix()" ng-options="p.label() for p in prefix_list track by idTracker(p)"/>'+
257                 '</div>'+
258                 '<div class="col-xs-3"><input type="text" ng-change="updateLabel()" ng-model="label"/></div>'+
259                 '<div class="col-xs-1">'+
260                     '<select ng-model="suffix" ng-change="updateSuffix()" ng-options="s.label() for s in suffix_list track by idTracker(s)"/>'+
261                 '</div>'+
262                 '<div class="col-xs-1"><input type="number" ng-model="copy_count" min="{{orig_copy_count}}" ng-change="changeCPCount()"></div>'+
263                 '<div class="col-xs-5">'+
264                     '<div class="container-fluid">'+
265                         '<eg-vol-copy-edit ng-repeat="cp in copies track by idTracker(cp)" copy="cp" call-number="callNumber"></eg-vol-copy-edit>'+
266                     '</div>'+
267                 '</div>'+
268             '</div>',
269
270         scope: {allcopies: "=", copies: "=" },
271         controller : ['$scope','itemSvc','egCore',
272             function ( $scope , itemSvc , egCore ) {
273                 $scope.new_cp_id = 0;
274                 $scope.callNumber =  $scope.copies[0].call_number();
275
276                 $scope.idTracker = function (x) { if (x) return x.id() };
277
278                 $scope.suffix_list = [];
279                 itemSvc.get_suffixes($scope.callNumber.owning_lib()).then(function(list){
280                     $scope.suffix_list = list;
281                 });
282                 $scope.updateSuffix = function () { $scope.callNumber.suffix($scope.suffix); $scope.callNumber.ischanged(1); };
283
284                 $scope.prefix_list = [];
285                 itemSvc.get_prefixes($scope.callNumber.owning_lib()).then(function(list){
286                     $scope.prefix_list = list;
287                 });
288                 $scope.updatePrefix = function () { $scope.callNumber.prefix($scope.prefix); $scope.callNumber.ischanged(1); };
289
290                 $scope.classification_list = [];
291                 itemSvc.get_classifications().then(function(list){
292                     $scope.classification_list = list;
293                 });
294                 $scope.updateClassification = function () { $scope.callNumber.label_class($scope.classification); $scope.callNumber.ischanged(1); };
295
296                 $scope.classification = $scope.callNumber.label_class();
297                 $scope.prefix = $scope.callNumber.prefix();
298                 $scope.suffix = $scope.callNumber.suffix();
299
300                 $scope.label = $scope.callNumber.label();
301                 $scope.updateLabel = function () { $scope.callNumber.label($scope.label); $scope.callNumber.ischanged(1); };
302
303                 $scope.copy_count = $scope.copies.length;
304                 $scope.orig_copy_count = $scope.copy_count;
305
306                 $scope.changeCPCount = function () {
307                     while ($scope.copy_count > $scope.copies.length) {
308                         var cp = new egCore.idl.acp();
309                         cp.id( --$scope.new_cp_id );
310                         cp.isnew( true );
311                         cp.circ_lib( $scope.lib );
312                         cp.call_number( $scope.callNumber );
313                         $scope.copies.push( cp );
314                         $scope.allcopies.push( cp );
315                     }
316
317                     var how_many = $scope.copies.length - $scope.copy_count;
318                     if (how_many > 0) {
319                         var dead = $scope.copies.splice($scope.copy_count,how_many);
320                         $scope.callNumber.copies($scope.copies);
321
322                         // Trimming the global list is a bit more tricky
323                         angular.forEach( dead, function (d) {
324                             angular.forEach( $scope.allcopies, function (l, i) { 
325                                 if (l === d) $scope.allcopies.splice(i,1);
326                             });
327                         });
328                     }
329                 }
330
331             }
332         ]
333
334     }
335 })
336
337 .directive("egVolEdit", function () {
338     return {
339         restrict: 'E',
340         replace: true,
341         template:
342             '<div class="row">'+
343                 '<div class="col-xs-1"><eg-org-selector selected="owning_lib" disableTest="cant_have_vols"></eg-org-selector></div>'+
344                 '<div class="col-xs-1"><input type="number" min="{{orig_cn_count}}" ng-model="cn_count" ng-change="changeCNCount()"/></div>'+
345                 '<div class="col-xs-10">'+
346                     '<div class="container-fluid">'+
347                         '<eg-vol-row ng-repeat="(cn,copies) in struct track by cn" copies="copies" allcopies="allcopies"></eg-vol-row>'+
348                     '</div>'+
349                 '</div>'+
350             '</div>',
351
352         scope: { allcopies: "=", struct: "=", lib: "@", record: "@" },
353         controller : ['$scope','itemSvc','egCore',
354             function ( $scope , itemSvc , egCore ) {
355                 $scope.new_cn_id = 0;
356                 $scope.first_cn = Object.keys($scope.struct)[0];
357                 $scope.full_cn = $scope.struct[$scope.first_cn][0].call_number();
358
359                 $scope.cn_count = Object.keys($scope.struct).length;
360                 $scope.orig_cn_count = $scope.cn_count;
361
362                 $scope.owning_lib = egCore.org.get($scope.lib);
363                 $scope.$watch('owning_lib', function (l) {
364                     angular.forEach( $scope.struct[$scope.first_cn], function (cp) {
365                         cp.call_number().owning_lib( $scope.owning_lib.id() );
366                     });
367                 });
368
369                 $scope.cant_have_vols = function (id) { return !egCore.org.CanHaveVolumes(id); };
370
371                 $scope.$watch('cn_count', function (n) {
372                     var o = Object.keys($scope.struct).length;
373                     if (n > o) { // adding
374                         for (var i = o; o < n; o++) {
375                             var cn = new egCore.idl.acn();
376                             cn.id( --$scope.new_cn_id );
377                             cn.isnew( true );
378                             cn.owning_lib( $scope.owning_lib.id() );
379                             cn.record( $scope.full_cn.record() );
380
381                             var cp = new egCore.idl.acp();
382                             cp.id( --$scope.new_cp_id );
383                             cp.isnew( true );
384                             cp.circ_lib( $scope.owning_lib.id() );
385                             cp.call_number( cn );
386
387                             $scope.struct[cn.id()] = [cp];
388                             $scope.allcopies.push(cp);
389                         }
390                     } else if (n < o) { // removing
391                         var how_many = o - n;
392                         var list = Object
393                                 .keys($scope.struct)
394                                 .sort(function(a, b){return a-b})
395                                 .reverse();
396                         for (var i = how_many; i > 0; i--) {
397                             // Trimming the global list is a bit more tricky
398                             angular.forEach($scope.struct[list[i]], function (d) {
399                                 angular.forEach( $scope.allcopies, function (l, j) { 
400                                     if (l === d) $scope.allcopies.splice(j,1);
401                                 });
402                             });
403                             delete $scope.struct[list[i]];
404                         }
405                     }
406                 });
407             }
408         ]
409
410     }
411 })
412
413 /**
414  * Edit controller!
415  */
416 .controller('EditCtrl', 
417        ['$scope','$q','$routeParams','$location','$timeout','egCore','egNet','egGridDataProvider','itemSvc',
418 function($scope , $q , $routeParams , $location , $timeout , egCore , egNet , egGridDataProvider , itemSvc) {
419
420     $scope.show_vols = true;
421     $scope.show_copies = true;
422
423     $scope.tracker = function (x,f) { if (x) return x[f]() };
424     $scope.idTracker = function (x) { if (x) return $scope.tracker(x,'id') };
425     $scope.cant_have_vols = function (id) { return !egCore.org.CanHaveVolumes(id); };
426
427     $scope.orgById = function (id) { return egCore.org.get(id) }
428     $scope.statusById = function (id) {
429         return $scope.status_list.filter( function (s) { return s.id() == id } )[0];
430     }
431     $scope.locationById = function (id) {
432         return $scope.location_cache[''+id];
433     }
434
435     $scope.workingToComplete = function () {
436         angular.forEach( $scope.workingGridControls.selectedItems(), function (c) {
437             angular.forEach( itemSvc.copies, function (w, i) {
438                 if (c === w)
439                     $scope.completed_copies = $scope.completed_copies.concat(itemSvc.copies.splice(i,1));
440             });
441         });
442     }
443
444     $scope.completeToWorking = function () {
445         angular.forEach( $scope.completedGridControls.selectedItems(), function (c) {
446             angular.forEach( $scope.completed_copies, function (w, i) {
447                 if (c === w)
448                     itemSvc.copies = itemSvc.copies.concat($scope.completed_copies.splice(i,1));
449             });
450         });
451     }
452
453     createSimpleUpdateWatcher = function (field) {
454         $scope.$watch('working.' + field, function () {
455             var newval = $scope.working[field];
456             if (angular.isObject(newval)) { // we'll use the pkey
457                 if (newval.id) newval = newval.id();
458                 else if (newval.code) newval = newval.code();
459             }
460
461             if ($scope.workingGridControls && $scope.workingGridControls.selectedItems) {
462                 angular.forEach(
463                     $scope.workingGridControls.selectedItems(),
464                     function (cp) { cp[field](newval); cp.ischanged(1); }
465                 );
466             }
467         });
468     }
469
470     $timeout(function(){
471
472     var dataKey = $routeParams.dataKey;
473     console.debug('dataKey: ' + dataKey);
474
475     if (dataKey && dataKey.length > 0) {
476         $scope.working = {};
477
478         $scope.copytab = 'working';
479         $scope.tab = 'edit';
480         $scope.summaryRecord = null;
481         $scope.record_id = null;
482         $scope.data = {};
483         $scope.completed_copies = [];
484         $scope.location_orgs = [];
485         $scope.location_cache = {};
486
487         $scope.completedGridDataProvider = egGridDataProvider.instance({
488             get : function(offset, count) {
489                 //return provider.arrayNotifier(itemSvc.copies, offset, count);
490                 return this.arrayNotifier($scope.completed_copies, offset, count);
491             }
492         });
493
494         $scope.completedGridControls = {};
495
496         $scope.workingGridDataProvider = egGridDataProvider.instance({
497             get : function(offset, count) {
498                 //return provider.arrayNotifier(itemSvc.copies, offset, count);
499                 return this.arrayNotifier(itemSvc.copies, offset, count);
500             }
501         });
502
503         $scope.workingGridControls = {};
504
505         egNet.request(
506             'open-ils.actor',
507             'open-ils.actor.anon_cache.get_value',
508             dataKey, 'edit-these-copies'
509         ).then(function (data) {
510
511             if (data.hide_vols) $scope.show_vols = false;
512             if (data.hide_copies) $scope.show_copies = false;
513
514             $scope.record_id = data.record_id;
515
516             if (data.copies && data.copies.length)
517                 return itemSvc.fetchIds(data.copies);
518
519             if (data.raw && data.raw.length) {
520
521                 /* data.raw must be an array of copies with (at least)
522                  * the call number fleshed on each.  For new copies
523                  * create from whole cloth, the id for each should
524                  * probably be negative and isnew() should return true.
525                  * Each /distinct/ call number must have a distinct id
526                  * as well, probably negative also if they're new. Clear?
527                  */
528
529                 angular.forEach(
530                     data.raw,
531                     function (cp) { itemSvc.addCopy(cp) }
532                 );
533
534                 return itemSvc.copies;
535             }
536
537         }).then( function() {
538             $scope.data = itemSvc;
539             $scope.workingGridDataProvider.refresh();
540         });
541
542         $scope.$watch('data.copies.length', function () {
543             if ($scope.data.copies) {
544                 var base_orgs = $scope.data.copies.map(function(cp){
545                     return cp.circ_lib()
546                 }).filter(function(e,i,a){
547                     return a.lastIndexOf(e) === i;
548                 });
549
550                 var all_orgs = [];
551                 angular.forEach(base_orgs, function(o) {
552                     all_orgs = all_orgs.concat( egCore.org.fullPath(o, true) );
553                 });
554
555                 var final_orgs = all_orgs.filter(function(e,i,a){
556                     return a.lastIndexOf(e) === i;
557                 }).sort(function(a,b){return b-a});
558
559                 if ($scope.location_orgs.toString() != final_orgs.toString()) {
560                     $scope.location_orgs = final_orgs;
561                     if ($scope.location_orgs.length) {
562                         itemSvc.get_locations($scope.location_orgs).then(function(list){
563                             angular.forEach(list, function(l) {
564                                 $scope.location_cache[ ''+l.id() ] = l;
565                             });
566                             $scope.location_list = list;
567                         });
568                     }
569                 }
570             }
571
572             $scope.workingGridDataProvider.refresh();
573         });
574
575         $scope.$watch('completed_copies.length', function () {
576             $scope.completedGridDataProvider.refresh();
577         });
578
579         $scope.location_list = [];
580         itemSvc.get_locations().then(function(list){
581             $scope.location_list = list;
582         });
583         createSimpleUpdateWatcher('location');
584
585         $scope.status_list = [];
586         itemSvc.get_statuses().then(function(list){
587             $scope.status_list = list;
588         });
589         createSimpleUpdateWatcher('status');
590
591         $scope.circ_mod_list = [];
592         itemSvc.get_circ_mods().then(function(list){
593             $scope.circ_mod_list = list;
594         });
595         createSimpleUpdateWatcher('circ_modifier');
596
597         $scope.circ_type_list = [];
598         itemSvc.get_circ_types().then(function(list){
599             $scope.circ_type_list = list;
600         });
601         createSimpleUpdateWatcher('circ_as_type');
602
603         $scope.age_protect_list = [];
604         itemSvc.get_age_protects().then(function(list){
605             $scope.age_protect_list = list;
606         });
607         createSimpleUpdateWatcher('age_protect');
608
609         createSimpleUpdateWatcher('circulate');
610         createSimpleUpdateWatcher('holdable');
611         createSimpleUpdateWatcher('fine_level');
612         createSimpleUpdateWatcher('loan_duration');
613         createSimpleUpdateWatcher('cost');
614         createSimpleUpdateWatcher('deposit');
615         createSimpleUpdateWatcher('deposit_amount');
616         createSimpleUpdateWatcher('mint_condition');
617         createSimpleUpdateWatcher('opac_visible');
618         createSimpleUpdateWatcher('ref');
619
620     }
621
622     });
623
624 }])
625
626