-
Notifications
You must be signed in to change notification settings - Fork 0
/
formProgressBar.jquery.min.js
1 lines (1 loc) · 3.22 KB
/
formProgressBar.jquery.min.js
1
$.fn.formProgressBar=function(e){var r=$.extend({},{readCount:!1,validClass:"valid",invalidClass:"error",percentCounting:!1,transitionTime:0,height:10,transitionType:"ease",parentElement:"body"},e);$(r.parentElement).prepend("<div id='jQueryProgressFormBar'><div></div></div>"),$("#jQueryProgressFormBar").css("position","fixed").css("top",0).css("left",0).css("width","100%").css("height",r.height).css("z-index",1e4),$("#jQueryProgressFormBar>div").css({WebkitTransition:"all "+r.transitionTime+"ms "+r.transitionType,MozTransition:"all "+r.transitionTime+"ms "+r.transitionType,MsTransition:"all "+r.transitionTime+"ms "+r.transitionType,OTransition:"all "+r.transitionTime+"ms "+r.transitionType,transition:"all "+r.transitionTime+"ms "+r.transitionType}).css("height",r.height),this.elementsRequied=function(){var e=$(this).find("input[required], textarea[required], select[required]").toArray();return arr=[],e.map(function(e){arr[e.name]=0,e.checked&&(arr[e.name]=1)}),arr},this.refresh=function(){t=this.elementsRequied()},this.renderBar=function(){var e=0,s=0,a=!1;for(var i in t)1==t[i]&&e++,-1==t[i]&&(a=!0),s++;var n=(e/s*100).toFixed(2);r.percentCounting&&$("#jQueryProgressFormBar>div").text(Math.round(n)+" %"),$("#jQueryProgressFormBar>div").css("width",n+"%"),1==a?$("#jQueryProgressFormBar>div").addClass("warn"):$("#jQueryProgressFormBar>div").removeClass("warn").removeClass("error")},this.bindElements=function(){var e=this.renderBar;if($(this).find("input[required], textarea[required], select[required]").change(function(){if(r.readCount)switch($(this).attr("type")){case"checkbox":case"radio":$(this).is(":checked")?t[$(this).attr("name")]=1:t[$(this).attr("name")]=-1}else switch($(this).prop("nodeName")){case"INPUT":switch($(this).attr("type")){case"text":""!=$(this).val()?t[$(this).attr("name")]=1:t[$(this).attr("name")]=-1;break;case"email":/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(String($(this).val()).toLowerCase())?t[$(this).attr("name")]=1:t[$(this).attr("name")]=-1;break;case"number":$.isNumeric($(this).val())?t[$(this).attr("name")]=1:t[$(this).attr("name")]=-1;break;case"checkbox":case"radio":$(this).is(":checked")?t[$(this).attr("name")]=1:t[$(this).attr("name")]=-1}break;case"SELECT":""!=$(this).val()?t[$(this).attr("name")]=1:t[$(this).attr("name")]=-1}e()}),r.readCount){var s=$(this).find("input[required], textarea[required], select[required]"),a=new MutationObserver(function(s){s.forEach(function(s){"class"===s.attributeName&&($(s.target).hasClass(r.validClass)?t[$(s.target).attr("name")]=1:$(s.target).hasClass(r.invalidClass)&&(t[$(s.target).attr("name")]=-1),e())})});s.map(function(e){a.observe(s[e],{attributes:!0})})}$(this).submit(function(e){if($("#jQueryProgressFormBar>div").hasClass("warn"))return $("#jQueryProgressFormBar>div").removeClass("warn").addClass("error"),void e.preventDefault();var r=0;for(var s in t)1==t[s]&&r++,length++;if(r!=length)return $("#jQueryProgressFormBar>div").removeClass("warn").addClass("error"),void e.preventDefault();$("#jQueryProgressFormBar>div").removeClass("warn").removeClass("error")})};var t=this.elementsRequied();return this.bindElements(),this},$.fn.formProgressBar;