/* tilda-popup содержит функции для работы попапов. Сюда вынесена часть общих функций из блоков, также, несколько функций, отвечающих за a11y. */ /** * a11y * Добавление ловушки фокуса внутри попапа * При навигации с клавиатуры фокус не выходит за пределы попапа * @param {node} popup - popup element */ function t_popup__trapFocus(popup) { var focusableElements = popup.querySelectorAll( 'a, button, input:not([type="hidden"]):not(.js-form-spec-comments), select, textarea, embed, video, iframe, [tabindex="0"]' ); var firstFocusableElement = focusableElements[0]; var lastFocusableElement = focusableElements[focusableElements.length - 1]; document.addEventListener('keydown', function (event) { if (!document.body.classList.contains('t-body_popupshowed')) return; var isTab = event.key === 'Tab' || event.keyCode === 9; var isTabPressed = isTab || (isTab && event.shiftKey); if (!isTabPressed) { return; } if (event.shiftKey && document.activeElement.classList.contains('t-popup_show')) { lastFocusableElement.focus(); } if (event.key === 'Tab' && !event.shiftKey && document.activeElement === lastFocusableElement) { event.preventDefault(); firstFocusableElement.focus(); } // if tab+shift and we moving back if (event.key === 'Tab' && event.shiftKey && document.activeElement === firstFocusableElement) { event.preventDefault(); lastFocusableElement.focus(); } }); // TODO: требует доработки фокусировка в первом поле при открытии попапа // на стороне форм проблема с тем, что не видна подсказка в поле // if (firstFocusableElement && // firstFocusableElement.closest('.t-form') && // firstFocusableElement.getAttribute('type') === 'text') { // firstFocusableElement.focus(); // } } /** * Добавление атрибутов для доступности к кнопке-тригеру * @param {string} hook - hook for current trigger button */ function t_popup__addAttributesForAccessibility(hook) { var allTriggerButtons = document.querySelectorAll('a[href="' + hook + '"]'); Array.prototype.forEach.call(allTriggerButtons, function (button) { if (button) { button.setAttribute('role', 'button'); button.setAttribute('aria-haspopup', 'dialog'); } }); } /** * Изменение отображения попапа при ресайзе по вертикали * @param {string} recId – recid of current block */ function t_popup__resizePopup(recId) { var rec = document.getElementById('rec' + recId); if (!rec) return; var popupContainer = rec.querySelector('.t-popup__container'); if (!popupContainer) return; var popupStyle = getComputedStyle(popupContainer, null); var popupPaddingTop = parseInt(popupStyle.paddingTop) || 0; var popupPaddingBottom = parseInt(popupStyle.paddingBottom) || 0; var popupHeight = popupContainer.clientHeight - (popupPaddingTop + popupPaddingBottom); var windowHeightIndent = 120; var recType = rec.getAttribute('data-parenttplid') || rec.getAttribute('data-record-type'); var isAlias = recType === '121'; var isZeroPopup = recType === '1093' || (isAlias && rec.querySelector('.t1093')); if (recType === '364' || recType === '365') { windowHeightIndent = 30; } if ( recType === '868' || recType === '331' || recType === '358' || recType === '1013' || recType === '746' || isZeroPopup ) { windowHeightIndent = 0; } if (popupHeight > window.innerHeight - windowHeightIndent) { popupContainer.classList.add('t-popup__container-static'); } else { popupContainer.classList.remove('t-popup__container-static'); } } /** * Отображение попапа после нажатия на кнопку-триггер * @param {node} popup - popup element */ function t_popup__showPopup(popup) { if (popup) popup.style.display = 'block'; setTimeout(function () { popup.focus(); var popupContainer = popup ? popup.querySelector('.t-popup__container') : null; if (popupContainer) popupContainer.classList.add('t-popup__container-animated'); if (popup) popup.classList.add('t-popup_show'); t_onFuncLoad('t_popup__trapFocus', function () { t_popup__trapFocus(popup); }); }, 50); } function t_popup__closePopup(popup) { const popupActive = document.querySelector('.t-popup.t-popup_show'); const popupContainer = popup.querySelector('.t-popup__container'); if (popup === popupActive) { const body = document.body; body.dispatchEvent(new CustomEvent('popupHidden')); body.classList.remove('t-body_popupshowed'); body.classList.remove('t-quiz__body_popupshowed'); } popup.classList.remove('t-popup_show'); popupContainer.style.removeProperty('transition'); popupContainer.classList.remove('t-popup__container_no-transform'); setTimeout(function () { const hiddenPopups = document.querySelectorAll('.t-popup:not(.t-popup_show)'); hiddenPopups.forEach(popup => { popup.style.display = 'none'; }); }, 300); t_popup__addFocusOnTriggerButton(); } /** * a11y * Добавление класса на кнопку-триггер для последующего фокусирования * @param {node} element * @param {string} hook - hook for current trigger button */ function t_popup__addClassOnTriggerButton(element, hook) { var allTriggeredButtons = document.querySelectorAll('.t-popup__triggered-btn'); Array.prototype.forEach.call(allTriggeredButtons, function (button) { button.classList.remove('t-popup__triggered-btn'); }); element.addEventListener('keydown', function (event) { if (event.keyCode === 13) { var target = event.target; var href = target.closest('a[href="' + hook + '"]') ? target : false; if (!href) return; if (!window.isMobile) href.classList.add('t-popup__triggered-btn'); } }); } /** * a11y * Добавление фокуса на кнопку-триггер после закрытия попапа */ function t_popup__addFocusOnTriggerButton() { const triggeredButton = document.querySelector('.t-popup__triggered-btn'); if (!triggeredButton || triggeredButton.classList.contains('t724__opener')) return; triggeredButton.focus(); triggeredButton.classList.remove('t-popup__triggered-btn'); } // Polyfill: Element.prototype.matches if (!Element.prototype.matches) { Element.prototype.matches = Element.prototype.matchesSelector || Element.prototype.msMatchesSelector || Element.prototype.mozMatchesSelector || Element.prototype.webkitMatchesSelector || Element.prototype.oMatchesSelector; } // Polyfill: Element.closest if (!Element.prototype.closest) { Element.prototype.closest = function (s) { var el = this; while (el && el.nodeType === 1) { if (Element.prototype.matches.call(el, s)) { return el; } el = el.parentElement || el.parentNode; } return null; }; }