5 angular.module('egVolCopy',
6 ['ngRoute', 'ui.bootstrap', 'egCoreMod', 'egUiMod', 'egGridMod'])
8 .filter('boolText', function(){
14 .config(function($routeProvider, $locationProvider, $compileProvider) {
15 $locationProvider.html5Mode(true);
16 $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|blob):/); // grid export
19 delay : ['egStartup', function(egStartup) { return egStartup.go(); }]
22 $routeProvider.when('/cat/volcopy/:dataKey', {
23 templateUrl: './cat/volcopy/t_view',
24 controller: 'EditCtrl',
32 function(egCore , $q) {
35 tree : {}, // holds lib->cn->copy hash stack
36 copies : [] // raw copy list
39 // returns a promise resolved with the list of circ mods
40 service.get_classifications = function() {
42 return $q.when(egCore.env.acnc.list);
44 return egCore.pcrud.retrieveAll('acnc', null, {atomic : true})
45 .then(function(list) {
46 egCore.env.absorbList(list, 'acnc');
51 service.get_prefixes = function(org) {
53 return $q.when(egCore.env.acnp.list);
55 return egCore.pcrud.search('acnp',
56 {owning_lib : egCore.org.fullPath(org, true)},
58 ).then(function(list) {
59 egCore.env.absorbList(list, 'acnp');
65 service.get_locations = function(orgs) {
66 return egCore.pcrud.search('acpl',
72 service.get_suffixes = function(org) {
74 return $q.when(egCore.env.acns.list);
76 return egCore.pcrud.search('acns',
77 {owning_lib : egCore.org.fullPath(org, true)},
79 ).then(function(list) {
80 egCore.env.absorbList(list, 'acns');
86 service.get_statuses = function() {
88 return $q.when(egCore.env.ccs.list);
90 return egCore.pcrud.retrieveAll('ccs', {}, {atomic : true}).then(
92 egCore.env.absorbList(list, 'ccs');
99 service.get_circ_mods = function() {
101 return $q.when(egCore.env.ccm.list);
103 return egCore.pcrud.retrieveAll('ccm', {}, {atomic : true}).then(
105 egCore.env.absorbList(list, 'ccm');
112 service.get_circ_types = function() {
114 return $q.when(egCore.env.citm.list);
116 return egCore.pcrud.retrieveAll('citm', {}, {atomic : true}).then(
118 egCore.env.absorbList(list, 'citm');
125 service.get_age_protects = function() {
126 if (egCore.env.crahp)
127 return $q.when(egCore.env.crahp.list);
129 return egCore.pcrud.retrieveAll('crahp', {}, {atomic : true}).then(
131 egCore.env.absorbList(list, 'crahp');
138 service.bmp_parts = {};
139 service.get_parts = function(rec) {
140 if (service.bmp_parts[rec])
141 return $q.when(service.bmp_parts[rec]);
143 return egCore.pcrud.search('bmp',
145 null, {atomic : true}
146 ).then(function(list) {
147 service.bmp_parts[rec] = list;
156 acp : ['call_number','parts'],
157 acn : ['label_class','prefix','suffix']
161 service.addCopy = function (cp) {
163 if (!cp.parts()) cp.parts([]); // just in case...
165 var lib = cp.call_number().owning_lib();
166 var cn = cp.call_number().id();
168 if (!service.tree[lib]) service.tree[lib] = {};
169 if (!service.tree[lib][cn]) service.tree[lib][cn] = [];
171 service.tree[lib][cn].push(cp);
172 service.copies.push(cp);
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,
180 service.addCopy(copy);
188 .directive("egVolCopyEdit", function () {
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>'+
199 scope: { copy: "=", callNumber: "=" },
200 controller : ['$scope','itemSvc',
201 function ( $scope , itemSvc ) {
202 $scope.new_part_id = 0;
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
210 if (p.length > 0) { // preexisting part
212 } else { // create one...
213 var part = new egCore.idl.bmp();
214 part.id( --$scope.new_part_id );
216 part.label( $scope.part );
217 part.record( $scope.callNumber.owning_lib() );
218 $scope.copy.parts([part]);
219 $scope.copy.ischanged(1);
223 $scope.barcode = $scope.copy.barcode();
224 $scope.copy_number = $scope.copy.copy_number();
226 if ($scope.copy.parts()) {
227 $scope.part = $scope.copy.parts()[0];
228 if ($scope.part) $scope.part = $scope.part.label();
232 $scope.part_list = [];
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()) });
245 .directive("egVolRow", function () {
252 '<div class="col-xs-1">'+
253 '<select ng-model="classification" ng-options="cl.name() for cl in classification_list track by idTracker(cl)"/>'+
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)"/>'+
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)"/>'+
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>'+
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();
276 $scope.idTracker = function (x) { if (x) return x.id() };
278 $scope.suffix_list = [];
279 itemSvc.get_suffixes($scope.callNumber.owning_lib()).then(function(list){
280 $scope.suffix_list = list;
282 $scope.updateSuffix = function () { $scope.callNumber.suffix($scope.suffix); $scope.callNumber.ischanged(1); };
284 $scope.prefix_list = [];
285 itemSvc.get_prefixes($scope.callNumber.owning_lib()).then(function(list){
286 $scope.prefix_list = list;
288 $scope.updatePrefix = function () { $scope.callNumber.prefix($scope.prefix); $scope.callNumber.ischanged(1); };
290 $scope.classification_list = [];
291 itemSvc.get_classifications().then(function(list){
292 $scope.classification_list = list;
294 $scope.updateClassification = function () { $scope.callNumber.label_class($scope.classification); $scope.callNumber.ischanged(1); };
296 $scope.classification = $scope.callNumber.label_class();
297 $scope.prefix = $scope.callNumber.prefix();
298 $scope.suffix = $scope.callNumber.suffix();
300 $scope.label = $scope.callNumber.label();
301 $scope.updateLabel = function () { $scope.callNumber.label($scope.label); $scope.callNumber.ischanged(1); };
303 $scope.copy_count = $scope.copies.length;
304 $scope.orig_copy_count = $scope.copy_count;
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 );
311 cp.circ_lib( $scope.lib );
312 cp.call_number( $scope.callNumber );
313 $scope.copies.push( cp );
314 $scope.allcopies.push( cp );
317 var how_many = $scope.copies.length - $scope.copy_count;
319 var dead = $scope.copies.splice($scope.copy_count,how_many);
320 $scope.callNumber.copies($scope.copies);
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);
337 .directive("egVolEdit", function () {
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>'+
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();
359 $scope.cn_count = Object.keys($scope.struct).length;
360 $scope.orig_cn_count = $scope.cn_count;
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() );
369 $scope.cant_have_vols = function (id) { return !egCore.org.CanHaveVolumes(id); };
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 );
378 cn.owning_lib( $scope.owning_lib.id() );
379 cn.record( $scope.full_cn.record() );
381 var cp = new egCore.idl.acp();
382 cp.id( --$scope.new_cp_id );
384 cp.circ_lib( $scope.owning_lib.id() );
385 cp.call_number( cn );
387 $scope.struct[cn.id()] = [cp];
388 $scope.allcopies.push(cp);
390 } else if (n < o) { // removing
391 var how_many = o - n;
394 .sort(function(a, b){return a-b})
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);
403 delete $scope.struct[list[i]];
416 .controller('EditCtrl',
417 ['$scope','$q','$routeParams','$location','$timeout','egCore','egNet','egGridDataProvider','itemSvc',
418 function($scope , $q , $routeParams , $location , $timeout , egCore , egNet , egGridDataProvider , itemSvc) {
420 $scope.show_vols = true;
421 $scope.show_copies = true;
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); };
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];
431 $scope.locationById = function (id) {
432 return $scope.location_cache[''+id];
435 $scope.workingToComplete = function () {
436 angular.forEach( $scope.workingGridControls.selectedItems(), function (c) {
437 angular.forEach( itemSvc.copies, function (w, i) {
439 $scope.completed_copies = $scope.completed_copies.concat(itemSvc.copies.splice(i,1));
444 $scope.completeToWorking = function () {
445 angular.forEach( $scope.completedGridControls.selectedItems(), function (c) {
446 angular.forEach( $scope.completed_copies, function (w, i) {
448 itemSvc.copies = itemSvc.copies.concat($scope.completed_copies.splice(i,1));
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();
461 if ($scope.workingGridControls && $scope.workingGridControls.selectedItems) {
463 $scope.workingGridControls.selectedItems(),
464 function (cp) { cp[field](newval); cp.ischanged(1); }
472 var dataKey = $routeParams.dataKey;
473 console.debug('dataKey: ' + dataKey);
475 if (dataKey && dataKey.length > 0) {
478 $scope.copytab = 'working';
480 $scope.summaryRecord = null;
481 $scope.record_id = null;
483 $scope.completed_copies = [];
484 $scope.location_orgs = [];
485 $scope.location_cache = {};
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);
494 $scope.completedGridControls = {};
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);
503 $scope.workingGridControls = {};
507 'open-ils.actor.anon_cache.get_value',
508 dataKey, 'edit-these-copies'
509 ).then(function (data) {
511 if (data.hide_vols) $scope.show_vols = false;
512 if (data.hide_copies) $scope.show_copies = false;
514 $scope.record_id = data.record_id;
516 if (data.copies && data.copies.length)
517 return itemSvc.fetchIds(data.copies);
519 if (data.raw && data.raw.length) {
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?
531 function (cp) { itemSvc.addCopy(cp) }
534 return itemSvc.copies;
537 }).then( function() {
538 $scope.data = itemSvc;
539 $scope.workingGridDataProvider.refresh();
542 $scope.$watch('data.copies.length', function () {
543 if ($scope.data.copies) {
544 var base_orgs = $scope.data.copies.map(function(cp){
546 }).filter(function(e,i,a){
547 return a.lastIndexOf(e) === i;
551 angular.forEach(base_orgs, function(o) {
552 all_orgs = all_orgs.concat( egCore.org.fullPath(o, true) );
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});
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;
566 $scope.location_list = list;
572 $scope.workingGridDataProvider.refresh();
575 $scope.$watch('completed_copies.length', function () {
576 $scope.completedGridDataProvider.refresh();
579 $scope.location_list = [];
580 itemSvc.get_locations().then(function(list){
581 $scope.location_list = list;
583 createSimpleUpdateWatcher('location');
585 $scope.status_list = [];
586 itemSvc.get_statuses().then(function(list){
587 $scope.status_list = list;
589 createSimpleUpdateWatcher('status');
591 $scope.circ_mod_list = [];
592 itemSvc.get_circ_mods().then(function(list){
593 $scope.circ_mod_list = list;
595 createSimpleUpdateWatcher('circ_modifier');
597 $scope.circ_type_list = [];
598 itemSvc.get_circ_types().then(function(list){
599 $scope.circ_type_list = list;
601 createSimpleUpdateWatcher('circ_as_type');
603 $scope.age_protect_list = [];
604 itemSvc.get_age_protects().then(function(list){
605 $scope.age_protect_list = list;
607 createSimpleUpdateWatcher('age_protect');
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');