2017-02-07 13:34:26 +00:00
|
|
|
import {module} from '../module';
|
|
|
|
import {validateAll} from '../lib/validator';
|
|
|
|
import {firstUpper} from '../lib/string';
|
2017-02-02 12:17:30 +00:00
|
|
|
|
2017-03-07 12:01:24 +00:00
|
|
|
directive.$inject = ['$interpolate', '$compile', '$window'];
|
2017-02-01 17:55:02 +00:00
|
|
|
export function directive(interpolate, compile, $window) {
|
2017-01-31 13:13:06 +00:00
|
|
|
return {
|
|
|
|
restrict: 'A',
|
2017-06-15 13:00:43 +00:00
|
|
|
require: ['ngModel', '?^^form'],
|
2017-02-02 12:17:30 +00:00
|
|
|
link: link
|
2017-03-07 11:37:59 +00:00
|
|
|
};
|
2017-02-01 17:55:02 +00:00
|
|
|
|
2017-02-06 17:01:04 +00:00
|
|
|
function link(scope, element, attrs, ctrl) {
|
2017-02-02 12:17:30 +00:00
|
|
|
let vnValidations = $window.validations;
|
2017-02-01 17:55:02 +00:00
|
|
|
|
2017-03-07 11:37:59 +00:00
|
|
|
if (!attrs.vnValidation || !vnValidations)
|
2017-02-02 12:17:30 +00:00
|
|
|
return;
|
2017-02-01 17:55:02 +00:00
|
|
|
|
2017-03-07 11:37:59 +00:00
|
|
|
let split = attrs.vnValidation.split('.');
|
2017-02-01 17:55:02 +00:00
|
|
|
|
2017-03-07 11:37:59 +00:00
|
|
|
if (split.length !== 2)
|
2017-02-02 12:17:30 +00:00
|
|
|
throw new Error(`vnValidation: Attribute must have this syntax: [entity].[field]`);
|
2017-02-01 17:55:02 +00:00
|
|
|
|
2017-02-02 12:17:30 +00:00
|
|
|
let entityName = firstUpper(split[0]);
|
|
|
|
let fieldName = split[1];
|
|
|
|
let entity = vnValidations[entityName];
|
2017-02-01 17:55:02 +00:00
|
|
|
|
2017-03-07 11:37:59 +00:00
|
|
|
if (!entity)
|
2017-02-02 12:17:30 +00:00
|
|
|
throw new Error(`vnValidation: Entity '${entityName}' doesn't exist`);
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2017-02-02 12:17:30 +00:00
|
|
|
let validations = entity.validations[fieldName];
|
2017-03-07 11:37:59 +00:00
|
|
|
if (!validations || validations.length == 0)
|
2017-02-02 12:17:30 +00:00
|
|
|
return;
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2017-03-07 11:37:59 +00:00
|
|
|
let input = ctrl[0];
|
|
|
|
let form = ctrl[1];
|
2017-04-28 13:04:29 +00:00
|
|
|
let errorSpan = angular.element('<span class="mdl-textfield__error"></span>');
|
|
|
|
let errorMsg;
|
|
|
|
let errorShown = false;
|
2017-03-07 11:37:59 +00:00
|
|
|
|
2017-09-13 07:45:42 +00:00
|
|
|
input.$options.$$options.allowInvalid = true;
|
2018-01-02 13:10:17 +00:00
|
|
|
input.$validators.entity = value => {
|
2017-03-07 11:37:59 +00:00
|
|
|
try {
|
|
|
|
validateAll(value, validations);
|
2017-03-07 12:01:24 +00:00
|
|
|
return true;
|
2017-03-07 11:37:59 +00:00
|
|
|
} catch (e) {
|
2017-04-28 13:04:29 +00:00
|
|
|
errorMsg = e.message;
|
|
|
|
if (errorShown) changeError();
|
2017-03-07 11:37:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-02 07:05:50 +00:00
|
|
|
scope.$watch(() => {
|
2017-03-07 11:37:59 +00:00
|
|
|
return (form.$submitted || input.$dirty) && input.$invalid;
|
2018-01-02 07:05:50 +00:00
|
|
|
}, value => {
|
2017-04-28 13:04:29 +00:00
|
|
|
let parent = element.parent();
|
|
|
|
|
2017-03-07 11:37:59 +00:00
|
|
|
if (value) {
|
2017-04-28 13:04:29 +00:00
|
|
|
changeError();
|
2017-03-07 11:37:59 +00:00
|
|
|
parent.addClass('invalid');
|
2017-04-28 13:04:29 +00:00
|
|
|
element.after(errorSpan);
|
|
|
|
} else if (errorShown) {
|
2017-03-07 11:37:59 +00:00
|
|
|
parent.removeClass('invalid');
|
2017-04-28 13:04:29 +00:00
|
|
|
parent.removeAttr('title');
|
|
|
|
errorSpan.remove();
|
|
|
|
errorSpan.empty();
|
2017-03-07 11:37:59 +00:00
|
|
|
}
|
2017-04-28 13:04:29 +00:00
|
|
|
|
|
|
|
errorShown = value;
|
2017-03-07 11:37:59 +00:00
|
|
|
});
|
2017-04-28 13:04:29 +00:00
|
|
|
|
|
|
|
function changeError() {
|
|
|
|
let parent = element.parent();
|
|
|
|
errorSpan.text(errorMsg);
|
|
|
|
parent.attr('title', errorMsg);
|
|
|
|
}
|
2017-02-01 17:55:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
module.directive('vnValidation', directive);
|
|
|
|
|