-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpasswordRevealer.js
More file actions
92 lines (75 loc) · 2.45 KB
/
passwordRevealer.js
File metadata and controls
92 lines (75 loc) · 2.45 KB
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
class PasswordRevealer {
element = '';
eyeEl;
strengthEl;
prContainer;
errorMessage = "You must pass and ID in the constructor";
constructor(inputId) { //metodo que vai ser chamado assim que a classe for instanciada
if (!inputId) { //!Input ID é igual a false se tiver !
throw new Error(this.errorMessage);
}
this.getElement(inputId); //metodos
this.build();
this.strength();
this.showPassword();
}
getElement(inputId) {
this.element = document.getElementById(inputId);
if (this.element === null){
throw new Error("Element not found.");
} else if (this.element.nodeName !== "INPUT"){
throw new Error ("Element is not an Input.");
}
}
build() {
this.prContainer = document.createElement('div');
this.element.parentNode.insertBefore(this.prContainer, this.element.nextSibling);
this.prContainer.className = 'pr-container';
this.element.className = 'pr-password';
this.prContainer.appendChild(this.element);
this.eyeEl = document.createElement('i');
this.eyeEl.className = 'fas fa-eye-slash pr-eye';
this.eyeEl.id = 'prEye';
this.prContainer.appendChild(this.eyeEl);
this.strengthEl = document.createElement('i');
this.strengthEl.className = 'pr-strength';
this.strengthEl.id = 'prStrength';
this.prContainer.appendChild(this.strengthEl);
}
strength(){
const input = this.element;
let strength = this.strengthEl;
input.addEventListener('keyup', function(){
let length = input.value.length;
let styleColor = '';
let className = '';
if (length <=3) {
className = "far fa-frown pr-strength";
styleColor = "red";
} else if (length >= 4 && length <= 6) {
className = "far fa-meh pr-strength";
styleColor = "orange";
} else if (length > 6) {
className = "far fa-smile pr-strength";
styleColor = "green";
}
strength.className = className;
strength.style.color = styleColor;
});
}
showPassword() {
const input = this.element;
let eye = this.eyeEl;
eye.addEventListener('click', function() {
if (input.type === 'password') {
eye.classList.remove('fa-eye-slash');
eye.classList.add('fa-eye');
input.type = "text";
} else {
eye.classList.remove('fa-eye');
eye.classList.add('fa-eye-slash');
input.type = "password";
}
});
}
}