Skip to content

Commit

Permalink
Bump version to 0.0.15
Browse files Browse the repository at this point in the history
  • Loading branch information
haacked committed Aug 15, 2023
1 parent 65e97ce commit 96a527d
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 11 deletions.
84 changes: 78 additions & 6 deletions dist/aspnet-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -680,14 +680,20 @@ var ValidationService = /** @class */ (function () {
/**
* Scans document for all validation message <span> generated by ASP.NET Core MVC, then tracks them.
*/
ValidationService.prototype.scanMessages = function (root) {
ValidationService.prototype.scanMessages = function (root, remove) {
if (remove === void 0) { remove = false; }
/* If a validation span explicitly declares a form, we group the span with that form. */
var validationMessageElements = Array.from(root.querySelectorAll('span[form]'));
for (var _i = 0, validationMessageElements_1 = validationMessageElements; _i < validationMessageElements_1.length; _i++) {
var span = validationMessageElements_1[_i];
var form = document.getElementById(span.getAttribute('form'));
if (form) {
this.pushValidationMessageSpan(form, span);
if (remove) {
this.removeValidationMessageSpan(form, span);
}
else {
this.pushValidationMessageSpan(form, span);
}
}
}
// Otherwise if a validation message span is inside a form, we group the span with the form it's inside.
Expand All @@ -702,7 +708,12 @@ var ValidationService = /** @class */ (function () {
var validationMessageElements_3 = Array.from(form.querySelectorAll('[data-valmsg-for]'));
for (var _b = 0, validationMessageElements_2 = validationMessageElements_3; _b < validationMessageElements_2.length; _b++) {
var span = validationMessageElements_2[_b];
this.pushValidationMessageSpan(form, span);
if (remove) {
this.removeValidationMessageSpan(form, span);
}
else {
this.pushValidationMessageSpan(form, span);
}
}
}
};
Expand All @@ -717,6 +728,21 @@ var ValidationService = /** @class */ (function () {
this.logger.log("Validation element for '%s' is already tracked", name, span);
}
};
ValidationService.prototype.removeValidationMessageSpan = function (form, span) {
var formId = this.getElementUID(form);
var name = "".concat(formId, ":").concat(span.getAttribute('data-valmsg-for'));
var spans = this.messageFor[name];
if (!spans) {
return;
}
var index = spans.indexOf(span);
if (index >= 0) {
spans.splice(index, 1);
}
else {
this.logger.log("Validation element for '%s' was already removed", name, span);
}
};
/**
* Given attribute map for an HTML input, returns the validation directives to be executed.
* @param attributes
Expand Down Expand Up @@ -797,7 +823,10 @@ var ValidationService = /** @class */ (function () {
var formValidators = [];
for (var i = 0; i < formInputUIDs.length; i++) {
var inputUID = formInputUIDs[i];
formValidators.push(this.validators[inputUID]);
var validator = this.validators[inputUID];
if (validator) {
formValidators.push(validator);
}
}
var tasks = formValidators.map(function (factory) { return factory(); });
return Promise.all(tasks).then(function (result) { return result.every(function (e) { return e; }); });
Expand Down Expand Up @@ -912,6 +941,19 @@ var ValidationService = /** @class */ (function () {
});
this.elementEvents[formUID] = cb;
};
ValidationService.prototype.untrackFormInput = function (form, inputUID) {
var formUID = this.getElementUID(form);
if (!this.formInputs[formUID]) {
return;
}
var indexToRemove = this.formInputs[formUID].indexOf(inputUID);
if (indexToRemove >= 0) {
this.formInputs[formUID].splice(indexToRemove, 1);
}
else {
this.logger.log("Form input for UID '%s' was already removed", inputUID);
}
};
/**
* Adds an input element to be managed and validated by the service.
* Triggers a debounced live validation when input value changes.
Expand Down Expand Up @@ -947,10 +989,20 @@ var ValidationService = /** @class */ (function () {
}
this.elementEvents[uid] = cb;
};
ValidationService.prototype.removeInput = function (input) {
var uid = this.getElementUID(input);
delete this.summary[uid];
delete this.elementEvents[uid];
delete this.validators[uid];
if (input.form) {
this.untrackFormInput(input.form, uid);
}
};
/**
* Scans the entire document for input elements to be validated.
*/
ValidationService.prototype.scanInputs = function (root) {
ValidationService.prototype.scanInputs = function (root, remove) {
if (remove === void 0) { remove = false; }
var inputs = Array.from(root.querySelectorAll(validatableSelector('[data-val="true"]')));
// querySelectorAll does not include the root element itself.
// we could use 'matches', but that's newer than querySelectorAll so we'll keep it simple and compatible.
Expand All @@ -959,7 +1011,12 @@ var ValidationService = /** @class */ (function () {
}
for (var i = 0; i < inputs.length; i++) {
var input = inputs[i];
this.addInput(input);
if (remove) {
this.removeInput(input);
}
else {
this.addInput(input);
}
}
};
/**
Expand Down Expand Up @@ -1203,6 +1260,14 @@ var ValidationService = /** @class */ (function () {
this.scanMessages(root);
this.scanInputs(root);
};
/**
* Scans the provided root element for any validation directives and removes behavior from them.
*/
ValidationService.prototype.remove = function (root) {
this.logger.log('Removing', root);
this.scanMessages(root, true);
this.scanInputs(root, true);
};
/**
* Watches the provided root element for mutations, and scans for new validation directives to attach behavior.
* @param root The root element to use, defaults to the document.documentElement.
Expand Down Expand Up @@ -1231,6 +1296,13 @@ var ValidationService = /** @class */ (function () {
this.scan(node);
}
}
for (var i = 0; i < mutation.removedNodes.length; i++) {
var node = mutation.removedNodes[i];
this.logger.log('Removed node', node);
if (node instanceof HTMLElement) {
this.remove(node);
}
}
}
else if (mutation.type === 'attributes') {
if (mutation.target instanceof HTMLElement) {
Expand Down
Loading

0 comments on commit 96a527d

Please sign in to comment.