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

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