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.

0 lines
8.8 KiB

10 years ago
  1. !function(a,b,c,d){a.fn.form=function(b,e){var f,g=a(this),h=a.extend(!0,{},a.fn.form.settings,e),i=a.extend({},a.fn.form.settings.defaults,b),j=h.namespace,k=h.metadata,l=h.selector,m=h.className,n=(h.error,"."+j),o="module-"+j,p=g.selector||"",q=(new Date).getTime(),r=[],s=arguments[0],t="string"==typeof s,u=[].slice.call(arguments,1);return g.each(function(){var b,e=a(this),j=a(this).find(l.field),v=a(this).find(l.group),w=a(this).find(l.message),x=(a(this).find(l.prompt),a(this).find(l.submit)),y=[],z=this,A=e.data(o);b={initialize:function(){b.verbose("Initializing form validation",e,i,h),b.bindEvents(),b.instantiate()},instantiate:function(){b.verbose("Storing instance of module",b),A=b,e.data(o,b)},destroy:function(){b.verbose("Destroying previous module",A),b.removeEvents(),e.removeData(o)},refresh:function(){b.verbose("Refreshing selector cache"),j=e.find(l.field)},submit:function(){b.verbose("Submitting form",e),e.submit()},attachEvents:function(c,d){d=d||"submit",a(c).on("click",function(a){b[d](),a.preventDefault()})},bindEvents:function(){h.keyboardShortcuts&&j.on("keydown"+n,b.event.field.keydown),e.on("submit"+n,b.validate.form),j.on("blur"+n,b.event.field.blur),b.attachEvents(x,"submit"),j.each(function(){var c=a(this).prop("type"),d=b.get.changeEvent(c);a(this).on(d+n,b.event.field.change)})},removeEvents:function(){e.off(n),j.off(n),x.off(n),j.off(n)},event:{field:{keydown:function(c){var d=a(this),e=c.which,f={enter:13,escape:27};e==f.escape&&(b.verbose("Escape key pressed blurring field"),d.blur()),!c.ctrlKey&&e==f.enter&&d.is(l.input)&&d.not(l.checkbox).size()>0&&(b.debug("Enter key pressed, submitting form"),x.addClass(m.down),d.one("keyup"+n,b.event.field.keyup))},keyup:function(){b.verbose("Doing keyboard shortcut form submit"),x.removeClass(m.down),b.submit()},blur:function(){var c=a(this),d=c.closest(v);d.hasClass(m.error)?(b.debug("Revalidating field",c,b.get.validation(c)),b.validate.field(b.get.validation(c))):("blur"==h.on||"change"==h.on)&&b.validate.field(b.get.validation(c))},change:function(){var c=a(this),d=c.closest(v);("change"==h.on||d.hasClass(m.error)&&h.revalidate)&&(clearTimeout(b.timer),b.timer=setTimeout(function(){b.debug("Revalidating field",c,b.get.validation(c)),b.validate.field(b.get.validation(c))},h.delay))}}},get:{changeEvent:function(a){return"checkbox"==a||"radio"==a?"change":c.createElement("input").oninput!==d?"input":c.createElement("input").onpropertychange!==d?"propertychange":"keyup"},field:function(c){return b.verbose("Finding field with identifier",c),j.filter("#"+c).size()>0?j.filter("#"+c):j.filter('[name="'+c+'"]').size()>0?j.filter('[name="'+c+'"]'):j.filter("[data-"+k.validate+'="'+c+'"]').size()>0?j.filter("[data-"+k.validate+'="'+c+'"]'):a("<input/>")},validation:function(c){var d;return a.each(i,function(a,e){b.get.field(e.identifier).get(0)==c.get(0)&&(d=e)}),d||!1}},has:{field:function(a){return b.verbose("Checking for existence of a field with identifier",a),j.filter("#"+a).size()>0?!0:j.filter('[name="'+a+'"]').size()>0?!0:j.filter("[data-"+k.validate+'="'+a+'"]').size()>0?!0:!1}},add:{prompt:function(c,f){var g=b.get.field(c),i=g.closest(v),j=i.find(l.prompt),k=0!==j.size();f="string"==typeof f?[f]:f,b.verbose("Adding field error state",c),i.addClass(m.error),h.inline&&(k||(j=h.templates.prompt(f),j.appendTo(i)),j.html(f[0]),k?b.verbose("Inline errors are disabled, no inline error added",c):h.transition&&a.fn.transition!==d&&e.transition("is supported")?(b.verbose("Displaying error with css transition",h.transition),j.transition(h.transition+" in",h.duration)):(b.verbose("Displaying error with fallback javascript animation"),j.fadeIn(h.duration)))},errors:function(a){b.debug("Adding form error messages",a),w.html(h.templates.error(a))}},remove:{prompt:function(c){var f=b.get.field(c.identifier),g=f.closest(v),i=g.find(l.prompt);g.removeClass(m.error),h.inline&&i.is(":visible")&&(b.verbose("Removing prompt for field",c),h.transition&&a.fn.transition!==d&&e.transition("is supported")?i.transition(h.transition+" out",h.duration,function(){i.remove()