1 | import * as i0 from '@angular/core';
|
---|
2 | import { forwardRef, EventEmitter, Directive, Input, Output, HostListener, NgModule } from '@angular/core';
|
---|
3 | import { CommonModule } from '@angular/common';
|
---|
4 | import { DomHandler } from 'primeng/dom';
|
---|
5 | import { NG_VALIDATORS } from '@angular/forms';
|
---|
6 |
|
---|
7 | const KEYFILTER_VALIDATOR = {
|
---|
8 | provide: NG_VALIDATORS,
|
---|
9 | useExisting: forwardRef(() => KeyFilter),
|
---|
10 | multi: true
|
---|
11 | };
|
---|
12 | const DEFAULT_MASKS = {
|
---|
13 | pint: /[\d]/,
|
---|
14 | 'int': /[\d\-]/,
|
---|
15 | pnum: /[\d\.]/,
|
---|
16 | money: /[\d\.\s,]/,
|
---|
17 | num: /[\d\-\.]/,
|
---|
18 | hex: /[0-9a-f]/i,
|
---|
19 | email: /[a-z0-9_\.\-@]/i,
|
---|
20 | alpha: /[a-z_]/i,
|
---|
21 | alphanum: /[a-z0-9_]/i
|
---|
22 | };
|
---|
23 | const KEYS = {
|
---|
24 | TAB: 9,
|
---|
25 | RETURN: 13,
|
---|
26 | ESC: 27,
|
---|
27 | BACKSPACE: 8,
|
---|
28 | DELETE: 46
|
---|
29 | };
|
---|
30 | const SAFARI_KEYS = {
|
---|
31 | 63234: 37,
|
---|
32 | 63235: 39,
|
---|
33 | 63232: 38,
|
---|
34 | 63233: 40,
|
---|
35 | 63276: 33,
|
---|
36 | 63277: 34,
|
---|
37 | 63272: 46,
|
---|
38 | 63273: 36,
|
---|
39 | 63275: 35 // end
|
---|
40 | };
|
---|
41 | class KeyFilter {
|
---|
42 | constructor(el) {
|
---|
43 | this.el = el;
|
---|
44 | this.ngModelChange = new EventEmitter();
|
---|
45 | this.isAndroid = DomHandler.isAndroid();
|
---|
46 | }
|
---|
47 | get pattern() {
|
---|
48 | return this._pattern;
|
---|
49 | }
|
---|
50 | set pattern(_pattern) {
|
---|
51 | this._pattern = _pattern;
|
---|
52 | this.regex = DEFAULT_MASKS[this._pattern] || this._pattern;
|
---|
53 | }
|
---|
54 | isNavKeyPress(e) {
|
---|
55 | let k = e.keyCode;
|
---|
56 | k = DomHandler.getBrowser().safari ? (SAFARI_KEYS[k] || k) : k;
|
---|
57 | return (k >= 33 && k <= 40) || k == KEYS.RETURN || k == KEYS.TAB || k == KEYS.ESC;
|
---|
58 | }
|
---|
59 | ;
|
---|
60 | isSpecialKey(e) {
|
---|
61 | let k = e.keyCode || e.charCode;
|
---|
62 | return k == 9 || k == 13 || k == 27 || k == 16 || k == 17 || (k >= 18 && k <= 20) ||
|
---|
63 | (DomHandler.getBrowser().opera && !e.shiftKey && (k == 8 || (k >= 33 && k <= 35) || (k >= 36 && k <= 39) || (k >= 44 && k <= 45)));
|
---|
64 | }
|
---|
65 | getKey(e) {
|
---|
66 | let k = e.keyCode || e.charCode;
|
---|
67 | return DomHandler.getBrowser().safari ? (SAFARI_KEYS[k] || k) : k;
|
---|
68 | }
|
---|
69 | getCharCode(e) {
|
---|
70 | return e.charCode || e.keyCode || e.which;
|
---|
71 | }
|
---|
72 | findDelta(value, prevValue) {
|
---|
73 | let delta = '';
|
---|
74 | for (let i = 0; i < value.length; i++) {
|
---|
75 | let str = value.substr(0, i) + value.substr(i + value.length - prevValue.length);
|
---|
76 | if (str === prevValue)
|
---|
77 | delta = value.substr(i, value.length - prevValue.length);
|
---|
78 | }
|
---|
79 | return delta;
|
---|
80 | }
|
---|
81 | isValidChar(c) {
|
---|
82 | return this.regex.test(c);
|
---|
83 | }
|
---|
84 | isValidString(str) {
|
---|
85 | for (let i = 0; i < str.length; i++) {
|
---|
86 | if (!this.isValidChar(str.substr(i, 1))) {
|
---|
87 | return false;
|
---|
88 | }
|
---|
89 | }
|
---|
90 | return true;
|
---|
91 | }
|
---|
92 | onInput(e) {
|
---|
93 | if (this.isAndroid && !this.pValidateOnly) {
|
---|
94 | let val = this.el.nativeElement.value;
|
---|
95 | let lastVal = this.lastValue || '';
|
---|
96 | let inserted = this.findDelta(val, lastVal);
|
---|
97 | let removed = this.findDelta(lastVal, val);
|
---|
98 | let pasted = inserted.length > 1 || (!inserted && !removed);
|
---|
99 | if (pasted) {
|
---|
100 | if (!this.isValidString(val)) {
|
---|
101 | this.el.nativeElement.value = lastVal;
|
---|
102 | this.ngModelChange.emit(lastVal);
|
---|
103 | }
|
---|
104 | }
|
---|
105 | else if (!removed) {
|
---|
106 | if (!this.isValidChar(inserted)) {
|
---|
107 | this.el.nativeElement.value = lastVal;
|
---|
108 | this.ngModelChange.emit(lastVal);
|
---|
109 | }
|
---|
110 | }
|
---|
111 | val = this.el.nativeElement.value;
|
---|
112 | if (this.isValidString(val)) {
|
---|
113 | this.lastValue = val;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|
117 | onKeyPress(e) {
|
---|
118 | if (this.isAndroid || this.pValidateOnly) {
|
---|
119 | return;
|
---|
120 | }
|
---|
121 | let browser = DomHandler.getBrowser();
|
---|
122 | let k = this.getKey(e);
|
---|
123 | if (browser.mozilla && (e.ctrlKey || e.altKey)) {
|
---|
124 | return;
|
---|
125 | }
|
---|
126 | else if (k == 17 || k == 18) {
|
---|
127 | return;
|
---|
128 | }
|
---|
129 | let c = this.getCharCode(e);
|
---|
130 | let cc = String.fromCharCode(c);
|
---|
131 | let ok = true;
|
---|
132 | if (!browser.mozilla && (this.isSpecialKey(e) || !cc)) {
|
---|
133 | return;
|
---|
134 | }
|
---|
135 | ok = this.regex.test(cc);
|
---|
136 | if (!ok) {
|
---|
137 | e.preventDefault();
|
---|
138 | }
|
---|
139 | }
|
---|
140 | onPaste(e) {
|
---|
141 | const clipboardData = e.clipboardData || window.clipboardData.getData('text');
|
---|
142 | if (clipboardData) {
|
---|
143 | const pastedText = clipboardData.getData('text');
|
---|
144 | for (let char of pastedText.toString()) {
|
---|
145 | if (!this.regex.test(char)) {
|
---|
146 | e.preventDefault();
|
---|
147 | return;
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|
151 | }
|
---|
152 | validate(c) {
|
---|
153 | if (this.pValidateOnly) {
|
---|
154 | let value = this.el.nativeElement.value;
|
---|
155 | if (value && !this.regex.test(value)) {
|
---|
156 | return {
|
---|
157 | validatePattern: false
|
---|
158 | };
|
---|
159 | }
|
---|
160 | }
|
---|
161 | }
|
---|
162 | }
|
---|
163 | KeyFilter.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.0.0", ngImport: i0, type: KeyFilter, deps: [{ token: i0.ElementRef }], target: i0.ɵɵFactoryTarget.Directive });
|
---|
164 | KeyFilter.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "12.0.0", version: "13.0.0", type: KeyFilter, selector: "[pKeyFilter]", inputs: { pValidateOnly: "pValidateOnly", pattern: ["pKeyFilter", "pattern"] }, outputs: { ngModelChange: "ngModelChange" }, host: { listeners: { "input": "onInput($event)", "keypress": "onKeyPress($event)", "paste": "onPaste($event)" }, classAttribute: "p-element" }, providers: [KEYFILTER_VALIDATOR], ngImport: i0 });
|
---|
165 | i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.0.0", ngImport: i0, type: KeyFilter, decorators: [{
|
---|
166 | type: Directive,
|
---|
167 | args: [{
|
---|
168 | selector: '[pKeyFilter]',
|
---|
169 | providers: [KEYFILTER_VALIDATOR],
|
---|
170 | host: {
|
---|
171 | 'class': 'p-element'
|
---|
172 | }
|
---|
173 | }]
|
---|
174 | }], ctorParameters: function () { return [{ type: i0.ElementRef }]; }, propDecorators: { pValidateOnly: [{
|
---|
175 | type: Input
|
---|
176 | }], ngModelChange: [{
|
---|
177 | type: Output
|
---|
178 | }], pattern: [{
|
---|
179 | type: Input,
|
---|
180 | args: ['pKeyFilter']
|
---|
181 | }], onInput: [{
|
---|
182 | type: HostListener,
|
---|
183 | args: ['input', ['$event']]
|
---|
184 | }], onKeyPress: [{
|
---|
185 | type: HostListener,
|
---|
186 | args: ['keypress', ['$event']]
|
---|
187 | }], onPaste: [{
|
---|
188 | type: HostListener,
|
---|
189 | args: ['paste', ['$event']]
|
---|
190 | }] } });
|
---|
191 | class KeyFilterModule {
|
---|
192 | }
|
---|
193 | KeyFilterModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.0.0", ngImport: i0, type: KeyFilterModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
|
---|
194 | KeyFilterModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "13.0.0", ngImport: i0, type: KeyFilterModule, declarations: [KeyFilter], imports: [CommonModule], exports: [KeyFilter] });
|
---|
195 | KeyFilterModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "13.0.0", ngImport: i0, type: KeyFilterModule, imports: [[CommonModule]] });
|
---|
196 | i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.0.0", ngImport: i0, type: KeyFilterModule, decorators: [{
|
---|
197 | type: NgModule,
|
---|
198 | args: [{
|
---|
199 | imports: [CommonModule],
|
---|
200 | exports: [KeyFilter],
|
---|
201 | declarations: [KeyFilter]
|
---|
202 | }]
|
---|
203 | }] });
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * Generated bundle index. Do not edit.
|
---|
207 | */
|
---|
208 |
|
---|
209 | export { KEYFILTER_VALIDATOR, KeyFilter, KeyFilterModule };
|
---|