mCaptcha/templates/components/showPassword/index.ts

95 lines
2.7 KiB
TypeScript
Raw Normal View History

2021-05-14 14:03:18 +03:00
/*
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2021-10-08 12:54:29 +03:00
const showPasswordButtonClassHidden = "show-password--hide";
const showPasswordButtonClassShowing = "show-password--show";
2021-05-14 14:03:18 +03:00
2021-10-08 12:54:29 +03:00
const container = "show-password-container";
2021-05-14 14:03:18 +03:00
2021-10-08 12:54:29 +03:00
let display = "hidden";
2021-05-14 14:03:18 +03:00
const showPasswordButtons = () => {
let buttons: NodeListOf<HTMLElement>;
return (() => {
if (buttons === undefined) {
buttons = <NodeListOf<HTMLElement>>(
document.querySelectorAll(`.${showPasswordButtonClassShowing}`)
);
}
return buttons;
})();
};
const hidePasswordButtons = () => {
let buttons: NodeListOf<HTMLElement>;
return (() => {
if (buttons === undefined) {
buttons = <NodeListOf<HTMLElement>>(
document.querySelectorAll(`.${showPasswordButtonClassHidden}`)
);
}
return buttons;
})();
};
// e is click event from show password container
2021-10-08 12:54:29 +03:00
export const showPassword = (): void => {
const inputs = document.body.querySelectorAll("input");
2021-05-14 14:03:18 +03:00
2021-10-08 12:54:29 +03:00
if (display == "hidden") {
display = "show";
2021-05-14 14:03:18 +03:00
inputs.forEach(element => {
2021-10-08 12:54:29 +03:00
if (element.type === "password") {
element.type = "text";
2021-05-14 14:03:18 +03:00
}
});
showPasswordButtons().forEach((button: HTMLInputElement) => {
2021-10-08 12:54:29 +03:00
button.style.display = "none";
2021-05-14 14:03:18 +03:00
});
hidePasswordButtons().forEach((button: HTMLInputElement) => {
2021-10-08 12:54:29 +03:00
button.style.display = "inline";
2021-05-14 14:03:18 +03:00
});
} else {
2021-10-08 12:54:29 +03:00
display = "hidden";
2021-05-14 14:03:18 +03:00
inputs.forEach(element => {
2021-10-08 12:54:29 +03:00
if (element.type === "text" && element.name.includes("password")) {
element.type = "password";
2021-05-14 14:03:18 +03:00
}
});
showPasswordButtons().forEach((button: HTMLInputElement) => {
2021-10-08 12:54:29 +03:00
button.style.display = "inline";
2021-05-14 14:03:18 +03:00
});
hidePasswordButtons().forEach((button: HTMLInputElement) => {
2021-10-08 12:54:29 +03:00
button.style.display = "none";
2021-05-14 14:03:18 +03:00
});
}
// posibily clicked on something else
};
2021-10-08 12:54:29 +03:00
export const registerShowPassword = (): void => {
2021-05-14 14:03:18 +03:00
document.querySelectorAll(`.${container}`).forEach(container => {
2021-10-08 12:54:29 +03:00
container.addEventListener("click", showPassword);
2021-05-14 14:03:18 +03:00
});
};
export default registerShowPassword;