bitwarden-estensione-browser/libs/angular/src/directives/box-row.directive.ts

60 lines
1.4 KiB
TypeScript
Raw Normal View History

2021-12-16 13:36:21 +01:00
import { Directive, ElementRef, HostListener, OnInit } from "@angular/core";
@Directive({
2021-12-16 13:36:21 +01:00
selector: "[appBoxRow]",
})
export class BoxRowDirective implements OnInit {
2021-12-16 13:36:21 +01:00
el: HTMLElement = null;
formEls: Element[];
2022-02-22 15:39:11 +01:00
constructor(elRef: ElementRef) {
2021-12-16 13:36:21 +01:00
this.el = elRef.nativeElement;
}
2021-12-16 13:36:21 +01:00
ngOnInit(): void {
this.formEls = Array.from(
this.el.querySelectorAll('input:not([type="hidden"]), select, textarea')
);
this.formEls.forEach((formEl) => {
formEl.addEventListener(
"focus",
2022-02-22 15:39:11 +01:00
() => {
2021-12-16 13:36:21 +01:00
this.el.classList.add("active");
},
false
);
2021-12-16 13:36:21 +01:00
formEl.addEventListener(
"blur",
2022-02-22 15:39:11 +01:00
() => {
2021-12-16 13:36:21 +01:00
this.el.classList.remove("active");
},
false
);
});
}
2021-12-16 13:36:21 +01:00
@HostListener("click", ["$event"]) onClick(event: Event) {
const target = event.target as HTMLElement;
if (
target !== this.el &&
!target.classList.contains("progress") &&
!target.classList.contains("progress-bar")
) {
return;
}
2021-12-16 13:36:21 +01:00
if (this.formEls.length > 0) {
const formEl = this.formEls[0] as HTMLElement;
if (formEl.tagName.toLowerCase() === "input") {
const inputEl = formEl as HTMLInputElement;
if (inputEl.type != null && inputEl.type.toLowerCase() === "checkbox") {
inputEl.click();
return;
}
2021-12-16 13:36:21 +01:00
}
formEl.focus();
}
2021-12-16 13:36:21 +01:00
}
}