UNPKG

ng-spin-box

Version:

<h2>Spinbox Directive for Angular.</h2>

2 lines 6.56 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@angular/core"),require("@angular/common")):"function"==typeof define&&define.amd?define("ng-spin-box",["exports","@angular/core","@angular/common"],t):t((e=e||self)["ng-spin-box"]={},e.ng.core,e.ng.common)}(this,(function(e,t,n){"use strict";Object.setPrototypeOf||Array;Object.assign;function i(e,t,n,i){var r,s=arguments.length,o=s<3?t:null===i?i=Object.getOwnPropertyDescriptor(t,n):i;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)o=Reflect.decorate(e,t,n,i);else for(var a=e.length-1;a>=0;a--)(r=e[a])&&(o=(s<3?r(o):s>3?r(t,n,o):r(t,n))||o);return s>3&&o&&Object.defineProperty(t,n,o),o}function r(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)}var s=function(){function e(e,n){this.el=e,this.renderer=n,this._hasFocus=!1,this.valueToBeSet=0,this.changeByMouse=new t.EventEmitter,this._initView()}return Object.defineProperty(e.prototype,"_isDisabled",{get:function(){return this.el.nativeElement.disabled},enumerable:!0,configurable:!0}),e.prototype._initView=function(){var e=this,t=this.el.nativeElement,n=t.parentNode,i=this.renderer.createElement("div");this.renderer.addClass(i,"ng-spin-box-wrapper"),this.renderer.setAttribute(i,"style","\n position: relative;\n display: inline-block;\n "),this.renderer.appendChild(i,t),this.renderer.appendChild(n,i);var r=this.renderer.createElement("button");this.renderer.appendChild(i,r),this.renderer.addClass(r,"ng-spin-box-btn-up"),this.renderer.listen(r,"click",(function(t){e._clickSpinBoxButton(t,!0)})),this.renderer.setAttribute(r,"tabIndex","-1"),this.renderer.setAttribute(r,"style","\n position: absolute;\n top: 0;\n right: 0;\n height: 50%;\n ");var s=this.renderer.createElement("button");this.renderer.appendChild(i,s),this.renderer.addClass(s,"ng-spin-box-btn-down"),this.renderer.listen(s,"click",(function(t){e._clickSpinBoxButton(t,!1)})),this.renderer.setAttribute(s,"tabIndex","-1"),this.renderer.setAttribute(s,"style","\n position: absolute;\n bottom: 0;\n right: 0;\n height: 50%;\n ")},e.prototype._clickSpinBoxButton=function(e,t){e.preventDefault(),e.stopPropagation(),this._isDisabled||(this.el.nativeElement.focus(),this._increaseDecreaseValue(t))},e.prototype._increaseDecreaseValue=function(e){var t=this.el.nativeElement.value,n=+(t||0),i=+(this.decimal||0),r=Math.pow(10,i+2),s=n*r;s=+s.toFixed(i+2);var o,a=+this.step||1,p=this.el.nativeElement.selectionStart,l=p-t.indexOf(this._getDecimalSeparator),u="string"==typeof this.snapToStep?JSON.parse(this.snapToStep):this.snapToStep;e?this.stepAtCursor&&p<=t.length-2?s+=1*Math.pow(10,i+2-l):u?((o=Math.round(s/(a*r))*(a*r))>s&&(o-=a*r),s=o+a*r):s+=a*r:this.stepAtCursor&&p<=t.length-2?s-=1*Math.pow(10,i+2-l):u?((o=Math.round(s/(a*r))*(a*r))<s&&(o+=a*r),s=o-a*r):s-=a*r;var c=(s/r).toFixed(i);+c>=(+this.min||-1/0)&&+c<=(+this.max||1/0)?this.valueToBeSet=+c:+c<(+this.min||-1/0)?this.valueToBeSet=this.min:+c>(+this.max||1/0)&&(this.valueToBeSet=this.max),this.changeByMouse&&this.changeByMouse.emit(c);var d,h=(+this.valueToBeSet).toFixed(i);this.renderer.setProperty(this.el.nativeElement,"value",h);try{d=new Event("input",{bubbles:!0,cancelable:!1})}catch(e){(d=document.createEvent("Event")).initEvent("input",!0,!1)}this.el.nativeElement.dispatchEvent(d),-1!==t.indexOf(this._getDecimalSeparator)&&(this.el.nativeElement.selectionStart=p,this.el.nativeElement.selectionEnd=p)},e.prototype.onFocus=function(){this._hasFocus=!0},e.prototype.onBlur=function(){if("string"==typeof this.setLimitOnBlur?JSON.parse(this.setLimitOnBlur):this.setLimitOnBlur){this._hasFocus=!1;var e=+(this.el.nativeElement.value||0),t=!1;e>this.max?this._increaseDecreaseValue(t):e<this.min&&(t=!0,this._increaseDecreaseValue(t))}},e.prototype.onMouseWheel=function(e){e.preventDefault(),this._hasFocus&&!this._isDisabled&&(e.deltaY>0?this._increaseDecreaseValue(!1):this._increaseDecreaseValue(!0))},e.prototype.onKeydown=function(e){var t=e.key,n=this.el.nativeElement.selectionStart;if("ArrowUp"===t||"Up"===t){if(e.shiftKey)return;this._increaseDecreaseValue(!0)}else if("ArrowDown"===t||"Down"===t){if(e.shiftKey)return;this._increaseDecreaseValue(!1)}e.ctrlKey||e.metaKey||"ArrowLeft"===t||"ArrowRight"===t||"Backspace"===t||"Delete"===t||"Tab"===t||"Left"===t||"Right"===t||"-"===t&&0===n&&this.min&&this.min<0||this._checkPrecision(t)||e.preventDefault()},Object.defineProperty(e.prototype,"_getDecimalSeparator",{get:function(){return 1.1.toLocaleString().substring(1,2)},enumerable:!0,configurable:!0}),e.prototype._checkPrecision=function(e){var t=+this.decimal||0,n=this.el.nativeElement,i=n.value.substr(0,n.selectionStart)+e+n.value.substr(n.selectionEnd),r="^\\d+\\"+this._getDecimalSeparator+"?\\d{0,"+t+"}$";return new RegExp(r,"g").test(i)},e.ctorParameters=function(){return[{type:t.ElementRef},{type:t.Renderer2}]},i([t.Input("min"),r("design:type",Number)],e.prototype,"min",void 0),i([t.Input("max"),r("design:type",Number)],e.prototype,"max",void 0),i([t.Input("step"),r("design:type",Number)],e.prototype,"step",void 0),i([t.Input("decimal"),r("design:type",Number)],e.prototype,"decimal",void 0),i([t.Input("snapToStep"),r("design:type",Boolean)],e.prototype,"snapToStep",void 0),i([t.Input("stepAtCursor"),r("design:type",Boolean)],e.prototype,"stepAtCursor",void 0),i([t.Input("setLimitOnBlur"),r("design:type",Boolean)],e.prototype,"setLimitOnBlur",void 0),i([t.Output("changeByMouse"),r("design:type",t.EventEmitter)],e.prototype,"changeByMouse",void 0),i([t.HostListener("focus"),r("design:type",Function),r("design:paramtypes",[]),r("design:returntype",void 0)],e.prototype,"onFocus",null),i([t.HostListener("blur"),r("design:type",Function),r("design:paramtypes",[]),r("design:returntype",void 0)],e.prototype,"onBlur",null),i([t.HostListener("wheel",["$event"]),r("design:type",Function),r("design:paramtypes",[WheelEvent]),r("design:returntype",void 0)],e.prototype,"onMouseWheel",null),i([t.HostListener("keydown",["$event"]),r("design:type",Function),r("design:paramtypes",[KeyboardEvent]),r("design:returntype",void 0)],e.prototype,"onKeydown",null),e=i([t.Directive({selector:"[ngSpinBox]"}),r("design:paramtypes",[t.ElementRef,t.Renderer2])],e)}(),o=function(){function e(){}return e=i([t.NgModule({imports:[n.CommonModule],declarations:[s],exports:[s]})],e)}();e.NgSpinBoxModule=o,e.ɵa=s,Object.defineProperty(e,"__esModule",{value:!0})})); //# sourceMappingURL=ng-spin-box.umd.min.js.map