1 angular.module('egSerialsAppDep')
3 .directive('egPredictionManager', function() {
11 templateUrl: './serials/t_prediction_manager',
13 ['$scope','$q','egSerialsCoreSvc','egCore','egGridDataProvider',
14 '$uibModal','$timeout','$location','egConfirmDialog','ngToast',
15 function($scope , $q , egSerialsCoreSvc , egCore , egGridDataProvider ,
16 $uibModal , $timeout , $location , egConfirmDialog , ngToast) {
18 $scope.has_pattern_to_import = false;
20 egSerialsCoreSvc.fetch($scope.bibId).then(function() {
21 reload($scope.ssubId);
22 egSerialsCoreSvc.fetch_patterns_from_bibs_mfhds($scope.bibId).then(function() {
23 if (egSerialsCoreSvc.potentialPatternList.length > 0) {
24 $scope.has_pattern_to_import = true;
29 function reload(ssubId) {
31 var ssub = egSerialsCoreSvc.get_ssub(ssubId);
32 $scope.predictions = egCore.idl.toTypedHash(ssub.scaps());
33 angular.forEach($scope.predictions, function(pred) {
34 pred._can_edit_or_delete = false;
37 'open-ils.serial.caption_and_pattern.safe_delete.dry_run',
40 ).then(function(result) {
41 if (result == 1) pred._can_edit_or_delete = true;
44 egSerialsCoreSvc.fetch_spt().then(function() {
45 $scope.pattern_templates = egCore.idl.toTypedHash(egSerialsCoreSvc.sptList);
46 $scope.active_pattern_template = { id : null };
47 if ($scope.pattern_templates.length > 0) {
48 $scope.active_pattern_template.id = $scope.pattern_templates[0].id;
53 $scope.createScap = function(pred) {
54 var scap = egCore.idl.fromTypedHash(pred);
55 egCore.pcrud.create(scap).then(function() {
56 // completely reset the model in order to reset the
57 // forms; causes a blink, alas
58 $scope.predictions = [];
59 $scope.new_prediction = null;
60 egSerialsCoreSvc.fetch($scope.bibId).then(function() {
61 reload($scope.ssubId);
65 $scope.updateScap = function(pred) {
66 var scap = egCore.idl.fromTypedHash(pred);
67 egCore.pcrud.update(scap).then(function() {
68 // completely reset the model in order to reset the
69 // forms; causes a blink, alas
70 $scope.predictions = [];
71 egSerialsCoreSvc.fetch($scope.bibId).then(function() {
72 reload($scope.ssubId);
76 $scope.deleteScap = function(pred) {
77 var scap = egCore.idl.fromTypedHash(pred);
79 egCore.strings.CONFIRM_DELETE_SCAP,
80 egCore.strings.CONFIRM_DELETE_SCAP_MESSAGE,
82 ).result.then(function () {
85 'open-ils.serial.caption_and_pattern.safe_delete',
88 ).then(function(resp){
89 var evt = egCore.evt.parse(resp);
91 ngToast.danger(egCore.strings.SERIALS_SCAP_FAIL_DELETE + ' : ' + evt.desc);
93 ngToast.success(egCore.strings.SERIALS_SCAP_SUCCESS_DELETE);
96 $scope.predictions = [];
97 egSerialsCoreSvc.fetch($scope.bibId).then(function() {
98 reload($scope.ssubId);
103 $scope.cancelNewScap = function() {
104 $scope.new_prediction = null;
106 $scope.startNewScap = function() {
107 $scope.new_prediction = egCore.idl.toTypedHash(new egCore.idl.scap());
108 $scope.new_prediction.type = 'basic';
109 $scope.new_prediction.active = true;
110 $scope.new_prediction.create_date = new Date();
111 $scope.new_prediction.subscription = $scope.ssubId;
112 $scope.new_prediction.pattern_code = null;
115 $scope.importScapFromBibRecord = function() {
117 templateUrl: './serials/t_select_pattern_dialog',
121 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
122 $scope.focusMe = true;
123 $scope.potentials = egSerialsCoreSvc.potentialPatternList.slice();
124 $scope.ok = function(patternCode) { $uibModalInstance.close($scope.potentials) }
125 $scope.cancel = function () { $uibModalInstance.dismiss() }
127 }).result.then(function (potentials) {
129 angular.forEach(potentials, function(pot) {
134 if (marc.length == 0) return;
137 'open-ils.serial.caption_and_pattern.create_from_records',
142 egSerialsCoreSvc.fetch($scope.bibId).then(function() {
143 reload($scope.ssubId);
149 $scope.importScapFromSpt = function() {
150 $scope.new_prediction = egCore.idl.toTypedHash(new egCore.idl.scap());
151 $scope.new_prediction.type = 'basic';
152 $scope.new_prediction.active = true;
153 $scope.new_prediction.create_date = new Date();
154 $scope.new_prediction.subscription = $scope.ssubId;
155 for (var i = 0; i < $scope.pattern_templates.length; i++) {
156 if ($scope.pattern_templates[i].id == $scope.active_pattern_template.id) {
157 $scope.new_prediction.pattern_code = $scope.pattern_templates[i].pattern_code;
161 // Mark form dirty because, when it's created from a template,
162 // it can be immediately saved if the user so chooses. The
163 // $watch() allows this to happen after the form is bound
164 // is bound to the scope.
165 $scope.$watch('forms.newpredform', function(form) {
166 if (form) form.$setDirty();
170 $scope.openPatternEditorDialog = function(pred, form, viewOnly) {
172 templateUrl: './serials/t_pattern_editor_dialog',
175 windowClass: 'eg-wide-modal',
178 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
179 $scope.viewOnly = viewOnly;
180 $scope.focusMe = true;
181 $scope.patternCode = pred.pattern_code;
182 $scope.ok = function(patternCode) { $uibModalInstance.close(patternCode) }
183 $scope.cancel = function () { $uibModalInstance.dismiss() }
185 }).result.then(function (patternCode) {
186 if (pred.pattern_code !== patternCode) {
187 pred.pattern_code = patternCode;
193 $scope.add_issuances = function() {
194 return egSerialsCoreSvc.fetchItemsForSub($scope.ssubId).then(function() {
195 egSerialsCoreSvc.add_issuances($scope.ssubId).then(function() {
196 $location.path('/serials/' + $scope.bibId + '/issues/' +