wmdeit_kaform/addons/kube-addons.min.js

2 lines
37 KiB
JavaScript
Raw Normal View History

2020-02-20 13:45:47 +00:00
!function(t){t.add("module","autocomplete",{init:function(t,e){this.app=t,this.$doc=t.$doc,this.$win=t.$win,this.$body=t.$body,this.animate=t.animate;var i={url:!1,min:2,labelClass:!1,target:!1,param:!1};this.context=e,this.params=e.getParams(i),this.$element=e.getElement(),this.$target=e.getTarget()},start:function(){this._build(),this.timeout=null,this.$element.on("keyup.kube.autocomplete",this._open.bind(this))},stop:function(){this.$box.remove(),this.$element.off(".kube.autocomplete"),this.$doc.off(".kube.autocomplete"),this.$win.off(".kube.autocomplete")},_build:function(){if(this.$box=t.dom("<div />"),this.$box.addClass("autocomplete"),this.$box.addClass("is-hidden"),this.$body.append(this.$box),this.$target&&!this._isInputTarget()){this.$target.addClass("autocomplete-labels");var e=this.$target.find(".close");e.on("click",this._removeLabel.bind(this))}},_open:function(t){t&&t.preventDefault(),clearTimeout(this.timeout);var e=this.$element.val();e.length>=this.params.min?(this._resize(),this.$win.on("resize.kube.autocomplete",this._resize.bind(this)),this.$doc.on("click.kube.autocomplete",this._close.bind(this)),this.$box.addClass("is-open"),this._listen(t)):this._close(t)},_close:function(t){t&&t.preventDefault(),this.$box.removeClass("is-open"),this.$box.addClass("is-hidden"),this.$doc.off(".kube.autocomplete"),this.$win.off(".kube.autocomplete")},_getPlacement:function(t,e){return this.$doc.height()-(t.top+e)<this.$box.height()?"top":"bottom"},_resize:function(){this.$box.width(this.$element.width())},_getParamName:function(){return this.params.param?this.params.param:this.$element.attr("name")},_getTargetName:function(){var t=this.$target.attr("data-name");return t?t:this.$target.attr("id")},_lookup:function(){var e=this._getParamName()+"="+this.$element.val();t.ajax.post({url:this.params.url,data:e,success:this._complete.bind(this)})},_complete:function(e){if(this.$box.html(""),0===e.length)return this._close();for(var i=0;i<e.length;i++){var s=t.dom("<a>");s.attr("href","#"),s.attr("rel",e[i].id),s.html(e[i].name),s.on("click",this._set.bind(this)),this.$box.append(s)}var a=this.$element.offset(),n=this.$element.height(),h=this.$element.width(),r=this._getPlacement(a,n),o="top"===r?a.top-this.$box.height()-n:a.top+n;this.$box.css({width:h+"px",top:o+"px",left:a.left+"px"}),this.$box.removeClass("is-hidden")},_listen:function(t){switch(t.which){case 40:t.preventDefault(),this._select("next");break;case 38:t.preventDefault(),this._select("prev");break;case 13:t.preventDefault(),this._set();break;case 27:this._close(t);break;default:this.timeout=setTimeout(this._lookup.bind(this),300)}},_select:function(t){var e=this.$box.find("a"),i=this.$box.find(".is-active");e.removeClass("is-active");var s=this._selectItem(i,e,t);s.addClass("is-active")},_selectItem:function(t,e,i){var s,a=0!==t.length,n="next"===i?0:e.length-1;return a&&(s=t[i]()),a&&s&&0!==s.length||(s=e.eq(n)),s},_set:function(e){var i=this.$box.find(".is-active");e&&(e.preventDefault(),i=t.dom(e.target));var s=i.attr("rel"),a=i.html();if(0!==this.$target.length){if(this._isInputTarget())this.$target.val(a);else{var n=this.$target.find('[data-id="'+s+'"]');0===n.length&&this._addLabel(s,a)}this.$element.val("")}else this.$element.val(a);this.$element.focus(),this.app.broadcast("autocomplete.set",this,a),this._close()},_addLabel:function(e,i){var s=t.dom("<span>");s.addClass("label"),s.attr("data-id",e),s.text(i+" "),this.params.labelClass&&s.addClass(this.params.labelClass);var a=t.dom("<span>");a.addClass("close"),a.on("click",this._removeLabel.bind(this));var n=t.dom("<input>");n.attr("type","hidden"),n.attr("name",this._getTargetName()+"[]"),n.val(i),s.append(a),s.append(n),this.$target.append(s)},_isInputTarget:function(){return"INPUT"===this.$target.get().tagName},_removeLabel:function(e){e.preventDefault();var i=t.dom(e.target),s=i.closest(".label");this.animate.run(s,"fadeOut",function(){s.remove()}.bind(this))}})}(Kube),function(t){t.add("module","combobox",{init:function(t,e){this.app=t,this.$win=t.$win;var i={placeholder:""};this.context=e,thi
if("undefined"!=typeof i.type&&"error"===i.type)this._setStatus("error"),this.response.parse(t),this.app.broadcast("upload.error",this,t);else{switch(this._setStatus("success"),this.params.type){case"image":this._completeBoxImage(t);break;case"file":this._completeBoxFile(t);break;default:this._completeBoxUpload(t)}this.app.broadcast("upload.complete",this,t),setTimeout(this._clearStatuses.bind(this),500)}},_completeBoxUpload:function(t){this.response.parse(t)},_completeBoxImage:function(e){for(var i in e){var s=t.dom("<img>");s.attr("src",e[i].url);var a=t.dom("<span>");a.addClass("close"),a.on("click",this._removeImage.bind(this));var n=t.dom("<input>");n.attr("type","hidden"),n.attr("name",this._getHiddenName()),n.val(e[i].id);var h=t.dom("<div>");if(h.addClass("upload-item is-uploaded"),h.attr("data-id",e[i].id),!this.isMultiple){var r=this.$box.find("img");return 0!==r.length&&this._removeFileRequest(this.$box.attr("data-id")),this.$box.html(""),this.$box.attr("data-id",e[i].id),this.$box.append(a),this.$box.append(s),void this.$box.append(n)}h.append(a),h.append(s),h.append(n),this.$box.last().before(h)}},_completeBoxFile:function(e){this.isMultiple||this._clearTarget();for(var i in e){var s=t.dom("<div>");s.addClass("upload-item"),s.attr("data-id",e[i].id);var a=t.dom("<span>");a.html(e[i].name);var n=t.dom("<span>");n.addClass("close"),n.on("click",this._removeFile.bind(this));var h=t.dom("<input>");if(h.attr("type","hidden"),h.attr("name",this._getHiddenName()),h.val(e[i].id),"undefined"!=typeof e[i].size){var r=t.dom("<em>");r.html(e[i].size),a.append(r)}s.append(n),s.append(a),s.append(h),this.$target.append(s),this._upCount()}}})}(Kube),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 i={errorClass:"is-error",send:!0,trigger:!1,shortcut:!1,progress:!1};this.context=e,this.params=e.getParams(i),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 i=t[e],s=this.$element.find("[name="+e+"]");0!==s.length&&(s.addClass(this.params.errorClass),this._setFieldEvent(s,e),""!==i&&this._showErrorText(e,i))}},_setFieldEvent:function(t,e){var i=this._getFieldEventName(t);t.on(i+".kube.validate",function(){this._clearError(t)}.bind(this))},_showErrorText:function(t,e){var i=this.$element.find("#"+t+"-validation-error");i.addClass(this.params.errorClass),i.html(e),i.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 i=t.dom(e),s=this.$element.find("#"+i.attr("name")+"-validation-error");s.removeClass(this.params.errorClass),s.html(""),s.addClass("is-hidden"),i.removeClass(this.params.errorClass).off(".kube.validate")},_saveCodeM