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

  1. (function(e,n,t,o){e.fn.form=function(n,t){var i,r=e(this),a=e.extend(!0,{},e.fn.form.settings,t),s=e.extend({},e.fn.form.settings.defaults,n),c="."+a.namespace,l=("module-"+a.namespace,r.selector||""),u=(new Date).getTime(),d=[],f=arguments[0],m="string"==typeof f,g=[].slice.call(arguments,1);return r.each(function(){var n,t=e(this),p=e(this).find(a.selector.field),h=e(this).find(a.selector.group),v=e(this).find(a.selector.message),b=(e(this).find(a.selector.prompt),e(this).find(a.selector.submit)),y=[],x=this,C=t.data("module-"+a.namespace),w=a.namespace,k=a.metadata,z=a.className,T=a.error;n={initialize:function(){n.verbose("Initializing form validation",t,s,a),a.keyboardShortcuts&&p.on("keydown"+c,n.event.field.keydown),t.on("submit"+c,n.validate.form),p.on("blur"+c,n.event.field.change),b.on("click"+c,n.submit)},destroy:function(){t.off(w)},refresh:function(){p=t.find(a.selector.field)},submit:function(){n.verbose("Submitting form",t),t.submit()},event:{field:{keydown:function(t){var i=e(this),r=t.which,s={enter:13,escape:27};return r==s.escape&&(n.verbose("Escape key pressed blurring field"),i.blur()),r==s.enter&&i.is(a.selector.input)?(n.debug("Enter key pressed, submitting form"),b.addClass(z.down),i.one("keyup"+c,n.event.field.keyup),t.preventDefault(),!1):o},keyup:function(){n.verbose("Doing keyboard shortcut form submit"),b.removeClass(z.down),n.submit()},change:function(){var t=e(this),o=t.closest(h);console.log("here",a.on),o.hasClass(z.error)?(n.debug("Revalidating field",t,n.get.validation(t)),n.validate.field(n.get.validation(t))):"change"==a.on&&n.validate.field(n.get.validation(t))}}},get:{field:function(t){return n.verbose("Finding field with identifier",t),p.filter("#"+t).size()>0?p.filter("#"+t):p.filter('[name="'+t+'"]').size()>0?p.filter('[name="'+t+'"]'):p.filter("[data-"+k.validate+'="'+t+'"]').size()>0?p.filter("[data-"+k.validate+'="'+t+'"]'):e("<input/>")},validation:function(t){var o;return e.each(s,function(e,i){n.get.field(i.identifier).get(0)==t.get(0)&&(o=i)}),o||!1}},has:{field:function(e){return n.verbose("Checking for existence of a field with identifier",e),p.filter("#"+e).size()>0?!0:p.filter('[name="'+e+'"]').size()>0?!0:p.filter("[data-"+k.validate+'="'+e+'"]').size()>0?!0:!1}},add:{prompt:function(e,t){var o=n.get.field(e.identifier),i=o.closest(h),r=i.find(a.selector.prompt),s=0!==r.size();n.verbose("Adding inline validation prompt"),i.addClass(z.error),a.inlineError&&(s||(r=a.templates.prompt(t),r.appendTo(i).hide()),r.html(t[0]),r.is(":not(:visible)")&&r.fadeIn(a.animateSpeed))},errors:function(e){n.debug("Adding form error messages",e),v.html(a.templates.error(e))}},remove:{prompt:function(e){var t=n.get.field(e.identifier),o=t.closest(h),i=o.find(a.selector.prompt);o.removeClass(z.error),a.inlineError&&i.hide()}},validate:{form:function(o){var i=!0;y=[],e.each(s,function(e,t){n.validate.field(t)||(i=!1)}),i?(t.removeClass(z.error).addClass(z.success),e.proxy(a.onSuccess,this)(o)):(t.addClass(z.error),a.inlineError||n.add.errors(y),e.proxy(a.onFailure,this)(y))},field:function(t){var i=n.get.field(t.identifier),r=!0,s=[];return t.rules!==o&&e.each(t.rules,function(e,o){n.has.field(t.identifier)&&!n.validate.rule(t,o)&&(n.debug("Field is invalid",t.identifier,o.type),s.push(o.prompt),r=!1)}),r?(n.remove.prompt(t,s),e.proxy(a.onValid,i)(),!0):(y=y.concat(s),n.add.prompt(t,s),e.proxy(a.onInvalid,i)(s),!1)},rule:function(i,r){var s,c,l=n.get.field(i.identifier),u=r.type,d=l.val(),f=/\[(.*?)\]/i,m=f.exec(u),g=!0;return m!==o&&null!=m?(s=m[1],c=u.replace(m[0],""),g=e.proxy(a.rules[c],t)(d,s)):g="checked"==u?l.filter(":checked").size()>0:a.rules[u](d),g}},setting:function(n,t){return t===o?a[n]:(e.isPlainObject(n)?e.extend(!0,a,n):a[n]=t,o)},internal:function(t,i){return i===o?n[t]:(e.isPlainObject(t)?e.extend(!0,n,t):n[t]=i,o)},debug:function(){a.debug&&(a.performance?n.performance.log(arguments):n.debug=Function.prototype.bind.call(console.info,console,a.moduleName+":"))},verbose:function(){a.verbose&&a.debug&&(a.performance?n.performance.log(arguments):n.verbose=Function.prototyp