501var windowHeight = window.innerHeight;502var isTop = userBarRect.top < windowHeight / 2;503var isLeft = userBarRect.left < windowWidth / 2;504var MARGIN = 10;505506userPopupWrap.style.maxHeight = null;507508// Абсолютное позиционирование относительно viewport509userPopup.style.position = 'fixed';510// Устанавливаем высокий z-index, чтобы перекрыть другие элементы на странице, например блоки меню511userPopup.style.zIndex = '1000000';512
if (isTop) { userPopup.style.top = userBarRect.bottom + MARGIN + 'px'; userPopup.style.bottom = 'auto'; } else { userPopup.style.bottom = windowHeight - userBarRect.top + MARGIN + 'px'; userPopup.style.top = 'auto'; userPopup.classList.add('tlk-userbar__popup_bottom'); } if (isLeft) { userPopup.style.left = userBarRect.left + 'px'; userPopup.style.right = 'auto'; userPopup.classList.add('tlk-userbar__popup_left'); const userPopupRect = userPopup.getBoundingClientRect(); if (windowWidth - (userPopupRect.left + userPopupRect.width) < 0) { const leftOffset = windowWidth - (userPopupRect.left + userPopupRect.width) - MARGIN; userPopup.style.left = `${leftOffset}px`; } } else { userPopup.style.right = windowWidth - userBarRect.right + 'px'; userPopup.style.left = 'auto'; } // Проверяем и корректируем высоту после позиционирования setTimeout(function () { var userPopupRect = userPopup.getBoundingClientRect(); if (isTop && windowHeight < userPopupRect.bottom) { userPopupWrap.style.maxHeight = userPopupRect.height - (userPopupRect.bottom - windowHeight + 10) + 'px'; } else if (!isTop && userPopupRect.top < 0) { userPopupWrap.style.maxHeight = userPopupRect.height - Math.abs(userPopupRect.top) - 10 + 'px'; } if (userPopupRect.left < 0) { userPopup.style.left = '10px'; } }, 0);}/** * Throttle утилита для ограничения частоты вызова функций * @param {Function} fn * @param {number} wait * @returns {Function} */function tma__userbar__throttle(fn, wait) { var lastTime = 0; var timeoutId; return function () { var now = Date.now(); var args = arguments; if (lastTime && now - lastTime < wait) { clearTimeout(timeoutId); timeoutId = setTimeout( function () { lastTime = Date.now(); fn.apply(null, args); }, wait - (now - lastTime) ); } else { lastTime = now; fn.apply(null, args); } };}/** * Вешает обработчик клика вне попапа для его закрытия. * Используем mousedown, чтобы закрывать раньше фокуса на элементах внутри. */function tma__userbar__addOutsideClickHandler() { if (window['userbar_outside_click']) return; // уже навешан /** @param {MouseEvent} e */ const handler = function (e) { // Игнорируем правый клик (contextmenu) — закрываем только при обычных кнопках if (e.button && e.button !== 0) return; const popup = document.querySelector('.tlk-userbar__popup'); if (!popup) return; // уже закрыт const target = /** @type {Element} */ (e.target); // Если клик внутри попапа — не закрываем if (popup.contains(target)) return;