You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

10 lines
15 KiB

10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
  1. /*!
  2. * # Semantic UI 2.0.8 - Form Validation
  3. * http://github.com/semantic-org/semantic-ui/
  4. *
  5. *
  6. * Copyright 2015 Contributors
  7. * Released under the MIT license
  8. * http://opensource.org/licenses/MIT
  9. *
  10. */
  11. !function(e,t,n,i){"use strict";e.fn.form=function(t){var r,o=e(this),a=o.selector||"",s=(new Date).getTime(),l=[],c=arguments[0],u=arguments[1],f="string"==typeof c,d=[].slice.call(arguments,1);return o.each(function(){var p,g,m,v,h,b,y,x,k,w,E,C,R,S,F,T,O,z,D=e(this),j=this,A=[],$=!1;z={initialize:function(){z.get.settings(),f?(O===i&&z.instantiate(),z.invoke(c)):(z.verbose("Initializing form validation",D,x),z.bindEvents(),z.set.defaults(),z.instantiate())},instantiate:function(){z.verbose("Storing instance of module",z),O=z,D.data(F,z)},destroy:function(){z.verbose("Destroying previous module",O),z.removeEvents(),D.removeData(F)},refresh:function(){z.verbose("Refreshing selector cache"),p=D.find(E.field),g=D.find(E.group),m=D.find(E.message),v=D.find(E.prompt),h=D.find(E.submit),b=D.find(E.clear),y=D.find(E.reset)},submit:function(){z.verbose("Submitting form",D),D.submit()},attachEvents:function(t,n){n=n||"submit",e(t).on("click"+T,function(e){z[n](),e.preventDefault()})},bindEvents:function(){z.verbose("Attaching form events"),D.on("submit"+T,z.validate.form).on("blur"+T,E.field,z.event.field.blur).on("click"+T,E.submit,z.submit).on("click"+T,E.reset,z.reset).on("click"+T,E.clear,z.clear),x.keyboardShortcuts&&D.on("keydown"+T,E.field,z.event.field.keydown),p.each(function(){var t=e(this),n=t.prop("type"),i=z.get.changeEvent(n,t);e(this).on(i+T,z.event.field.change)})},clear:function(){p.each(function(){var t=e(this),n=t.parent(),i=t.closest(g),r=i.find(E.prompt),o=t.data(w.defaultValue)||"",a=n.is(E.uiCheckbox),s=n.is(E.uiDropdown),l=i.hasClass(C.error);l&&(z.verbose("Resetting error on field",i),i.removeClass(C.error),r.remove()),s?(z.verbose("Resetting dropdown value",n,o),n.dropdown("clear")):a?t.prop("checked",!1):(z.verbose("Resetting field value",t,o),t.val(""))})},reset:function(){p.each(function(){var t=e(this),n=t.parent(),r=t.closest(g),o=r.find(E.prompt),a=t.data(w.defaultValue),s=n.is(E.uiCheckbox),l=n.is(E.uiDropdown),c=r.hasClass(C.error);a!==i&&(c&&(z.verbose("Resetting error on field",r),r.removeClass(C.error),o.remove()),l?(z.verbose("Resetting dropdown value",n,a),n.dropdown("restore defaults")):s?(z.verbose("Resetting checkbox value",n,a),t.prop("checked",a)):(z.verbose("Resetting field value",t,a),t.val(a)))})},is:{valid:function(){var t=!0;return z.verbose("Checking if form is valid"),e.each(k,function(e,n){z.validate.field(n)||(t=!1)}),t}},removeEvents:function(){D.off(T),p.off(T),h.off(T),p.off(T)},event:{field:{keydown:function(t){var n=e(this),i=t.which,r={enter:13,escape:27};i==r.escape&&(z.verbose("Escape key pressed blurring field"),n.blur()),!t.ctrlKey&&i==r.enter&&n.is(E.input)&&n.not(E.checkbox).length>0&&($||(n.one("keyup"+T,z.event.field.keyup),z.submit(),z.debug("Enter pressed on input submitting form")),$=!0)},keyup:function(){$=!1},blur:function(){var t=e(this),n=t.closest(g),i=z.get.validation(t);n.hasClass(C.error)?(z.debug("Revalidating field",t,i),z.validate.field(i)):("blur"==x.on||"change"==x.on)&&z.validate.field(i)},change:function(){var t=e(this),n=t.closest(g);("change"==x.on||n.hasClass(C.error)&&x.revalidate)&&(clearTimeout(z.timer),z.timer=setTimeout(function(){z.debug("Revalidating field",t,z.get.validation(t)),z.validate.field(z.get.validation(t))},x.delay))}}},get:{changeEvent:function(e,t){return"checkbox"==e||"radio"==e||"hidden"==e||t.is("select")?"change":z.get.inputEvent()},inputEvent:function(){return n.createElement("input").oninput!==i?"input":n.createElement("input").onpropertychange!==i?"propertychange":"keyup"},settings:function(){if(e.isPlainObject(t)){var n=Object.keys(t),r=n.length>0?t[n[0]].identifier!==i&&t[n[0]].rules!==i:!1;r?(x=e.extend(!0,{},e.fn.form.settings,u),k=e.extend({},e.fn.form.settings.defaults,t),z.error(x.error.oldSyntax,j),z.verbose("Extending settings from legacy parameters",k,x)):(x=e.extend(!0,{},e.fn.form.settings,t),k=e.extend({},e.fn.form.settings.defaults,x.fields),z.verbose("Extending settings",k,x))}else x=e.fn.form.settings,k=e.fn.form.settings.defaults,z.verbose("Using default form validation",k,x);S=x.namespace,w=x.met