(function (){
'use strict';
const focusableSelector=[
'a[href]',
'button:not([disabled])',
'input:not([disabled]):not([type="hidden"])',
'select:not([disabled])',
'textarea:not([disabled])',
'[tabindex]:not([tabindex="-1"])',
].join(',');
const popupSelector='.lepopup-popup-container';
const backgroundStates=new Map();
let activePopup=null;
let lastFocusedElement=null;
let pendingPopupSync=false;
function isVisible(element){
if(! element||element.hidden){
return false;
}
const style=window.getComputedStyle(element);
return style.display!=='none'&&style.visibility!=='hidden'&&element.getClientRects().length > 0;
}
function getOpenPopup(){
return Array.from(document.querySelectorAll(popupSelector)).find(function(popup){
const overlay=popup.id ? document.getElementById(popup.id + '-overlay'):null;
return isVisible(popup)&&(! overlay||isVisible(overlay));
})||null;
}
function getFocusableElements(container){
return Array.from(container.querySelectorAll(focusableSelector)).filter(isVisible);
}
function getPopupCloseControl(popup){
const selector=[
'.lepopup-close',
'.lepopup-popup-close',
'[class*="close"]',
'[data-action*="close"]',
'[title*="Закрыть"]',
'[aria-label*="Закрыть"]',
].join(',');
const explicitControl=Array.from(popup.querySelectorAll(selector)).find(isVisible);
if(explicitControl){
return explicitControl;
}
return Array.from(popup.querySelectorAll('button, a, [role="button"], div, span')).find(function(element){
return isVisible(element)&&/^(×|x|закрыть)$/iu.test(element.textContent.trim());
})||null;
}
function setPageInert(isOpen, popup){
if(isOpen){
Array.from(document.body.children).forEach(function(element){
if(element===popup||element.classList.contains('lepopup-popup-overlay')||element.tagName==='SCRIPT'||element.tagName==='STYLE'){
return;
}
if(! backgroundStates.has(element)){
backgroundStates.set(element, {
ariaHidden: element.getAttribute('aria-hidden'),
inert: element.inert,
});
}
element.setAttribute('aria-hidden', 'true');
element.inert=true;
});
return;
}
backgroundStates.forEach(function(state, element){
if(state.ariaHidden===null){
element.removeAttribute('aria-hidden');
}else{
element.setAttribute('aria-hidden', state.ariaHidden);
}
element.inert=state.inert;
});
backgroundStates.clear();
}
function enhancePopupInputs(popup){
popup.querySelectorAll('input:not([type="hidden"]), textarea, select').forEach(function(field){
const existingLabel=field.getAttribute('aria-label');
const isGenericLabel=/^(name|phone|email|field)$/iu.test(existingLabel||'');
if(! existingLabel||isGenericLabel){
const label=field.getAttribute('placeholder')||field.name||'Поле формы';
field.setAttribute('aria-label', label);
}});
}
function makeCloseControlKeyboardAccessible(closeControl){
if(! closeControl){
return;
}
closeControl.setAttribute('aria-label', 'Закрыть окно');
if(! /^(A|BUTTON)$/i.test(closeControl.tagName)){
closeControl.setAttribute('role', 'button');
closeControl.tabIndex=0;
closeControl.addEventListener('keydown', function(event){
if(event.key==='Enter'||event.key===' '){
event.preventDefault();
closeControl.click();
}});
}}
function activatePopup(popup){
activePopup=popup;
lastFocusedElement=document.activeElement instanceof HTMLElement ? document.activeElement:null;
popup.setAttribute('role', 'dialog');
popup.setAttribute('aria-modal', 'true');
popup.setAttribute('aria-label', 'Получите консультацию и персональную скидку');
popup.tabIndex=-1;
const closeControl=getPopupCloseControl(popup);
makeCloseControlKeyboardAccessible(closeControl);
enhancePopupInputs(popup);
setPageInert(true, popup);
window.requestAnimationFrame(function (){
if(activePopup===popup&&isVisible(popup)){
(closeControl||getFocusableElements(popup)[0]||popup).focus({ preventScroll: true });
}});
}
function deactivatePopup(shouldRestoreFocus){
setPageInert(false);
activePopup=null;
if(shouldRestoreFocus&&lastFocusedElement&&document.contains(lastFocusedElement)){
lastFocusedElement.focus({ preventScroll: true });
}
lastFocusedElement=null;
}
function requestPopupClose(){
if(! activePopup){
return;
}
const closeControl=getPopupCloseControl(activePopup);
if(closeControl){
closeControl.click();
return;
}
const overlay=activePopup.id ? document.getElementById(activePopup.id + '-overlay'):null;
if(overlay){
overlay.click();
}}
function syncPopupState(){
pendingPopupSync=false;
enhanceFeedbackWidget();
const openPopup=getOpenPopup();
if(openPopup&&openPopup!==activePopup){
if(activePopup){
deactivatePopup(false);
}
activatePopup(openPopup);
}else if(! openPopup&&activePopup){
deactivatePopup(true);
}}
function enhanceFeedbackWidget(){
const widget=document.getElementById('feedback_vk');
if(! widget){
return;
}
widget.querySelectorAll('img').forEach(function(image){
if(! image.hasAttribute('alt')){
const review=image.closest('.user_kupiapp');
const authorLink=review ? review.querySelector('.link_kupiapp'):null;
const author=authorLink ? authorLink.textContent.trim():'';
image.alt=author ? 'Фотография автора отзыва: ' + author:'';
}
function setIntrinsicSize(){
if(image.naturalWidth&&! image.hasAttribute('width')){
image.setAttribute('width', String(image.naturalWidth));
}
if(image.naturalHeight&&! image.hasAttribute('height')){
image.setAttribute('height', String(image.naturalHeight));
}}
if(image.complete){
setIntrinsicSize();
}else if(! image.hasAttribute('data-conarium-image-listener')){
image.setAttribute('data-conarium-image-listener', 'true');
image.addEventListener('load', setIntrinsicSize, { once: true });
}});
}
function schedulePopupStateSync(){
if(pendingPopupSync){
return;
}
pendingPopupSync=true;
window.requestAnimationFrame(syncPopupState);
}
function trapPopupFocus(event){
if(! activePopup){
return;
}
if(event.key==='Escape'){
event.preventDefault();
requestPopupClose();
return;
}
if(event.key!=='Tab'){
return;
}
const focusable=getFocusableElements(activePopup);
const first=focusable[0]||activePopup;
const last=focusable[focusable.length - 1]||activePopup;
if(! activePopup.contains(document.activeElement)){
event.preventDefault();
first.focus();
}else if(event.shiftKey&&document.activeElement===first){
event.preventDefault();
last.focus();
}else if(! event.shiftKey&&document.activeElement===last){
event.preventDefault();
first.focus();
}}
const fieldLabels={
'your-name': 'Ваше имя',
'your-phone': 'Номер телефона',
'your-email': 'Email',
'your-message': 'Сообщение',
'message': 'Сообщение',
'class': 'Класс',
'child-name': 'ФИО ребёнка',
'child-grade': 'Класс',
'school-year': 'Учебный год',
'school_select': 'Выберите школу',
'login_platform': 'Входил ли ребёнок в личный кабинет',
'zayav': 'Заявление на возврат денежных средств',
};
const validationMessages={
'your-phone': 'Укажите номер телефона.',
'your-email': 'Укажите email.',
'class': 'Выберите класс.',
'child-grade': 'Выберите класс.',
'school_select': 'Выберите школу.',
'login_platform': 'Выберите вариант.',
'zayav': 'Прикрепите заявление.',
};
function addDescribedBy(field, id){
const ids=(field.getAttribute('aria-describedby')||'').split(/\s+/).filter(Boolean);
if(! ids.includes(id)){
ids.push(id);
field.setAttribute('aria-describedby', ids.join(' '));
}}
function removeDescribedBy(field, id){
const ids=(field.getAttribute('aria-describedby')||'').split(/\s+/).filter(function(currentId){
return currentId&&currentId!==id;
});
if(ids.length){
field.setAttribute('aria-describedby', ids.join(' '));
}else{
field.removeAttribute('aria-describedby');
}}
function getControlLabel(form, control){
const directLabel=form.querySelector('label[for="' + control.id + '"]')||control.closest('label');
const labelText=directLabel ? directLabel.textContent.replace(/\s+/g, ' ').trim():'';
return labelText||fieldLabels[ control.name ]||control.getAttribute('placeholder')||'';
}
function getValidationMessage(form, control){
if(validationMessages[ control.name ]){
return validationMessages[ control.name ];
}
const label=getControlLabel(form, control).replace(/[.:]+$/u, '');
const labelLowercase=label ? label.charAt(0).toLocaleLowerCase('ru-RU') + label.slice(1):'поле';
if(control.type==='file'){
return 'Прикрепите ' + labelLowercase + '.';
}
if(control.tagName==='SELECT'){
return 'Выберите ' + labelLowercase + '.';
}
return 'Укажите ' + labelLowercase + '.';
}
function applyValidationMessages(form){
form.querySelectorAll('.wpcf7-not-valid-tip').forEach(function(error){
const wrapper=error.closest('.wpcf7-form-control-wrap');
const control=wrapper ? wrapper.querySelector('.wpcf7-form-control'):null;
if(control){
error.textContent=getValidationMessage(form, control);
}});
const formWrapper=form.closest('.wpcf7')||form;
formWrapper.querySelectorAll('.screen-reader-response a[href^="#"]').forEach(function(errorLink){
const invalidControl=form.querySelector(errorLink.getAttribute('href'));
if(invalidControl){
errorLink.textContent=getValidationMessage(form, invalidControl);
}});
}
function clearPrematureValidation(form){
if(form.dataset.conariumSubmitAttempted==='true'){
return;
}
form.querySelectorAll('.wpcf7-form-control.wpcf7-not-valid').forEach(function(control){
control.setAttribute('aria-invalid', 'false');
const wrapper=control.closest('.wpcf7-form-control-wrap')||control.parentElement;
const error=wrapper.querySelector('.wpcf7-not-valid-tip');
if(error&&error.id){
removeDescribedBy(control, error.id);
}});
const formWrapper=form.closest('.wpcf7')||form;
formWrapper.querySelectorAll('.screen-reader-response ul').forEach(function(list){
list.replaceChildren();
});
const responseOutput=form.querySelector('.wpcf7-response-output');
if(responseOutput){
responseOutput.textContent='';
responseOutput.setAttribute('aria-hidden', 'true');
}}
function schedulePrematureValidationCleanup(form){
[ 0, 250 ].forEach(function(delay){
window.setTimeout(function (){
clearPrematureValidation(form);
}, delay);
});
}
function enhanceContactForm(form, formIndex){
form.classList.add('conarium-form');
form.querySelectorAll('.cf7-upload-field br').forEach(function(lineBreak){
lineBreak.remove();
});
const controls=Array.from(form.querySelectorAll('.wpcf7-form-control')).filter(function(control){
return /^(INPUT|SELECT|TEXTAREA)$/i.test(control.tagName)&&control.type!=='hidden'&&control.type!=='submit';
});
controls.forEach(function(control, controlIndex){
if(! control.id){
const fieldName=(control.name||'field').replace(/[^a-z0-9_-]/giu, '-');
control.id='conarium-form-' + formIndex + '-' + controlIndex + '-' + fieldName;
}
const labelText=fieldLabels[control.name]||control.getAttribute('placeholder');
const hasLabel=form.querySelector('label[for="' + control.id + '"]')||control.closest('label');
if(labelText&&! hasLabel){
const label=document.createElement('label');
label.className='conarium-screen-reader-text';
label.htmlFor=control.id;
label.textContent=labelText;
const wrapper=control.closest('.wpcf7-form-control-wrap')||control.parentElement;
wrapper.insertBefore(label, wrapper.firstChild);
}
if(control.classList.contains('wpcf7-validates-as-required')){
control.setAttribute('aria-required', 'true');
}
if(control.name==='your-name'){
control.setAttribute('autocomplete', 'name');
}
if(control.name==='your-phone'){
control.setAttribute('autocomplete', 'tel');
control.setAttribute('inputmode', 'tel');
}});
form.querySelectorAll('.wpcf7-response-output').forEach(function(response){
response.setAttribute('role', 'status');
response.setAttribute('aria-live', 'polite');
});
const markSubmitAttempt=function (){
form.dataset.conariumSubmitAttempted='true';
};
form.querySelectorAll('input[type="submit"], button[type="submit"]').forEach(function(submit){
submit.addEventListener('click', markSubmitAttempt);
});
form.addEventListener('submit', markSubmitAttempt);
form.addEventListener('wpcf7reset', function (){
delete form.dataset.conariumSubmitAttempted;
});
form.addEventListener('wpcf7mailsent', function (){
delete form.dataset.conariumSubmitAttempted;
});
form.querySelectorAll('.wpcf7-acceptance input[type="checkbox"]').forEach(function(consent){
consent.addEventListener('change', function (){
schedulePrematureValidationCleanup(form);
});
});
}
function syncContactFormErrors(form, focusFirstError){
const invalidControls=Array.from(form.querySelectorAll('.wpcf7-form-control.wpcf7-not-valid'));
invalidControls.forEach(function(control){
control.setAttribute('aria-invalid', 'true');
const wrapper=control.closest('.wpcf7-form-control-wrap')||control.parentElement;
const error=wrapper.querySelector('.wpcf7-not-valid-tip');
if(error){
if(! error.id){
error.id=control.id + '-error';
}
error.setAttribute('role', 'alert');
addDescribedBy(control, error.id);
}});
if(focusFirstError&&invalidControls[0]){
const firstError=invalidControls[0];
firstError.focus({ preventScroll: true });
firstError.scrollIntoView({
behavior: window.matchMedia('(prefers-reduced-motion: reduce)').matches ? 'auto':'smooth',
block: 'center',
});
}}
function setupContactForms(){
document.querySelectorAll('form.wpcf7-form').forEach(function(form, formIndex){
enhanceContactForm(form, formIndex);
syncContactFormErrors(form, false);
});
}
function getContactFormFromEvent(event){
if(event.target.matches&&event.target.matches('form.wpcf7-form')){
return event.target;
}
return event.target.querySelector ? event.target.querySelector('form.wpcf7-form'):null;
}
function init(){
setupContactForms();
enhanceFeedbackWidget();
syncPopupState();
document.addEventListener('keydown', trapPopupFocus, true);
document.addEventListener('wpcf7invalid', function(event){
const form=getContactFormFromEvent(event);
if(form){
window.setTimeout(function (){
if(form.dataset.conariumSubmitAttempted!=='true'){
schedulePrematureValidationCleanup(form);
return;
}
applyValidationMessages(form);
syncContactFormErrors(form, true);
}, 0);
}});
document.addEventListener('wpcf7submit', function(event){
const form=getContactFormFromEvent(event);
if(form){
window.setTimeout(function (){
syncContactFormErrors(form, false);
}, 0);
}});
const popupObserver=new MutationObserver(schedulePopupStateSync);
popupObserver.observe(document.body, {
attributes: true,
attributeFilter: [ 'class', 'style', 'hidden' ],
childList: true,
subtree: true,
});
}
if(document.readyState==='loading'){
document.addEventListener('DOMContentLoaded', init);
}else{
init();
}}());