-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmaskedinput.android.ts
224 lines (201 loc) · 9.81 KB
/
maskedinput.android.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import * as application from 'application';
import common = require("./maskedinput-common");
import dependencyObservable = require("ui/core/dependency-observable");
import utils = require("utils/utils");
global.moduleMerge(common, exports);
export class MaskedInput extends common.MaskedInput {
private textBefore: string;
private selectionBefore: number;
private editTextChange: boolean;
private newIndex: number;
private _android: android.widget.EditText;
get android(): android.widget.EditText {
return this._android;
}
public _createUI() {
// console.log("_createUI");
this._android = new android.widget.EditText(this._context);
this._configureEditText();
let that = new WeakRef(this);
let focusChangeListener = new android.view.View.OnFocusChangeListener({
onFocusChange: function(view: android.view.View, hasFocus: boolean) {
// console.log("onFocusChange");
let owner = that.get();
if (!owner) {
return;
}
if (!hasFocus) {
// console.log("Lost Focus!");
owner.dismissSoftInput();
owner.bypassEvent = true;
owner.text = owner.FormattedText;
}
else {
// console.log("Gain Focus");
owner.initialText = false; //Set initial to false because when field is not bound, initialText is still set to true.
owner.focus();
owner.editTextChange = true;
owner.text = owner.stringBuilder;
}
}
});
this.android.setOnFocusChangeListener(focusChangeListener);
this.buildRegEx();
}
public _configureEditText() {
// console.log("_configureEditText");
let that = new WeakRef(this);
this.initialText = true; //Always for first creation
this.android.addTextChangedListener(new android.text.TextWatcher({
beforeTextChanged(s: string, index: number, toBeReplaced: number, addedCount: number): void {
let owner = that.get();
// console.log("beforeTextChanged");
// console.log("isUpdate: " + isUpdate);
owner.textBefore = s.toString();
owner.selectionBefore = owner.android.getSelectionEnd();
// console.log("s: " + s);
// console.log("stringBuilder:" + owner.stringBuilder);
// console.log("index: " + index);
// console.log("toBeReplaced: " + toBeReplaced);
// console.log("addedCount: " + addedCount);
},
onTextChanged(s: string, index: number, replacedCount: number, addedCount: number): void {
let owner = that.get();
// console.log("onTextChanged");
if (owner.bypassEvent) {
// console.log("bypassEvent");
owner.initialText = false;
owner.editTextChange = false;
owner.bypassEvent = false;
return;
}
else if (owner.initialText) {
let sbIdx: number = 0;
let s1: string = s.toString(); //Force String Type
// console.log("initialText");
// console.log("s: " + s1);
// console.log("FormattedText: " + owner.FormattedText);
// console.log("s.length: " + s1.length);
// console.log("Pre stringBuilder: " + owner.stringBuilder);
for (let i = 0; i < s1.length; i++) {
// console.log("regex test:" + owner.testCharAtIndex(s1.charAt(i),sbIdx));
if (owner.testCharAtIndex(s1.charAt(i), sbIdx)) {
//This works for FormattedText
owner.replacePlaceholder(sbIdx, s1.charAt(i));
sbIdx++;
}
else {
//Try to convert RawText
// console.log("next placeholder index: " + owner.findIndex());
let nextIdx = owner.findIndex();
// console.log("regex test:" + owner.testCharAtIndex(s1.charAt(i),nextIdx));
if (owner.testCharAtIndex(s1.charAt(i), nextIdx)) {
owner.replacePlaceholder(nextIdx, s1.charAt(i));
sbIdx = nextIdx + 1;
}
}
// console.log("sbIdx: " + sbIdx.toString());
}
// console.log("Post stringBuilder: " + owner.stringBuilder);
owner.bypassEvent = true;
owner.text = owner.FormattedText;
//owner.initialText = false;
// owner.editTextChange = true;
return;
}
else if (owner.editTextChange) {
// console.log("editTextChange");
// console.log("owner.newIndex: " + owner.newIndex);
if (owner.newIndex) {
owner.android.setSelection(owner.newIndex);
}
else{
owner.android.setSelection(owner.findIndex());
}
owner.setInputTypeBasedOnMask();
owner.editTextChange = false;
return;
}
try {
// console.log("s: " + s);
// console.log("index: " + index);
// console.log("toBeReplaced: " + replacedCount);
// console.log("addedCount: " + addedCount);
// console.log("stringBuilder Length:" + owner.stringBuilder.length);
//Override Events
//Handle Delete/Backspace
if (replacedCount > 0 && addedCount === 0) {
// console.log("backspace");
owner.editTextChange = true;
owner.replacePlaceholder(index, owner.placeholder);
return;
}
//Handle backspace hack for last index, otherwise fatal error.
else if (index + replacedCount === owner.stringBuilder.length) {
// console.log("backspace hack");
// console.log("s.length: " + s.toString().length);
// console.log("owner.textBefore: " + owner.textBefore);
owner.editTextChange = true;
if(s.toString().length > owner.stringBuilder.length){
//Additional Character Test after last index
owner.android.setText(owner.textBefore);
}else{
//Last Index Backspace/Delete
owner.replacePlaceholder(owner.stringBuilder.length - 1, owner.placeholder);
}
return;
}
//End Override Events
//Handle Text Change
if (!owner.editTextChange) {
owner.editTextChange = true;
let newChar: string = s.charAt(index);
//Test Char for Pattern
// console.log("newChar: " + newChar);
// console.log("regex test:" + owner.testCharAtIndex(newChar,index));
if (owner.testCharAtIndex(newChar, index)) {
owner.replacePlaceholder(index, newChar);
// console.log("new builder:" + that.stringBuilder);
// that.android.setText(that.stringBuilder);
// that.android.setSelection(that.findIndex());
}
else {
// console.log("prevString: " + owner.textBefore);
owner.android.setText(owner.textBefore);
// owner.text = owner.textBefore;
// owner.android.setSelection(owner.findIndex());
}
}
else {
owner.editTextChange = false;
}
}
catch (e) {
// console.log("textChange Catch");
owner.editTextChange = true;
owner.newIndex = owner.selectionBefore;
owner.android.setText(owner.textBefore);
// owner.text = owner.textBefore;
}
},
afterTextChanged(s: android.text.Editable): void {
let owner = that.get();
// console.log("afterTextChanged");
// console.log("editTextChange: " + owner.editTextChange);
// console.log("initialText: " + owner.initialText);
if (owner.initialText) {
owner.initialText = false;
return;
}
if (owner.editTextChange) {
// console.log("stringBuilder: " + owner.stringBuilder);
// console.log("stringBuilder Length:" + owner.stringBuilder.length);
owner.text = owner.stringBuilder;
owner.android.setSelection(owner.findIndex());
owner.setInputTypeBasedOnMask();
// console.log("full RegEx: " + owner.regEx);
}
}
}));
}
}