-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoi.min.js
1 lines (1 loc) · 2.91 KB
/
oi.min.js
1
var oi=function(t,e){"use strict";function a(a){a=a||{},h={formSelector:a.formSelector||t.getElementsByTagName("form"),errorHTML:a.errorHTML||'<span class="oi-message" '+p+'="{{id}}" role="alert">{{message}}</span>',errorPosition:a.errorPosition||"afterend",errorScrollOffset:a.errorScrollOffset||75,interactedClass:a.interactedClass||"oi-has-interacted",onInvalid:a.onInvalid,onValid:a.onValid,watchInputs:a.watchInputs===e||a.watchInputs,validateHidden:a.validateHidden!==e&&a.validateHidden},"required"in t.createElement("input")&&g(h.formSelector,function(t){r(t),h.watchInputs&&o(t)})}function r(t){t.noValidate=!0,t.addEventListener("submit",function(t){var e=this;e.checkValidity()||(t.preventDefault(),l(e))},!0)}function i(e){if(!h.validateHidden||!d(e)){if(n(e),"url"===e.getAttribute("type")&&c(e),e.checkValidity()){e.setAttribute(A,"false");var a=t.querySelector("["+p+'="'+e.id+'"]');a&&a.parentNode.removeChild(a),h.onValid&&h.onValid(e)}else u(e);e.classList.add(h.interactedClass)}}function n(e){var a,r,i=!1;e.hasAttribute("data-has-match")&&(a=e,r=t.getElementById(e.getAttribute("data-has-match")),i=!0),e.hasAttribute("data-match")&&(a=t.getElementById(e.getAttribute("data-match")),r=e,i=!0),i&&a&&r&&(a.value!==r.value?(r.setCustomValidity(r.getAttribute(b+"-match")),u(r)):r.setCustomValidity(""))}function o(e){var a=(e||t).querySelectorAll(y);g(a,function(t){t.addEventListener("change",function(t){var e=t.target;i(e)},!0)})}function l(e){g(e.querySelectorAll(y),function(t){i(t)});var a=e.querySelectorAll("input:invalid, select:invalid, textarea:invalid");a.length>0&&(v(t.activeElement)?"false"===t.activeElement.getAttribute("aria-invalid")&&s(a[0]):s(a[0]))}function u(e){e.setAttribute(A,"true");var a=e.validity,r=!!a.valueMissing&&e.getAttribute(b+"-required")||!!a.typeMismatch&&e.getAttribute(b+"-type")||!!a.patternMismatch&&e.getAttribute(b+"-pattern")||!!a.patternMismatch&&e.getAttribute(b+"-regex")||!!a.tooShort&&e.getAttribute(b+"-short")||!!a.tooLong&&e.getAttribute(b+"-long")||!!a.customError&&e.getAttribute(b+"-custom")||e.getAttribute(b)||e.validationMessage,i=t.querySelector("["+p+'="'+e.id+'"]');i?i.textContent=r:e.insertAdjacentHTML(h.errorPosition,m(h.errorHTML,{id:e.id,message:r})),h.onInvalid&&h.onInvalid(e)}function s(t){t.focus(),f(t)}function c(t){var e=t.value;e&&e.search(/^http[s]?\:\/\//)===-1&&(e="http://"+e),t.value=e}function d(t){return null===t.offsetParent}function f(e){var a=e.getBoundingClientRect().top-h.errorScrollOffset+(window.pageYOffset||t.documentElement.scrollTop);window.scrollTo(0,a)}function v(t){var e=t.tagName;return!!e&&("input"===e.toLowerCase()||"textarea"===e.toLowerCase()||"select"===e.toLowerCase())}function g(t,e){for(var a=0,r=t.length;a<r;a++)e(t[a])}function m(t,e){for(var a in e)t=t.replace(new RegExp("{{"+a+"}}","g"),e[a]);return t}var h={},p="data-oi-id",A="aria-invalid",b="data-msg",y="input, select, textarea";return{init:a,validateInput:i,validateForm:l}}(document);