@ngx-mask/core
Version:
[@ngx-mask/core](https://github.com/IKatsuba/ngx-mask#readme)
149 lines • 5.03 kB
JavaScript
var NgxMaskDirective_1;
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';
let NgxMaskDirective = NgxMaskDirective_1 = class NgxMaskDirective {
constructor(maskService, elementRef, renderer, ngControl) {
this.maskService = maskService;
this.elementRef = elementRef;
this.renderer = renderer;
this.ngControl = ngControl;
this.valueChange = new EventEmitter();
this._mask = '';
this.onChange = (_) => {
};
this.onTouched = () => {
};
if (ngControl) {
ngControl.valueAccessor = this;
}
}
get value() {
return this.elementRef.nativeElement.value;
}
set value(value) {
this.nativeValue = value;
this._onInput();
}
get mask() {
return this._mask;
}
set mask(value) {
this._mask = value;
this._onInput();
}
set nativeValue(value) {
this.lastNativeValue = value;
this.elementRef.nativeElement.value = value;
this.valueChange.emit(value);
}
get input() {
return this.elementRef.nativeElement;
}
static indexOfFirstDifferentChar(value, diff = '') {
let index;
for (let 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);
}
registerOnChange(fn) {
this.onChange = fn;
}
registerOnTouched(fn) {
this.onTouched = fn;
}
setDisabledState(isDisabled) {
isDisabled
? this.renderer.setAttribute(this.elementRef, 'disabled', 'disabled')
: this.renderer.removeAttribute(this.elementRef, 'disabled');
}
writeValue(obj) {
this.value = obj;
this._onInput();
}
onBlur() {
this.onTouched();
}
_onKeydown(event) {
const { selectionStart, selectionEnd } = this.input;
this.isBackspaceChange = event.keyCode === BACKSPACE && selectionEnd === selectionStart;
}
_onInput({ emitChange = true } = {}) {
const { value, selectionStart } = this.input;
const maskedValue = this.maskService.applyMask(value, this.mask);
const 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);
}
}
ngAfterViewInit() {
if (this.ngControl && this.ngControl.value !== this.value) {
timer(0).subscribe(() => this.onChange(this.value));
}
}
};
NgxMaskDirective.ctorParameters = () => [
{ 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);
export { NgxMaskDirective };
//# sourceMappingURL=mask.directive.js.map