1 line
20 KiB
JavaScript
1 line
20 KiB
JavaScript
|
!function(T,e,D,O){"use strict";e=void 0!==e&&e.Math==Math?e:"undefined"!=typeof self&&self.Math==Math?self:Function("return this")(),T.fn.form=function(x){var k,E=T(this),w=E.selector||"",C=(new Date).getTime(),V=[],R=x,F=arguments[1],S="string"==typeof R,A=[].slice.call(arguments,1);return E.each(function(){var n,s,t,e,d,u,c,f,p,r,l,i,a,m,g,h,o=T(this),v=this,b=[],y=!1;(h={initialize:function(){h.get.settings(),S?(g===O&&h.instantiate(),h.invoke(R)):(g!==O&&g.invoke("destroy"),h.verbose("Initializing form validation",o,d),h.bindEvents(),h.set.defaults(),h.instantiate())},instantiate:function(){h.verbose("Storing instance of module",h),g=h,o.data(a,h)},destroy:function(){h.verbose("Destroying previous module",g),h.removeEvents(),o.removeData(a)},refresh:function(){h.verbose("Refreshing selector cache"),n=o.find(f.field),s=o.find(f.group),t=o.find(f.message),o.find(f.prompt),e=o.find(f.submit),o.find(f.clear),o.find(f.reset)},submit:function(){h.verbose("Submitting form",o),o.submit()},attachEvents:function(e,t){t=t||"submit",T(e).on("click"+m,function(e){h[t](),e.preventDefault()})},bindEvents:function(){h.verbose("Attaching form events"),o.on("submit"+m,h.validate.form).on("blur"+m,f.field,h.event.field.blur).on("click"+m,f.submit,h.submit).on("click"+m,f.reset,h.reset).on("click"+m,f.clear,h.clear),d.keyboardShortcuts&&o.on("keydown"+m,f.field,h.event.field.keydown),n.each(function(){var e=T(this),t=e.prop("type"),n=h.get.changeEvent(t,e);T(this).on(n+m,h.event.field.change)})},clear:function(){n.each(function(){var e=T(this),t=e.parent(),n=e.closest(s),r=n.find(f.prompt),i=e.data(c.defaultValue)||"",a=t.is(f.uiCheckbox),o=t.is(f.uiDropdown);n.hasClass(p.error)&&(h.verbose("Resetting error on field",n),n.removeClass(p.error),r.remove()),o?(h.verbose("Resetting dropdown value",t,i),t.dropdown("clear")):a?e.prop("checked",!1):(h.verbose("Resetting field value",e,i),e.val(""))})},reset:function(){n.each(function(){var e=T(this),t=e.parent(),n=e.closest(s),r=n.find(f.prompt),i=e.data(c.defaultValue),a=t.is(f.uiCheckbox),o=t.is(f.uiDropdown),l=n.hasClass(p.error);i!==O&&(l&&(h.verbose("Resetting error on field",n),n.removeClass(p.error),r.remove()),o?(h.verbose("Resetting dropdown value",t,i),t.dropdown("restore defaults")):a?(h.verbose("Resetting checkbox value",t,i),e.prop("checked",i)):(h.verbose("Resetting field value",e,i),e.val(i)))})},determine:{isValid:function(){var n=!0;return T.each(u,function(e,t){h.validate.field(t,e,!0)||(n=!1)}),n}},is:{bracketedRule:function(e){return e.type&&e.type.match(d.regExp.bracket)},shorthandFields:function(e){var t=e[Object.keys(e)[0]];return h.is.shorthandRules(t)},shorthandRules:function(e){return"string"==typeof e||T.isArray(e)},empty:function(e){return!e||0===e.length||(e.is('input[type="checkbox"]')?!e.is(":checked"):h.is.blank(e))},blank:function(e){return""===T.trim(e.val())},valid:function(e){var n=!0;return e?(h.verbose("Checking if field is valid",e),h.validate.field(u[e],e,!1)):(h.verbose("Checking if form is valid"),T.each(u,function(e,t){h.is.valid(e)||(n=!1)}),n)}},removeEvents:function(){o.off(m),n.off(m),e.off(m),n.off(m)},event:{field:{keydown:function(e){var t=T(this),n=e.which,r=t.is(f.input),i=t.is(f.checkbox),a=0<t.closest(f.uiDropdown).length,o=13;n==27&&(h.verbose("Escape key pressed blurring field"),t.blur()),e.ctrlKey||n!=o||!r||a||i||(y||(t.one("keyup"+m,h.event.field.keyup),h.submit(),h.debug("Enter pressed on input submitting form")),y=!0)},keyup:function(){y=!1},blur:function(e){var t=T(this),n=t.closest(s),r=h.get.validation(t);n.hasClass(p.error)?(h.debug("Revalidating field",t,r),r&&h.validate.field(r)):"blur"==d.on&&r&&h.validate.field(r)},change:function(e){var t=T(this),n=t.closest(s),r=h.get.validation(t);r&&("change"==d.on||n.hasClass(p.error)&&d.revalidate)&&(clearTimeout(h.timer),h.timer=setTimeout(function(){h.debug("Revalidating field",t,h.get.validation(t)),h.validate.field(r)},d.delay))}}},get:{ancillaryValue:function(e){return!(!e.type||!e.value&&!h.is.bracketedRule(e))&&(e.value!==O?e.value:e.type.match(d.regExp.bracket)[1]+"")},ruleNam
|