-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssn.controller.js
143 lines (130 loc) · 4.86 KB
/
ssn.controller.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
// Copyright (c) 2016 Amanda Murphy
// This code is available under the "MIT License".
// Please see the file COPYING in this distribution
// for license terms.
class SsnController {
constructor($scope) {
'ngInject';
this.$scope = $scope;
SsnController.storedSsn = $scope.storedSsn;
this.firstDash = 1;
this.secondDash = 2;
this.noDashesYet = 3;
this.hasOneDash = 6;
this.maxMaskedSsnLength = 11;
}
isFocused(elem) {
return elem === document.activeElement && (!document.hasFocus || document.hasFocus()) && !!(elem.type || elem.href || ~elem.tabIndex);
}
getCaretPosition(input) {
if (!input) {
return 0;
}
if (input.selectionStart != undefined) {
return input.selectionStart;
}
return 0;
}
setCaretPosition(input, pos) {
if (!input)
return 0;
if (input.offsetWidth === 0 || input.offsetHeight === 0) {
return;
}
if (input.setSelectionRange && this.isFocused(input)) {
input.focus();
input.setSelectionRange(pos, pos);
}
}
repeat(str, times) {
return (new Array(times + 1)).join(str);
}
isDisplayedSsnBiggerThanSsn(displayedSsnLength, ssnLength) { //we need to consider the dashes when testing sizes
if(ssnLength == null && displayedSsnLength != null) {
return true;
}
if(displayedSsnLength <= this.noDashesYet) {
return (displayedSsnLength > ssnLength)
}
else if(displayedSsnLength <= this.hasOneDash) {
return ((displayedSsnLength - this.firstDash) > ssnLength)
}
else {
return ((displayedSsnLength - this.secondDash) > ssnLength)
}
}
parseSsn(displayedSsn, Ssn) {
if(displayedSsn.length <= this.noDashesYet) {
return displayedSsn.slice(Ssn.length, displayedSsn.length);
}
else if (displayedSsn.length <= this.hasOneDash) {
return displayedSsn.slice((Ssn.length + this.firstDash), displayedSsn.length);
}
else {
return displayedSsn.slice((Ssn.length + this.secondDash), displayedSsn.length);
}
}
generateMaskedSsn(length, displayedSsn, storedSsn) {
if(length == 0) { //this is only for when an ssn is passed in when the page is loaded
return ('***-**-' + storedSsn.slice(5, storedSsn.length));
}
if(length < this.noDashesYet) {
return this.repeat('*', length);
}
if(length == this.noDashesYet) {
return ('***-');
}
if(length < this.hasOneDash) {
return ('***-' + this.repeat('*', (length - 4)));
}
if (length == this.hasOneDash) {
return ('***-**-');
}
return displayedSsn;
}
deleteKeyPressedSsn(ssn, length) {
if(length < this.noDashesYet) {
return ssn.slice(0, length);
}
if(length < this.hasOneDash) {
return ssn.slice(0, (length - this.firstDash));
}
return ssn.slice(0, (length - this.secondDash));
}
maskSsn(elem, storedSsn) {
var len = elem[0].value.length;
var displayed = elem[0].value;
if(storedSsn == null) {
storedSsn = "";
} //if the actual ssn is null initialize it
var storedValue = storedSsn;
if(len > this.maxMaskedSsnLength) { //do not allow more than 11 characters
elem[0].value = elem[0].value.slice(0, this.maxMaskedSsnLength);
return storedSsn;
}
var notIllegalCharRegex = /\d|\*|-/i.exec(elem[0].value.slice(len-1, len));
if(notIllegalCharRegex == null && len != 0) { //if an illegal character is entered delete it and leave.
elem[0].value = elem[0].value.slice(0, len-this.firstDash);
return storedSsn;
}
//if the move the curser somewhere in the middle move it back and restore the input
var caretPos = this.getCaretPosition(elem[0]) || 0;
if(caretPos != len) {
this.setCaretPosition(elem[0], len);
elem[0].value = this.generateMaskedSsn(0, '', storedValue);
return storedSsn;
}
if(this.isDisplayedSsnBiggerThanSsn(len, storedValue.length)) {
storedSsn += this.parseSsn(displayed, storedValue);
elem[0].value = this.generateMaskedSsn(len, displayed, storedValue);
}
else { //the delete key was pressed
if(len == this.noDashesYet || len == this.hasOneDash) { //if they deleted a dash removed last digit as well
elem[0].value = displayed.slice(0, (len - this.firstDash));
}
storedSsn = this.deleteKeyPressedSsn(storedValue, len);
}
return storedSsn;
}
}
export default SsnController;