1 line
3.0 KiB
JavaScript
1 line
3.0 KiB
JavaScript
|
!function(t){t.add("module","validate",{init:function(t,e){this.app=t,this.$win=t.$win,this.progress=t.progress,this.response=t.response;var s={errorClass:"is-error",send:!0,trigger:!1,shortcut:!1,progress:!1};this.context=e,this.params=e.getParams(s),this.$element=e.getElement()},start:function(){this._disableDefaultValidation(),this._enableShortcut(),this.params.trigger?this._startTrigger():this._startSubmit()},stop:function(){this.enableButtons(),this.clear(),this.$element.off(".kube.validate"),this.$win.off(".kube.validate"),this.$trigger&&this.$trigger.off(".")},clear:function(){this.$element.find("."+this.params.errorClass).each(this._clearError.bind(this))},disableButtons:function(){this.$element.find("button").attr("disabled",!0)},enableButtons:function(){this.$element.find("button").removeAttr("disabled")},_build:function(t){return t.preventDefault(),this.params.send?this._send():this.app.broadcast("validate.send",this),!1},_send:function(){return this.params.progress&&this.progress.show(),this.disableButtons(),this._saveCodeMirror(),this.app.broadcast("validate.send",this),t.ajax.post({url:this.$element.attr("action"),data:this.$element.serialize(),success:this._parse.bind(this)}),!1},_parse:function(t){this.enableButtons(),this.clear(),this.params.progress&&this.progress.hide();var e=this.response.parse(t);e?"undefined"!=typeof e.type&&"error"===e.type?(this._setErrors(e.errors),this.app.broadcast("validate.error",this,e.errors)):this.app.broadcast("validate.success",this,e):this.app.broadcast("validate.error",this,e)},_setErrors:function(t){for(var e in t){var s=t[e],i=this.$element.find("[name="+e+"]");0!==i.length&&(i.addClass(this.params.errorClass),this._setFieldEvent(i,e),""!==s&&this._showErrorText(e,s))}},_setFieldEvent:function(t,e){var s=this._getFieldEventName(t);t.on(s+".kube.validate",function(){this._clearError(t)}.bind(this))},_showErrorText:function(t,e){var s=this.$element.find("#"+t+"-validation-error");s.addClass(this.params.errorClass),s.html(e),s.removeClass("is-hidden")},_getFieldEventName:function(t){return"SELECT"===t.get().tagName||"checkbox"===t.attr("type")||"radio"===t.attr("type")?"change":"keyup"},_clearError:function(e){var s=t.dom(e),i=this.$element.find("#"+s.attr("name")+"-validation-error");i.removeClass(this.params.errorClass),i.html(""),i.addClass("is-hidden"),s.removeClass(this.params.errorClass).off(".kube.validate")},_saveCodeMirror:function(){t.dom(".CodeMirror").each(function(t){t.CodeMirror.save()})},_disableDefaultValidation:function(){this.$element.attr("novalidate","novalidate")},_enableShortcut:function(){this.params.shortcut&&this.$win.on("keydown.kube.validate",this._handleShortcut.bind(this))},_handleShortcut:function(t){return(t.ctrlKey||t.metaKey)&&83===t.which?(t.preventDefault(),this._send()):!0},_startTrigger:function(){this.$trigger=$(this.opts.trigger),this.$element.on("submit",function(){return!1}),this.$trigger.off(".kube.validate"),this.$trigger.on("click.kube.validate",this._build.bind(this))},_startSubmit:function(){this.$element.on("submit.kube.validate",this._build.bind(this))}})}(Kube);
|