UNPKG

primeng

Version:

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![npm version](https://badge.fury.io/js/primeng.svg)](https://badge.fury.io/js/primeng) [![Build Status](https://travis-ci.org/primefaces/primeng.

211 lines (207 loc) 6.56 kB
import { forwardRef, EventEmitter, ElementRef, Input, Output, HostListener, Directive, NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { DomHandler } from 'primeng/dom'; import { NG_VALIDATORS } from '@angular/forms'; 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; }; const KEYFILTER_VALIDATOR = { provide: NG_VALIDATORS, useExisting: forwardRef(() => KeyFilter), multi: true }; const DEFAULT_MASKS = { pint: /[\d]/, 'int': /[\d\-]/, pnum: /[\d\.]/, money: /[\d\.\s,]/, num: /[\d\-\.]/, hex: /[0-9a-f]/i, email: /[a-z0-9_\.\-@]/i, alpha: /[a-z_]/i, alphanum: /[a-z0-9_]/i }; const KEYS = { TAB: 9, RETURN: 13, ESC: 27, BACKSPACE: 8, DELETE: 46 }; const SAFARI_KEYS = { 63234: 37, 63235: 39, 63232: 38, 63233: 40, 63276: 33, 63277: 34, 63272: 46, 63273: 36, 63275: 35 // end }; let KeyFilter = class KeyFilter { constructor(el) { this.el = el; this.ngModelChange = new EventEmitter(); this.isAndroid = DomHandler.isAndroid(); } get pattern() { return this._pattern; } set pattern(_pattern) { this._pattern = _pattern; this.regex = DEFAULT_MASKS[this._pattern] || this._pattern; } isNavKeyPress(e) { let k = e.keyCode; k = DomHandler.getBrowser().safari ? (SAFARI_KEYS[k] || k) : k; return (k >= 33 && k <= 40) || k == KEYS.RETURN || k == KEYS.TAB || k == KEYS.ESC; } ; isSpecialKey(e) { let k = e.keyCode || e.charCode; return k == 9 || k == 13 || k == 27 || k == 16 || k == 17 || (k >= 18 && k <= 20) || (DomHandler.getBrowser().opera && !e.shiftKey && (k == 8 || (k >= 33 && k <= 35) || (k >= 36 && k <= 39) || (k >= 44 && k <= 45))); } getKey(e) { let k = e.keyCode || e.charCode; return DomHandler.getBrowser().safari ? (SAFARI_KEYS[k] || k) : k; } getCharCode(e) { return e.charCode || e.keyCode || e.which; } findDelta(value, prevValue) { let delta = ''; for (let i = 0; i < value.length; i++) { let str = value.substr(0, i) + value.substr(i + value.length - prevValue.length); if (str === prevValue) delta = value.substr(i, value.length - prevValue.length); } return delta; } isValidChar(c) { return this.regex.test(c); } isValidString(str) { for (let i = 0; i < str.length; i++) { if (!this.isValidChar(str.substr(i, 1))) { return false; } } return true; } onInput(e) { if (this.isAndroid && !this.pValidateOnly) { let val = this.el.nativeElement.value; let lastVal = this.lastValue || ''; let inserted = this.findDelta(val, lastVal); let removed = this.findDelta(lastVal, val); let pasted = inserted.length > 1 || (!inserted && !removed); if (pasted) { if (!this.isValidString(val)) { this.el.nativeElement.value = lastVal; this.ngModelChange.emit(lastVal); } } else if (!removed) { if (!this.isValidChar(inserted)) { this.el.nativeElement.value = lastVal; this.ngModelChange.emit(lastVal); } } val = this.el.nativeElement.value; if (this.isValidString(val)) { this.lastValue = val; } } } onKeyPress(e) { if (this.isAndroid || this.pValidateOnly) { return; } let browser = DomHandler.getBrowser(); let k = this.getKey(e); if (browser.mozilla && (e.ctrlKey || e.altKey)) { return; } else if (k == 17 || k == 18) { return; } let c = this.getCharCode(e); let cc = String.fromCharCode(c); let ok = true; if (!browser.mozilla && (this.isSpecialKey(e) || !cc)) { return; } ok = this.regex.test(cc); if (!ok) { e.preventDefault(); } } onPaste(e) { const clipboardData = e.clipboardData || window.clipboardData.getData('text'); if (clipboardData) { const pastedText = clipboardData.getData('text'); for (let char of pastedText.toString()) { if (!this.regex.test(char)) { e.preventDefault(); return; } } } } validate(c) { if (this.pValidateOnly) { let value = this.el.nativeElement.value; if (value && !this.regex.test(value)) { return { validatePattern: false }; } } } }; KeyFilter.ctorParameters = () => [ { type: ElementRef } ]; __decorate([ Input() ], KeyFilter.prototype, "pValidateOnly", void 0); __decorate([ Output() ], KeyFilter.prototype, "ngModelChange", void 0); __decorate([ Input('pKeyFilter') ], KeyFilter.prototype, "pattern", null); __decorate([ HostListener('input', ['$event']) ], KeyFilter.prototype, "onInput", null); __decorate([ HostListener('keypress', ['$event']) ], KeyFilter.prototype, "onKeyPress", null); __decorate([ HostListener('paste', ['$event']) ], KeyFilter.prototype, "onPaste", null); KeyFilter = __decorate([ Directive({ selector: '[pKeyFilter]', providers: [KEYFILTER_VALIDATOR] }) ], KeyFilter); let KeyFilterModule = class KeyFilterModule { }; KeyFilterModule = __decorate([ NgModule({ imports: [CommonModule], exports: [KeyFilter], declarations: [KeyFilter] }) ], KeyFilterModule); /** * Generated bundle index. Do not edit. */ export { KEYFILTER_VALIDATOR, KeyFilter, KeyFilterModule }; //# sourceMappingURL=primeng-keyfilter.js.map