38 lines
1.2 KiB
JavaScript
38 lines
1.2 KiB
JavaScript
(function () {
|
|
function init() {
|
|
document.querySelectorAll('[data-password-toggle]').forEach(function (toggle) {
|
|
var input = document.getElementById(toggle.getAttribute('aria-controls'));
|
|
var form = toggle.closest('form');
|
|
if (!input || !form) return;
|
|
|
|
function sync() {
|
|
var empty = input.value === '';
|
|
toggle.hidden = empty;
|
|
if (empty && input.type === 'text') {
|
|
input.type = 'password';
|
|
toggle.textContent = form.dataset.labelShow;
|
|
toggle.setAttribute('aria-pressed', 'false');
|
|
}
|
|
}
|
|
|
|
toggle.addEventListener('click', function () {
|
|
var show = input.type === 'password';
|
|
input.type = show ? 'text' : 'password';
|
|
toggle.textContent = show ? form.dataset.labelHide : form.dataset.labelShow;
|
|
toggle.setAttribute('aria-pressed', show ? 'true' : 'false');
|
|
input.focus();
|
|
});
|
|
['input', 'change', 'focus'].forEach(function (name) {
|
|
input.addEventListener(name, sync);
|
|
});
|
|
sync();
|
|
});
|
|
}
|
|
|
|
if (document.readyState === 'loading') {
|
|
document.addEventListener('DOMContentLoaded', init);
|
|
} else {
|
|
init();
|
|
}
|
|
})();
|