ngx-phone-mask-br
Version:
Angular directive for autoformatting brazilian phone numbers.
208 lines (207 loc) • 5.98 kB
JavaScript
import { Directive, ElementRef, HostListener, Input, NgModule, forwardRef } from '@angular/core';
import { CommonModule } from '@angular/common';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
var noop = function () { };
var masks = [
'(1',
'(11',
'(11) 1',
'(11) 11',
'(11) 111',
'(11) 1111',
'(11) 1111-1',
'(11) 1111-11',
'(11) 1111-111',
'(11) 1111-1111',
'(11) 11111-1111'
];
var clean = function (number) {
return number
.toString()
.replace(/[^\d\^]/gm, '');
};
var format = function (number) {
var /** @type {?} */ lastCharIndex = 0;
var /** @type {?} */ cleanValue = clean(number);
var /** @type {?} */ charCount = cleanValue.replace(/\^/gm, '').length;
if (charCount === 0) {
return {
formatted: '',
cursorPosition: 0
};
}
var /** @type {?} */ mask = masks[charCount - 1];
if (charCount > 1 && !mask) {
return null;
}
var /** @type {?} */ cursorPosition;
var /** @type {?} */ formatted = mask.split('').map(function (c, i) {
if (c === '1') {
if (cleanValue[lastCharIndex] == '^') {
cursorPosition = i + 1;
lastCharIndex++;
}
lastCharIndex++;
return cleanValue[lastCharIndex - 1];
}
else {
return c;
}
}).join('');
if (!cursorPosition) {
cursorPosition = formatted.length;
}
cursorPosition++; // because of '+'
return {
formatted: "" + formatted,
cursorPosition: cursorPosition
};
};
var NgxPhoneMaskBrDirective = (function () {
/**
* @param {?} input
*/
function NgxPhoneMaskBrDirective(input) {
this.input = input;
this.onTouchedCallback = noop;
this.onChangeCallback = noop;
this.valueType = 'clean';
this.showMask = true;
this.oldValue = '';
}
/**
* @return {?}
*/
NgxPhoneMaskBrDirective.prototype.updateInputView = function () {
var /** @type {?} */ input = this.input.nativeElement;
var /** @type {?} */ cursorPosition = input.selectionStart;
var /** @type {?} */ value = this._value;
var /** @type {?} */ valueWithCursor = value.substring(0, cursorPosition) + '^' + value.substring(cursorPosition);
var /** @type {?} */ formatted = format(valueWithCursor);
if (value === '') {
return;
}
if (!formatted) {
input.value = this.oldValue;
return;
}
var /** @type {?} */ newValue = formatted.formatted;
if (newValue != input.value) {
input.value = newValue;
input.setSelectionRange(formatted.cursorPosition, formatted.cursorPosition);
}
this.oldValue = newValue;
this.emitValue(newValue);
};
/**
* @param {?} v
* @return {?}
*/
NgxPhoneMaskBrDirective.prototype.emitValue = function (v) {
var /** @type {?} */ value;
switch (this.valueType) {
case 'clean':
value = v.replace(/[^\d\+]/gm, '');
break;
case 'full':
value = v;
break;
}
this.onChangeCallback(value);
};
/**
* @return {?}
*/
NgxPhoneMaskBrDirective.prototype.onInput = function () {
this._value = this.input.nativeElement.value;
this.updateInputView();
};
Object.defineProperty(NgxPhoneMaskBrDirective.prototype, "value", {
/**
* @param {?} v
* @return {?}
*/
set: function (v) {
var /** @type {?} */ value = v ? v : '';
this._value = value;
this.updateInputView();
},
enumerable: true,
configurable: true
});
/**
* @param {?} value
* @return {?}
*/
NgxPhoneMaskBrDirective.prototype.writeValue = function (value) {
this.value = value;
};
/**
* @param {?} fn
* @return {?}
*/
NgxPhoneMaskBrDirective.prototype.registerOnChange = function (fn) {
this.onChangeCallback = fn;
};
/**
* @param {?} fn
* @return {?}
*/
NgxPhoneMaskBrDirective.prototype.registerOnTouched = function (fn) {
this.onTouchedCallback = fn;
};
/**
* @param {?} isDisabled
* @return {?}
*/
NgxPhoneMaskBrDirective.prototype.setDisabledState = function (isDisabled) {
this.disabled = isDisabled;
};
return NgxPhoneMaskBrDirective;
}());
NgxPhoneMaskBrDirective.decorators = [
{ type: Directive, args: [{
selector: '[ngxPhoneMaskBr]',
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(function () { return NgxPhoneMaskBrDirective; }),
multi: true
}
]
},] },
];
/**
* @nocollapse
*/
NgxPhoneMaskBrDirective.ctorParameters = function () { return [
{ type: ElementRef, },
]; };
NgxPhoneMaskBrDirective.propDecorators = {
'valueType': [{ type: Input },],
'showMask': [{ type: Input },],
'onInput': [{ type: HostListener, args: ['input',] },],
};
var NgxPhoneMaskBrModule = (function () {
function NgxPhoneMaskBrModule() {
}
return NgxPhoneMaskBrModule;
}());
NgxPhoneMaskBrModule.decorators = [
{ type: NgModule, args: [{
imports: [
CommonModule
],
declarations: [NgxPhoneMaskBrDirective],
exports: [NgxPhoneMaskBrDirective]
},] },
];
/**
* @nocollapse
*/
NgxPhoneMaskBrModule.ctorParameters = function () { return []; };
/**
* Generated bundle index. Do not edit.
*/
export { NgxPhoneMaskBrModule, NgxPhoneMaskBrDirective as ɵa };
//# sourceMappingURL=ngx-phone-mask-br.es5.js.map