-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatPersist.js
More file actions
151 lines (135 loc) · 5.38 KB
/
chatPersist.js
File metadata and controls
151 lines (135 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
document.addEventListener('DOMContentLoaded', () => {
console.log("chatPersist.js: DOM fully loaded.");
if (typeof jQuery === 'undefined') {
console.error("chatPersist.js: jQuery is required but not found.");
return;
}
jQuery(document).ready(function($) {
const popupId = 932;
const MAX_RETRIES = 20;
// Wait for element with streamlined retries
function waitForElement(selector, callback, retries = MAX_RETRIES) {
const $element = $(selector);
if ($element.length) {
callback($element);
} else if (retries > 0) {
setTimeout(() => waitForElement(selector, callback, retries - 1), 100);
} else {
console.error(`chatPersist.js: ${selector} not found after ${MAX_RETRIES} retries.`);
}
}
// Show popup with robust fallback
function showPopup() {
const popupSelector = `[data-elementor-id="${popupId}"]`;
let popupShown = false;
if (typeof elementorProFrontend?.modules?.popup?.showPopup === 'function') {
try {
console.log("chatPersist.js: Attempting Elementor Pro popup...");
elementorProFrontend.modules.popup.showPopup({ id: popupId });
const $popup = $(popupSelector);
popupShown = $popup.length && $popup.is(':visible');
if (popupShown) console.log("chatPersist.js: Popup shown via Elementor Pro.");
} catch (error) {
console.warn("chatPersist.js: Elementor Pro failed:", error);
}
}
if (!popupShown) {
waitForElement(popupSelector, ($popup) => {
$popup.show().addClass('elementor-popup-modal--active dialog-visible');
popupShown = $popup.is(':visible');
if (popupShown) console.log("chatPersist.js: Popup shown via jQuery fallback.");
finalizePopup(popupShown);
});
} else {
finalizePopup(popupShown);
}
}
// Finalize popup state
function finalizePopup(popupShown) {
if (popupShown) {
sessionStorage.setItem('chatPopupActive', 'true');
console.log("chatPersist.js: Popup active, session tagged.");
reinitializeChat();
}
}
// Reinitialize chat
function reinitializeChat(retries = MAX_RETRIES) {
waitForElement('#chat-container', ($chatContainer) => {
if ($chatContainer.is(':visible')) {
document.dispatchEvent(new CustomEvent('chatReinitialize'));
console.log("chatPersist.js: Chat reinitialized.");
} else if (retries > 0) {
console.warn(`chatPersist.js: #chat-container not visible, retrying (${MAX_RETRIES - retries + 1}/${MAX_RETRIES})...`);
setTimeout(() => reinitializeChat(retries - 1), 100);
} else {
console.error("chatPersist.js: #chat-container not visible after retries.");
}
}, retries);
}
// Open popup
function openPopup() {
console.log("chatPersist.js: Opening popup...");
showPopup();
}
// Close popup
function closePopup() {
const $popup = $(`[data-elementor-id="${popupId}"]`);
if ($popup.length) {
$popup.hide().removeClass('elementor-popup-modal--active dialog-visible');
console.log("chatPersist.js: Popup closed.");
}
sessionStorage.removeItem('chatPopupActive');
if (window.location.hash === '#synthia') {
history.replaceState(null, null, window.location.pathname);
console.log("chatPersist.js: Removed #synthia from URL.");
}
}
// Check popup state on load
function checkPopupState() {
if (sessionStorage.getItem('chatPopupActive') === 'true') {
console.log("chatPersist.js: Chat popup active, ensuring #synthia...");
if (window.location.hash !== '#synthia') {
window.location.hash = '#synthia';
} else {
openPopup();
}
}
}
// Setup event listeners
function setupListeners() {
$('a[href="#synthia"]').on('click.synthia', (e) => {
e.preventDefault();
openPopup();
});
$(document).on('elementor/popup/hide.synthia', (event, id) => {
if (id === popupId) {
console.log("chatPersist.js: Elementor popup hide detected.");
closePopup();
}
});
$('a:not([href="#synthia"]):not([href^="javascript"])').on('click.synthia', (e) => {
const href = e.currentTarget.href;
if (sessionStorage.getItem('chatPopupActive') === 'true' && href && !href.includes('#')) {
e.preventDefault();
console.log("chatPersist.js: Navigation detected, appending #synthia to", href);
window.location.href = href + '#synthia';
}
});
$(window).on('hashchange.synthia', () => {
if (window.location.hash === '#synthia' && sessionStorage.getItem('chatPopupActive') === 'true') {
console.log("chatPersist.js: Hashchange to #synthia, opening popup...");
openPopup();
}
});
}
// Initialize
console.log("chatPersist.js: Starting initialization...");
setupListeners();
checkPopupState();
$(document).on('elementor/loaded', () => {
console.log("chatPersist.js: Elementor loaded, rechecking popup state...");
checkPopupState();
});
console.log("chatPersist.js: Initialization complete.");
});
});