1 angular.module('egAcqAdmin',
2 ['ngRoute', 'ui.bootstrap', 'egCoreMod','egUiMod'])
4 .config(['$routeProvider','$locationProvider','$compileProvider',
5 function($routeProvider , $locationProvider , $compileProvider) {
7 $locationProvider.html5Mode(true);
8 $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|blob):/);
9 var resolver = {delay : function(egStartup) {return egStartup.go()}};
11 $routeProvider.when('/admin/acq/edi_attr_set', {
12 templateUrl: './admin/acq/t_edi_attr_set',
13 controller: 'EDIAttrSet',
18 '<eg-embed-frame allow-escape="true" min-height="min_height" url="acq_admin_url" handlers="funcs"></eg-embed-frame>';
20 $routeProvider.when('/admin/acq/:noun/:verb/:extra?', {
21 template: eframe_template,
22 controller: 'EmbedAcqCtl',
27 $routeProvider.otherwise({
28 templateUrl : './admin/acq/t_splash',
33 .controller('EmbedAcqCtl',
34 ['$scope','$routeParams','$location','egCore',
35 function($scope , $routeParams , $location , egCore) {
38 ses : egCore.auth.token(),
41 var acq_path = '/eg/';
43 if ($routeParams.noun == 'conify') {
44 acq_path += 'conify/global/acq/' + $routeParams.verb
45 + (typeof $routeParams.extra != 'undefined'
46 ? '/' + $routeParams.extra
51 + $routeParams.noun + '/' + $routeParams.verb
52 + (typeof $routeParams.extra != 'undefined'
53 ? '/' + $routeParams.extra
58 $scope.min_height = 2000; // give lots of space to start
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);
65 console.log('Loading Admin Acq URL: ' + $scope.acq_admin_url);
69 .controller('EDIAttrSet',
70 ['$scope','$q','egCore','ngToast','egConfirmDialog',
71 function($scope , $q , egCore , ngToast , egConfirmDialog) {
73 $scope.cur_attr_set = null;
75 // fetch all the data needed to render the page.
76 function load_data() {
78 return egCore.pcrud.retrieveAll('aea', {},
79 {atomic : true, authoritative : true})
83 return egCore.pcrud.retrieveAll('aeas',
84 {flesh : 1, flesh_fields : {'aeas' : ['attr_maps']}},
85 {atomic : true, authoritative : true}
89 ).then(function(sets) {
90 $scope.attr_sets = sets.sort(function(a, b) {
91 return a.label() < b.label() ? -1 : 1;
94 // create a simple true/false attr_set => attr mapping
96 angular.forEach(sets, function(set) {
98 angular.forEach(set.attr_maps(), function(map) {
99 set._local_map[map.attr()] = true;
102 if ($scope.cur_attr_set && set.id()
103 == $scope.cur_attr_set.id()) {
108 $scope.select_set(select_me || $scope.attr_sets[0]);
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());
121 if (new_sets.length == 0) return $q.when();
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(
128 $scope.attr_sets = $scope.attr_sets.filter(
129 function(set) { return (set.label() && !set.isnew()) });
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);
141 function modify_maps() {
142 var update_maps = [];
144 angular.forEach($scope.attr_sets, function(set) {
145 console.debug('inspecting attr set ' + set.label());
147 if (!set.label()) return; // skip (new) unnamed sets
149 // find maps that need deleting
150 angular.forEach(set.attr_maps(), function(oldmap) {
151 if (!set._local_map[oldmap.attr()]) {
152 console.debug('\tdeleting map for ' + oldmap.attr());
153 oldmap.isdeleted(true);
154 update_maps.push(oldmap);
158 // find maps that need creating
159 angular.forEach(set._local_map, function(value, key) {
162 var existing = set.attr_maps().filter(
163 function(emap) { return emap.attr() == key })[0];
165 if (existing) return;
167 console.debug('\tcreating map for ' + key);
169 var newmap = new egCore.idl.aeasm();
172 newmap.attr_set(set.id());
173 update_maps.push(newmap);
177 return egCore.pcrud.apply(update_maps);
180 // mark the currently selected attr set as the main display set.
181 $scope.select_set = function(set) {
182 $scope.cur_attr_set_uses = 0; // how many edi accounts use this set
184 $scope.cur_attr_set = set;
186 egCore.pcrud.search('acqedi', {attr_set : set.id()}, {},
187 {idlist : true, atomic : true}
188 ).then(function(accts) {
189 $scope.cur_attr_set_uses = accts.length;
190 $scope.cur_attr_set = set;
195 $scope.new_set = function() {
196 var set = new egCore.idl.aeas();
200 $scope.select_set(set);
201 $scope.attr_sets.push(set);
204 $scope.apply = function() {
205 $scope.save_in_progress = true;
210 ngToast.create(egCore.strings.ATTR_SET_SUCCESS)
213 ngToast.warning(egCore.strings.ATTR_SET_ERROR);
218 function() { $scope.save_in_progress = false; }
222 // Delete the currently selected attr set.
223 // Attr set maps will cascade delete.
224 $scope.remove = function() {
225 egConfirmDialog.open(
226 egCore.strings.ATTR_SET_DELETE_CONFIRM,
227 $scope.cur_attr_set.label()
228 ).result.then(function() {
229 $scope.save_in_progress = true;
230 ( // remove from server if necessary
231 $scope.cur_attr_set.isnew() ?
233 egCore.pcrud.remove($scope.cur_attr_set)
235 // remove from the local att_sets list
237 ngToast.create(egCore.strings.ATTR_SET_SUCCESS);
238 $scope.attr_sets = $scope.attr_sets.filter(
240 return set.id() != $scope.cur_attr_set.id()
243 $scope.cur_attr_set = $scope.attr_sets[0];
245 function() { ngToast.warning(egCore.strings.ATTR_SET_ERROR) }
248 function() { $scope.save_in_progress = false; }
253 $scope.clone_set = function(source_set) {
254 var set = new egCore.idl.aeas();
259 // Copy attr info from cloned attr set. No need to create the
260 // maps now, just indicate in the local mapping that attr maps
262 angular.forEach(source_set.attr_maps(), function(map) {
263 set._local_map[map.attr()] = true;
266 $scope.select_set(set);
267 $scope.attr_sets.push(set);