2018-02-10 15:18:01 +00:00
|
|
|
import ngModule from '../module';
|
2017-05-25 09:48:10 +00:00
|
|
|
|
2017-09-21 08:23:51 +00:00
|
|
|
function vnAcl(aclService, $timeout) {
|
2018-03-01 09:54:02 +00:00
|
|
|
let acls = [];
|
|
|
|
|
2017-11-23 13:53:37 +00:00
|
|
|
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) {
|
2017-11-23 13:53:37 +00:00
|
|
|
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]) {
|
2017-11-23 13:53:37 +00:00
|
|
|
let type = getMaterialType(find[0]);
|
2019-01-23 12:11:44 +00:00
|
|
|
if (type && input.parentNode[`Material${type}`] && input.parentNode[`Material${type}`].updateClasses_)
|
2017-11-23 13:53:37 +00:00
|
|
|
input.parentNode[`Material${type}`].updateClasses_();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-01 09:54:02 +00:00
|
|
|
function getDynamicConditions($attrs) {
|
|
|
|
let atributes = $attrs.$attr;
|
|
|
|
let conditions = {};
|
|
|
|
|
|
|
|
Object.keys(atributes).forEach(atribute => {
|
|
|
|
if (atribute.startsWith('aclConditionalTo')) {
|
|
|
|
let role = atributes[atribute].split('-').slice(-1)[0];
|
|
|
|
conditions[atribute] = {
|
|
|
|
role: role
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return conditions;
|
|
|
|
}
|
|
|
|
|
2019-06-27 09:48:33 +00:00
|
|
|
function permissionElement($element, action) {
|
2019-01-23 12:11:44 +00:00
|
|
|
if (!aclService.hasAny(acls)) {
|
2018-03-01 09:54:02 +00:00
|
|
|
if (action === 'disabled') {
|
2019-06-27 09:48:33 +00:00
|
|
|
let element = $element[0];
|
2019-02-14 19:17:22 +00:00
|
|
|
let selector = 'input, textarea, button, submit, md-checkbox';
|
2018-03-01 09:54:02 +00:00
|
|
|
|
2019-06-27 09:48:33 +00:00
|
|
|
if (element.$ctrl) {
|
|
|
|
element.setAttribute('disabled', 'true');
|
|
|
|
element.$ctrl.disabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!element.matches(selector))
|
|
|
|
element = element.querySelector(selector);
|
2018-03-01 09:54:02 +00:00
|
|
|
|
2019-06-27 09:48:33 +00:00
|
|
|
if (element) {
|
2018-03-01 09:54:02 +00:00
|
|
|
$timeout(() => {
|
2019-06-27 09:48:33 +00:00
|
|
|
element.setAttribute('disabled', 'true');
|
|
|
|
updateMaterial(element);
|
2018-03-01 09:54:02 +00:00
|
|
|
});
|
2018-10-11 07:41:30 +00:00
|
|
|
$element[0].querySelectorAll('vn-drop-down').forEach(element => {
|
2018-03-01 09:54:02 +00:00
|
|
|
element.parentNode.removeChild(element);
|
|
|
|
});
|
|
|
|
}
|
2019-01-23 12:11:44 +00:00
|
|
|
} else
|
2018-03-01 09:54:02 +00:00
|
|
|
$element.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateAcls(role, toAdd) {
|
|
|
|
let position = acls.indexOf(role);
|
|
|
|
|
2019-01-23 12:11:44 +00:00
|
|
|
if (!toAdd && position > -1)
|
2018-03-01 09:54:02 +00:00
|
|
|
acls.splice(position, 1);
|
2019-10-14 10:01:19 +00:00
|
|
|
// XXX: add acl and enabled element if previusly was disabled
|
2018-03-01 09:54:02 +00:00
|
|
|
}
|
|
|
|
|
2017-05-25 09:48:10 +00:00
|
|
|
return {
|
|
|
|
restrict: 'A',
|
2017-09-21 08:23:51 +00:00
|
|
|
priority: -1,
|
2017-05-25 12:23:19 +00:00
|
|
|
link: function($scope, $element, $attrs) {
|
2019-01-24 09:57:43 +00:00
|
|
|
acls = $attrs.vnAcl.split(',').map(i => i.trim());
|
2019-03-12 14:04:09 +00:00
|
|
|
|
|
|
|
if (acls[0] == '') return;
|
|
|
|
|
2017-05-25 12:23:19 +00:00
|
|
|
let action = $attrs.vnAclAction || 'disabled';
|
2018-03-01 09:54:02 +00:00
|
|
|
let conditions = getDynamicConditions($attrs);
|
2019-03-12 14:04:09 +00:00
|
|
|
|
2019-06-27 09:48:33 +00:00
|
|
|
permissionElement($element, action);
|
2018-03-01 09:54:02 +00:00
|
|
|
|
|
|
|
if (Object.keys(conditions).length) {
|
|
|
|
let watchConditions = $scope.$watch(() => {
|
|
|
|
Object.keys(conditions).forEach(attrName => {
|
|
|
|
let hasPermission = $scope.$eval($attrs[attrName]);
|
|
|
|
if (!hasPermission) {
|
|
|
|
updateAcls(conditions[attrName].role, hasPermission);
|
2019-06-27 09:48:33 +00:00
|
|
|
permissionElement($element, action);
|
2018-03-01 09:54:02 +00:00
|
|
|
delete conditions[attrName];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (Object.keys(conditions).length === 0) {
|
|
|
|
// unWacth
|
|
|
|
watchConditions();
|
2017-05-25 12:23:19 +00:00
|
|
|
}
|
2018-03-01 09:54:02 +00:00
|
|
|
});
|
2017-05-25 09:48:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2017-09-21 08:23:51 +00:00
|
|
|
vnAcl.$inject = ['aclService', '$timeout'];
|
2017-05-25 09:48:10 +00:00
|
|
|
|
2018-02-10 15:18:01 +00:00
|
|
|
ngModule.directive('vnAcl', vnAcl);
|