117 lines
4.2 KiB
JavaScript
117 lines
4.2 KiB
JavaScript
console.log('wf form load');
|
|
|
|
// Получаем данные из атрибутов текущего скрипта
|
|
const currentScript = document.currentScript;
|
|
const formNameFancyBox = currentScript.dataset.formNameFancyBox;
|
|
const buttonClass = currentScript.dataset.buttonClass || '.wf-button'; // Добавляем fallback значение
|
|
|
|
// Кэш для cabinetHost чтобы не определять его каждый раз
|
|
const CABINET_HOST = location.host === 'wmtmed.ru'
|
|
? 'https://cabinet.wmtmed.ru'
|
|
: 'https://cabinet.sovamed.ru';
|
|
|
|
function wf_renderModal(formName, query, wfId, fbStyle, wfStyle) {
|
|
// Удаляем предыдущее модальное окно, если оно существует
|
|
const existingPopup = document.getElementById(formNameFancyBox);
|
|
if (existingPopup) {
|
|
existingPopup.remove();
|
|
}
|
|
|
|
// Создаем структуру модального окна
|
|
const wf_popup = document.createElement('div');
|
|
wf_popup.id = formNameFancyBox;
|
|
wf_popup.className = 'fancybox-content';
|
|
wf_popup.style.cssText = fbStyle || '';
|
|
wf_popup.style.display = 'none';
|
|
document.body.appendChild(wf_popup);
|
|
|
|
const wf_modalDialog = document.createElement('div');
|
|
wf_modalDialog.className = 'modal-dialog';
|
|
wf_modalDialog.setAttribute('role', 'document');
|
|
wf_popup.appendChild(wf_modalDialog);
|
|
|
|
const wf_modalContent = document.createElement('div');
|
|
wf_modalContent.className = 'modal-content';
|
|
wf_modalDialog.appendChild(wf_modalContent);
|
|
|
|
const wf_modalHeader = document.createElement('div');
|
|
wf_modalHeader.className = 'modal-header';
|
|
wf_modalContent.appendChild(wf_modalHeader);
|
|
|
|
const wf_modalTitle = document.createElement('h5');
|
|
wf_modalTitle.className = 'modal-title';
|
|
wf_modalTitle.textContent = formName;
|
|
wf_modalHeader.appendChild(wf_modalTitle);
|
|
|
|
const wf_popupBody = document.createElement('div');
|
|
wf_popupBody.id = 'wf_popup-body';
|
|
wf_popupBody.className = 'modal-body';
|
|
wf_modalContent.appendChild(wf_popupBody);
|
|
|
|
const wf_iframe = document.createElement('iframe');
|
|
wf_iframe.id = `${formNameFancyBox}-frame`;
|
|
wf_iframe.src = `${CABINET_HOST}/widget/form/${wfId}?${query}`;
|
|
wf_iframe.style.cssText = wfStyle || '';
|
|
wf_popupBody.appendChild(wf_iframe);
|
|
|
|
return true;
|
|
}
|
|
|
|
function getSessionId() {
|
|
try {
|
|
const calltrackingParams = window.ct?.('calltracking_params');
|
|
if (Array.isArray(calltrackingParams) && calltrackingParams.length > 0) {
|
|
console.error('getting sessionId:', calltrackingParams[0]?.sessionId);
|
|
return calltrackingParams[0]?.sessionId || null;
|
|
}
|
|
return null;
|
|
} catch (e) {
|
|
console.error('Error getting sessionId:', e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function wf_listiner(wf_evn) {
|
|
try {
|
|
const target = wf_evn.target;
|
|
if (!target.dataset) return;
|
|
|
|
// Базовые параметры
|
|
const baseQuery = {
|
|
'ref': btoa(location.origin),
|
|
'sessionId': getSessionId(),
|
|
'ymNum': target.dataset?.ymNum,
|
|
'ymId': target.dataset?.ymId
|
|
};
|
|
|
|
// Дополнительные параметры из data-row
|
|
let additionalParams = {};
|
|
if (target.dataset.row) {
|
|
try {
|
|
additionalParams = JSON.parse(atob(target.dataset.row));
|
|
} catch (e) {
|
|
console.error('Error parsing dataset.row:', e);
|
|
}
|
|
}
|
|
|
|
// Объединяем параметры
|
|
const mergedQuery = {...additionalParams, ...baseQuery};
|
|
const queryString = new URLSearchParams(mergedQuery).toString();
|
|
|
|
// Вызываем рендер модального окна
|
|
wf_renderModal(
|
|
target.dataset.formName,
|
|
queryString,
|
|
target.dataset.wfId,
|
|
target.dataset.fbStyle,
|
|
target.dataset.wfStyle
|
|
);
|
|
} catch (error) {
|
|
console.error('Error in wf_listiner:', error);
|
|
}
|
|
}
|
|
|
|
// Инициализация слушателей событий
|
|
document.querySelectorAll(buttonClass).forEach(button => {
|
|
button.addEventListener('click', wf_listiner);
|
|
}); |