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
16 KiB

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