PageSourceSearch

https://schoolattestation.ru/wp-content/themes/schoolattestation-child/js/popup.js?ver=1.1.5

js schoolattestation.ru collected 2026-10-06 00:12:32 UTC 6,418 bytes, 152 lines download raw bytes

1document.addEventListener("DOMContentLoaded", function() {
2    console.log("Popup script loaded.");
3
4    const formCache = {};
5    const COUNTDOWN_SECONDS = 5;
6    let countdownInterval;
7
8    function ensurePopupElement() {
9        let popup = document.getElementById("contact-form-popup");
10        if (!popup) {
11            popup = document.createElement("div");
12            popup.id = "contact-form-popup";
13            popup.className = "popup-overlay";
14            popup.style.display = "none";
15            popup.innerHTML = '<div class="popup-content"><button class="popup-close">&times;</button><div class="form-container"></div><div class="thank-you-message" style="display: none;"><h2>Спасибо за вашу заявку!</h2><p>Мы скоро с вами свяжемся.</p><p>Это окно закроется через <span id="countdown-timer">5</span> секунд.</p></div></div>';
16            document.body.appendChild(popup);
17        }
18        return popup;
19    }
20
21    function closePopup() {
22        const popup = document.getElementById("contact-form-popup");
23        if (!popup) return;
24        popup.style.display = "none";
25        document.body.style.overflow = "";
26        const formContainer = popup.querySelector(".form-container");
27        const thankYouMessage = popup.querySelector(".thank-you-message");
28        const countdownTimerSpan = document.getElementById("countdown-timer");
29
30        if (formContainer) {
31            formContainer.innerHTML = "";
32            formContainer.style.display = "block";
33        }
34        if (thankYouMessage) thankYouMessage.style.display = "none";
35        if (countdownTimerSpan) countdownTimerSpan.textContent = COUNTDOWN_SECONDS;
36        clearInterval(countdownInterval);
37    }
38
39    function startCountdown() {
40        const popup = document.getElementById("contact-form-popup");
41        const countdownTimerSpan = document.getElementById("countdown-timer");
42        let timeLeft = COUNTDOWN_SECONDS;
43        if (countdownTimerSpan) countdownTimerSpan.textContent = timeLeft;
44        countdownInterval = setInterval(function() {
45            timeLeft--;
46            if (countdownTimerSpan) countdownTimerSpan.textContent = timeLeft;
47            if (timeLeft <= 0) {
48                clearInterval(countdownInterval);
49                closePopup();
50            }
51        }, 1000);
52    }
53
54    async function openPopupAndLoadForm(buttonElement) {
55        const popup = ensurePopupElement();
56        const formContainer = popup.querySelector(".form-container");
57        const formId = buttonElement.dataset.formId;
58        const nonce = buttonElement.dataset.nonce;
59        const title = buttonElement.dataset.title;
60
61        console.log("Opening popup for form_id:", formId, "title:", title);
62
63        popup.style.display = "flex";
64        document.body.style.overflow = "hidden";
65
66        if (formCache[formId]) {
67            injectFormHtml(formContainer, formCache[formId], title);
68            return;
69        }
70
71        formContainer.classList.add("loading");
72        const loadingText = (typeof popup_data !== "undefined" && popup_data.loading_text) || "Загрузка...";
73        formContainer.innerHTML = "<p>" + loadingText + "</p>";
74
75        const formData = new FormData();
76        formData.append("action", "load_cf7_form");
77        formData.append("form_id", formId);
78        formData.append("nonce", nonce);
79
80        try {
81            const ajaxUrl = (typeof popup_data !== "undefined" && popup_data.ajax_url) || "/wp-admin/admin-ajax.php";
82            const response = await fetch(ajaxUrl, { method: "POST", body: formData });
83            const result = await response.json();
84            formContainer.classList.remove("loading");
85            if (result.success) {
86                formCache[formId] = result.data.html;
87                injectFormHtml(formContainer, result.data.html, title);
88            } else {
89                formContainer.innerHTML = '<p style="color: red;">Ошибка: ' + result.data + '</p>';
90            }
91        } catch (error) {
92            formContainer.classList.remove("loading");
93            formContainer.innerHTML = '<p style="color: red;">Критическая ошибка при загрузке формы.</p>';
94        }
95    }
96
97    function injectFormHtml(formContainer, html, title) {
98        const titleHtml = title ? "<h2>" + title + "</h2>" : "";
99        formContainer.innerHTML = titleHtml + html;
100        try {
101            if (typeof wpcf7 !== "undefined" && typeof wpcf7.init === "function") {
102                const formElement = formContainer.querySelector("form");
103                if (formElement) wpcf7.init(formElement);
104            }
105        } catch(e) {
106            console.error("Error during Contact Form 7 initialization:", e);
107        }
108    }
109
110    // Global click listener for open, close, and outside click
111    document.addEventListener("click", function(event) {
112        // 1. Open popup
113        const openTarget = event.target.closest(".open-popup-btn");
114        if (openTarget) {
115            event.preventDefault();
116            openPopupAndLoadForm(openTarget);
117            return;
118        }
119
120        // 2. Close button click
121        if (event.target.closest(".popup-close") || event.target.classList.contains("popup-close")) {
122            event.preventDefault();
123            closePopup();
124            return;
125        }
126
127        // 3. Outside click (backdrop)
128        const popup = document.getElementById("contact-form-popup");
129        if (popup && popup.style.display === "flex") {
130            if (event.target === popup || event.target.classList.contains("popup-overlay")) {
131                event.preventDefault();
132                closePopup();
133                return;
134            }
135        }
136    });
137
138    document.addEventListener("keydown", function(event) {
139        if (event.key === "Escape") closePopup();
140    });
141
142    document.addEventListener("wpcf7mailsent", function(event) {
143        const popup = document.getElementById("contact-form-popup");
144        if (popup && popup.contains(event.target)) {
145            const formContainer = popup.querySelector(".form-container");
146            const thankYouMessage = popup.querySelector(".thank-you-message");
147            if (formContainer) formContainer.style.display = "none";
148            if (thankYouMessage) thankYouMessage.style.display = "block";
149            startCountdown();
150        }
151    }, false);
152});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.