Skip to content

Commit 0b6c2a1

Browse files
refactor(igxMask): have input parsing go through one method
1 parent 04dc9c5 commit 0b6c2a1

File tree

3 files changed

+79
-208
lines changed

3 files changed

+79
-208
lines changed

projects/igniteui-angular/src/lib/core/utils.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,13 @@ export const enum KEYCODES {
139139
RIGHT_ARROW = 39,
140140
DOWN_ARROW = 40,
141141
F2 = 113,
142-
TAB = 9
142+
TAB = 9,
143+
Ctrl = 17,
144+
Z = 90,
145+
Y = 89,
146+
X = 88,
147+
BACKSPACE = 8,
148+
DELETE = 46
143149
}
144150

145151
/**

projects/igniteui-angular/src/lib/directives/mask/mask-parsing.service.ts

+47-183
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,6 @@ import { Injectable } from '@angular/core';
55
*/
66
export const MASK_FLAGS = ['C', '&', 'a', 'A', '?', 'L', '9', '0', '#'];
77

8-
/**
9-
* @hidden
10-
*/
11-
export const KEYS = {
12-
Ctrl: 17,
13-
Z: 90,
14-
Y: 89,
15-
X: 88,
16-
BACKSPACE: 8,
17-
DELETE: 46
18-
};
19-
208
/**
219
* @hidden
2210
*/
@@ -29,67 +17,6 @@ export class MaskParsingService {
2917
return this._cursor;
3018
}
3119

32-
public parseValueWithoutSelection(value, maskOptions, cursor): string {
33-
let inputValue: string = value;
34-
const mask: string = maskOptions.format;
35-
const literals: Map<number, string> = this.getMaskLiterals(mask);
36-
const literalKeys: number[] = Array.from(literals.keys());
37-
const nonLiteralIndices: number[] = this.getNonLiteralIndeces(mask, literalKeys);
38-
39-
if (inputValue.length < mask.length) { // BACKSPACE, DELETE
40-
if (inputValue === '' && cursor === -1) {
41-
this._cursor = 0;
42-
return this.parseValueByMaskOnInit(value, maskOptions);
43-
} // workaround for IE 'x' button
44-
45-
if (nonLiteralIndices.indexOf(cursor + 1) !== -1) {
46-
inputValue = this.insertCharAt(inputValue, cursor + 1, maskOptions.promptChar);
47-
this._cursor = cursor + 1;
48-
} else {
49-
inputValue = this.insertCharAt(inputValue, cursor + 1, mask[cursor + 1]);
50-
this._cursor = cursor + 1;
51-
for (let i = this._cursor; i < 0; i--) {
52-
if (literalKeys.indexOf(this._cursor) !== -1) {
53-
this._cursor--;
54-
} else {
55-
break;
56-
}
57-
}
58-
}
59-
} else {
60-
const char = inputValue[cursor];
61-
let isCharValid = this.validateCharOnPosition(char, cursor, mask);
62-
if (nonLiteralIndices.indexOf(cursor) !== -1) {
63-
inputValue = this.replaceCharAt(inputValue, cursor, '');
64-
if (isCharValid) {
65-
inputValue = this.replaceCharAt(inputValue, cursor, char);
66-
this._cursor = cursor + 1;
67-
} else {
68-
this._cursor = cursor;
69-
}
70-
} else {
71-
inputValue = this.replaceCharAt(inputValue, cursor, '');
72-
this._cursor = ++cursor;
73-
for (let i = cursor; i < mask.length; i++) {
74-
if (literalKeys.indexOf(this._cursor) !== -1) {
75-
this._cursor = ++cursor;
76-
} else {
77-
isCharValid = this.validateCharOnPosition(char, cursor, mask);
78-
if (isCharValid) {
79-
inputValue = this.replaceCharAt(inputValue, cursor, char);
80-
this._cursor = ++cursor;
81-
break;
82-
} else {
83-
break;
84-
}
85-
}
86-
}
87-
}
88-
}
89-
90-
return inputValue;
91-
}
92-
9320
public parseMask(maskOptions): string {
9421
let outputVal = '';
9522
const mask: string = maskOptions.format;
@@ -106,13 +33,13 @@ export class MaskParsingService {
10633
return outputVal;
10734
}
10835

109-
public parseValueByMaskOnInit(inputVal, maskOptions): string {
36+
public parseMaskOnInit(inputVal, maskOptions): string {
11037
let outputVal = '';
11138
let value = '';
11239
const mask: string = maskOptions.format;
11340
const literals: Map<number, string> = this.getMaskLiterals(mask);
11441
const literalKeys: number[] = Array.from(literals.keys());
115-
const nonLiteralIndeces: number[] = this.getNonLiteralIndeces(mask, literalKeys);
42+
const nonLiteralIndeces: number[] = this.getNonLiteralIndices(mask, literalKeys);
11643
const literalValues: string[] = Array.from(literals.values());
11744

11845
if (inputVal != null) {
@@ -172,135 +99,72 @@ export class MaskParsingService {
17299
return outputVal;
173100
}
174101

175-
public parseValueWithSelection(value, maskOptions, cursor, selection, hasDeleteAction): string {
176-
let isCharValid: boolean;
177-
let inputValue: string = value;
178-
const char: string = inputValue[cursor];
102+
public parseMaskValue(value, inputText, maskOptions, cursor, clipboardData, selection, hasDeleteAction?): string {
179103
const mask: string = maskOptions.format;
180104
const literals: Map<number, string> = this.getMaskLiterals(mask);
181105
const literalKeys: number[] = Array.from(literals.keys());
182-
const nonLiteralIndeces: number[] = this.getNonLiteralIndeces(mask, literalKeys);
183-
184-
if (!hasDeleteAction) {
185-
this._cursor = cursor < 0 ? ++cursor : cursor;
186-
if (nonLiteralIndeces.indexOf(this._cursor) !== -1) {
187-
isCharValid = this.validateCharOnPosition(char, this._cursor, mask);
188-
inputValue = isCharValid ? this.replaceCharAt(inputValue, this._cursor++, char) :
189-
inputValue = this.replaceCharAt(inputValue, this._cursor++, maskOptions.promptChar);
190-
selection--;
191-
if (selection > 0) {
192-
for (let i = 0; i < selection; i++) {
193-
cursor++;
194-
inputValue = nonLiteralIndeces.indexOf(cursor) !== -1 ?
195-
this.insertCharAt(inputValue, cursor, maskOptions.promptChar) :
196-
this.insertCharAt(inputValue, cursor, mask[cursor]);
197-
}
106+
const nonLiteralIndices: number[] = this.getNonLiteralIndices(mask, literalKeys);
107+
const selectionEnd = cursor + selection;
108+
109+
if (hasDeleteAction) {
110+
if (inputText === '') {
111+
this._cursor = 0;
112+
return this.parseMaskOnInit(inputText, maskOptions);
113+
}
114+
let i = 0;
115+
this._cursor = ++cursor;
116+
do {
117+
inputText = this.updateValue(nonLiteralIndices, inputText, cursor++, maskOptions, mask);
118+
} while (++i < selection);
119+
value = inputText;
120+
} else {
121+
this._cursor = cursor;
122+
for (const char of clipboardData) {
123+
if (this._cursor > mask.length) {
124+
return value;
198125
}
199-
} else {
200-
inputValue = this.replaceCharAt(inputValue, this._cursor, mask[this._cursor]);
201-
this._cursor++;
202-
selection--;
203-
let isMarked = false;
204-
if (selection > 0) {
205-
cursor = this._cursor;
206-
for (let i = 0; i < selection; i++) {
207-
if (nonLiteralIndeces.indexOf(cursor) !== -1) {
208-
isCharValid = this.validateCharOnPosition(char, cursor, mask);
209-
if (isCharValid && !isMarked) {
210-
inputValue = this.insertCharAt(inputValue, cursor, char);
211-
cursor++;
212-
this._cursor++;
213-
isMarked = true;
214-
} else {
215-
inputValue = this.insertCharAt(inputValue, cursor, maskOptions.promptChar);
216-
cursor++;
217-
}
126+
127+
if (nonLiteralIndices.indexOf(this._cursor) !== -1) {
128+
const isCharValid = this.validateCharOnPosition(char, this._cursor, mask);
129+
if (isCharValid) {
130+
value = this.replaceCharAt(value, this._cursor++, char);
131+
}
132+
} else {
133+
for (let i = cursor; i < mask.length; i++) {
134+
if (literalKeys.indexOf(this._cursor) !== -1) {
135+
this._cursor++;
218136
} else {
219-
inputValue = this.insertCharAt(inputValue, cursor, mask[cursor]);
220-
if (cursor === this._cursor) {
221-
this._cursor++;
137+
const isCharValid = this.validateCharOnPosition(char, this._cursor, mask);
138+
if (isCharValid) {
139+
value = this.replaceCharAt(value, this._cursor++, char);
222140
}
223-
cursor++;
141+
break;
224142
}
225143
}
226144
}
227-
}
228-
} else {
229-
if (inputValue === '' && cursor === -1) {
230-
this._cursor = 0;
231-
return this.parseValueByMaskOnInit(value, maskOptions);
232-
} // workaround for IE 'x' button
233-
234-
if (this._cursor < 0) {
235-
this._cursor++;
236-
cursor++;
237-
}
238-
cursor++;
239-
this._cursor = cursor;
240-
for (let i = 0; i < selection; i++) {
241-
if (nonLiteralIndeces.indexOf(cursor) !== -1) {
242-
inputValue = this.insertCharAt(inputValue, cursor, maskOptions.promptChar);
243-
cursor++;
244-
} else {
245-
inputValue = this.insertCharAt(inputValue, cursor, mask[cursor]);
246-
cursor++;
247-
}
248-
}
249-
}
250145

251-
return inputValue;
252-
}
253-
254-
public parseValueOnPaste(value, maskOptions, cursor, clipboardData, selection): string {
255-
let inputValue: string = value;
256-
const mask: string = maskOptions.format;
257-
const literals: Map<number, string> = this.getMaskLiterals(mask);
258-
const literalKeys: number[] = Array.from(literals.keys());
259-
const nonLiteralIndices: number[] = this.getNonLiteralIndeces(mask, literalKeys);
260-
261-
const selectionEnd = cursor + selection;
262-
263-
this._cursor = cursor;
264-
for (const char of clipboardData) {
265-
if (this._cursor > mask.length) {
266-
return inputValue;
146+
selection--;
267147
}
268148

269-
if (nonLiteralIndices.indexOf(this._cursor) !== -1) {
270-
const isCharValid = this.validateCharOnPosition(char, this._cursor, mask);
271-
if (isCharValid) {
272-
inputValue = this.replaceCharAt(inputValue, this._cursor++, char);
273-
}
274-
} else {
275-
for (let i = cursor; i < mask.length; i++) {
149+
if (selection > 0) {
150+
for (let i = this._cursor; i < selectionEnd; i++) {
276151
if (literalKeys.indexOf(this._cursor) !== -1) {
277152
this._cursor++;
278153
} else {
279-
const isCharValid = this.validateCharOnPosition(char, this._cursor, mask);
280-
if (isCharValid) {
281-
inputValue = this.replaceCharAt(inputValue, this._cursor++, char);
282-
}
283-
break;
154+
value = this.replaceCharAt(value, this._cursor++, maskOptions.promptChar);
284155
}
285156
}
286157
}
287-
288-
selection--;
289158
}
290159

291-
if (selection > 0) {
292-
for (let i = this._cursor; i < selectionEnd; i++) {
293-
if (literalKeys.indexOf(this._cursor) !== -1) {
294-
this._cursor++;
295-
} else {
296-
inputValue = this.replaceCharAt(inputValue, this._cursor++, maskOptions.promptChar);
297-
}
298-
}
299-
}
300-
301-
return inputValue;
160+
return value;
302161
}
303162

163+
private updateValue(nonLiteralIndices, value, cursorPosition, maskOptions, mask) {
164+
return nonLiteralIndices.indexOf(cursorPosition) !== -1 ?
165+
this.insertCharAt(value, cursorPosition, maskOptions.promptChar) :
166+
this.insertCharAt(value, cursorPosition, mask[cursorPosition]);
167+
}
304168
private validateCharOnPosition(inputChar: string, position: number, mask: string): boolean {
305169
let regex: RegExp;
306170
let isValid: boolean;
@@ -377,7 +241,7 @@ export class MaskParsingService {
377241

378242
return literals;
379243
}
380-
private getNonLiteralIndeces(mask: string, literalKeys: number[]): number[] {
244+
private getNonLiteralIndices(mask: string, literalKeys: number[]): number[] {
381245
const nonLiteralsIndeces: number[] = new Array();
382246

383247
for (let i = 0; i < mask.length; i++) {

0 commit comments

Comments
 (0)