@ioniczoo/chameleon-mask-directive
Version:
Maks Directive for Ionic Designed to Work with Numbers
87 lines • 3.38 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
import { Directive, Attribute, NgModule } from '@angular/core';
/**
* Cameleão Mask Directive
*
* Mask Directive for Ionic Designed to Work with Numbers
*
* @example
*<ion-input type="text" mask="(**) * ****-****"></ion-input>
*
*- This will be util to numbers like (77) 9 9933-8525
*
*/
var MaskDirective = /** @class */ (function () {
/**
* Gets pattern that will be applied to the entry using the "mask"
*
* @param {string} maskPattern Original Mask
* @return {void}
*/
function MaskDirective(maskPattern) {
this.pattern = maskPattern;
}
/**
* Called when a javascript event with the attribute 'target.value' is triggered (keydown, keyup, keypress)
*
* @param {jsEvent} event javascript event with the attribute 'target.value'
*/
MaskDirective.prototype.onInputChange = function (event) {
if (event.keyCode != 8 || event.which != 8 || event.key != "Backspace")
event.target.value = this.mask(event.target.value);
};
/**
* Applies the mask pattern corresponding to the string and its length
*
* @param {string} origin String to apply the mask pattern
* @return {string} String with the mask pattern correspondent
*/
MaskDirective.prototype.mask = function (origin) {
var left, right, part = origin.length;
if (part >= this.pattern.length)
return origin.slice(0, this.pattern.length - 1);
while (this.pattern[part] && this.pattern[part] != '*') {
left = origin.slice(0, part);
right = origin.slice(part);
origin = left + this.pattern[part] + right;
part = origin.length;
}
return origin;
};
MaskDirective = __decorate([
Directive({
selector: '[mask]',
host: {
'(keydown)': 'onInputChange($event)'
}
}),
__param(0, Attribute("mask")),
__metadata("design:paramtypes", [String])
], MaskDirective);
return MaskDirective;
}());
export { MaskDirective };
var MaskDirectiveModule = /** @class */ (function () {
function MaskDirectiveModule() {
}
MaskDirectiveModule = __decorate([
NgModule({
declarations: [MaskDirective],
exports: [MaskDirective]
})
], MaskDirectiveModule);
return MaskDirectiveModule;
}());
export { MaskDirectiveModule };
//# sourceMappingURL=index.js.map