This repository has been archived by the owner on May 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 356
/
mask.js
375 lines (332 loc) · 14.8 KB
/
mask.js
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
Attaches input mask onto input element
*/
angular.module('ui.directives').directive('uiMask', [
function () {
var maskDefinitions = {
'9': /\d/,
'A': /[a-zA-Z]/,
'*': /[a-zA-Z0-9]/
};
return {
priority: 100,
require: 'ngModel',
restrict: 'A',
link: function (scope, iElement, iAttrs, controller) {
var maskProcessed = false, eventsBound = false,
maskCaretMap, maskPatterns, maskPlaceholder,
value, valueMasked, isValid,
// Vars for initializing/uninitializing
originalPlaceholder = iAttrs.placeholder,
originalMaxlength = iAttrs.maxlength,
// Vars used exclusively in eventHandler()
oldValue, oldValueUnmasked, oldCaretPosition, oldSelectionLength;
iAttrs.$observe('uiMask', initialize);
controller.$formatters.push(formatter);
controller.$parsers.push(parser);
function initialize(maskAttr) {
if (!angular.isDefined(maskAttr))
return uninitialize();
processRawMask(maskAttr);
if (!maskProcessed)
return uninitialize();
initializeElement();
bindEventListeners();
}
function uninitialize() {
maskProcessed = false;
unbindEventListeners();
if (angular.isDefined(originalPlaceholder))
iElement.attr('placeholder', originalPlaceholder);
else
iElement.removeAttr('placeholder');
if (angular.isDefined(originalMaxlength))
iElement.attr('maxlength', originalMaxlength);
else
iElement.removeAttr('maxlength');
iElement.val(controller.$modelValue);
controller.$viewValue = controller.$modelValue;
return false;
}
function initializeElement() {
value = oldValueUnmasked = unmaskValue(controller.$modelValue || '');
valueMasked = oldValue = maskValue(value);
isValid = validateValue(value);
viewValue = isValid && value.length ? valueMasked : '';
if (iAttrs.maxlength) // Double maxlength to allow pasting new val at end of mask
iElement.attr('maxlength', maskCaretMap[maskCaretMap.length-1]*2);
iElement.attr('placeholder', maskPlaceholder);
iElement.val(viewValue);
controller.$viewValue = viewValue;
// Not using $setViewValue so we don't clobber the model value and dirty the form
// without any kind of user interaction.
}
function bindEventListeners() {
if (eventsBound)
return true;
iElement.bind('blur', blurHandler);
iElement.bind('input propertychange keyup click mouseout', eventHandler);
eventsBound = true;
}
function unbindEventListeners() {
if (!eventsBound)
return true;
iElement.unbind('blur', blurHandler);
iElement.unbind('input', eventHandler);
iElement.unbind('propertychange', eventHandler);
iElement.unbind('keyup', eventHandler);
iElement.unbind('click', eventHandler);
iElement.unbind('mouseout', eventHandler);
eventsBound = false;
}
function formatter(fromModelValue) {
if (!maskProcessed)
return fromModelValue;
value = unmaskValue(fromModelValue || '');
isValid = validateValue(value);
controller.$setValidity('mask', isValid);
return isValid ? maskValue(value) : undefined;
}
function parser(fromViewValue) {
if (!maskProcessed)
return fromViewValue;
value = unmaskValue(fromViewValue || '');
isValid = validateValue(value);
viewValue = value.length ? maskValue(value) : '';
// We have to set viewValue manually as the reformatting of the input
// value performed by eventHandler() doesn't happen until after
// this parser is called, which causes what the user sees in the input
// to be out-of-sync with what the controller's $viewValue is set to.
controller.$viewValue = viewValue;
controller.$setValidity('mask', isValid);
if (value === '' && controller.$error.required !== undefined)
controller.$setValidity('required', false);
return isValid ? value : undefined;
}
function validateValue(value) {
// Zero-length value validity is ngRequired's determination
return value.length ? value.length === maskCaretMap.length - 1 : true;
}
function unmaskValue(value) {
var valueUnmasked = '',
maskPatternCopys = maskPatterns.slice();
angular.forEach(value.toString().split(''), function(chr, i) {
if (maskPatternCopys.length && maskPatternCopys[0].test(chr)) {
valueUnmasked += chr;
maskPatternCopys.shift();
}
});
return valueUnmasked;
}
function maskValue(unmaskedValue) {
var valueMasked = '',
maskCaretMapCopy = maskCaretMap.slice();
angular.forEach(maskPlaceholder.split(''), function(chr, i) {
if (unmaskedValue.length && i === maskCaretMapCopy[0]) {
valueMasked += unmaskedValue.charAt(0) || '_';
unmaskedValue = unmaskedValue.substr(1);
maskCaretMapCopy.shift(); }
else
valueMasked += chr;
});
return valueMasked;
}
function processRawMask(mask) {
var characterCount = 0;
maskCaretMap = [];
maskPatterns = [];
maskPlaceholder = '';
// If mask is an array, it's a complex mask!
if (mask instanceof Array) {
angular.forEach(mask, function(item, i) {
if (item instanceof RegExp) {
maskCaretMap.push(characterCount++);
maskPlaceholder += '_';
maskPatterns.push(item);
}
else if (typeof item == 'string') {
angular.forEach(item.split(''), function(chr, i) {
maskPlaceholder += chr;
characterCount++;
});
}
});
}
// Otherwise it's a simple mask
else if (typeof mask === 'string') {
angular.forEach(mask.split(''), function(chr, i) {
if (maskDefinitions[chr]) {
maskCaretMap.push(characterCount);
maskPlaceholder += '_';
maskPatterns.push(maskDefinitions[chr]);
}
else
maskPlaceholder += chr;
characterCount++;
});
}
// Caret position immediately following last position is valid.
maskCaretMap.push(maskCaretMap.slice().pop() + 1);
maskProcessed = maskCaretMap.length > 1 ? true : false;
}
function blurHandler(e) {
oldCaretPosition = 0;
oldSelectionLength = 0;
if (!isValid || value.length === 0) {
valueMasked = '';
iElement.val('');
scope.$apply(function() {
controller.$setViewValue('');
});
}
}
function eventHandler(e) {
// Allows more efficient minification
var eventWhich = e.which,
eventType = e.type;
// Prevent shift and ctrl from mucking with old values
if (eventWhich == 16 || eventWhich == 91) return true;
var elem = iElement,
val = elem.val(),
valOld = oldValue,
valMasked,
valUnmasked = unmaskValue(val),
valUnmaskedOld = oldValueUnmasked,
valAltered = false,
caretPos = getCaretPosition(this) || 0,
caretPosOld = oldCaretPosition || 0,
caretPosDelta = caretPos - caretPosOld,
caretPosMin = maskCaretMap[0],
caretPosMax = maskCaretMap[valUnmasked.length] || maskCaretMap.slice().shift(),
selectionLen = getSelectionLength(this),
selectionLenOld = oldSelectionLength || 0,
isSelected = selectionLen > 0,
wasSelected = selectionLenOld > 0,
// Case: Typing a character to overwrite a selection
isAddition = (val.length > valOld.length) || (selectionLenOld && val.length > valOld.length - selectionLenOld),
// Case: Delete and backspace behave identically on a selection
isDeletion = (val.length < valOld.length) || (selectionLenOld && val.length == valOld.length - selectionLenOld),
isSelection = (eventWhich >= 37 && eventWhich <= 40) && e.shiftKey, // Arrow key codes
isKeyLeftArrow = eventWhich == 37,
// Necessary due to "input" event not providing a key code
isKeyBackspace = eventWhich == 8 || (eventType != 'keyup' && isDeletion && (caretPosDelta === -1)),
isKeyDelete = eventWhich == 46 || (eventType != 'keyup' && isDeletion && (caretPosDelta === 0 ) && !wasSelected),
// Handles cases where caret is moved and placed in front of invalid maskCaretMap position. Logic below
// ensures that, on click or leftward caret placement, caret is moved leftward until directly right of
// non-mask character. Also applied to click since users are (arguably) more likely to backspace
// a character when clicking within a filled input.
caretBumpBack = (isKeyLeftArrow || isKeyBackspace || eventType == 'click') && caretPos > caretPosMin;
oldSelectionLength = selectionLen;
// These events don't require any action
if (eventType == 'mouseout' || isSelection || (isSelected && (eventType == 'click' || eventType == 'keyup')))
return true;
// Value Handling
// ==============
// User attempted to delete but raw value was unaffected--correct this grievous offense
if ((eventType == 'input' || eventType == 'propertychange') && isDeletion && !wasSelected && valUnmasked === valUnmaskedOld) {
while (isKeyBackspace && caretPos > 0 && !isValidCaretPosition(caretPos))
caretPos--;
while (isKeyDelete && caretPos < maskPlaceholder.length && maskCaretMap.indexOf(caretPos) == -1)
caretPos++;
var charIndex = maskCaretMap.indexOf(caretPos);
// Strip out non-mask character that user would have deleted if mask hadn't been in the way.
valUnmasked = valUnmasked.substring(0, charIndex) + valUnmasked.substring(charIndex + 1);
valAltered = true;
}
// Update values
valMasked = maskValue(valUnmasked);
oldValue = valMasked;
oldValueUnmasked = valUnmasked;
elem.val(valMasked);
if (valAltered) {
// We've altered the raw value after it's been $digest'ed, we need to $apply the new value.
scope.$apply(function() {
controller.$setViewValue(valUnmasked);
});
}
// Caret Repositioning
// ===================
// Ensure that typing always places caret ahead of typed character in cases where the first char of
// the input is a mask char and the caret is placed at the 0 position.
if (isAddition && (caretPos <= caretPosMin))
caretPos = caretPosMin + 1;
if (caretBumpBack)
caretPos--;
// Make sure caret is within min and max position limits
caretPos = caretPos > caretPosMax ? caretPosMax : caretPos < caretPosMin ? caretPosMin : caretPos;
// Scoot the caret back or forth until it's in a non-mask position and within min/max position limits
while (!isValidCaretPosition(caretPos) && caretPos > caretPosMin && caretPos < caretPosMax)
caretPos += caretBumpBack ? -1 : 1;
if ((caretBumpBack && caretPos < caretPosMax) || (isAddition && !isValidCaretPosition(caretPosOld)))
caretPos++;
oldCaretPosition = caretPos;
setCaretPosition(this, caretPos);
}
function isValidCaretPosition(pos) { return maskCaretMap.indexOf(pos) > -1; }
function getCaretPosition(input) {
if (input.selectionStart !== undefined)
return input.selectionStart;
else if (document.selection) {
// Curse you IE
input.focus();
var selection = document.selection.createRange();
selection.moveStart('character', -input.value.length);
return selection.text.length;
}
}
function setCaretPosition(input, pos) {
if (input.setSelectionRange) {
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();
}
}
function getSelectionLength(input) {
if (input.selectionStart !== undefined)
return (input.selectionEnd - input.selectionStart);
if (document.selection)
return (document.selection.createRange().text.length);
}
// https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
"use strict";
if (this === null) {
throw new TypeError();
}
var t = Object(this);
var len = t.length >>> 0;
if (len === 0) {
return -1;
}
var n = 0;
if (arguments.length > 1) {
n = Number(arguments[1]);
if (n != n) { // shortcut for verifying if it's NaN
n = 0;
} else if (n !== 0 && n !== Infinity && n !== -Infinity) {
n = (n > 0 || -1) * Math.floor(Math.abs(n));
}
}
if (n >= len) {
return -1;
}
var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
for (; k < len; k++) {
if (k in t && t[k] === searchElement) {
return k;
}
}
return -1;
};
}
}
};
}
]);