salix/front/core/directives/acl.js

42 lines
1.1 KiB
JavaScript

import ngModule from '../module';
import FormInput from '../components/form-input';
function vnAcl(aclService) {
let acls = [];
return {
restrict: 'A',
priority: -1,
link: function(_, $element, $attrs) {
acls = $attrs.vnAcl.split(',').map(i => i.trim());
if (acls[0] == '') return;
let action = $attrs.vnAclAction || 'disable';
if (aclService.hasAny(acls)) return;
if (action === 'disable') {
let element = $element[0];
let elementToDisable = element.$ctrl;
if (!(elementToDisable instanceof FormInput)) {
let selector = 'input, textarea, button, submit';
if (!element.matches(selector))
element = element.querySelector(selector);
elementToDisable = element;
}
if (elementToDisable)
elementToDisable.disabled = true;
} else
$element.remove();
}
};
}
vnAcl.$inject = ['aclService'];
ngModule.directive('vnAcl', vnAcl);