]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/volcopy/app.js
webstaff: Vol/Copy edit (volumes ATM)
[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_suffixes = function(org) {
66         if (egCore.env.acns)
67             return $q.when(egCore.env.acns.list);
68
69         return egCore.pcrud.search('acns',
70             {owning_lib : egCore.org.fullPath(org, true)},
71             null, {atomic : true}
72         ).then(function(list) {
73             egCore.env.absorbList(list, 'acns');
74             return list;
75         });
76
77     };
78
79     service.bmp_parts = {};
80     service.get_parts = function(rec) {
81         if (service.bmp_parts[rec])
82             return $q.when(service.bmp_parts[rec]);
83
84         return egCore.pcrud.search('bmp',
85             {record : rec},
86             null, {atomic : true}
87         ).then(function(list) {
88             service.bmp_parts[rec] = list;
89             return list;
90         });
91
92     };
93
94     service.flesh = {   
95         flesh : 3, 
96         flesh_fields : {
97             acp : ['call_number','parts'],
98             acn : ['label_class','prefix','suffix']
99         },
100         select : { 
101             // avoid fleshing MARC on the bre
102             // note: don't add simple_record.. not sure why
103             bre : ['id','tcn_value','creator','editor'],
104         } 
105     }
106
107     service.addCopy = function (cp) {
108
109         var lib = cp.call_number().owning_lib();
110         var cn = cp.call_number().id();
111
112         // XXX need to change this data structure, likely...
113         if (!service.tree[lib]) service.tree[lib] = {};
114         if (!service.tree[lib][cn]) service.tree[lib][cn] = [];
115
116         service.tree[lib][cn].push(cp);
117         service.copies.push(cp);
118     }
119
120     service.fetchIds = function(idList) {
121         service.tree = {}; // clear the tree on fetch
122         service.copies = []; // clear the copy list on fetch
123         return egCore.pcrud.search('acp', { 'id' : idList }, service.flesh).then(null,null,
124             function(copy) {
125                 service.addCopy(copy);
126             }
127         );
128     }
129
130     return service;
131 }])
132
133 .directive("egVolCopyEdit", function () {
134     return {
135         restrict: 'E',
136         replace: true,
137         template:
138             '<div class="row">'+
139                 '<div class="col-xs-6"><input type="text" ng-model="barcode" ng-change="updateBarcode()"/></div>'+
140                 '<div class="col-xs-2"><input type="number" ng-model="copy_number" ng-change="updateCopyNo()"/></div>'+
141                 '<div class="col-xs-4"><eg-basic-combo-box list="parts" selected="part"></eg-basic-combo-box></div>'+
142             '</div>',
143
144         scope: { copy: "=", callNumber: "=" },
145         controller : ['$scope','itemSvc',
146             function ( $scope , itemSvc ) {
147                 $scope.new_part_id = 0;
148
149                 $scope.updateBarcode = function () { $scope.copy.barcode($scope.barcode) };
150                 $scope.updateCopyNo = function () { $scope.copy.copy_number($scope.copy_number) };
151                 $scope.updatePart = function () {
152                     var p = angular.filter($scope.part_list, function (x) {
153                         return x.label() == $scope.part
154                     });
155                     if (p.length > 0) { // preexisting part
156                         $scope.copy.parts(p)
157                     } else { // create one...
158                         var part = new egCore.idl.bmp();
159                         part.id( --$scope.new_part_id );
160                         part.isnew( true );
161                         part.label( $scope.part );
162                         part.record( $scope.callNumber.owning_lib() );
163                         $scope.copy.parts([part]);
164                     }
165                 }
166
167                 $scope.barcode = $scope.copy.barcode();
168                 $scope.copy_number = $scope.copy.copy_number();
169
170                 if ($scope.copy.parts()) {
171                     $scope.part = $scope.copy.parts()[0];
172                     if ($scope.part) $scope.part = $scope.part.label();
173                 };
174
175                 $scope.parts = [];
176                 $scope.part_list = [];
177
178                 itemSvc.get_parts($scope.callNumber.record()).then(function(list){
179                     $scope.part_list = list;
180                     angular.forEach(list, function(p){ $scope.parts.push(p.label()) });
181                 });
182
183             }
184         ]
185
186     }
187 })
188
189 .directive("egVolRow", function () {
190     return {
191         restrict: 'E',
192         replace: true,
193         transclude: true,
194         template:
195             '<div class="row">'+
196                 '<div class="col-xs-1">'+
197                     '<select ng-model="classification" ng-options="cl.name() for cl in classification_list track by idTracker(cl)"/>'+
198                 '</div>'+
199                 '<div class="col-xs-1">'+
200                     '<select ng-model="prefix" ng-change="updatePrefix()" ng-options="p.label() for p in prefix_list track by idTracker(p)"/>'+
201                 '</div>'+
202                 '<div class="col-xs-3"><input type="text" ng-change="updateLabel()" ng-model="label"/></div>'+
203                 '<div class="col-xs-1">'+
204                     '<select ng-model="suffix" ng-change="updateSuffix()" ng-options="s.label() for s in suffix_list track by idTracker(s)"/>'+
205                 '</div>'+
206                 '<div class="col-xs-1"><input type="number" ng-model="copy_count" min="{{orig_copy_count}}" ng-change="changeCPCount()"></div>'+
207                 '<div class="col-xs-5">'+
208                     '<div class="container-fluid">'+
209                         '<eg-vol-copy-edit ng-repeat="cp in copies track by idTracker(cp)" copy="cp" call-number="callNumber"></eg-vol-copy-edit>'+
210                     '</div>'+
211                 '</div>'+
212             '</div>',
213
214         scope: { copies: "=" },
215         controller : ['$scope','itemSvc','egCore',
216             function ( $scope , itemSvc , egCore ) {
217                 $scope.new_cp_id = 0;
218                 $scope.callNumber =  $scope.copies[0].call_number();
219
220                 $scope.idTracker = function (x) { if (x) return x.id() };
221
222                 $scope.suffix_list = [];
223                 itemSvc.get_suffixes($scope.callNumber.owning_lib()).then(function(list){
224                     $scope.suffix_list = list;
225                 });
226                 $scope.updateSuffix = function () { $scope.callNumber.suffix($scope.suffix) };
227
228                 $scope.prefix_list = [];
229                 itemSvc.get_prefixes($scope.callNumber.owning_lib()).then(function(list){
230                     $scope.prefix_list = list;
231                 });
232                 $scope.updatePrefix = function () { $scope.callNumber.prefix($scope.prefix) };
233
234                 $scope.classification_list = [];
235                 itemSvc.get_classifications().then(function(list){
236                     $scope.classification_list = list;
237                 });
238                 $scope.updateClassification = function () { $scope.callNumber.label_class($scope.classification) };
239
240                 $scope.classification = $scope.callNumber.label_class();
241                 $scope.prefix = $scope.callNumber.prefix();
242                 $scope.suffix = $scope.callNumber.suffix();
243
244                 $scope.label = $scope.callNumber.label();
245                 $scope.updateLabel = function () { $scope.callNumber.label($scope.label) };
246
247                 $scope.copy_count = $scope.copies.length;
248                 $scope.orig_copy_count = $scope.copy_count;
249
250                 $scope.changeCPCount = function () {
251                     while ($scope.copy_count > $scope.copies.length) {
252                         var cp = new egCore.idl.acp();
253                         cp.id( --$scope.new_cp_id );
254                         cp.isnew( true );
255                         cp.circ_lib( $scope.lib );
256                         cp.call_number( $scope.callNumber );
257                         $scope.copies.push( cp );
258                     }
259
260                     var how_many = $scope.copies.length - $scope.copy_count;
261                     if (how_many > 0) {
262                         $scope.copies.splice($scope.copy_count,how_many);
263                         $scope.callNumber.copies($scope.copies);
264                     }
265                 }
266
267             }
268         ]
269
270     }
271 })
272
273 .directive("egVolEdit", function () {
274     return {
275         restrict: 'E',
276         replace: true,
277         template:
278             '<div class="row">'+
279                 '<div class="col-xs-1"><eg-org-selector selected="owning_lib" disableTest="cant_have_vols"></eg-org-selector></div>'+
280                 '<div class="col-xs-1"><input type="number" min="{{orig_cn_count}}" ng-model="cn_count" ng-change="changeCNCount()"/></div>'+
281                 '<div class="col-xs-10">'+
282                     '<div class="container-fluid">'+
283                         '<eg-vol-row ng-repeat="(cn,copies) in struct track by cn" copies="copies"></eg-vol-row>'+
284                     '</div>'+
285                 '</div>'+
286             '</div>',
287
288         scope: { struct: "=", lib: "@", record: "@" },
289         controller : ['$scope','itemSvc','egCore',
290             function ( $scope , itemSvc , egCore ) {
291                 $scope.new_cn_id = 0;
292                 $scope.first_cn = Object.keys($scope.struct)[0];
293                 $scope.full_cn = $scope.struct[$scope.first_cn][0].call_number();
294
295                 $scope.cn_count = Object.keys($scope.struct).length;
296                 $scope.orig_cn_count = $scope.cn_count;
297
298                 $scope.owning_lib = egCore.org.get($scope.lib);
299
300                 $scope.cant_have_vols = function (id) { return !egCore.org.CanHaveVolumes(id); };
301
302                 $scope.$watch('cn_count', function (n) {
303                     var o = Object.keys($scope.struct).length;
304                     if (n > o) { // adding
305                         for (var i = o; o < n; o++) {
306                             var cn = new egCore.idl.acn();
307                             cn.id( --$scope.new_cn_id );
308                             cn.isnew( true );
309                             cn.owning_lib( $scope.owning_lib );
310                             cn.record( $scope.full_cn.record() );
311
312                             var cp = new egCore.idl.acp();
313                             cp.id( --$scope.new_cp_id );
314                             cp.isnew( true );
315                             cp.circ_lib( $scope.owning_lib );
316                             cp.call_number( cn );
317
318                             $scope.struct[cn.id()] = [cp];
319                         }
320                     } else if (n < o) { // removing
321                         var how_many = o - n;
322                         var list = Object
323                                 .keys($scope.struct)
324                                 .sort(function(a, b){return a-b})
325                                 .reverse();
326                         for (var i = how_many; i > 0; i--) {
327                             delete $scope.struct[list[i]];
328                         }
329                     }
330                 });
331             }
332         ]
333
334     }
335 })
336
337 /**
338  * Edit controller!
339  */
340 .controller('EditCtrl', 
341        ['$scope','$q','$routeParams','$location','$timeout','egCore','egNet','egGridDataProvider','itemSvc',
342 function($scope , $q , $routeParams , $location , $timeout , egCore , egNet , egGridDataProvider , itemSvc) {
343
344     $timeout(function(){
345
346     var dataKey = $routeParams.dataKey;
347     console.debug('dataKey: ' + dataKey);
348
349     if (dataKey && dataKey.length > 0) {
350         $scope.tab = 'edit';
351         $scope.summaryRecord = null;
352         $scope.record_id = null;
353         $scope.data = {};
354
355         $scope.gridDataProvider = egGridDataProvider.instance({
356             get : function(offset, count) {
357                 //return provider.arrayNotifier(itemSvc.copies, offset, count);
358                 return this.arrayNotifier(itemSvc.copies, offset, count);
359             }
360         });
361
362         egNet.request(
363             'open-ils.actor',
364             'open-ils.actor.anon_cache.get_value',
365             dataKey, 'edit-these-copies'
366         ).then(function (data) {
367             $scope.record_id = data.record_id;
368             return itemSvc.fetchIds(data.copies);
369         }).then( function() {
370             $scope.data = itemSvc.tree;
371             $scope.gridDataProvider.refresh();
372         });
373     }
374
375     });
376
377 }])
378
379