Last active
October 19, 2016 15:35
-
-
Save j1cs/da964fc5d91473537eaec373d634cb0e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input class="form-control" type="text" custom-required="166, 165" custom-disabled="1, 162"> | |
<script> | |
function formularioPerfiles($log, perfilesFactory, $rootScope, $timeout, $compile) { | |
return { | |
require: 'form', | |
link: function(scope, element, attrs, ctrl) { | |
scope.$watch(function() { | |
var name = angular.element(element).attr('name'); | |
return scope[name]; | |
}, function(inputs) { | |
if (angular.isDefined(inputs) && angular.isObject(inputs)) { | |
var jerarquia = ['166', '165', '164', '163', '162', '1']; // parametrizable, en algun lugar o en alguna oportunidad | |
var perfiles = ['1']; | |
for (var key in inputs) { | |
if (inputs.hasOwnProperty(key) && key.indexOf('$') != 0) { | |
var $input = angular.element(document.querySelector('[name="' + key + '"]')), | |
data_required = $input.attr('custom-required'), | |
data_disabled = $input.attr('custom-disabled'); | |
if (angular.isDefined(data_required)) { | |
var required = data_required.split(','); | |
for (var j = 0; j < required.length; j++) { | |
// pregunta si el id esta en los perfiles del usuario cargado | |
if (perfiles.indexOf(required[j].trim()) >= 0) { | |
/** | |
* Se agrega clase y asterisco al label del input required | |
*/ | |
var label = angular.element(document.querySelector('[for="' + key + '"]')); | |
label.text(label.text() + ' * '); | |
var parent = label.parent(); | |
parent.addClass('has-error'); | |
// Se preguntan los numeros mas bajos primeros, pero los ultimos tiene acceso a todo | |
var isRequired = ([2, 3, 4, 5].indexOf(jerarquia.indexOf(required[j].trim())) >= 0) | |
ctrl[$input.attr('id')].$setValidity($input.attr('id'), isRequired); | |
isRequired = ([0, 1].indexOf(jerarquia.indexOf(required[j].trim())) >= 0); | |
ctrl[$input.attr('id')].$setValidity($input.attr('id'), isRequired); | |
if (isRequired) { | |
$input.attr('required', true); | |
} | |
// con todas las validaciones de arriba esta variable no debería ser undefined | |
$compile($input[0])(scope); | |
ctrl[$input.attr('id')].$render(); | |
$timeout(function() { | |
scope.$apply(); | |
}, 500); | |
} | |
} | |
} | |
if (angular.isDefined(data_disabled)) { | |
var disabled = data_disabled.split(','); | |
for (var a = 0; a < disabled.length; a++) { | |
if (perfiles.indexOf(disabled[a].trim()) >= 0) { | |
var isDisabled = ([2, 3, 4, 5].indexOf(jerarquia.indexOf(disabled[a].trim())) >= 0) | |
$input.attr('disabled', isDisabled) | |
if (!isDisabled) { | |
isDisabled = ([0, 1].indexOf(jerarquia.indexOf(disabled[a].trim())) >= 0); | |
$input.attr('disabled', isDisabled) | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
}, true); | |
} | |
}; | |
}; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment