-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
173 lines (147 loc) · 5.56 KB
/
Copy pathscript.js
File metadata and controls
173 lines (147 loc) · 5.56 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const form = document.querySelector("#enquiryForm");
const statusEl = document.querySelector("#formStatus");
const submissionBalloons = document.querySelector("#submissionBalloons");
function launchSubmissionBalloons() {
if (!submissionBalloons || window.matchMedia("(prefers-reduced-motion: reduce)").matches) return;
const colors = [
["#25d366", "#6cf19a"],
["#d99a29", "#f2c56e"],
["#f24b5d", "#ff8b98"],
["#0f5f55", "#35b69f"]
];
submissionBalloons.replaceChildren();
Array.from({ length: 12 }).forEach((_, index) => {
const balloon = document.createElement("span");
const [color, light] = colors[index % colors.length];
const size = 28 + (index % 4) * 5;
const left = 8 + ((index * 8) % 86);
const drift = index % 2 === 0 ? 20 + index * 2 : -22 - index * 2;
const tilt = index % 2 === 0 ? -8 : 9;
const duration = 2.6 + (index % 3) * 0.25;
balloon.className = "submission-balloon";
balloon.style.setProperty("--balloon-color", color);
balloon.style.setProperty("--balloon-light", light);
balloon.style.setProperty("--balloon-size", `${size}px`);
balloon.style.setProperty("--balloon-left", `${left}%`);
balloon.style.setProperty("--balloon-drift", `${drift}px`);
balloon.style.setProperty("--balloon-tilt", `${tilt}deg`);
balloon.style.setProperty("--balloon-end-tilt", `${tilt * -1}deg`);
balloon.style.setProperty("--balloon-duration", `${duration}s`);
balloon.style.animationDelay = `${index * 0.08}s`;
submissionBalloons.appendChild(balloon);
});
window.setTimeout(() => submissionBalloons.replaceChildren(), 3600);
}
document.querySelectorAll("[data-fill-lead]").forEach((button) => {
button.addEventListener("click", () => {
const leadMagnet = document.querySelector('[name="leadMagnet"]');
if (leadMagnet) leadMagnet.value = button.dataset.fillLead;
const leadMagnetSource = document.querySelector('[name="leadMagnetSource"]');
if (leadMagnetSource) leadMagnetSource.value = "lead magnet card";
});
});
if (form && statusEl) {
const params = new URLSearchParams(window.location.search);
const attribution = {
pageUrl: window.location.href,
referrer: document.referrer,
utmSource: params.get("utm_source") || "",
utmMedium: params.get("utm_medium") || "",
utmCampaign: params.get("utm_campaign") || "",
utmTerm: params.get("utm_term") || "",
utmContent: params.get("utm_content") || ""
};
Object.entries(attribution).forEach(([name, value]) => {
const input = form.elements[name];
if (input) input.value = value;
});
form.addEventListener("submit", async (event) => {
event.preventDefault();
statusEl.className = "form-status";
statusEl.textContent = "Sending enquiry...";
try {
const formData = new FormData(form);
const body = Object.fromEntries(formData.entries());
Object.assign(body, attribution);
body.userAgent = navigator.userAgent;
const response = await fetch(form.action, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body)
});
const result = await response.json().catch(() => ({
ok: response.ok,
message: response.ok
? "Thank you for your submission, we will get back to you as soon as possible."
: "Unable to send enquiry."
}));
if (!response.ok || !result.ok) {
throw new Error(result.message || "Unable to send enquiry.");
}
statusEl.classList.add("success");
statusEl.textContent =
result.message || "Thank you for your submission, we will get back to you as soon as possible.";
launchSubmissionBalloons();
form.reset();
} catch (error) {
statusEl.classList.add("error");
statusEl.textContent =
error.message || "Unable to send enquiry. Please email or WhatsApp us directly.";
}
});
}
(function whatsappWidget() {
const widget = document.getElementById("waWidget");
if (!widget) return;
const phone = "6596983731";
const launcher = document.getElementById("waLauncher");
const panel = document.getElementById("waPanel");
const closeBtn = document.getElementById("waClose");
const suggestions = document.getElementById("waSuggestions");
const compose = document.getElementById("waCompose");
const input = document.getElementById("waInput");
if (!launcher || !panel) return;
const openWhatsApp = (message) => {
const url = `https://wa.me/${phone}?text=${encodeURIComponent(message)}`;
window.open(url, "_blank", "noopener");
};
const openPanel = () => {
panel.hidden = false;
launcher.setAttribute("aria-expanded", "true");
if (input) input.focus();
};
const closePanel = () => {
panel.hidden = true;
launcher.setAttribute("aria-expanded", "false");
launcher.focus();
};
launcher.addEventListener("click", () => {
if (panel.hidden) {
openPanel();
} else {
closePanel();
}
});
if (closeBtn) {
closeBtn.addEventListener("click", closePanel);
}
if (suggestions) {
suggestions.addEventListener("click", (event) => {
const chip = event.target.closest(".wa-chip");
if (!chip) return;
openWhatsApp(chip.textContent.trim());
});
}
if (compose && input) {
compose.addEventListener("submit", (event) => {
event.preventDefault();
const message = input.value.trim();
if (!message) return;
openWhatsApp(message);
input.value = "";
});
}
document.addEventListener("keydown", (event) => {
if (event.key === "Escape" && !panel.hidden) closePanel();
});
})();