1 angular.module('egAdminPermGrpTreeApp',
2 ['ngRoute','ui.bootstrap','egCoreMod','egUiMod','treeControl'])
4 .config(function($routeProvider, $locationProvider, $compileProvider) {
5 $locationProvider.html5Mode(true);
6 $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|blob):/);
8 var resolver = {delay :
9 ['egStartup', function(egStartup) {return egStartup.go()}]}
11 $routeProvider.when('/admin/local/permission/grp_tree_display_entry', {
12 templateUrl : './admin/local/permission/t_grp_tree_display_entry',
13 controller : 'PermGrpTreeCtrl',
17 $routeProvider.otherwise({redirectTo : '/admin/local/permission/grp_tree'});
20 .factory('egPermGrpTreeSvc',
21 ['$q','egCore', function($q , egCore) {
30 // determine which user groups our user is not allowed to modify
31 service.set_edit_profiles = function() {
32 var all_app_perms = [];
33 var failed_perms = [];
35 // extract the application permissions
36 angular.forEach(service.profiles, function(grp) {
37 if (grp.application_perm())
38 all_app_perms.push(grp.application_perm());
41 // fill in service.edit_profiles by inspecting failed_perms
42 function traverse_grp_tree(grp, failed) {
44 failed_perms.indexOf(grp.application_perm()) > -1;
46 if (!failed) service.edit_profiles.push(grp);
49 service.profiles.filter( // children of grp
50 function(p) { return p.parent() == grp.id() }),
51 function(child) {traverse_grp_tree(child, failed)}
55 return egCore.perm.hasPermAt(all_app_perms, true).then(
57 angular.forEach(all_app_perms, function(p) {
58 if (perm_orgs[p].length == 0)
62 traverse_grp_tree(egCore.env.pgt.tree);
67 service.get_perm_groups = function() {
69 service.profiles = egCore.env.pgt.list;
70 return service.set_edit_profiles();
72 return egCore.pcrud.search('pgt', {parent : null},
73 {flesh : -1, flesh_fields : {pgt : ['children']}}
76 egCore.env.absorbTree(tree, 'pgt')
77 service.profiles = egCore.env.pgt.list;
78 return service.set_edit_profiles();
84 service.fetchDisplayEntries = function(ou_id) {
85 service.edit_profiles = [];
86 service.get_perm_groups();
87 return egCore.pcrud.search('pgtde',
88 {org : egCore.org.ancestors(ou_id, true)},
93 order_by: {pgtde : 'id'}
96 ).then(function(entries) {
97 service.pgtde_array = [];
98 service.disabled_entries = [];
99 angular.forEach(entries, function(entry) {
100 service.pgtde_array.push(entry);
105 service.organizeDisplayEntries = function(selectedOrg) {
106 service.display_entries = [];
108 angular.forEach(service.pgtde_array, function(pgtde) {
109 if (pgtde.org().id() == selectedOrg) {
110 if (!pgtde.child_entries) pgtde.child_entries = [];
112 angular.forEach(service.display_entries, function(entry) {
113 if (pgtde.parent() && pgtde.parent() == entry.id()) {
114 entry.child_entries.push(pgtde);
118 if (service.iterateChildEntries(pgtde, entry)) {
124 if (!pgtde.parent() || !isChild) {
125 service.display_entries.push(pgtde);
131 service.iterateChildEntries = function(pgtde, entry) {
132 if (entry.child_entries.length) {
133 return angular.forEach(entry.child_entries, function(child) {
134 if (pgtde.parent() == child.id()) {
135 child.child_entries.push(pgtde);
138 return service.iterateChildEntries(pgtde, child);
144 service.updateDisplayEntries = function(tree, ou_id) {
145 return egCore.pcrud.search('pgtde',
149 pgtde: ['grp', 'org']
151 order_by: {pgtde : 'id'}
154 ).then(function(entries) {
155 return egCore.pcrud.update(tree).then(function(res) {
161 service.removeDisplayEntries = function(entries) {
162 return egCore.pcrud.remove(entries).then(function(res) {
167 service.addDisplayEntries = function(entries) {
168 return egCore.pcrud.create(entries).then(function(res) {
173 service.findEntry = function(id, entries) {
175 angular.forEach(entries, function(entry) {
177 if (!entry.child_entries) entry.child_entries = [];
178 if (id == entry.id()) {
180 } else if (entry.child_entries.length) {
181 match = service.findEntry(id, entry.child_entries);
192 .controller('PermGrpTreeCtrl',
193 ['$scope','$q','$timeout','$location','$uibModal','egCore','egPermGrpTreeSvc', 'ngToast', 'egProgressDialog',
194 function($scope , $q , $timeout , $location , $uibModal , egCore , egPermGrpTreeSvc, ngToast, egProgressDialog) {
195 $scope.perm_tree = [{
198 name: function() {return egCore.strings.ROOT_NODE_NAME;}
204 $scope.display_entries = [];
205 $scope.new_entries = [];
206 $scope.tree_options = {nodeChildren: 'child_entries'};
207 $scope.selected_entry;
208 $scope.expanded_nodes = [];
209 $scope.orderby = ['position()','grp().name()'];
211 if (!$scope.selectedOrg)
212 $scope.selectedOrg = egCore.org.get(egCore.auth.user().ws_ou());
214 $scope.updateSelection = function(node, selected) {
215 $scope.selected_entry = node;
216 if (!selected) $scope.selected_entry = null;
219 $scope.setPosition = function(node, direction) {
220 var newPos = node.position();
223 siblings = egPermGrpTreeSvc.findEntry(node.parent(), $scope.perm_tree[0].child_entries).child_entries;
225 siblings = $scope.perm_tree[0].child_entries;
227 if (direction == 'up' && newPos < siblings.length) newPos++;
228 if (direction == 'down' && newPos > 1) newPos--;
230 angular.forEach(siblings, function(entry) {
231 if (entry.position() == newPos) entry.position(node.position());
232 angular.forEach($scope.display_entries, function(display_entry) {
233 if (display_entry.id() == entry.id()) {
234 if (display_entry.position() == newPos) {
235 display_entry.position(node.position);
241 angular.forEach($scope.display_entries, function(display_entry) {
242 if (display_entry.id() == node.id()) {
243 display_entry.position(newPos);
247 node.position(newPos);
250 $scope.addChildEntry = function(node) {
252 $scope.openAddDialog(node, $scope.disabled_entries, $scope.edit_profiles, $scope.display_entries, $scope.selectedOrg)
253 .then(function(res) {
257 var new_entry = new egCore.idl.pgtde();
258 new_entry.org($scope.selectedOrg.id());
259 new_entry.grp(res.selected_grp);
260 new_entry.position(1);
261 new_entry.child_entries = [];
263 if (res.selected_parent) {
264 new_entry.parent(res.selected_parent);
265 angular.forEach($scope.expanded_nodes, function(expanded_node) {
266 if (expanded_node == res.selected_parent) is_expanded = true;
268 if (!is_expanded) $scope.expanded_nodes.push(res.selected_parent);
270 angular.forEach($scope.expanded_nodes, function(expanded_node) {
271 if (expanded_node == $scope.perm_tree[0]) is_expanded = true;
273 if (!is_expanded) $scope.expanded_nodes.push($scope.perm_tree[0]);
276 $scope.display_entries.push(new_entry);
277 egPermGrpTreeSvc.addDisplayEntries([new_entry]).then(function(addRes) {
279 if (res.is_root || !res.selected_parent) {
280 angular.forEach($scope.perm_tree[0].child_entries, function(entry) {
281 var newPos = entry.position();
283 entry.position(newPos);
284 siblings.push(entry);
287 var parent = egPermGrpTreeSvc.findEntry(res.selected_parent.id(), $scope.perm_tree[0].child_entries);
288 angular.forEach(parent.child_entries, function(entry) {
289 var newPos = entry.position();
291 entry.position(newPos);
292 siblings.push(entry);
296 egPermGrpTreeSvc.updateDisplayEntries(siblings).then(function(updateRes) {
297 ngToast.create(egCore.strings.ADD_SUCCESS);
298 $scope.refreshTree($scope.selectedOrg, $scope.selected_entry);
301 ngToast.create(egCore.strings.ADD_FAILURE);
308 $scope.openAddDialog = function(node, disabled_entries, edit_profiles, display_entries, selected_org) {
310 return $uibModal.open({
311 templateUrl : './admin/local/permission/t_pgtde_add_dialog',
314 '$scope','$uibModalInstance',
315 function($scope , $uibModalInstance) {
316 var getIsRoot = function() {
317 if (!node || node.permanent) return true;
321 var getSelectedParent = function() {
322 if (!node || node.permanent) return $scope.perm_tree;
326 var available_profiles = [];
327 angular.forEach(edit_profiles, function(grp) {
328 grp._filter_grp = false;
329 angular.forEach(display_entries, function(entry) {
330 if (entry.org().id() == selected_org.id()) {
331 if (entry.grp().id() == grp.id()) grp._filter_grp = true;
334 if (!grp._filter_grp) available_profiles.push(grp);
338 is_root : getIsRoot(),
339 selected_parent : getSelectedParent(),
340 edit_profiles : available_profiles,
341 display_entries : display_entries,
342 selected_org : selected_org
345 $scope.context.selected_grp = $scope.context.edit_profiles[0];
347 $scope.ok = function() {
348 $uibModalInstance.close($scope.context);
351 $scope.cancel = function() {
352 $uibModalInstance.dismiss();
359 var iteratePermTree = function(arr1, arr2) {
360 angular.forEach(arr1, function(entry) {
362 if (entry.child_entries) iteratePermTree(entry.child_entries, arr2);
366 $scope.removeEntry = function(node) {
367 $scope.disabled_entries.push(node);
368 iteratePermTree(node.child_entries, $scope.disabled_entries);
372 siblings = egPermGrpTreeSvc.findEntry(node.parent(), $scope.perm_tree[0].child_entries).child_entries;
374 siblings = $scope.perm_tree[0].child_entries;
376 angular.forEach(siblings, function(sibling) {
377 var newPos = sibling.position();
378 if (node.position() < sibling.position()) {
381 sibling.position(newPos);
384 $scope.selected_entry = null;
386 egPermGrpTreeSvc.removeDisplayEntries($scope.disabled_entries).then(function(res) {
388 ngToast.create(egCore.strings.REMOVE_SUCCESS);
389 $scope.refreshTree($scope.selectedOrg);
391 ngToast.create(egCore.strings.REMOVE_FAILURE);
396 var getDisplayEntries = function() {
397 $scope.edit_profiles = egPermGrpTreeSvc.edit_profiles;
398 egPermGrpTreeSvc.organizeDisplayEntries($scope.selectedOrg.id());
399 $scope.perm_tree[0].child_entries = egPermGrpTreeSvc.display_entries;
400 $scope.display_entries = egPermGrpTreeSvc.pgtde_array;
401 $scope.new_entries = [];
402 $scope.disabled_entries = [];
403 $scope.selected_entry = $scope.perm_tree[0];
404 if (!$scope.expanded_nodes.length) iteratePermTree($scope.perm_tree, $scope.expanded_nodes);
407 $scope.saveEntries = function() {
408 egProgressDialog.open();
410 // Save Remaining Display Entries
411 egPermGrpTreeSvc.updateDisplayEntries($scope.display_entries, $scope.selectedOrg.id())
412 .then(function(res) {
414 ngToast.create(egCore.strings.UPDATE_SUCCESS);
415 $scope.refreshTree($scope.selectedOrg, $scope.selected_entry);
417 ngToast.create(egCore.strings.UPDATE_FAILURE);
419 }).finally(egProgressDialog.close);
422 $scope.org_changed = function(org) {
423 $scope.refreshTree(org.id());
426 $scope.refreshTree = function(ou_id, node) {
427 egPermGrpTreeSvc.fetchDisplayEntries(ou_id).then(function() {
429 if (node) $scope.selected_entry = node;
433 egCore.startup.go(function() {
434 $scope.refreshTree(egCore.auth.user().ws_ou());