@@ -5,18 +5,6 @@ import { Injectable } from '@angular/core';
5
5
*/
6
6
export const MASK_FLAGS = [ 'C' , '&' , 'a' , 'A' , '?' , 'L' , '9' , '0' , '#' ] ;
7
7
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
-
20
8
/**
21
9
* @hidden
22
10
*/
@@ -29,67 +17,6 @@ export class MaskParsingService {
29
17
return this . _cursor ;
30
18
}
31
19
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
-
93
20
public parseMask ( maskOptions ) : string {
94
21
let outputVal = '' ;
95
22
const mask : string = maskOptions . format ;
@@ -106,13 +33,13 @@ export class MaskParsingService {
106
33
return outputVal ;
107
34
}
108
35
109
- public parseValueByMaskOnInit ( inputVal , maskOptions ) : string {
36
+ public parseMaskOnInit ( inputVal , maskOptions ) : string {
110
37
let outputVal = '' ;
111
38
let value = '' ;
112
39
const mask : string = maskOptions . format ;
113
40
const literals : Map < number , string > = this . getMaskLiterals ( mask ) ;
114
41
const literalKeys : number [ ] = Array . from ( literals . keys ( ) ) ;
115
- const nonLiteralIndeces : number [ ] = this . getNonLiteralIndeces ( mask , literalKeys ) ;
42
+ const nonLiteralIndeces : number [ ] = this . getNonLiteralIndices ( mask , literalKeys ) ;
116
43
const literalValues : string [ ] = Array . from ( literals . values ( ) ) ;
117
44
118
45
if ( inputVal != null ) {
@@ -172,135 +99,72 @@ export class MaskParsingService {
172
99
return outputVal ;
173
100
}
174
101
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 {
179
103
const mask : string = maskOptions . format ;
180
104
const literals : Map < number , string > = this . getMaskLiterals ( mask ) ;
181
105
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 ;
198
125
}
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 ++ ;
218
136
} 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 ) ;
222
140
}
223
- cursor ++ ;
141
+ break ;
224
142
}
225
143
}
226
144
}
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
- }
250
145
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 -- ;
267
147
}
268
148
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 ++ ) {
276
151
if ( literalKeys . indexOf ( this . _cursor ) !== - 1 ) {
277
152
this . _cursor ++ ;
278
153
} 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 ) ;
284
155
}
285
156
}
286
157
}
287
-
288
- selection -- ;
289
158
}
290
159
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 ;
302
161
}
303
162
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
+ }
304
168
private validateCharOnPosition ( inputChar : string , position : number , mask : string ) : boolean {
305
169
let regex : RegExp ;
306
170
let isValid : boolean ;
@@ -377,7 +241,7 @@ export class MaskParsingService {
377
241
378
242
return literals ;
379
243
}
380
- private getNonLiteralIndeces ( mask : string , literalKeys : number [ ] ) : number [ ] {
244
+ private getNonLiteralIndices ( mask : string , literalKeys : number [ ] ) : number [ ] {
381
245
const nonLiteralsIndeces : number [ ] = new Array ( ) ;
382
246
383
247
for ( let i = 0 ; i < mask . length ; i ++ ) {
0 commit comments