UNPKG

24.4 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/common'), require('primeng/dom'), require('primeng/inputtext'), require('@angular/forms')) :
3 typeof define === 'function' && define.amd ? define('primeng/inputmask', ['exports', '@angular/core', '@angular/common', 'primeng/dom', 'primeng/inputtext', '@angular/forms'], factory) :
4 (global = global || self, factory((global.primeng = global.primeng || {}, global.primeng.inputmask = {}), global.ng.core, global.ng.common, global.primeng.dom, global.primeng.inputtext, global.ng.forms));
5}(this, (function (exports, core, common, dom, inputtext, forms) { 'use strict';
6
7 var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
8 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
9 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
10 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;
11 return c > 3 && r && Object.defineProperty(target, key, r), r;
12 };
13 var INPUTMASK_VALUE_ACCESSOR = {
14 provide: forms.NG_VALUE_ACCESSOR,
15 useExisting: core.forwardRef(function () { return InputMask; }),
16 multi: true
17 };
18 var InputMask = /** @class */ (function () {
19 function InputMask(el) {
20 this.el = el;
21 this.type = 'text';
22 this.slotChar = '_';
23 this.autoClear = true;
24 this.characterPattern = '[A-Za-z]';
25 this.onComplete = new core.EventEmitter();
26 this.onFocus = new core.EventEmitter();
27 this.onBlur = new core.EventEmitter();
28 this.onInput = new core.EventEmitter();
29 this.onModelChange = function () { };
30 this.onModelTouched = function () { };
31 }
32 InputMask.prototype.ngOnInit = function () {
33 var ua = dom.DomHandler.getUserAgent();
34 this.androidChrome = /chrome/i.test(ua) && /android/i.test(ua);
35 this.initMask();
36 };
37 Object.defineProperty(InputMask.prototype, "mask", {
38 get: function () {
39 return this._mask;
40 },
41 set: function (val) {
42 this._mask = val;
43 this.initMask();
44 this.writeValue('');
45 this.onModelChange(this.value);
46 },
47 enumerable: true,
48 configurable: true
49 });
50 InputMask.prototype.initMask = function () {
51 this.tests = [];
52 this.partialPosition = this.mask.length;
53 this.len = this.mask.length;
54 this.firstNonMaskPos = null;
55 this.defs = {
56 '9': '[0-9]',
57 'a': this.characterPattern,
58 '*': this.characterPattern + "|[0-9]"
59 };
60 var maskTokens = this.mask.split('');
61 for (var i = 0; i < maskTokens.length; i++) {
62 var c = maskTokens[i];
63 if (c == '?') {
64 this.len--;
65 this.partialPosition = i;
66 }
67 else if (this.defs[c]) {
68 this.tests.push(new RegExp(this.defs[c]));
69 if (this.firstNonMaskPos === null) {
70 this.firstNonMaskPos = this.tests.length - 1;
71 }
72 if (i < this.partialPosition) {
73 this.lastRequiredNonMaskPos = this.tests.length - 1;
74 }
75 }
76 else {
77 this.tests.push(null);
78 }
79 }
80 this.buffer = [];
81 for (var i = 0; i < maskTokens.length; i++) {
82 var c = maskTokens[i];
83 if (c != '?') {
84 if (this.defs[c])
85 this.buffer.push(this.getPlaceholder(i));
86 else
87 this.buffer.push(c);
88 }
89 }
90 this.defaultBuffer = this.buffer.join('');
91 };
92 InputMask.prototype.writeValue = function (value) {
93 this.value = value;
94 if (this.inputViewChild && this.inputViewChild.nativeElement) {
95 if (this.value == undefined || this.value == null)
96 this.inputViewChild.nativeElement.value = '';
97 else
98 this.inputViewChild.nativeElement.value = this.value;
99 this.checkVal();
100 this.focusText = this.inputViewChild.nativeElement.value;
101 this.updateFilledState();
102 }
103 };
104 InputMask.prototype.registerOnChange = function (fn) {
105 this.onModelChange = fn;
106 };
107 InputMask.prototype.registerOnTouched = function (fn) {
108 this.onModelTouched = fn;
109 };
110 InputMask.prototype.setDisabledState = function (val) {
111 this.disabled = val;
112 };
113 InputMask.prototype.caret = function (first, last) {
114 var range, begin, end;
115 if (!this.inputViewChild.nativeElement.offsetParent || this.inputViewChild.nativeElement !== document.activeElement) {
116 return;
117 }
118 if (typeof first == 'number') {
119 begin = first;
120 end = (typeof last === 'number') ? last : begin;
121 if (this.inputViewChild.nativeElement.setSelectionRange) {
122 this.inputViewChild.nativeElement.setSelectionRange(begin, end);
123 }
124 else if (this.inputViewChild.nativeElement['createTextRange']) {
125 range = this.inputViewChild.nativeElement['createTextRange']();
126 range.collapse(true);
127 range.moveEnd('character', end);
128 range.moveStart('character', begin);
129 range.select();
130 }
131 }
132 else {
133 if (this.inputViewChild.nativeElement.setSelectionRange) {
134 begin = this.inputViewChild.nativeElement.selectionStart;
135 end = this.inputViewChild.nativeElement.selectionEnd;
136 }
137 else if (document['selection'] && document['selection'].createRange) {
138 range = document['selection'].createRange();
139 begin = 0 - range.duplicate().moveStart('character', -100000);
140 end = begin + range.text.length;
141 }
142 return { begin: begin, end: end };
143 }
144 };
145 InputMask.prototype.isCompleted = function () {
146 var completed;
147 for (var i = this.firstNonMaskPos; i <= this.lastRequiredNonMaskPos; i++) {
148 if (this.tests[i] && this.buffer[i] === this.getPlaceholder(i)) {
149 return false;
150 }
151 }
152 return true;
153 };
154 InputMask.prototype.getPlaceholder = function (i) {
155 if (i < this.slotChar.length) {
156 return this.slotChar.charAt(i);
157 }
158 return this.slotChar.charAt(0);
159 };
160 InputMask.prototype.seekNext = function (pos) {
161 while (++pos < this.len && !this.tests[pos])
162 ;
163 return pos;
164 };
165 InputMask.prototype.seekPrev = function (pos) {
166 while (--pos >= 0 && !this.tests[pos])
167 ;
168 return pos;
169 };
170 InputMask.prototype.shiftL = function (begin, end) {
171 var i, j;
172 if (begin < 0) {
173 return;
174 }
175 for (i = begin, j = this.seekNext(end); i < this.len; i++) {
176 if (this.tests[i]) {
177 if (j < this.len && this.tests[i].test(this.buffer[j])) {
178 this.buffer[i] = this.buffer[j];
179 this.buffer[j] = this.getPlaceholder(j);
180 }
181 else {
182 break;
183 }
184 j = this.seekNext(j);
185 }
186 }
187 this.writeBuffer();
188 this.caret(Math.max(this.firstNonMaskPos, begin));
189 };
190 InputMask.prototype.shiftR = function (pos) {
191 var i, c, j, t;
192 for (i = pos, c = this.getPlaceholder(pos); i < this.len; i++) {
193 if (this.tests[i]) {
194 j = this.seekNext(i);
195 t = this.buffer[i];
196 this.buffer[i] = c;
197 if (j < this.len && this.tests[j].test(t)) {
198 c = t;
199 }
200 else {
201 break;
202 }
203 }
204 }
205 };
206 InputMask.prototype.handleAndroidInput = function (e) {
207 var _this = this;
208 var curVal = this.inputViewChild.nativeElement.value;
209 var pos = this.caret();
210 if (this.oldVal && this.oldVal.length && this.oldVal.length > curVal.length) {
211 // a deletion or backspace happened
212 this.checkVal(true);
213 while (pos.begin > 0 && !this.tests[pos.begin - 1])
214 pos.begin--;
215 if (pos.begin === 0) {
216 while (pos.begin < this.firstNonMaskPos && !this.tests[pos.begin])
217 pos.begin++;
218 }
219 setTimeout(function () {
220 _this.caret(pos.begin, pos.begin);
221 _this.updateModel(e);
222 if (_this.isCompleted()) {
223 _this.onComplete.emit();
224 }
225 }, 0);
226 }
227 else {
228 this.checkVal(true);
229 while (pos.begin < this.len && !this.tests[pos.begin])
230 pos.begin++;
231 setTimeout(function () {
232 _this.caret(pos.begin, pos.begin);
233 _this.updateModel(e);
234 if (_this.isCompleted()) {
235 _this.onComplete.emit();
236 }
237 }, 0);
238 }
239 };
240 InputMask.prototype.onInputBlur = function (e) {
241 this.focused = false;
242 this.onModelTouched();
243 this.checkVal();
244 this.updateFilledState();
245 this.onBlur.emit(e);
246 if (this.inputViewChild.nativeElement.value != this.focusText || this.inputViewChild.nativeElement.value != this.value) {
247 this.updateModel(e);
248 var event_1 = document.createEvent('HTMLEvents');
249 event_1.initEvent('change', true, false);
250 this.inputViewChild.nativeElement.dispatchEvent(event_1);
251 }
252 };
253 InputMask.prototype.onKeyDown = function (e) {
254 if (this.readonly) {
255 return;
256 }
257 var k = e.which || e.keyCode, pos, begin, end;
258 var iPhone = /iphone/i.test(dom.DomHandler.getUserAgent());
259 this.oldVal = this.inputViewChild.nativeElement.value;
260 //backspace, delete, and escape get special treatment
261 if (k === 8 || k === 46 || (iPhone && k === 127)) {
262 pos = this.caret();
263 begin = pos.begin;
264 end = pos.end;
265 if (end - begin === 0) {
266 begin = k !== 46 ? this.seekPrev(begin) : (end = this.seekNext(begin - 1));
267 end = k === 46 ? this.seekNext(end) : end;
268 }
269 this.clearBuffer(begin, end);
270 this.shiftL(begin, end - 1);
271 this.updateModel(e);
272 this.onInput.emit(e);
273 e.preventDefault();
274 }
275 else if (k === 13) { // enter
276 this.onInputBlur(e);
277 this.updateModel(e);
278 }
279 else if (k === 27) { // escape
280 this.inputViewChild.nativeElement.value = this.focusText;
281 this.caret(0, this.checkVal());
282 this.updateModel(e);
283 e.preventDefault();
284 }
285 };
286 InputMask.prototype.onKeyPress = function (e) {
287 var _this = this;
288 if (this.readonly) {
289 return;
290 }
291 var k = e.which || e.keyCode, pos = this.caret(), p, c, next, completed;
292 if (e.ctrlKey || e.altKey || e.metaKey || k < 32 || (k > 34 && k < 41)) { //Ignore
293 return;
294 }
295 else if (k && k !== 13) {
296 if (pos.end - pos.begin !== 0) {
297 this.clearBuffer(pos.begin, pos.end);
298 this.shiftL(pos.begin, pos.end - 1);
299 }
300 p = this.seekNext(pos.begin - 1);
301 if (p < this.len) {
302 c = String.fromCharCode(k);
303 if (this.tests[p].test(c)) {
304 this.shiftR(p);
305 this.buffer[p] = c;
306 this.writeBuffer();
307 next = this.seekNext(p);
308 if (/android/i.test(dom.DomHandler.getUserAgent())) {
309 //Path for CSP Violation on FireFox OS 1.1
310 var proxy = function () {
311 _this.caret(next);
312 };
313 setTimeout(proxy, 0);
314 }
315 else {
316 this.caret(next);
317 }
318 if (pos.begin <= this.lastRequiredNonMaskPos) {
319 completed = this.isCompleted();
320 }
321 this.onInput.emit(e);
322 }
323 }
324 e.preventDefault();
325 }
326 this.updateModel(e);
327 this.updateFilledState();
328 if (completed) {
329 this.onComplete.emit();
330 }
331 };
332 InputMask.prototype.clearBuffer = function (start, end) {
333 var i;
334 for (i = start; i < end && i < this.len; i++) {
335 if (this.tests[i]) {
336 this.buffer[i] = this.getPlaceholder(i);
337 }
338 }
339 };
340 InputMask.prototype.writeBuffer = function () {
341 this.inputViewChild.nativeElement.value = this.buffer.join('');
342 };
343 InputMask.prototype.checkVal = function (allow) {
344 //try to place characters where they belong
345 var test = this.inputViewChild.nativeElement.value, lastMatch = -1, i, c, pos;
346 for (i = 0, pos = 0; i < this.len; i++) {
347 if (this.tests[i]) {
348 this.buffer[i] = this.getPlaceholder(i);
349 while (pos++ < test.length) {
350 c = test.charAt(pos - 1);
351 if (this.tests[i].test(c)) {
352 this.buffer[i] = c;
353 lastMatch = i;
354 break;
355 }
356 }
357 if (pos > test.length) {
358 this.clearBuffer(i + 1, this.len);
359 break;
360 }
361 }
362 else {
363 if (this.buffer[i] === test.charAt(pos)) {
364 pos++;
365 }
366 if (i < this.partialPosition) {
367 lastMatch = i;
368 }
369 }
370 }
371 if (allow) {
372 this.writeBuffer();
373 }
374 else if (lastMatch + 1 < this.partialPosition) {
375 if (this.autoClear || this.buffer.join('') === this.defaultBuffer) {
376 // Invalid value. Remove it and replace it with the
377 // mask, which is the default behavior.
378 if (this.inputViewChild.nativeElement.value)
379 this.inputViewChild.nativeElement.value = '';
380 this.clearBuffer(0, this.len);
381 }
382 else {
383 // Invalid value, but we opt to show the value to the
384 // user and allow them to correct their mistake.
385 this.writeBuffer();
386 }
387 }
388 else {
389 this.writeBuffer();
390 this.inputViewChild.nativeElement.value = this.inputViewChild.nativeElement.value.substring(0, lastMatch + 1);
391 }
392 return (this.partialPosition ? i : this.firstNonMaskPos);
393 };
394 InputMask.prototype.onInputFocus = function (event) {
395 var _this = this;
396 if (this.readonly) {
397 return;
398 }
399 this.focused = true;
400 clearTimeout(this.caretTimeoutId);
401 var pos;
402 this.focusText = this.inputViewChild.nativeElement.value;
403 pos = this.checkVal();
404 this.caretTimeoutId = setTimeout(function () {
405 if (_this.inputViewChild.nativeElement !== document.activeElement) {
406 return;
407 }
408 _this.writeBuffer();
409 if (pos == _this.mask.replace("?", "").length) {
410 _this.caret(0, pos);
411 }
412 else {
413 _this.caret(pos);
414 }
415 }, 10);
416 this.onFocus.emit(event);
417 };
418 InputMask.prototype.onInputChange = function (event) {
419 if (this.androidChrome)
420 this.handleAndroidInput(event);
421 else
422 this.handleInputChange(event);
423 this.onInput.emit(event);
424 };
425 InputMask.prototype.handleInputChange = function (event) {
426 var _this = this;
427 if (this.readonly) {
428 return;
429 }
430 setTimeout(function () {
431 var pos = _this.checkVal(true);
432 _this.caret(pos);
433 _this.updateModel(event);
434 if (_this.isCompleted()) {
435 _this.onComplete.emit();
436 }
437 }, 0);
438 };
439 InputMask.prototype.getUnmaskedValue = function () {
440 var unmaskedBuffer = [];
441 for (var i = 0; i < this.buffer.length; i++) {
442 var c = this.buffer[i];
443 if (this.tests[i] && c != this.getPlaceholder(i)) {
444 unmaskedBuffer.push(c);
445 }
446 }
447 return unmaskedBuffer.join('');
448 };
449 InputMask.prototype.updateModel = function (e) {
450 var updatedValue = this.unmask ? this.getUnmaskedValue() : e.target.value;
451 if (updatedValue !== null || updatedValue !== undefined) {
452 this.value = updatedValue;
453 this.onModelChange(this.value);
454 }
455 };
456 InputMask.prototype.updateFilledState = function () {
457 this.filled = this.inputViewChild.nativeElement && this.inputViewChild.nativeElement.value != '';
458 };
459 InputMask.prototype.focus = function () {
460 this.inputViewChild.nativeElement.focus();
461 };
462 InputMask.prototype.ngOnDestroy = function () {
463 };
464 InputMask.ctorParameters = function () { return [
465 { type: core.ElementRef }
466 ]; };
467 __decorate([
468 core.Input()
469 ], InputMask.prototype, "type", void 0);
470 __decorate([
471 core.Input()
472 ], InputMask.prototype, "slotChar", void 0);
473 __decorate([
474 core.Input()
475 ], InputMask.prototype, "autoClear", void 0);
476 __decorate([
477 core.Input()
478 ], InputMask.prototype, "style", void 0);
479 __decorate([
480 core.Input()
481 ], InputMask.prototype, "inputId", void 0);
482 __decorate([
483 core.Input()
484 ], InputMask.prototype, "styleClass", void 0);
485 __decorate([
486 core.Input()
487 ], InputMask.prototype, "placeholder", void 0);
488 __decorate([
489 core.Input()
490 ], InputMask.prototype, "size", void 0);
491 __decorate([
492 core.Input()
493 ], InputMask.prototype, "maxlength", void 0);
494 __decorate([
495 core.Input()
496 ], InputMask.prototype, "tabindex", void 0);
497 __decorate([
498 core.Input()
499 ], InputMask.prototype, "title", void 0);
500 __decorate([
501 core.Input()
502 ], InputMask.prototype, "ariaLabel", void 0);
503 __decorate([
504 core.Input()
505 ], InputMask.prototype, "ariaRequired", void 0);
506 __decorate([
507 core.Input()
508 ], InputMask.prototype, "disabled", void 0);
509 __decorate([
510 core.Input()
511 ], InputMask.prototype, "readonly", void 0);
512 __decorate([
513 core.Input()
514 ], InputMask.prototype, "unmask", void 0);
515 __decorate([
516 core.Input()
517 ], InputMask.prototype, "name", void 0);
518 __decorate([
519 core.Input()
520 ], InputMask.prototype, "required", void 0);
521 __decorate([
522 core.Input()
523 ], InputMask.prototype, "characterPattern", void 0);
524 __decorate([
525 core.Input()
526 ], InputMask.prototype, "autoFocus", void 0);
527 __decorate([
528 core.Input()
529 ], InputMask.prototype, "autocomplete", void 0);
530 __decorate([
531 core.ViewChild('input')
532 ], InputMask.prototype, "inputViewChild", void 0);
533 __decorate([
534 core.Output()
535 ], InputMask.prototype, "onComplete", void 0);
536 __decorate([
537 core.Output()
538 ], InputMask.prototype, "onFocus", void 0);
539 __decorate([
540 core.Output()
541 ], InputMask.prototype, "onBlur", void 0);
542 __decorate([
543 core.Output()
544 ], InputMask.prototype, "onInput", void 0);
545 __decorate([
546 core.Input()
547 ], InputMask.prototype, "mask", null);
548 InputMask = __decorate([
549 core.Component({
550 selector: 'p-inputMask',
551 template: "<input #input pInputText [attr.id]=\"inputId\" [attr.type]=\"type\" [attr.name]=\"name\" [ngStyle]=\"style\" [ngClass]=\"styleClass\" [attr.placeholder]=\"placeholder\" [attr.title]=\"title\"\n [attr.size]=\"size\" [attr.autocomplete]=\"autocomplete\" [attr.maxlength]=\"maxlength\" [attr.tabindex]=\"tabindex\" [attr.aria-label]=\"ariaLabel\" [attr.aria-required]=\"ariaRequired\" [disabled]=\"disabled\" [readonly]=\"readonly\" [attr.required]=\"required\"\n (focus)=\"onInputFocus($event)\" (blur)=\"onInputBlur($event)\" (keydown)=\"onKeyDown($event)\" (keypress)=\"onKeyPress($event)\" [attr.autofocus]=\"autoFocus\"\n (input)=\"onInputChange($event)\" (paste)=\"handleInputChange($event)\">",
552 host: {
553 '[class.ui-inputwrapper-filled]': 'filled',
554 '[class.ui-inputwrapper-focus]': 'focused'
555 },
556 providers: [INPUTMASK_VALUE_ACCESSOR],
557 changeDetection: core.ChangeDetectionStrategy.Default
558 })
559 ], InputMask);
560 return InputMask;
561 }());
562 var InputMaskModule = /** @class */ (function () {
563 function InputMaskModule() {
564 }
565 InputMaskModule = __decorate([
566 core.NgModule({
567 imports: [common.CommonModule, inputtext.InputTextModule],
568 exports: [InputMask],
569 declarations: [InputMask]
570 })
571 ], InputMaskModule);
572 return InputMaskModule;
573 }());
574
575 exports.INPUTMASK_VALUE_ACCESSOR = INPUTMASK_VALUE_ACCESSOR;
576 exports.InputMask = InputMask;
577 exports.InputMaskModule = InputMaskModule;
578
579 Object.defineProperty(exports, '__esModule', { value: true });
580
581})));
582//# sourceMappingURL=primeng-inputmask.umd.js.map