ng2-input-filter
Version:
Input filter directive for Angular
189 lines (183 loc) • 6.79 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core')) :
typeof define === 'function' && define.amd ? define('ng2-input-filter', ['exports', '@angular/core'], factory) :
(global = global || self, factory(global['ng2-input-filter'] = {}, global.ng.core));
}(this, (function (exports, core) { 'use strict';
/**
* @fileoverview added by tsickle
* Generated from: lib/input-filter.directive.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
var InputFilterDirective = /** @class */ (function () {
function InputFilterDirective() {
this.regExpMap = new Map();
}
/**
* @return {?}
*/
InputFilterDirective.prototype.ngOnInit = /**
* @return {?}
*/
function () {
this.previousValue = this.ngModel ? this.ngModel.toString() : '';
this.regExpMap.set('INTEGER', new RegExp('^\\d*$'));
this.regExpMap.set('REAL', new RegExp('^[-+]?\\d*[\\.\\,]?\\d*$'));
this.regExpMap.set('NON_NEGATIVE_REAL', new RegExp('^[+]?\\d*[\\.\\,]?\\d*$'));
};
/**
* @param {?} value
* @return {?}
*/
InputFilterDirective.prototype.isReal = /**
* @param {?} value
* @return {?}
*/
function (value) {
return !isNaN(+value - +value);
};
/**
* @param {?} value
* @return {?}
*/
InputFilterDirective.prototype.checkValueOnBlur = /**
* @param {?} value
* @return {?}
*/
function (value) {
switch (this.ng2InputFilter) {
case 'REAL':
return this.isReal(value);
case 'NON_NEGATIVE_REAL':
return this.isReal(value) && +value >= 0;
case 'POSITIVE_REAL':
return this.isReal(value) && +value > 0;
case 'POSITIVE_INTEGER':
return !(+value < 1);
default:
return true;
}
};
/**
* @param {?} value
* @return {?}
*/
InputFilterDirective.prototype.checkValueOnType = /**
* @param {?} value
* @return {?}
*/
function (value) {
if (value.length === 0) {
return true;
}
switch (this.ng2InputFilter) {
case 'REAL':
return this.regexCheck('REAL', value);
case 'NON_NEGATIVE_REAL':
return this.regexCheck('NON_NEGATIVE_REAL', value);
case 'POSITIVE_REAL': // non_negative + 0.
return this.regexCheck('NON_NEGATIVE_REAL', value);
case 'POSITIVE_INTEGER':
return this.regexCheck('INTEGER', value) && +value > 0;
case 'NON_NEGATIVE_INTEGER':
return this.regexCheck('INTEGER', value) && +value >= 0;
case 'INTEGER':
return this.regexCheck('INTEGER', value);
default:
return true;
}
};
/**
* @param {?} key
* @param {?} value
* @return {?}
*/
InputFilterDirective.prototype.regexCheck = /**
* @param {?} key
* @param {?} value
* @return {?}
*/
function (key, value) {
return this.regExpMap.get(key).test(value);
};
/**
* @param {?} input
* @return {?}
*/
InputFilterDirective.prototype.onBlur = /**
* @param {?} input
* @return {?}
*/
function (input) {
/** @type {?} */
var valid = this.checkValueOnBlur(input.value);
if (!valid) {
input.value = '';
}
};
/**
* @param {?} input
* @return {?}
*/
InputFilterDirective.prototype.onType = /**
* @param {?} input
* @return {?}
*/
function (input) {
input.value = input.value.replace(',', '.');
/** @type {?} */
var valid = this.checkValueOnType(input.value);
if (!valid) {
input.value = this.previousValue;
input.dispatchEvent(new Event('input'));
}
else {
this.previousValue = input.value;
}
};
InputFilterDirective.decorators = [
{ type: core.Directive, args: [{
selector: '[ng2InputFilter]'
},] }
];
/** @nocollapse */
InputFilterDirective.ctorParameters = function () { return []; };
InputFilterDirective.propDecorators = {
ng2InputFilter: [{ type: core.Input }],
ngModel: [{ type: core.Input }],
onBlur: [{ type: core.HostListener, args: ['blur', ['$event.target'],] }],
onType: [{ type: core.HostListener, args: ['input', ['$event.target'],] }]
};
return InputFilterDirective;
}());
if (false) {
/** @type {?} */
InputFilterDirective.prototype.ng2InputFilter;
/** @type {?} */
InputFilterDirective.prototype.ngModel;
/** @type {?} */
InputFilterDirective.prototype.previousValue;
/** @type {?} */
InputFilterDirective.prototype.regExpMap;
}
/**
* @fileoverview added by tsickle
* Generated from: lib/ng2-input-filter.module.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
var Ng2InputFilterModule = /** @class */ (function () {
function Ng2InputFilterModule() {
}
Ng2InputFilterModule.decorators = [
{ type: core.NgModule, args: [{
declarations: [InputFilterDirective],
imports: [],
exports: [InputFilterDirective]
},] }
];
return Ng2InputFilterModule;
}());
exports.InputFilterDirective = InputFilterDirective;
exports.Ng2InputFilterModule = Ng2InputFilterModule;
Object.defineProperty(exports, '__esModule', { value: true });
})));
//# sourceMappingURL=ng2-input-filter.umd.js.map