forked from casperin/nod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnod.min.js
5 lines (5 loc) · 7.38 KB
/
nod.min.js
1
2
3
4
5
/* jquery-nod - v1.0.1 - 2013-04-13
* https://github.com/casperin/nod
* Gorm Casper: Licensed MIT
*/
(function(t){var e,s=function(t,e){return function(){return t.apply(e,arguments)}};e=function(){function t(t,e){this.metric=e,this.run=s(this.run,this),this.getVal=this.makeGetVal(t)}return t.prototype.run=function(){return this.verify(this.metric,this.getVal())},t.prototype.makeGetVal=function(t){var e,s;return s=t.attr("type"),"checkbox"===s?function(){return t.is(":checked")}:"radio"===s?(e=t.attr("name"),function(){return jQuery("[name="+e+"]").filter(":checked").val()}):function(){return jQuery.trim(t.val())}},t.prototype.verify=function(t,e){var s,r,i,n;if(t&&t.constructor&&t.call&&t.apply)return t(e);if(t instanceof RegExp)return t.test(e);if(n=jQuery.map(t.split(":"),jQuery.trim),i=n[0],s=n[1],r=n[2],"same-as"===i&&1!==jQuery(s).length)throw Error("same-as selector must target one and only one element");if(!e&&"presence"!==i)return!0;switch(i){case"presence":return!!e;case"exact":return e===s;case"not":return e!==s;case"same-as":return e===jQuery(s).val();case"min-num":return+e>=+s;case"max-num":return+s>=+e;case"between-num":return+e>=+s&&+r>=+e;case"min-length":return e.length>=+s;case"max-length":return+s>=e.length;case"exact-length":return e.length===+s;case"between":return e.length>=+s&&+r>=e.length;case"integer":return/^\s*\d+\s*$/.test(e);case"float":return/^[-+]?[0-9]+(\.[0-9]+)?$/.test(e);case"email":return this.email(e);default:throw Error("I don't know "+i+", sorry.")}},t.prototype.email=function(t){var e;return e=/^([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22))*\x40([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d))*$/,e.test(t)},t}();var r,s=function(t,e){return function(){return t.apply(e,arguments)}};r=function(){function t(t,r,n,o){this.get=r,this.change_status=s(this.change_status,this),this.runCheck=s(this.runCheck,this),this.delayedCheck=s(this.delayedCheck,this),this.events=s(this.events,this),this.$el=jQuery(t),this.delayId="",this.status=!0,this.checker=new e(this.$el,n),this.msg=new i(this.$el,this.get,o),this.events()}return t.prototype.events=function(){return"radio"===this.$el.attr("type")?jQuery("[name="+this.$el.attr("name")+"]").on("change",this.runCheck):(this.$el.on("change",this.runCheck),this.$el.on("blur",this.runCheck),this.get.delay?this.$el.on("keyup",this.delayedCheck):void 0)},t.prototype.delayedCheck=function(){return clearTimeout(this.delayId),this.delayId=setTimeout(this.runCheck,this.get.delay)},t.prototype.runCheck=function(){return jQuery.when(this.checker.run()).then(this.change_status)},t.prototype.change_status=function(t){var e;return e=!!t,this.status!==e?(this.status=e,this.msg.toggle(this.status),jQuery(this).trigger("nod_toggle")):void 0},t}();var i,s=function(t,e){return function(){return t.apply(e,arguments)}};i=function(){function t(t,e,r){this.$el=t,this.get=e,this.createShowMsg=s(this.createShowMsg,this),this.toggle=s(this.toggle,this),this.createMsg=s(this.createMsg,this),this.$msg=this.createMsg(r),this.showMsg=this.createShowMsg()}return t.prototype.createMsg=function(t){return jQuery("<span/>",{html:t,"class":this.get.helpSpanDisplay+" "+this.get.errorClass})},t.prototype.toggle=function(t){return t?this.$msg.remove():(this.showMsg(),this.broadcastError?this.broadcast():void 0)},t.prototype.createShowMsg=function(){var t,e;return e=this.$el.attr("type"),"checkbox"===e||"radio"===e?function(){return this.$el.parent().append(this.$msg)}:(t=this.findPos(this.$el),function(){return t.after(this.$msg)})},t.prototype.findPos=function(t){return this.elHasClass("parent",t)?this.findPos(t.parent()):this.elHasClass("next",t)?this.findPos(t.next()):t},t.prototype.elHasClass=function(t,e){var s,r,i,n;for(n=this.get.errorPosClasses,r=0,i=n.length;i>r;r++)if(s=n[r],e[t](s).length)return!0;return!1},t.prototype.broadcast=function(){return jQuery(window).trigger("nod_error_fired",{el:this.$el,msg:this.$msg.html()})},t}();var n,s=function(t,e){return function(){return t.apply(e,arguments)}};n=function(){function t(t,e,r){this.form=t,this.formIsErrorFree=s(this.formIsErrorFree,this),this.toggleSubmitBtnText=s(this.toggleSubmitBtnText,this),this.toggleSubmitBtn=s(this.toggleSubmitBtn,this),this.toggleGroupClass=s(this.toggleGroupClass,this),this.toggle_status=s(this.toggle_status,this),this.massCheck=s(this.massCheck,this),this.listenForEnter=s(this.listenForEnter,this),this.events=s(this.events,this),this.createListeners=s(this.createListeners,this),e&&(this.get=jQuery.extend({delay:700,disableSubmitBtn:!0,helpSpanDisplay:"help-inline",groupClass:"error",submitBtnSelector:"[type=submit]",metricsSplitter:":",errorPosClasses:[".help-inline",".add-on","button",".input-append"],broadcastError:!1,errorClass:"nod_msg",groupSelector:".control-group"},r),this.listeners=this.createListeners(e),this.submit=this.form.find(this.get.submitBtnSelector),this.checkIfElementsExist(this.form,this.submit,this.get.disableSubmitBtn),this.events())}return t.prototype.createListeners=function(t){var e,s,i,n,o,h,u,a,l,c,g;for(i=[],u=0,l=t.length;l>u;u++)for(s=t[u],3!==s.length&&this["throw"]("field",s),h=s[0],n=s[1],o=s[2],g=this.form.find(h),a=0,c=g.length;c>a;a++)e=g[a],i.push(new r(e,this.get,n,o));return i},t.prototype.events=function(){var t,e,s,r;for(r=this.listeners,e=0,s=r.length;s>e;e++)t=r[e],jQuery(t).on("nod_toggle",this.toggle_status);return this.submit.length?this.submit.on("click",this.massCheck):this.form.on("keyup",this.listenForEnter)},t.prototype.listenForEnter=function(t){return 13===t.keyCode?this.massCheck():void 0},t.prototype.massCheck=function(t){var e,s,r,i,n,o=this;for(null!=t&&t.preventDefault(),e=[],n=this.listeners,r=0,i=n.length;i>r;r++)s=n[r],e.push(s.runCheck());return this.toggleSubmitBtnText(),jQuery.when.apply(window,e).then(function(){return o.formIsErrorFree()?o.form.submit():void 0}).then(this.toggleSubmitBtnText)},t.prototype.toggle_status=function(t){return this.toggleGroupClass(t.target.$el.parents(this.get.groupSelector)),this.get.disableSubmitBtn?this.toggleSubmitBtn():void 0},t.prototype.toggleGroupClass=function(t){return t.find("."+this.get.errorClass).length?t.addClass(this.get.groupClass):t.removeClass(this.get.groupClass)},t.prototype.toggleSubmitBtn=function(){return this.formIsErrorFree()?this.submit.removeClass("disabled").removeAttr("disabled"):this.submit.addClass("disabled").attr("disabled","disabled")},t.prototype.toggleSubmitBtnText=function(){var t;return t=this.submit.attr("data-loading-text"),t?(this.submit.attr("data-loading-text",this.submit.html()),this.submit.html(t)):void 0},t.prototype.formIsErrorFree=function(){return!jQuery(this.listeners).filter(function(){return!this.status}).length},t.prototype.checkIfElementsExist=function(t,e,s){return t.selector&&t.length||this["throw"]("form",t),!e.length&&s?this["throw"]("submit",e):void 0},t.prototype["throw"]=function(t,e){var s;switch(t){case"form":s="Couldn't find form: ";break;case"submit":s="Couldn't find submit button: ";break;case"field":s="Metrics for each field must have three parts: "}throw Error(s+e)},t}(),t.fn.nod=function(t,e){return new n(this,t,e),this}})(jQuery);