-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmask-behavior.html
195 lines (177 loc) · 6.26 KB
/
mask-behavior.html
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
<script src="https://unpkg.com/string-mask@0.3.0/src/string-mask.js"></script>
<script>
MaskBehavior = {
properties: {
mask: {
type: String,
value: "(000) 000-0000",
notify: true
},
formattedValue: {
type:String,
observer: 'changeValue'
},
value: {
type: String,
reflectToAttribute: true,
notify: true,
observer: 'addMask'
},
doMask: {
type: Boolean,
value: true
},
preformat: {
type: Boolean,
value: false
},
inputIndex: {
type: Number
},
changeVal: {
type: Boolean,
value: true
}
},
listeners: {
'focusin': 'receivedFocusMask',
'tap': 'receivedFocusMask',
'keyup': 'enterInfo'
},
receivedFocusMask: function() {
var el = this.$$('input');
var cursorPosition = this.getCursorPosition();
this.setCaretPosition(el,this.formattedValue.indexOf("_"));
},
enterInfo: function(val) {
if (val.key == 'Backspace') {
var el = this.$$('input');
var startPos = el.selectionStart;
var endPos = el.selectionEnd;
console.log("start:"+startPos+" end:"+endPos);
var lastLetter = this.formattedValue.substring(endPos-1,endPos);
console.log("lastLetter:"+lastLetter);
var goBack = 0;
while (!lastLetter.match(/\d/)) {
if (this.value.length ==0) {
return;
}
// fail-safe
if(goBack > 20) {
return;
}
goBack++;
lastLetter = this.formattedValue.substring(endPos-(goBack+1),endPos-goBack);
}
this.setCaretPosition(el, endPos-goBack);
}
},
created: function() {
},
setCaretPosition: function(input, pos) {
//input.focus();
if (!input)
return 0;
if (input.offsetWidth === 0 || input.offsetHeight === 0) {
return; // Input's hidden
}
if (input.setSelectionRange) {
//if (isFocused(iElement[0])) {
input.focus();
input.setSelectionRange(pos, pos);
//}
}
else if (input.createTextRange) {
// Curse you IE
var range = input.createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
//console.log("Moved cursor to:"+pos);
},
ready: function() {
},
_strip: function(val) {
var rv = "";
if (val) {
rv = val.replace(/[()_-]/g,'');
rv = rv.replace(/\s/g,'');
}
return rv;
},
convertMask: function() {
return this.mask.replace(/[0]/g,'_');
},
getCursorPosition: function() {
var index = this.value.length;
return this.formattedValue.split("_", index).join("_").length;
},
changeValue: function(newval, oldval) {
console.log("CHANGEVALUE. old:",oldval+" new:"+newval);
// all we want to do is edit the value here
var newActual = this._strip(newval);
var oldActual = this._strip(oldval);
console.log("Set value to:"+newActual);
this.value = newActual;
this.addMask(this.value);
},
removeChars: function(val) {
var lastLetter = val.substring(val.length-1);
var remove = 0;
while (lastLetter =='-' || lastLetter == ' ' || lastLetter == ')') {
remove++;
lastLetter = val.substring(val.length-remove+1);
}
return remove;
},
addMask: function(newval,oldval) {
console.log("MASKING old:"+oldval+" new:"+newval+" actual value:"+this.value);
//if (newActual != oldActual) {
var formatter = new StringMask(this.mask);
var masked = formatter.apply(newval);
//console.log("Masked value:"+masked);
//if (newval != masked) {
//console.log("replacing value with masked:"+masked);
if (this.preformat) {
// now replace everything still missing in the mask with underscores
masked = this.replaceRemaining();
//console.log("preformatted mask:",masked);
}
console.log("Setting masked value to:"+masked);
this.formattedValue = masked;
if (this.preformat) {
var el = this.$$('input');
var cursorPosition = this.getCursorPosition();
this.setCaretPosition(el,this.formattedValue.indexOf("_"));
}
//}
//}
},
replaceRemaining: function() {
var replaced = "";
var valueIndex = 0;
for (var i=0; i < this.mask.length; i++) {
var letter = this.mask[i];
if (letter == '0') {
if (this.value[valueIndex]) {
replaced += this.value[valueIndex];
valueIndex++;
} else {
replaced += "_";
}
} else {
replaced += this.mask[i];
}
}
return replaced;
}
};
</script>
<!--
DELETE SCENARIO WHEN WE SHOULD HAVE REMOVED THE 9 AT THE END:
CHANGEVALUE. old: (322) 349-____ new:(322) 349____
or
CHANGEVALUE. old: (343) ___-____ new:(343)___-____
-->