salix/client/core/src/directives/acl.js

56 lines
2.0 KiB
JavaScript
Raw Normal View History

2018-02-10 15:18:01 +00:00
import ngModule from '../module';
2017-09-21 08:23:51 +00:00
function vnAcl(aclService, $timeout) {
function getMaterialType(className) {
let type = '';
if (className) {
type = className.replace('mdl-', '').replace('__input', '');
type = type.charAt(0).toUpperCase() + type.slice(1);
}
return type;
}
2018-02-10 15:18:01 +00:00
function updateMaterial(input) {
if (input && input.className) {
let find = input.className.match(/mdl-[\w]+input/g);
2018-01-25 13:05:50 +00:00
if (find && find.length && find[0]) {
let type = getMaterialType(find[0]);
if (type && input.parentNode[`Material${type}`] && input.parentNode[`Material${type}`].updateClasses_) {
input.parentNode[`Material${type}`].updateClasses_();
}
}
}
}
return {
restrict: 'A',
2017-09-21 08:23:51 +00:00
priority: -1,
link: function($scope, $element, $attrs) {
let acls = $attrs.vnAcl.split(',');
let action = $attrs.vnAclAction || 'disabled';
if (!aclService.aclPermission(acls)) {
if (action === 'disabled') {
let input = $element[0];
let selector = 'input, textarea, button, submit';
if (!input.matches(selector))
input = input.querySelector(selector);
if (input) {
2017-09-21 08:23:51 +00:00
$timeout(() => {
input.setAttribute("disabled", "true");
2018-02-10 15:18:01 +00:00
updateMaterial(input);
2017-09-21 08:23:51 +00:00
});
$element[0].querySelectorAll('i, vn-drop-down').forEach(element => {
element.parentNode.removeChild(element);
2017-09-21 08:23:51 +00:00
});
}
} else {
$element.remove();
}
}
}
};
}
2017-09-21 08:23:51 +00:00
vnAcl.$inject = ['aclService', '$timeout'];
2018-02-10 15:18:01 +00:00
ngModule.directive('vnAcl', vnAcl);