-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.veryform.min.js
1 lines (1 loc) · 9.47 KB
/
jquery.veryform.min.js
1
!function(a){"use strict";function c(a){var b=".";return b+a+" "}var b={main:"veryform",elem:"veryform-item",wrap:"veryform-wrap",warning:"veryform-warning",positive:"veryform-true",negative:"veryform-false",failure:"veryform-failure",success:"veryform-success",js:"js-veryform"};void 0===window.veryform&&(window.veryform={}),window.veryform.classnames=b;var d=function(){var b={text:"/[^\\s]+/i",email:"/^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$/i",checkbox:1,textarea:"/[^\\s]+/i",radio:1,select:1,file:!0,password:"/[^\\s]+/i"},d={text:"nothing was written",email:"incorrect email",checkbox:"choose something",textarea:"nothing was written",radio:"choose something",select:"choose something",file:"choose the file",password:"incorrect password"},e=function(b,c){this.$instance,this.elems=a(),this.datas={},this.defaults={wrap:!0,displayWarnings:!0,maxFileSize:5e4,validateOnSubmit:!0,init:!0,autoinit:!1,on:"keyup",exclude:"",autocomplete:!1,regex:{},warnings:{}},this.updating=!1,this.elemsCounter=0,this.updateOptions(c),this.$instance=b,"string"==typeof b&&(this.$instance=a(b));var d=this.$instance.data("veryform");return d?d:(this.defaults.init&&this.init(),void 0)};return e.prototype.updateOptions=function(b){b?a.extend(this.defaults,b):"",this.updateData(this.elems)},e.prototype.init=function(){var b=this;b.$instance.find("input[type!=submit][type!=hidden][type!=reset], textarea, select").each(function(){b.add(a(this))}),b.setHandlers()},e.prototype.add=function(b){var c=this;b.each(function(){var b=a(this),d=b.attr("type"),e=b.attr("name");switch(d){case"radio":case"checkbox":if(!e)return b.remove(),void 0}c.updateData(b),c.defaults.autoinit&&(c.performAction(b),c.checkGlobalStatus())})},e.prototype.updateData=function(b){var c=this;b.each(function(){var b=a(this);c.updating=!0,c.setData(b),c.applyRegex(b),c.applyWarnings(b),b.removeAttr("required"),c.defaults.autocomplete?"":b.attr("autocomplete","off"),c.setShouldbe(b),c.valid(b),c.$instance.find(c.defaults.exclude).is(b)?c.exclude(b):c.elems=c.elems.is(b)?c.elems:c.elems.add(b),c.updating=!1})},e.prototype.setData=function(a){var b=this,c=a.data(window.veryform.classnames.main);if(!c){b.repairType(a);var d=a.attr("name"),e=a.attr("type"),f={},c={};f.id=b.elemsCounter,f.data=c,f.validOnStart=!1;for(var g in b.datas.elems)if(b.datas.elems[g].data.name==d)return b.datas.elems[g].data.sum++,f.data=b.datas.elems[g].data,b.datas.elems.push(f),a.data(window.veryform.classnames.main,f),b.applyHandler(a),b.elemsCounter++,void 0;c.elem=a,c.type=e?e:a[0].localName,c.name=d,c.valid=!1,c.sum=1,c.is=0,c.wrapped=!1,c.shouldbe=0,c.out=!1,f.eventHandler=void 0,f.eventName=void 0,b.datas.elems?"":b.datas.elems=[],b.datas.elems.push(f),a.data(window.veryform.classnames.main,f),b.elemsCounter++,b.applyHandler(a),a.addClass(window.veryform.classnames.elem)}},e.prototype.applyRegex=function(a){var c=this,d=a.data(window.veryform.classnames.main),e=!1;for(var f in c.defaults.regex)c.$instance.find(f).is(a)&&(c.setDataParameter(a,"regex",c.defaults.regex[f]),e=!0);e?"":c.setDataParameter(a,"regex",b[d.data.type]),c.repairRegex(a)},e.prototype.repairRegex=function(a){var c=this,d=a.data(window.veryform.classnames.main),e=d.data.type,f=d.data.regex;switch(e){case"text":case"textarea":case"email":case"password":f.match(/^\/.*\/[img]{1,3}/g)?"":c.setDataParameter(a,"regex",b[e]);break;case"radio":case"checkbox":case"select":isNaN(f)?c.setDataParameter(a,"regex",b[e]):"";break;case"file":f!==!0&&f!==!1?c.setDataParameter(a,"regex",b[e]):""}},e.prototype.applyWarnings=function(a){var b=this,c=a.data(window.veryform.classnames.main),e=!1;for(var f in b.defaults.warnings)b.$instance.find(f).is(a)&&(b.setDataParameter(a,"warning",b.defaults.warnings[f]),e=!0);e?"":b.setDataParameter(a,"warning",d[c.data.type]),b.wrap(a),b.appendWarning(a)},e.prototype.exclude=function(b){var c=this;b.each(function(){var b=a(this),d=b.data(window.veryform.classnames.main);if(d){c.off(b,d.eventName,d.eventHandler),d.data.sum--;for(var e in c.datas.elems){var f=parseInt(e);if(c.datas.elems[f].id==d.id){c.datas.elems.splice(f,1);break}}c.unwrap(b),b.removeData(window.veryform.classnames.main),c.elems=c.elems.not(b)}})},e.prototype.setDataParameter=function(a,b,c){var d=a.data(window.veryform.classnames.main);d.data[b]=c},e.prototype.repairType=function(a){"input"!=a[0].localName||a.attr("type")?"":a.attr("type","text")},e.prototype.setShouldbe=function(a){var b=this,c=a.data(window.veryform.classnames.main);switch(c.data.type){case"radio":case"checkbox":c.data.shouldbe=c.data.regex;break;case"select":c.data.shouldbe=c.data.regex;break;case"file":c.data.shouldbe=1==c.data.regex?1:0,c.data.maxFileSize=b.defaults.maxFileSize;break;default:c.data.shouldbe=1}},e.prototype.valid=function(a){var b=this,c=a.data(window.veryform.classnames.main),d=c.validOnStart;switch(c.data.type){case"text":case"email":case"password":case"textarea":var e=c.data.regex.match(/\/(.*)\/(.*)/),f=a.val(),g=new RegExp(e[1],e[2]);c.data.is=f.match(g)?Math.min(++c.data.is,1):Math.max(--c.data.is,0);break;case"radio":a.is(":checked")?c.data.is=1:b.updating?"":c.data.is=0;break;case"checkbox":a.is(":checked")?b.updating&&d?"":c.data.is++:b.updating?"":c.data.is--;break;case"select":var f=a.val();c.data.is=f.match(/.+/i)?1:0;break;case"file":var f=a.val();c.data.is=f.length>0&&c.data.recentFileSize&&c.data.recentFileSize<=c.data.maxFileSize?1:0}c.validOnStart=!0,b.checkStatus(a)},e.prototype.checkStatus=function(a){var b=a.data(window.veryform.classnames.main);b.data.valid=b.data.is>=b.data.shouldbe?!0:!1},e.prototype.performAction=function(a){var b=a.data(window.veryform.classnames.main);b&&(b.data.valid?a.trigger("ontrue"):a.trigger("onfalse"))},e.prototype.wrap=function(b){var c=this,d=b.data(window.veryform.classnames.main),e=d.data.type,f=d.data.name,g=void 0;if(c.defaults.wrap&&!d.data.wrapped){var h=a();switch(e){case"radio":case"checkbox":g=a("[name="+f+"]");break;default:g=b}var i=a('<div class="'+window.veryform.classnames.wrap+" veryform_"+d.data.name+" veryform_"+d.data.type+'" />');h=h.add(g),g.each(function(){h=h.add(a(this).parents("form").find("label[for="+a(this).attr("id")+"]"))}),h.wrapAll(i),d.data.wrapped=!0}},e.prototype.unwrap=function(b){b.each(function(){var b=a(this),d=b.data(window.veryform.classnames.main);if(d.data.wrapped){var e=d.data.name,f=b.parents(c(window.veryform.classnames.wrap)),g=a();a("[name="+e+"]").each(function(){g=g.add(a(this)),g=g.add(a(this).parents("form").find("label[for="+a(this).attr("id")+"]"))}),f.find(c(window.veryform.classnames.warning)).remove(),g.unwrap(),d.data.wrapped=!1}})},e.prototype.appendWarning=function(b){var e=b.data(window.veryform.classnames.main),f=a("<span>").addClass(window.veryform.classnames.warning).text(e.data.warning).css("display","none"),g=b.parents(c(window.veryform.classnames.wrap));g.find(c(window.veryform.classnames.warning)).length?g.find(c(window.veryform.classnames.warning)).html(e.data.warning):g.append(f)},e.prototype.on=function(a,b,c){a.on(b,c)},e.prototype.off=function(a,b,c){a.off(b,c)},e.prototype.out=function(b){b.each(function(){var b=a(this),c=b.data(window.veryform.classnames.main);c.data.out=!0})},e.prototype.in=function(b){b.each(function(){var b=a(this),c=b.data(window.veryform.classnames.main);c.data.out=!1})},e.prototype.applyHandler=function(a){var b=this,d=a.data(window.veryform.classnames.main),e=d.data.type,f=void 0,g=void 0;switch(e){case"radio":case"checkbox":case"select":g="change init",f=function(){b.valid(a),b.performAction(a),b.checkGlobalStatus()};break;case"text":case"email":case"password":case"textarea":g=b.defaults.on+" init",f=function(c){var d=c.which;9!=d&&(b.valid(a),b.performAction(a),b.checkGlobalStatus())};break;case"file":g="change click init",f=function(){var c=this.files[0]?this.files[0].size/1024:void 0;d.data.recentFileSize=c,b.valid(a),b.performAction(a),b.checkGlobalStatus()}}d.eventName=g,d.eventHandler=f,b.on(a,g,f),b.on(a,"ontrue",function(){a.parents(c(window.veryform.classnames.wrap)).removeClass(window.veryform.classnames.negative).addClass(window.veryform.classnames.positive),b.defaults.displayWarnings&&a.parents(c(window.veryform.classnames.wrap)).find(c(window.veryform.classnames.warning)).hide(300)}),b.on(a,"onfalse",function(){a.parents(c(window.veryform.classnames.wrap)).removeClass(window.veryform.classnames.positive).addClass(window.veryform.classnames.negative),b.defaults.displayWarnings&&a.parents(c(window.veryform.classnames.wrap)).find(c(window.veryform.classnames.warning)).show(300)})},e.prototype.setHandlers=function(){var b=this;b.$instance.submit(function(a){if(b.defaults.validateOnSubmit){var c=!1,d=!1;for(var e in b.datas.elems)if(!b.datas.elems[e].data.valid){d=!0;var f=b.datas.elems[e].data.elem;f.trigger("onfalse"),c||(f.focus(),c=!0)}d&&(console.log("ole"),a.preventDefault(),a.stopPropagation(),a.stopImmediatePropagation())}})},e.prototype.checkGlobalStatus=function(){var a=this,b=!0;for(var c in a.datas.elems){var d=a.datas.elems[c].data.valid,e=a.datas.elems[c].data.out;if(!d&&!e)return b=!1,a.$instance.removeClass(window.veryform.classnames.success).addClass(window.veryform.classnames.failure),void 0}a.$instance.removeClass(window.veryform.classnames.failure).addClass(window.veryform.classnames.success)},e}();window.Veryform=d;var e=a(c(window.veryform.classnames.js));e.each(function(){var b=a(this),c=JSON.parse(b.attr("data-veryform-options")),e=new d(b,c);a(this).data(window.veryform.classnames.main,e)}),a.fn.veryform=function(b){a(this).each(function(){var c=a(this),e=new d(c,b);a(this).addClass(window.veryform.classnames.js),a(this).data(window.veryform.classnames.main,e)})}}(jQuery);