UNPKG

@encoreskytech/ng-char-limit-hint

Version:

To initialize the char limit feature on text input

2 lines 3.35 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@angular/core"),require("@angular/platform-browser")):"function"==typeof define&&define.amd?define("@encoreskytech/ng-char-limit-hint",["exports","@angular/core","@angular/platform-browser"],e):e(((t="undefined"!=typeof globalThis?globalThis:t||self).encoreskytech=t.encoreskytech||{},t.encoreskytech["ng-char-limit-hint"]={}),t.ng.core,t.ng.platformBrowser)}(this,(function(t,e,n){"use strict";var i=function(){function t(){this.maxCharLimit=100,this.format="",this.parentClass="",this.hintTextClass=""}return t.prototype.ngOnInit=function(){this.remainingCharCount=this.maxCharLimit,this.getFormattedResult()},t.prototype.ngAfterViewInit=function(){var t=this;if(!this.content.nativeElement.hasChildNodes("input"))throw"Valid input element not found.";this.content.nativeElement.firstElementChild.addEventListener("paste",(function(e){var n=e.clipboardData.getData("text");n.length>t.remainingCharCount?(e.target.value+=n.substring(0,e.target.value.length>0?t.remainingCharCount:t.maxCharLimit),t.remainingCharCount=0):(e.target.value+=n,t.remainingCharCount=t.remainingCharCount-n.length),t.getFormattedResult(),e.preventDefault()})),this.content.nativeElement.firstElementChild.addEventListener("keydown",(function(e){t.remainCount(e)}))},t.prototype.remainCount=function(t){if((!t.getModifierState||!t.getModifierState("Control")&&!t.getModifierState("Meta"))&&this.isValidKey(t.keyCode)){var e=t.target.value,n=parseInt(e.length),i=t.target.selectionEnd-t.target.selectionStart;8==t.keyCode||127==t.keyCode?i>0?n=e.length-i:e.length<=0?n=0:e.length>=1&&(n=e.length-1):i>0?(t.target.value=e.substr(0,t.target.selectionStart)+String.fromCharCode(t.keyCode)+e.substr(t.target.selectionEnd),n=t.target.value.length,this.remainingCharCount=this.maxCharLimit-n,this.getFormattedResult(),t.preventDefault(),t.target.setSelectionRange(t.target.selectionStart,t.target.selectionStart)):e.length==this.maxCharLimit?t.preventDefault():e.length>=0&&(n=e.length+1),this.remainingCharCount=this.maxCharLimit-n,this.getFormattedResult()}},t.prototype.getFormattedResult=function(){var t=this.format;this.format.length?(t=(t=t.replace("REMAIN",this.remainingCharCount.toString())).replace("TOTAL",this.maxCharLimit.toString()),this.formattedRemainCharCount=t):this.formattedRemainCharCount=this.remainingCharCount.toString()},t.prototype.isValidKey=function(t){return t>47&&t<58||32==t||13==t||t>64&&t<91||t>95&&t<112||t>185&&t<193||t>218&&t<223||8==t||127==t},t}();i.decorators=[{type:e.Component,args:[{selector:"ng-char-limit-hint",template:'\n <div #content [ngClass]="parentClass">\n <ng-content></ng-content>\n <p [ngClass]="hintTextClass"> {{formattedRemainCharCount}}</p>\n </div>\n ',encapsulation:e.ViewEncapsulation.None}]}],i.ctorParameters=function(){return[]},i.propDecorators={content:[{type:e.ViewChild,args:["content",{static:!1}]}],maxCharLimit:[{type:e.Input}],format:[{type:e.Input}],parentClass:[{type:e.Input}],hintTextClass:[{type:e.Input}]};var r=function(){};r.decorators=[{type:e.NgModule,args:[{declarations:[i],imports:[n.BrowserModule],exports:[i]}]}],t.CharLimitHintComponent=i,t.CharLimitHintModule=r,Object.defineProperty(t,"__esModule",{value:!0})})); //# sourceMappingURL=encoreskytech-ng-char-limit-hint.umd.min.js.map