-
Notifications
You must be signed in to change notification settings - Fork 2
/
TextAreaLimitation.min.js
2 lines (2 loc) · 5.41 KB
/
TextAreaLimitation.min.js
1
2
var TextAreaLimitation=function(){function n(t,i){if(this.Element=t,this.param=i,t.length>1)t.each(function(t,i){var r=new n($(i))});else{this.textArea=this.Element[0];var r=t.parent();r.hasClass("form-group")==!1&&t.wrap("<div class='form-group'><\/div>");t.hasClass("form-control")==!1&&t.addClass("form-control");this.formGroup=t.closest(".form-group");this.setDefaultValues();this.registerEvents()}}return n.prototype.registerEvents=function(){var n=this,t=null;this.Element.keydown(function(i){var f;if(!i.ctrlKey&&i.keyCode!==46&&i.keyCode!==37&&i.keyCode!==38&&i.keyCode!==39&&i.keyCode!==40&&i.keyCode!==16&&i.keyCode!==17&&i.keyCode!==18&&i.keyCode!==8){var u=n.getInfo(),e=n.validateLinesLenght(u,!1,i.keyCode),r=!0;return e||(r=!1),i.keyCode!==13&&(f=n.validateText(!1,u),f||(r=!1)),r?void 0:(t=n.Element.val(),setTimeout(function(){n.Element.trigger("cs.TextAreaLimitation.KeyDownCanceled",["valid"]);n.param.onKeyDownCanceled!=null&&$.isFunction(n.param.onKeyDownCanceled)&&n.param.onKeyDownCanceled.call(n.Element)},100),i.preventDefault(),i.returnValue=!1,!1)}});this.Element.keyup(function(){t!=null&&n.Element.val()!=t&&n.Element.val(t);t=null});this.Element.change(function(){var t=n.getInfo(),i=n.validateLinesLenght(t,!0),r;if(!i){n.displayLinesExceedError(t.length);return}r=n.validateText(!0,t);r&&i&&n.isInError&&(n.Element.trigger("cs.TextAreaLimitation.Invalid",["valid"]),n.param.onInvalid!=null&&$.isFunction(n.param.onInvalid)&&n.param.onInvalid.call(n.Element,"valid"),n.Element.removeClass("cs-invalid"),n.isInError=!1,n.formGroup.removeClass("has-error"),n.Element.popover("hide"))})},n.prototype.setDefaultValues=function(){this.param==null?this.param={lang:"en",maxCharPerLines:null,maxLines:null,popOverPosition:"top"}:(this.param.lang==null&&(this.param.lang="en"),this.param.popOverPosition==null&&(this.param.popOverPosition="top"));this.hasData("maxlines")&&(this.param.maxLines=parseInt(this.Element.data("maxlines")));this.hasData("maxcharperlines")&&(this.param.maxCharPerLines=parseInt(this.Element.data("maxcharperlines")));this.hasData("popoverposition")&&(this.param.popOverPosition=this.Element.data("popoverposition"));this.hasData("lang")&&(this.param.lang=this.Element.data("lang"));this.hasData("usepopover")&&(this.param.usePopOver=(""+this.Element.data("usepopover")).toLowerCase()=="true")},n.prototype.hasData=function(n){return typeof this.Element.data(n)!="undefined"?!0:!1},n.prototype.getLineNumber=function(){return this.textArea.value.substr(0,this.textArea.selectionStart).split("\n").length},n.prototype.validateText=function(n,t){var r,u,i;if(this.param.maxCharPerLines==null)return!0;for(r=0,u=this.getLineNumber()-1,i=0;i<t.length;i++){if(r=t[i].length,!n&&r+1>this.param.maxCharPerLines&&u==i)return!1;if(n&&r>this.param.maxCharPerLines)return this.isInError=!0,this.formGroup.addClass("has-error"),this.Element.addClass("cs-invalid"),this.displayErrorLenghtMessage(i,t[i]),!1}return!0},n.prototype.validateLinesLenght=function(n,t,i){return this.param.maxLines==null?!0:n.length>this.param.maxLines||i!=null&&i===13&&n.length+1>this.param.maxLines?(t&&(this.isInError=!0,this.formGroup.addClass("has-error"),this.Element.addClass("cs-invalid"),this.displayLinesExceedError(n.length)),!1):!0},n.prototype.getInfo=function(){var n=this.Element.val();return n.split("\n")},n.prototype.displayErrorLenghtMessage=function(n,t){var i=this,r;this.param.usePopOver&&(r="",r=this.param.lang=="fr"?"Chaque ligne ne doit pas dépasser "+this.param.maxCharPerLines+" caractères par ligne.<br />La ligne "+(n+1)+" dépasse ce maximum. Elle a "+t.length+" caractères.<br / ><blockquote><p>"+t+"<\/p><footer>"+(this.param.maxCharPerLines-t.length)+" caractères de trop.<\/foorter><\/blockquote>":"Each line must not exceed "+this.param.maxCharPerLines+" characters per line.<br /> Line "+(n+1)+" exceeds this maximum.It has "+t.length+" characters.<br / ><blockquote><p>"+t+"<\/p><footer>It exceed this limit by "+(this.param.maxCharPerLines-t.length)+".<\/foorter><\/blockquote>",this.Element.popover("destroy"),setTimeout(function(){i.Element.popover({html:!0,content:r,placement:i.param.popOverPosition,trigger:"manual"});i.Element.popover("show")},300));this.Element.trigger("cs.TextAreaLimitation.Invalid",["invalid"]);this.Element.trigger("cs.TextAreaLimitation.InvalidLineLength",[n,t]);this.param.onInvalid!=null&&$.isFunction(this.param.onInvalid)&&this.param.onInvalid.call(this.Element,"invalid");this.param.onInvalidLineLength!=null&&$.isFunction(this.param.onInvalidLineLength)&&this.param.onInvalidLineLength.call(this.Element,n,t)},n.prototype.displayLinesExceedError=function(n){var t=this,i;this.param.usePopOver&&(i="",i=this.param.lang=="fr"?"Vous devez entrer un maximum de "+this.param.maxLines+" lignes.<br />Votre texte contient "+n.toString()+" lignes.":"You must enter a maximum of "+this.param.maxLines+" lines. Your text contains "+n.toString()+" lines.",this.Element.popover("destroy"),setTimeout(function(){t.Element.popover({html:!0,content:i,placement:t.param.popOverPosition,trigger:"manual"});t.Element.popover("show")},300));this.Element.trigger("cs.TextAreaLimitation.Invalid",["invalid"]);this.Element.trigger("cs.TextAreaLimitation.InvalidLines",[n]);this.param.onInvalid!=null&&$.isFunction(this.param.onInvalid)&&this.param.onInvalid.call(this.Element,"invalid");this.param.onInvalidLines!=null&&$.isFunction(this.param.onInvalidLines)&&this.param.onInvalidLines.call(this.Element,n)},n}();
//# sourceMappingURL=TextAreaLimitation.min.js.map