ngx-mask-fixed
Version:
awesome ngx mask
246 lines (245 loc) • 9.99 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [0, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
import { Directive, forwardRef, HostListener, Inject, Input } from '@angular/core';
import { DOCUMENT } from '@angular/common';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import { MaskService } from './mask.service';
var MaskDirective = (function () {
function MaskDirective(
// tslint:disable-next-line
document, _maskService) {
this.document = document;
this._maskService = _maskService;
// tslint:disable-next-line
this.onChange = function (_) {
};
this.onTouch = function () {
};
this._position = null;
}
Object.defineProperty(MaskDirective.prototype, "maskExpression", {
set: function (value) {
this._maskValue = value || '';
if (!this._maskValue) {
return;
}
this._maskService.maskExpression = this._maskValue;
this._maskService.formElementProperty = [
'value',
this._maskService.applyMask(this._inputValue, this._maskService.maskExpression)
];
},
enumerable: true,
configurable: true
});
Object.defineProperty(MaskDirective.prototype, "specialCharacters", {
set: function (value) {
if (!value || !Array.isArray(value) || Array.isArray(value) && !value.length) {
return;
}
this._maskService.maskSpecialCharacters = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(MaskDirective.prototype, "patterns", {
set: function (value) {
if (!value) {
return;
}
this._maskService.maskAvailablePatterns = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(MaskDirective.prototype, "prefix", {
set: function (value) {
if (!value) {
return;
}
this._maskService.prefix = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(MaskDirective.prototype, "sufix", {
set: function (value) {
if (!value) {
return;
}
this._maskService.sufix = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(MaskDirective.prototype, "dropSpecialCharacters", {
set: function (value) {
this._maskService.dropSpecialCharacters = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(MaskDirective.prototype, "showTemplate", {
set: function (value) {
this._maskService.showTemplate = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(MaskDirective.prototype, "clearIfNotMatch", {
set: function (value) {
this._maskService.clearIfNotMatch = value;
},
enumerable: true,
configurable: true
});
MaskDirective.prototype.onInput = function (e) {
var el = e.target;
this._inputValue = el.value;
if (!this._maskValue) {
this.onChange(el.value);
return;
}
var position = el.selectionStart;
var caretShift = 0;
this._maskService.applyValueChanges(position, function (shift) { return caretShift = shift; });
// only set the selection if the element is active
if (this.document.activeElement !== el) {
return;
}
el.selectionStart = el.selectionEnd = this._position !== null
? this._position
: position + (
// tslint:disable-next-line
e.inputType === 'deleteContentBackward'
? 0
: caretShift);
this._position = null;
};
MaskDirective.prototype.onBlur = function () {
this._maskService.clearIfNotMatchFn();
this.onTouch();
};
MaskDirective.prototype.onFocus = function (e) {
if (!this._maskService.prefix) {
return;
}
var el = e.target;
if ((el.selectionStart === el.selectionEnd) &&
el.selectionStart &&
el.selectionStart > this._maskService.prefix.length &&
// tslint:disable-next-line
e.keyCode !== 38) {
return;
}
e.preventDefault();
el.selectionStart = el.selectionEnd = this._maskService.prefix.length;
};
MaskDirective.prototype.a = function (e) {
if (e.keyCode === 37 || e.keyCode === 38 || e.keyCode === 8) {
this.onFocus(e);
}
};
MaskDirective.prototype.onPaste = function () {
this._position = Number.MAX_SAFE_INTEGER;
};
/** It writes the value in the input */
MaskDirective.prototype.writeValue = function (inputValue) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
if (inputValue === undefined) {
return [2 /*return*/];
}
inputValue && this._maskService.maskExpression
? this._maskService.formElementProperty = [
'value',
this._maskService.applyMask(inputValue, this._maskService.maskExpression)
]
: this._maskService.formElementProperty = ['value', inputValue];
this._inputValue = inputValue;
return [2 /*return*/];
});
});
};
// tslint:disable-next-line
MaskDirective.prototype.registerOnChange = function (fn) {
this.onChange = fn;
this._maskService.onChange = this.onChange;
};
// tslint:disable-next-line
MaskDirective.prototype.registerOnTouched = function (fn) {
this.onTouch = fn;
};
/** It disables the input element */
MaskDirective.prototype.setDisabledState = function (isDisabled) {
this._maskService.formElementProperty = ['disabled', isDisabled];
};
MaskDirective.decorators = [
{ type: Directive, args: [{
selector: '[mask]',
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(function () { return MaskDirective; }),
multi: true
},
MaskService
],
},] },
];
/** @nocollapse */
MaskDirective.ctorParameters = function () { return [
{ type: undefined, decorators: [{ type: Inject, args: [DOCUMENT,] },] },
{ type: MaskService, },
]; };
MaskDirective.propDecorators = {
'maskExpression': [{ type: Input, args: ['mask',] },],
'specialCharacters': [{ type: Input },],
'patterns': [{ type: Input },],
'prefix': [{ type: Input },],
'sufix': [{ type: Input },],
'dropSpecialCharacters': [{ type: Input },],
'showTemplate': [{ type: Input },],
'clearIfNotMatch': [{ type: Input },],
'onInput': [{ type: HostListener, args: ['input', ['$event'],] },],
'onBlur': [{ type: HostListener, args: ['blur',] },],
'onFocus': [{ type: HostListener, args: ['click', ['$event'],] },],
'a': [{ type: HostListener, args: ['keydown', ['$event'],] },],
'onPaste': [{ type: HostListener, args: ['paste',] },],
};
return MaskDirective;
}());
export { MaskDirective };