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