ngx-mask-fixed
Version:
awesome ngx mask
103 lines (102 loc) • 4.28 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import { ElementRef, Inject, Injectable, Renderer2 } from '@angular/core';
import { config } from './config';
import { DOCUMENT } from '@angular/common';
import { MaskApplierService } from './mask-applier.service';
var MaskService = (function (_super) {
__extends(MaskService, _super);
function MaskService(
// tslint:disable-next-line
document, _config, _elementRef, _renderer) {
var _this = _super.call(this, _config) || this;
_this.document = document;
_this._config = _config;
_this._elementRef = _elementRef;
_this._renderer = _renderer;
_this.maskExpression = '';
// tslint:disable-next-line
_this.onChange = function (_) {
};
_this.onTouch = function () {
};
_this._formElement = _this._elementRef.nativeElement;
return _this;
}
Object.defineProperty(MaskService.prototype, "formElementProperty", {
set: function (_a) {
var name = _a[0], value = _a[1];
this._renderer.setProperty(this._formElement, name, value);
},
enumerable: true,
configurable: true
});
MaskService.prototype.applyMask = function (inputValue, maskExpression, position, cb) {
if (position === void 0) { position = 0; }
if (cb === void 0) { cb = function () {
}; }
var result = _super.prototype.applyMask.call(this, inputValue, maskExpression, position, cb);
Array.isArray(this.dropSpecialCharacters)
? this.onChange(this._removeMask(this._removePrefix(result), this.dropSpecialCharacters))
: this.dropSpecialCharacters === true
? this.onChange(this._removeMask(this._removePrefix(result), this.maskSpecialCharacters))
: this.onChange(this._removePrefix(result));
return result;
};
MaskService.prototype.applyValueChanges = function (position, cb) {
if (position === void 0) { position = 0; }
if (cb === void 0) { cb = function () {
}; }
var maskedInput = this.applyMask(this._formElement.value, this.maskExpression, position, cb);
this._formElement.value = maskedInput;
if (this._formElement === this.document.activeElement) {
return;
}
this.clearIfNotMatchFn();
};
MaskService.prototype.clearIfNotMatchFn = function () {
if (this.clearIfNotMatch === true && this.maskExpression.length
!== this._formElement.value.length) {
this.formElementProperty = ['value', ''];
this.applyMask(this._formElement.value, this.maskExpression);
}
};
MaskService.prototype._removeMask = function (value, specialCharactersForRemove) {
return value
? value.replace(this._regExpForRemove(specialCharactersForRemove), '')
: value;
};
MaskService.prototype._removePrefix = function (value) {
if (!this.prefix) {
return value;
}
return value
? value.replace(this.prefix, '')
: value;
};
MaskService.prototype._regExpForRemove = function (specialCharactersForRemove) {
return new RegExp(specialCharactersForRemove
.map(function (item) { return "\\" + item; })
.join('|'), 'gi');
};
MaskService.decorators = [
{ type: Injectable },
];
/** @nocollapse */
MaskService.ctorParameters = function () { return [
{ type: undefined, decorators: [{ type: Inject, args: [DOCUMENT,] },] },
{ type: undefined, decorators: [{ type: Inject, args: [config,] },] },
{ type: ElementRef, },
{ type: Renderer2, },
]; };
return MaskService;
}(MaskApplierService));
export { MaskService };