2022-11-28 14:04:41 +01:00
|
|
|
import {
|
|
|
|
AfterContentInit,
|
|
|
|
Directive,
|
|
|
|
EventEmitter,
|
|
|
|
Host,
|
2022-12-19 23:14:29 +01:00
|
|
|
HostBinding,
|
2022-11-28 14:04:41 +01:00
|
|
|
HostListener,
|
|
|
|
Input,
|
|
|
|
OnChanges,
|
|
|
|
Output,
|
|
|
|
} from "@angular/core";
|
|
|
|
|
2022-12-19 23:14:29 +01:00
|
|
|
import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
|
|
|
|
|
|
|
|
import { BitIconButtonComponent } from "../icon-button/icon-button.component";
|
2022-11-28 14:04:41 +01:00
|
|
|
|
|
|
|
import { BitFormFieldComponent } from "./form-field.component";
|
|
|
|
|
|
|
|
@Directive({
|
|
|
|
selector: "[bitPasswordInputToggle]",
|
|
|
|
})
|
|
|
|
export class BitPasswordInputToggleDirective implements AfterContentInit, OnChanges {
|
2022-12-19 23:14:29 +01:00
|
|
|
/**
|
|
|
|
* Whether the input is toggled to show the password.
|
|
|
|
*/
|
|
|
|
@HostBinding("attr.aria-pressed") @Input() toggled = false;
|
2022-11-28 14:04:41 +01:00
|
|
|
@Output() toggledChange = new EventEmitter<boolean>();
|
|
|
|
|
2022-12-19 23:14:29 +01:00
|
|
|
@HostBinding("attr.title") title = this.i18nService.t("toggleVisibility");
|
|
|
|
@HostBinding("attr.aria-label") label = this.i18nService.t("toggleVisibility");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Click handler to toggle the state of the input type.
|
|
|
|
*/
|
2022-11-28 14:04:41 +01:00
|
|
|
@HostListener("click") onClick() {
|
|
|
|
this.toggled = !this.toggled;
|
|
|
|
this.toggledChange.emit(this.toggled);
|
|
|
|
|
|
|
|
this.update();
|
|
|
|
|
|
|
|
this.formField.input?.focus();
|
|
|
|
}
|
|
|
|
|
2022-12-19 23:14:29 +01:00
|
|
|
constructor(
|
|
|
|
@Host() private button: BitIconButtonComponent,
|
|
|
|
private formField: BitFormFieldComponent,
|
|
|
|
private i18nService: I18nService
|
|
|
|
) {}
|
2022-11-28 14:04:41 +01:00
|
|
|
|
|
|
|
get icon() {
|
|
|
|
return this.toggled ? "bwi-eye-slash" : "bwi-eye";
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnChanges(): void {
|
|
|
|
this.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
ngAfterContentInit(): void {
|
|
|
|
this.toggled = this.formField.input.type !== "password";
|
|
|
|
this.button.icon = this.icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
private update() {
|
|
|
|
this.button.icon = this.icon;
|
|
|
|
if (this.formField.input?.type != null) {
|
|
|
|
this.formField.input.type = this.toggled ? "text" : "password";
|
|
|
|
this.formField.input.spellcheck = this.toggled ? false : undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|