]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/serials/directives/prediction_manager.js
LP#1689325 - require most modals have explicit 'exit' or 'cancel' action inside the...
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / serials / directives / prediction_manager.js
1 angular.module('egSerialsAppDep')
2
3 .directive('egPredictionManager', function() {
4     return {
5         transclude: true,
6         restrict:   'E',
7         scope: {
8             bibId  : '=',
9             ssubId : '='
10         },
11         templateUrl: './serials/t_prediction_manager',
12         controller:
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) {
17
18     $scope.has_pattern_to_import = false;
19     $scope.forms = [];
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;
25             }
26         });
27     });
28
29     function reload(ssubId) {
30         if (!ssubId) return;
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;
35             egCore.net.request(
36                 'open-ils.serial',
37                 'open-ils.serial.caption_and_pattern.safe_delete.dry_run',
38                 egCore.auth.token(),
39                 pred.id
40             ).then(function(result) {
41                 if (result == 1) pred._can_edit_or_delete = true;
42             });
43         });
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;
49             }
50         });
51     }
52
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);
62             });
63         });
64     }
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);
73             });
74         });
75     }
76     $scope.deleteScap = function(pred) {
77         var scap = egCore.idl.fromTypedHash(pred);
78         egConfirmDialog.open(
79             egCore.strings.CONFIRM_DELETE_SCAP,
80             egCore.strings.CONFIRM_DELETE_SCAP_MESSAGE,
81             {}
82         ).result.then(function () {
83             egCore.net.request(
84                 'open-ils.serial',
85                 'open-ils.serial.caption_and_pattern.safe_delete',
86                 egCore.auth.token(),
87                 scap.id()
88             ).then(function(resp){
89                 var evt = egCore.evt.parse(resp);
90                 if (evt) {
91                     ngToast.danger(egCore.strings.SERIALS_SCAP_FAIL_DELETE + ' : ' + evt.desc);
92                 } else {
93                     ngToast.success(egCore.strings.SERIALS_SCAP_SUCCESS_DELETE);
94                 }
95  
96                 $scope.predictions = [];
97                 egSerialsCoreSvc.fetch($scope.bibId).then(function() {
98                     reload($scope.ssubId);
99                 });
100             })
101         });
102     }
103     $scope.cancelNewScap = function() {
104         $scope.new_prediction = null;
105     }
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;
113     }
114
115     $scope.importScapFromBibRecord = function() {
116         $uibModal.open({
117             templateUrl: './serials/t_select_pattern_dialog',
118             backdrop: 'static',
119             size: 'md',
120             controller:
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() }
126             }]
127         }).result.then(function (potentials) {
128             var marc = [];
129             angular.forEach(potentials, function(pot) {
130                 if (pot.selected) {
131                     marc.push(pot.marc);
132                 }
133             });
134             if (marc.length == 0) return;
135             egCore.net.request(
136                 'open-ils.serial',
137                 'open-ils.serial.caption_and_pattern.create_from_records',
138                 egCore.auth.token(),
139                 $scope.ssubId,
140                 marc
141             ).then(function() {
142                 egSerialsCoreSvc.fetch($scope.bibId).then(function() {
143                     reload($scope.ssubId);
144                 });
145             });
146         });
147     }
148     
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;
158                 break;
159             }
160         }
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();
167         });
168     }
169
170     $scope.openPatternEditorDialog = function(pred, form, viewOnly) {
171         $uibModal.open({
172             templateUrl: './serials/t_pattern_editor_dialog',
173             backdrop: 'static',
174             size: 'lg',
175             windowClass: 'eg-wide-modal',
176             backdrop: 'static',
177             controller:
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() }
184             }]
185         }).result.then(function (patternCode) {
186             if (pred.pattern_code !== patternCode) {
187                 pred.pattern_code = patternCode;
188                 form.$setDirty();        
189             }
190         });
191     }
192
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/' +
197                                 $scope.ssubId);
198             });
199         });
200     }
201
202 }]
203     }
204 })