]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/admin/acq/app.js
LP#1373690 EDI attribute sets admin UI
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / admin / acq / app.js
1 angular.module('egAcqAdmin',
2     ['ngRoute', 'ui.bootstrap', 'egCoreMod','egUiMod'])
3
4 .config(['$routeProvider','$locationProvider','$compileProvider', 
5  function($routeProvider , $locationProvider , $compileProvider) {
6
7     $locationProvider.html5Mode(true);
8     $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|blob):/); 
9     var resolver = {delay : function(egStartup) {return egStartup.go()}};
10
11     $routeProvider.when('/admin/acq/edi_attr_set', {
12         templateUrl: './admin/acq/t_edi_attr_set',
13         controller: 'EDIAttrSet',
14         resolve : resolver
15     });
16
17     var eframe_template = 
18         '<eg-embed-frame allow-escape="true" min-height="min_height" url="acq_admin_url" handlers="funcs"></eg-embed-frame>';
19
20     $routeProvider.when('/admin/acq/:noun/:verb/:extra?', {
21         template: eframe_template,
22         controller: 'EmbedAcqCtl',
23         resolve : resolver
24     });
25
26     // default page 
27     $routeProvider.otherwise({
28         templateUrl : './admin/acq/t_splash',
29         resolve : resolver
30     });
31 }])
32
33 .controller('EmbedAcqCtl',
34        ['$scope','$routeParams','$location','egCore',
35 function($scope , $routeParams , $location , egCore) {
36
37     $scope.funcs = {
38         ses : egCore.auth.token(),
39     }
40
41     var acq_path = '/eg/';
42
43     if ($routeParams.noun == 'conify') {
44         acq_path += 'conify/global/acq/' + $routeParams.verb
45             + (typeof $routeParams.extra != 'undefined'
46                 ? '/' + $routeParams.extra
47                 : '')
48             + location.search;
49     } else {
50         acq_path += 'acq/'
51             + $routeParams.noun + '/' + $routeParams.verb
52             + (typeof $routeParams.extra != 'undefined'
53                 ? '/' + $routeParams.extra
54                 : '')
55             + location.search;
56     }
57
58     $scope.min_height = 2000; // give lots of space to start
59
60     // embed URL must include protocol/domain or it will be loaded via
61     // push-state, resulting in an infinitely nested pages.
62     $scope.acq_admin_url =
63         $location.absUrl().replace(/\/eg\/staff.*/, acq_path);
64
65     console.log('Loading Admin Acq URL: ' + $scope.acq_admin_url);
66
67 }])
68
69 .controller('EDIAttrSet',
70        ['$scope','$q','egCore','ngToast','egConfirmDialog',
71 function($scope , $q , egCore , ngToast , egConfirmDialog) {
72     
73     $scope.cur_attr_set = null;
74
75     // fetch all the data needed to render the page.
76     function load_data() {
77
78         return egCore.pcrud.retrieveAll('aea', {}, 
79             {atomic : true, authoritative : true})
80         .then(
81             function(attrs) { 
82                 $scope.attrs = attrs 
83                 return egCore.pcrud.retrieveAll('aeas', 
84                     {flesh : 1, flesh_fields : {'aeas' : ['attr_maps']}}, 
85                     {atomic : true, authoritative : true}
86                 )
87             }
88
89         ).then(function(sets) { 
90             $scope.attr_sets = sets.sort(function(a, b) {
91                 return a.label() < b.label() ? -1 : 1;
92             });
93
94             // create a simple true/false attr_set => attr mapping
95             var select_me;
96             angular.forEach(sets, function(set) {
97                 set._local_map = {};
98                 angular.forEach(set.attr_maps(), function(map) {
99                     set._local_map[map.attr()] = true;
100                 })
101
102                 if ($scope.cur_attr_set && set.id() 
103                         == $scope.cur_attr_set.id()) {
104                     select_me = set;
105                 }
106             });
107
108             $scope.select_set(select_me || $scope.attr_sets[0]);
109         });
110     }
111
112     function create_sets() {
113         var new_sets = $scope.attr_sets.filter(function(set) { 
114             if (set.isnew() && set.label()) {
115                 console.debug('creating new set: ' + set.label());
116                 return true;
117             } 
118             return false;
119         });
120
121         if (new_sets.length == 0) return $q.when();
122
123         // create the new attrs sets and collect the newly generated 
124         // ID in the local data store.
125         return egCore.pcrud.apply(new_sets).then(
126             null,
127             function() { 
128                 $scope.attr_sets = $scope.attr_sets.filter(
129                     function(set) { return (set.label() && !set.isnew()) });
130                 return $q.reject();
131             },
132             function(new_set) { 
133                 var old_set = new_sets.filter(function(s) {
134                     return (s.isnew() && s.label() == new_set.label()) })[0];
135                 old_set.id(new_set.id());
136                 old_set.isnew(false);
137             }
138         );
139     }
140
141     function modify_maps() {
142         var update_maps = [];
143
144         angular.forEach($scope.attr_sets, function(set) {
145             console.debug('inspecting attr set ' + set.label());
146
147             // find maps that need deleting
148             angular.forEach(set.attr_maps(), function(oldmap) {
149                 if (!set._local_map[oldmap.attr()]) {
150                     console.debug('\tdeleting map for ' + oldmap.attr());
151                     oldmap.isdeleted(true);
152                     update_maps.push(oldmap);
153                 }
154             });
155
156             // find maps that need creating
157             angular.forEach(set._local_map, function(value, key) {
158                 if (!value) return;
159
160                 var existing = set.attr_maps().filter(
161                     function(emap) { return emap.attr() == key })[0];
162
163                 if (existing) return;
164
165                 console.debug('\tcreating map for ' + key);
166
167                 var newmap = new egCore.idl.aeasm();
168                 newmap.isnew(true);
169                 newmap.attr(key);
170                 newmap.attr_set(set.id());
171                 update_maps.push(newmap);
172             });
173         });
174
175         return egCore.pcrud.apply(update_maps);
176     }
177
178     // mark the currently selected attr set as the main display set.
179     $scope.select_set = function(set) {
180         $scope.cur_attr_set_uses = 0; // how many edi accounts use this set
181         if (set.isnew()) {
182             $scope.cur_attr_set = set;
183         } else {
184             egCore.pcrud.search('acqedi', {attr_set : set.id()}, {}, 
185                 {idlist : true, atomic : true}
186             ).then(function(accts) {
187                 $scope.cur_attr_set_uses = accts.length;
188                 $scope.cur_attr_set = set;
189             });
190         }
191     }
192
193     $scope.new_set = function() {
194         var set = new egCore.idl.aeas();
195         set.isnew(true);
196         set.attr_maps([]);
197         set._local_map = {};
198         $scope.select_set(set);
199         $scope.attr_sets.push(set);
200     }
201
202     $scope.apply = function() {
203         $scope.save_in_progress = true;
204         create_sets()
205             .then(modify_maps)
206             .then(
207                 function() { 
208                     ngToast.create(egCore.strings.ATTR_SET_SUCCESS) 
209                 },
210                 function() { 
211                     ngToast.warning(egCore.strings.ATTR_SET_ERROR);
212                     return $q.reject();
213                 })
214             .then(load_data)
215             .finally(
216                 function() { $scope.save_in_progress = false; }
217             );
218     }
219
220     // Delete the currently selected attr set.
221     // Attr set maps will cascade delete.
222     $scope.remove = function() {
223         egConfirmDialog.open(
224             egCore.strings.ATTR_SET_DELETE_CONFIRM, 
225             $scope.cur_attr_set.label()
226         ).result.then(function() {
227             $scope.save_in_progress = true;
228             (   // remove from server if necessary
229                 $scope.cur_attr_set.isnew() ?
230                 $q.when() :
231                 egCore.pcrud.remove($scope.cur_attr_set)
232             ).then(
233                 // remove from the local att_sets list
234                 function() {
235                     ngToast.create(egCore.strings.ATTR_SET_SUCCESS);
236                     $scope.attr_sets = $scope.attr_sets.filter(
237                         function(set) {
238                             return set.id() != $scope.cur_attr_set.id() 
239                         }
240                     );
241                     $scope.cur_attr_set = $scope.attr_sets[0];
242                 },
243                 function() { ngToast.warning(egCore.strings.ATTR_SET_ERROR) }
244
245             ).finally(
246                 function() { $scope.save_in_progress = false; }
247             );
248         });
249     }
250
251     load_data();
252 }])
253
254