UNPKG

@ngx-mask/core

Version:

[@ngx-mask/core](https://github.com/IKatsuba/ngx-mask#readme)

170 lines 6.52 kB
import { __decorate, __metadata, __param } from "tslib"; import { AfterViewInit, Directive, ElementRef, EventEmitter, HostListener, Input, Optional, Output, Renderer2 } from '@angular/core'; import { ControlValueAccessor, NgControl } from '@angular/forms'; import { NgxMaskService } from './mask.service'; import { BACKSPACE } from '@angular/cdk/keycodes'; import { timer } from 'rxjs'; var NgxMaskDirective = /** @class */ (function () { function NgxMaskDirective(maskService, elementRef, renderer, ngControl) { this.maskService = maskService; this.elementRef = elementRef; this.renderer = renderer; this.ngControl = ngControl; this.valueChange = new EventEmitter(); this._mask = ''; this.onChange = function (_) { }; this.onTouched = function () { }; if (ngControl) { ngControl.valueAccessor = this; } } NgxMaskDirective_1 = NgxMaskDirective; Object.defineProperty(NgxMaskDirective.prototype, "value", { get: function () { return this.elementRef.nativeElement.value; }, set: function (value) { this.nativeValue = value; this._onInput(); }, enumerable: true, configurable: true }); Object.defineProperty(NgxMaskDirective.prototype, "mask", { get: function () { return this._mask; }, set: function (value) { this._mask = value; this._onInput(); }, enumerable: true, configurable: true }); Object.defineProperty(NgxMaskDirective.prototype, "nativeValue", { set: function (value) { this.lastNativeValue = value; this.elementRef.nativeElement.value = value; this.valueChange.emit(value); }, enumerable: true, configurable: true }); Object.defineProperty(NgxMaskDirective.prototype, "input", { get: function () { return this.elementRef.nativeElement; }, enumerable: true, configurable: true }); NgxMaskDirective.indexOfFirstDifferentChar = function (value, diff) { if (diff === void 0) { diff = ''; } var index; for (var i = 0; i < diff.length && i < value.length; i++) { if (value.charAt(i) !== diff.charAt(i)) { index = i; break; } } return (index !== null && index !== void 0 ? index : value.length); }; NgxMaskDirective.prototype.registerOnChange = function (fn) { this.onChange = fn; }; NgxMaskDirective.prototype.registerOnTouched = function (fn) { this.onTouched = fn; }; NgxMaskDirective.prototype.setDisabledState = function (isDisabled) { isDisabled ? this.renderer.setAttribute(this.elementRef, 'disabled', 'disabled') : this.renderer.removeAttribute(this.elementRef, 'disabled'); }; NgxMaskDirective.prototype.writeValue = function (obj) { this.value = obj; this._onInput(); }; NgxMaskDirective.prototype.onBlur = function () { this.onTouched(); }; NgxMaskDirective.prototype._onKeydown = function (event) { var _a = this.input, selectionStart = _a.selectionStart, selectionEnd = _a.selectionEnd; this.isBackspaceChange = event.keyCode === BACKSPACE && selectionEnd === selectionStart; }; NgxMaskDirective.prototype._onInput = function (_a) { var _b = (_a === void 0 ? {} : _a).emitChange, emitChange = _b === void 0 ? true : _b; var _c = this.input, value = _c.value, selectionStart = _c.selectionStart; var maskedValue = this.maskService.applyMask(value, this.mask); var indexOfFirstDifferentChar = this.lastNativeValue ? NgxMaskDirective_1.indexOfFirstDifferentChar(maskedValue, this.lastNativeValue) : maskedValue.length; this.nativeValue = maskedValue; this.input.selectionStart = this.input.selectionEnd = this.isBackspaceChange ? selectionStart : indexOfFirstDifferentChar + 1; this.isBackspaceChange = false; if (emitChange) { this.onChange(this.value); } }; NgxMaskDirective.prototype.ngAfterViewInit = function () { var _this = this; if (this.ngControl && this.ngControl.value !== this.value) { timer(0).subscribe(function () { return _this.onChange(_this.value); }); } }; var NgxMaskDirective_1; NgxMaskDirective.ctorParameters = function () { return [ { type: NgxMaskService }, { type: ElementRef }, { type: Renderer2 }, { type: NgControl, decorators: [{ type: Optional }] } ]; }; __decorate([ Output(), __metadata("design:type", Object) ], NgxMaskDirective.prototype, "valueChange", void 0); __decorate([ Input(), __metadata("design:type", String), __metadata("design:paramtypes", [String]) ], NgxMaskDirective.prototype, "value", null); __decorate([ Input('ngxMask'), __metadata("design:type", String), __metadata("design:paramtypes", [String]) ], NgxMaskDirective.prototype, "mask", null); __decorate([ HostListener('blur'), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], NgxMaskDirective.prototype, "onBlur", null); __decorate([ HostListener('keydown', ['$event']), __metadata("design:type", Function), __metadata("design:paramtypes", [KeyboardEvent]), __metadata("design:returntype", void 0) ], NgxMaskDirective.prototype, "_onKeydown", null); __decorate([ HostListener('change'), HostListener('input'), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", void 0) ], NgxMaskDirective.prototype, "_onInput", null); NgxMaskDirective = NgxMaskDirective_1 = __decorate([ Directive({ selector: 'input[ngxMask][type=text]' }), __param(3, Optional()), __metadata("design:paramtypes", [NgxMaskService, ElementRef, Renderer2, NgControl]) ], NgxMaskDirective); return NgxMaskDirective; }()); export { NgxMaskDirective }; //# sourceMappingURL=mask.directive.js.map