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

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