93 lines
3.0 KiB
JavaScript
93 lines
3.0 KiB
JavaScript
document.addEventListener('DOMContentLoaded', function () {
|
|
const iconContainer = document.querySelector('.icon-container');
|
|
const dropdownMenu = document.querySelector('.dropdown-menu');
|
|
|
|
iconContainer.addEventListener('click', function (event) {
|
|
dropdownMenu.style.display = dropdownMenu.style.display === 'block' ? 'none' : 'block';
|
|
event.stopPropagation();
|
|
});
|
|
|
|
document.addEventListener('click', function () {
|
|
dropdownMenu.style.display = 'none';
|
|
});
|
|
|
|
dropdownMenu.addEventListener('click', function (event) {
|
|
event.stopPropagation();
|
|
});
|
|
|
|
// Делегируем клики для открытия логина/регистрации внутри любых модалок
|
|
document.addEventListener('click', function (e) {
|
|
const target = e.target.closest('[data-action]');
|
|
if (!target) return;
|
|
|
|
e.preventDefault();
|
|
|
|
const action = target.dataset.action;
|
|
if (action === 'open-register') {
|
|
openRegisterModal();
|
|
} else if (action === 'open-login') {
|
|
openLoginModal();
|
|
}
|
|
});
|
|
});
|
|
|
|
function closeAllModals() {
|
|
const modalContainer = document.getElementById('modal-container');
|
|
if (modalContainer) modalContainer.remove();
|
|
}
|
|
|
|
function openRegisterModal() {
|
|
closeAllModals();
|
|
|
|
const modalContainer = document.createElement('div');
|
|
modalContainer.id = 'modal-container';
|
|
document.body.appendChild(modalContainer);
|
|
|
|
fetch('/register-modal')
|
|
.then(response => response.text())
|
|
.then(html => {
|
|
modalContainer.innerHTML = html;
|
|
reloadRecaptcha(modalContainer);
|
|
});
|
|
}
|
|
|
|
function openLoginModal() {
|
|
closeAllModals();
|
|
|
|
const modalContainer = document.createElement('div');
|
|
modalContainer.id = 'modal-container';
|
|
document.body.appendChild(modalContainer);
|
|
|
|
fetch('/login-modal')
|
|
.then(response => response.text())
|
|
.then(html => {
|
|
modalContainer.innerHTML = html;
|
|
reloadRecaptcha(modalContainer);
|
|
});
|
|
}
|
|
|
|
function reloadRecaptcha(modalContainer) {
|
|
const recaptchaScript = modalContainer.querySelector('script[src*="recaptcha"]');
|
|
if (recaptchaScript) recaptchaScript.remove();
|
|
|
|
const script = document.createElement('script');
|
|
script.src = 'https://www.google.com/recaptcha/api.js?render=explicit&onload=recaptchaCallback';
|
|
document.head.appendChild(script);
|
|
}
|
|
|
|
window.recaptchaCallback = function () {
|
|
const recaptchaElement = document.querySelector('.g-recaptcha');
|
|
if (recaptchaElement && typeof grecaptcha !== 'undefined') {
|
|
grecaptcha.render(recaptchaElement, {
|
|
theme: "dark",
|
|
size: "default"
|
|
});
|
|
}
|
|
};
|
|
document.addEventListener('click', function (e) {
|
|
const modal = document.querySelector('#modal-container .modal');
|
|
if (modal && !modal.contains(e.target)) {
|
|
closeAllModals();
|
|
}
|
|
});
|