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.
1 lines
6.9 KiB
1 lines
6.9 KiB
!function(a,b,c,d){a.fn.form=function(b,c){var e,f=a(this),g=a.extend(!0,{},a.fn.form.settings,c),h=a.extend({},a.fn.form.settings.defaults,b),i="."+g.namespace,j=("module-"+g.namespace,f.selector||""),k=(new Date).getTime(),l=[],m=arguments[0],n="string"==typeof m,o=[].slice.call(arguments,1);return f.each(function(){var b,c=a(this),p=a(this).find(g.selector.field),q=a(this).find(g.selector.group),r=a(this).find(g.selector.message),s=(a(this).find(g.selector.prompt),a(this).find(g.selector.submit)),t=[],u=this,v=c.data("module-"+g.namespace),w=g.namespace,x=g.metadata,y=g.className,z=g.error;b={initialize:function(){b.verbose("Initializing form validation",c,h,g),g.keyboardShortcuts&&p.on("keydown"+i,b.event.field.keydown),c.on("submit"+i,b.validate.form),p.on("blur"+i,b.event.field.change),s.on("click"+i,b.submit)},destroy:function(){c.off(w)},refresh:function(){p=c.find(g.selector.field)},submit:function(){b.verbose("Submitting form",c),c.submit()},event:{field:{keydown:function(c){var d=a(this),e=c.which,f={enter:13,escape:27};return e==f.escape&&(b.verbose("Escape key pressed blurring field"),d.blur()),e==f.enter&&d.is(g.selector.input)?(b.debug("Enter key pressed, submitting form"),s.addClass(y.down),d.one("keyup"+i,b.event.field.keyup),c.preventDefault(),!1):void 0},keyup:function(){b.verbose("Doing keyboard shortcut form submit"),s.removeClass(y.down),b.submit()},change:function(){var c=a(this),d=c.closest(q);d.hasClass(y.error)?(b.debug("Revalidating field",c,b.get.validation(c)),b.validate.field(b.get.validation(c))):"change"==g.on&&b.validate.field(b.get.validation(c))}}},get:{field:function(c){return b.verbose("Finding field with identifier",c),p.filter("#"+c).size()>0?p.filter("#"+c):p.filter('[name="'+c+'"]').size()>0?p.filter('[name="'+c+'"]'):p.filter("[data-"+x.validate+'="'+c+'"]').size()>0?p.filter("[data-"+x.validate+'="'+c+'"]'):a("<input/>")},validation:function(c){var d;return a.each(h,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),p.filter("#"+a).size()>0?!0:p.filter('[name="'+a+'"]').size()>0?!0:p.filter("[data-"+x.validate+'="'+a+'"]').size()>0?!0:!1}},add:{prompt:function(a,c){var d=b.get.field(a.identifier),e=d.closest(q),f=e.find(g.selector.prompt),h=0!==f.size();b.verbose("Adding inline validation prompt"),e.addClass(y.error),g.inlineError&&(h||(f=g.templates.prompt(c),f.appendTo(e).hide()),f.html(c[0]),f.is(":not(:visible)")&&f.fadeIn(g.animateSpeed))},errors:function(a){b.debug("Adding form error messages",a),r.html(g.templates.error(a))}},remove:{prompt:function(a){var c=b.get.field(a.identifier),d=c.closest(q),e=d.find(g.selector.prompt);d.removeClass(y.error),g.inlineError&&e.hide()}},validate:{form:function(d){var e=!0;return t=[],a.each(h,function(a,c){b.validate.field(c)||(e=!1)}),e?(b.debug("Form has no validation errors, submitting"),c.removeClass(y.error).addClass(y.success),a.proxy(g.onSuccess,this)(d),void 0):(b.debug("Form has errors"),c.addClass(y.error),g.inlineError||b.add.errors(t),a.proxy(g.onFailure,this)(t))},field:function(c){var e=b.get.field(c.identifier),f=!0,h=[];return c.rules!==d&&a.each(c.rules,function(a,d){b.has.field(c.identifier)&&!b.validate.rule(c,d)&&(b.debug("Field is invalid",c.identifier,d.type),h.push(d.prompt),f=!1)}),f?(b.remove.prompt(c,h),a.proxy(g.onValid,e)(),!0):(t=t.concat(h),b.add.prompt(c,h),a.proxy(g.onInvalid,e)(h),!1)},rule:function(e,f){var h,i,j=b.get.field(e.identifier),k=f.type,l=j.val(),m=/\[(.*?)\]/i,n=m.exec(k),o=!0;return n!==d&&null!==n?(h=n[1],i=k.replace(n[0],""),o=a.proxy(g.rules[i],c)(l,h)):o="checked"==k?j.filter(":checked").size()>0:g.rules[k](l),o}},setting:function(b,c){return c===d?g[b]:(a.isPlainObject(b)?a.extend(!0,g,b):g[b]=c,void 0)},internal:function(c,e){return e===d?b[c]:(a.isPlainObject(c)?a.extend(!0,b,c):b[c]=e,void 0)},debug:function(){g.debug&&(g.performance?b.performance.log(arguments):b.debug=Function.prototype.bind.call(console.info,console,g.moduleName+":"))},verbose:function(){g.verbose&&g.debug&&(g.performance?b.performance.log(arguments):b.verbose=Function.prototype.bind.call(console.info,console,g.moduleName+":"))},error:function(){b.error=Function.prototype.bind.call(console.log,console,g.moduleName+":")},performance:{log:function(a){var c,d,e;g.performance&&(c=(new Date).getTime(),e=k||c,d=c-e,k=c,l.push({Element:u,Name:a[0],Arguments:a[1]||"None","Execution Time":d}),clearTimeout(b.performance.timer),b.performance.timer=setTimeout(b.performance.display,100))},display:function(){var b=g.moduleName,c=(g.moduleName+": "+j+"("+f.size()+" elements)",0);j&&(b+=" Performance ("+j+")"),(console.group!==d||console.table!==d)&&l.length>0&&(console.groupCollapsed(b),console.table?(a.each(l,function(a,b){c+=b["Execution Time"]}),console.table(l)):a.each(l,function(a,b){c+=b["Execution Time"],console.log(b.Name+": "+b["Execution Time"]+"ms")}),console.log("Total Execution Time:",c+"ms"),console.groupEnd(),l=[],k=!1)}},invoke:function(c,e,f){var g,h;return e=e||o,f=u||f,"string"==typeof c&&v!==d&&(c=c.split("."),g=c.length-1,a.each(c,function(c,e){a.isPlainObject(v[e])&&c!=g?v=v[e]:v[e]!==d?h=v[e]:b.error(z.method)})),a.isFunction(h)?(v.verbose("Executing invoked function",h),h.apply(f,e)):h||!1}},n?(v===d&&b.initialize(),e=b.invoke(m)):(v!==d&&b.destroy(),b.initialize())}),e?e:this},a.fn.form.settings={moduleName:"Form",debug:!0,verbose:!0,performance:!1,namespace:"validate",keyboardShortcuts:!0,on:"submit",animateSpeed:150,inlineError:!1,onValid:function(){},onInvalid:function(){},onSuccess:function(){return!0},onFailure:function(){return!1},metadata:{validate:"validate"},selector:{message:".error.message",field:"input, textarea, select",group:".field",input:"input",prompt:".prompt",submit:".submit"},className:{error:"error",success:"success",down:"down",label:"ui label prompt"},error:{method:"The method you called is not defined."},templates:{error:function(b){var c='<ul class="list">';return a.each(b,function(a,b){c+="<li>"+b+"</li>"}),c+="</ul>",a(c)},prompt:function(b){return a("<div/>").addClass("ui red pointing prompt label").html(b[0])}},rules:{empty:function(a){return!(a===d||""===a)},email:function(a){var b=new RegExp("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?");return b.test(a)},length:function(a,b){return a!==d?a.length>=b:!1},not:function(a,b){return a!=b},is:function(a,b){return a==b},maxLength:function(a,b){return a!==d?a.length<=b:!1},match:function(b,c){var e,f=a(this);return f.find("#"+c).size()>0?e=f.find("#"+c).val():f.find("[name="+c+"]").size()>0?e=f.find("[name="+c+"]").val():f.find('[data-validate="'+c+'"]').size()>0&&(e=f.find('[data-validate="'+c+'"]').val()),e!==d?b.toString()==e.toString():!1},url:function(a){var b=/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;return b.test(a)}}}}(jQuery,window,document);
|