forked from thatisuday/ngx-password-toggle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirective.ts
52 lines (42 loc) · 5.65 KB
/
directive.ts
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
import { Directive, ElementRef, OnInit, Renderer2 } from '@angular/core';
const eye = 'data:image/svg+xml;utf8;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iaXNvLTg4NTktMSI/Pgo8IS0tIEdlbmVyYXRvcjogQWRvYmUgSWxsdXN0cmF0b3IgMTkuMC4wLCBTVkcgRXhwb3J0IFBsdWctSW4gLiBTVkcgVmVyc2lvbjogNi4wMCBCdWlsZCAwKSAgLS0+CjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgdmVyc2lvbj0iMS4xIiBpZD0iQ2FwYV8xIiB4PSIwcHgiIHk9IjBweCIgdmlld0JveD0iMCAwIDQ4OC44NSA0ODguODUiIHN0eWxlPSJlbmFibGUtYmFja2dyb3VuZDpuZXcgMCAwIDQ4OC44NSA0ODguODU7IiB4bWw6c3BhY2U9InByZXNlcnZlIiB3aWR0aD0iMTZweCIgaGVpZ2h0PSIxNnB4Ij4KPGc+Cgk8cGF0aCBkPSJNMjQ0LjQyNSw5OC43MjVjLTkzLjQsMC0xNzguMSw1MS4xLTI0MC42LDEzNC4xYy01LjEsNi44LTUuMSwxNi4zLDAsMjMuMWM2Mi41LDgzLjEsMTQ3LjIsMTM0LjIsMjQwLjYsMTM0LjIgICBzMTc4LjEtNTEuMSwyNDAuNi0xMzQuMWM1LjEtNi44LDUuMS0xNi4zLDAtMjMuMUM0MjIuNTI1LDE0OS44MjUsMzM3LjgyNSw5OC43MjUsMjQ0LjQyNSw5OC43MjV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uMDAzYzU1LjUyNS0wLjM5MiwxMDUuNzc2LTMzLjU2NiwxMzAuNzg4LTUzLjMwN2MzMS42MDMtMjQuOTQyLDU4LjIzMS01Ni4wMTEsNTguMTQ4LTY3Ljg0NSAgQzM3OS40ODQsMTc2Ljk4OCwzNTIuMDQzLDE0Ni4xMjYsMzIwLjgzOCwxMjEuOTI5eiBNMTExLjMxNCwxODkuODY0Yy0wLjMwNS00My4zMzYsMzQuNTc4LTc4LjcxNCw3Ny45MTMtNzkuMDE5ICBjMTUuOTc1LTAuMTEzLDMwLjg2LDQuNTY5LDQzLjMxNCwxMi42ODFsLTEwNy45MzgsMTA5LjQ3QzExNi4zMTgsMjIwLjY1NywxMTEuNDI3LDIwNS44MzksMTExLjMxNCwxODkuODY0eiBNMTkwLjMzNCwyNjcuNzc3ICBjLTE1Ljc4NCwwLjExMS0zMC41LTQuNDYyLTQyLjg2My0xMi4zOTZsMTA3Ljc3Ny0xMDkuMzA4YzguMTA3LDEyLjI1LDEyLjg4NywyNi45LDEyLjk5OSw0Mi42ODQgIEMyNjguNTUyLDIzMi4wOTQsMjMzLjY2OSwyNjcuNDcyLDE5MC4zMzQsMjY3Ljc3N3oiIGZpbGw9IiMwMDAwMDAiLz4KPGc+CjwvZz4KPGc+CjwvZz4KPGc+CjwvZz4KPGc+CjwvZz4KPGc+CjwvZz4KPGc+CjwvZz4KPGc+CjwvZz4KPGc+CjwvZz4KPGc+CjwvZz4KPGc+CjwvZz4KPGc+CjwvZz4KPGc+CjwvZz4KPGc+CjwvZz4KPGc+CjwvZz4KPGc+CjwvZz4KPC9zdmc+Cg==';
@Directive({
selector: '[passwordToggle]'
})
export class NgxPasswordToggleDirective implements OnInit {
visible: boolean = false;
constructor(
private elem: ElementRef,
private renderer: Renderer2
) { }
ngOnInit() {
let inputElem = this.elem.nativeElement;
let parentElem = inputElem.parentElement;
this.renderer.setStyle(parentElem, 'position', 'relative');
let inputElemHeight = inputElem.offsetHeight;
let inputElemTop = inputElem.offsetTop;
this.renderer.setStyle(inputElem, 'padding-right', (inputElemHeight + 5) + 'px');
// create toggle button
let toggleElem = this.renderer.createElement('div');
this.renderer.setStyle(toggleElem, 'position', 'absolute');
this.renderer.setStyle(toggleElem, 'top', inputElemTop + 'px');
this.renderer.setStyle(toggleElem, 'right', '0px');
this.renderer.setStyle(toggleElem, 'width', inputElemHeight + 'px');
this.renderer.setStyle(toggleElem, 'height', inputElemHeight + 'px');
this.renderer.setStyle(toggleElem, 'cursor', 'pointer');
this.renderer.setStyle(toggleElem, 'opacity', '0.6');
this.renderer.setStyle(toggleElem, 'background-color', 'transparent');
this.renderer.setStyle(toggleElem, 'background-repeat', 'no-repeat');
this.renderer.setStyle(toggleElem, 'background-position', 'center center');
this.renderer.setStyle(toggleElem, 'background-image', `url(${eyeCrossed})`);
// append toggle element to input element holder
this.renderer.appendChild(parentElem, toggleElem);
// listen to click event on toggle button
this.renderer.listen(toggleElem, 'click', (event) => {
this.visible = !this.visible;
this.renderer.setStyle(toggleElem, 'background-image', `url(${this.visible ? eye : eyeCrossed})`);
this.renderer.setAttribute(inputElem, 'type', this.visible ? 'text' : 'password');
});
}
}