forked from pono4ka/nod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnod.min.js
5 lines (5 loc) · 8.15 KB
/
nod.min.js
1
2
3
4
5
/* jquery-nod - v1.0.4 - 2013-06-29
* https://github.com/casperin/nod
* Gorm Casper: Licensed MIT
*/
(function($){var Checker,__bind=function(t,e){return function(){return t.apply(e,arguments)}};Checker=function(){function t(t,e){this.run=__bind(this.run,this);var s;s=e[0],this.metric=e[1],this.getVal=this.makeGetVal(t,s)}return t.prototype.run=function(){return this.verify(this.metric,this.getVal())},t.prototype.makeGetVal=function(t,e){var s,r,i;return i=t.attr("type"),"checkbox"===i?function(){return t.is(":checked")}:"radio"===i?(r=t.attr("name"),function(){return $('[name="'+r+'"]').filter(":checked").val()}):"one-of"===this.metric?(s=$(e),function(){return s.map(function(){return $.trim(this.value)}).get().join("")}):function(){return $.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=$.map(t.split(":"),$.trim),i=n[0],s=n[1],r=n[2],"same-as"===i&&1!==$(s).length)throw Error("same-as selector must target one and only one element");if(!e&&"presence"!==i&&"one-of"!==i)return!0;switch(i){case"presence":return!!e;case"one-of":return!!e;case"exact":return e===s;case"not":return e!==s;case"same-as":return e===$(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 Listener,__bind=function(t,e){return function(){return t.apply(e,arguments)}};Listener=function(){function Listener(t,e,s){this.get=e,this.field=s,this.change_status=__bind(this.change_status,this),this.runCheck=__bind(this.runCheck,this),this.delayedCheck=__bind(this.delayedCheck,this),this.events=__bind(this.events,this),this.$el=$(t),this.delayId="",this.status=null,this.checker=new Checker(this.$el,this.field),this.msg=new Msg(this.$el,this.get,this.field),this.events()}return Listener.prototype.events=function(){return"radio"===this.$el.attr("type")?$('[name="'+this.$el.attr("name")+'"]').on("change",this.runCheck):(this.$el.on("change",this.runCheck),this.$el.on("blur",this.runCheck),"one-of"===this.field[1]&&$(window).on("nod-run-one-of",this.runCheck),this.get.delay?this.$el.on("keyup",this.delayedCheck):void 0)},Listener.prototype.delayedCheck=function(){return clearTimeout(this.delayId),this.delayId=setTimeout(this.runCheck,this.get.delay)},Listener.prototype.runCheck=function(){return $.when(this.checker.run()).then(this.change_status)},Listener.prototype.change_status=function(status){var isCorrect;try{status=eval(status)}catch(_error){}return isCorrect=!!status,this.status!==isCorrect?(this.status=isCorrect,this.msg.toggle(this.status),$(this).trigger("nod_toggle"),"one-of"===this.field[1]&&status?$(window).trigger("nod-run-one-of"):void 0):void 0},Listener}();var Msg,__bind=function(t,e){return function(){return t.apply(e,arguments)}};Msg=function(){function t(t,e,s){this.$el=t,this.get=e,this.createShowMsg=__bind(this.createShowMsg,this),this.toggle=__bind(this.toggle,this),this.createMsg=__bind(this.createMsg,this),this.$msg=this.createMsg(s[2]),this.showMsg=this.createShowMsg()}return t.prototype.createMsg=function(t){return $("<span/>",{html:t,"class":this.get.helpSpanDisplay+" "+this.get.errorClass})},t.prototype.toggle=function(t){return t?this.$msg.remove():(this.showMsg(),this.get.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 $(window).trigger("nod_error_fired",{el:this.$el,msg:this.$msg.html()})},t}();var Nod,__bind=function(t,e){return function(){return t.apply(e,arguments)}};Nod=function(){function t(t,e,s){this.form=t,this.formIsErrorFree=__bind(this.formIsErrorFree,this),this.submitForm=__bind(this.submitForm,this),this.toggleSubmitBtnText=__bind(this.toggleSubmitBtnText,this),this.toggleSubmitBtn=__bind(this.toggleSubmitBtn,this),this.toggleGroupClass=__bind(this.toggleGroupClass,this),this.toggle_status=__bind(this.toggle_status,this),this.massCheck=__bind(this.massCheck,this),this.listenForEnter=__bind(this.listenForEnter,this),this.events=__bind(this.events,this),this.createListeners=__bind(this.createListeners,this),e&&(this.form[0].__nod=this,this.get=$.extend({delay:700,disableSubmitBtn:!0,helpSpanDisplay:"help-inline",groupClass:"error",submitBtnSelector:"[type=submit]",metricsSplitter:":",errorPosClasses:[".help-inline",".add-on","button",".input-append"],silentSubmit:!1,broadcastError:!1,errorClass:"nod_msg",groupSelector:".control-group"},s),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,r,i,n,o,h,u;for(r=[],i=0,o=t.length;o>i;i++)for(s=t[i],3!==s.length&&this["throw"]("field",s),u=this.form.find(s[0]),n=0,h=u.length;h>n;n++)e=u[n],r.push(new Listener(e,this.get,s));return r},t.prototype.events=function(){var t,e,s,r;for(r=this.listeners,e=0,s=r.length;s>e;e++)t=r[e],$(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;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(),$.when.apply($,e).then(this.submitForm).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.submitForm=function(){var t;if(this.formIsErrorFree())return this.get.silentSubmit?(t=$(this.form),t.trigger("silentSubmit",t.serialize())):this.form.submit()},t.prototype.formIsErrorFree=function(){return!$(this.listeners).filter(function(){return null===this.status&&this.runCheck(),!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}(),$.fn.nod=function(t,e){return t||e?(new Nod(this,t,e),this):this[0].__nod}})(jQuery);