-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoggle-password.directive.ts
More file actions
57 lines (45 loc) · 2.29 KB
/
toggle-password.directive.ts
File metadata and controls
57 lines (45 loc) · 2.29 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
import { Directive, ElementRef, Renderer2 } from '@angular/core';
@Directive({
selector: '[appTogglePassword]'
})
export class TogglePasswordDirective {
constructor(private element: ElementRef, private renderer: Renderer2) {
const tagName: string = element.nativeElement.tagName;
if (tagName.toLowerCase() !== 'input') {
throw new Error('Please use this directive on an input element.');
}
const parent = this.element.nativeElement.parentNode;
const inputGroup = this.renderer.createElement('div');
this.renderer.addClass(inputGroup, 'input-group');
this.renderer.appendChild(parent, inputGroup);
this.renderer.removeChild(parent, this.element.nativeElement);
this.renderer.appendChild(inputGroup, this.element.nativeElement);
const appendGroup = this.renderer.createElement('div');
this.renderer.setAttribute(appendGroup, 'class', 'input-group-append');
this.renderer.setAttribute(appendGroup, 'style', 'cursor: pointer;');
this.renderer.appendChild(inputGroup, appendGroup);
const button = this.renderer.createElement('button');
this.renderer.setAttribute(button, 'class', 'btn btn-outline-secondary');
this.renderer.setAttribute(button, 'title', 'Click to show/hide password');
this.renderer.setAttribute(button, 'type', 'button');
this.renderer.appendChild(appendGroup, button);
const icon = this.renderer.createElement('i');
const inputType: string = this.element.nativeElement.attributes.getNamedItem('type').value;
if (inputType.toLowerCase() === 'password') {
this.renderer.setAttribute(icon, 'class', 'fa icon-eye-open fa-eye');
} else {
this.renderer.setAttribute(icon, 'class', 'fa icon-eye-close fa-eye-slash');
}
this.renderer.appendChild(button, icon);
this.renderer.listen(button, 'click', (event) => {
const type: string = this.element.nativeElement.attributes.getNamedItem('type').value;
if (type.toLowerCase() === 'password') {
this.renderer.setAttribute(icon, 'class', 'fa icon-eye-close fa-eye-slash');
this.renderer.setAttribute(element.nativeElement, 'type', 'text');
} else {
this.renderer.setAttribute(icon, 'class', 'fa icon-eye-open fa-eye');
this.renderer.setAttribute(element.nativeElement, 'type', 'password');
}
});
}
}