minify
This commit is contained in:
@@ -1,263 +1,198 @@
|
|||||||
const WEBHOOK_URL = "https://002-001-5dd6e535-4d1c-46bc-9bd9-42ad4bc5f082.odoo4projects.com/webhook/c76e6b4e-af2f-4bc3-9875-6460d0ffc8e3";
|
// --- Category Configuration ---
|
||||||
|
const CATEGORY_CONFIG = {
|
||||||
|
3: { showLocation: true, webhook: "https://002-001-5dd6e535-4d1c-46bc-9bd9-42ad4bc5f082.odoo4projects.com/webhook/c76e6b4e-af2f-4bc3-9875-6460d0ffc8e3" }, // services
|
||||||
|
4: { showLocation: true, webhook: "https://002-001-5dd6e535-4d1c-46bc-9bd9-42ad4bc5f082.odoo4projects.com/webhook/c76e6b4e-af2f-4bc3-9875-6460d0ffc8e3" }, // hosting
|
||||||
|
5: { showLocation: true, webhook: "https://002-001-5dd6e535-4d1c-46bc-9bd9-42ad4bc5f082.odoo4projects.com/webhook/c76e6b4e-af2f-4bc3-9875-6460d0ffc8e3" }, // Workshops
|
||||||
|
7: { showLocation: false, webhook: "https://002-001-5dd6e535-4d1c-46bc-9bd9-42ad4bc5f082.odoo4projects.com/webhook/c76e6b4e-af2f-4bc3-9875-6460d0ffc8e3" }, // modules
|
||||||
|
// Add more categories as needed
|
||||||
|
};
|
||||||
|
|
||||||
// Get URL parameters as an object
|
// --- Get URL parameters as an object ---
|
||||||
function getUrlParams() {
|
function getUrlParams() {
|
||||||
const params = {};
|
return Object.fromEntries(new URLSearchParams(window.location.search).entries());
|
||||||
window.location.search.substring(1).split("&").forEach(pair => {
|
|
||||||
const [key, value] = pair.split("=");
|
|
||||||
if (key) params[decodeURIComponent(key)] = decodeURIComponent(value || "");
|
|
||||||
});
|
|
||||||
return params;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add UTM fields to form
|
// --- Add UTM fields to form ---
|
||||||
function addUtmFields(form) {
|
function addUtmFields(form) {
|
||||||
const utmParams = ["utm_source", "utm_medium", "utm_campaign", "utm_term", "utm_content"];
|
const utmParams = ["utm_source", "utm_medium", "utm_campaign", "utm_term", "utm_content"];
|
||||||
const urlParams = getUrlParams();
|
const urlParams = getUrlParams();
|
||||||
|
|
||||||
utmParams.forEach(param => {
|
utmParams.forEach(param => {
|
||||||
let input = form.querySelector(`input[name="${param}"]`);
|
let input = form.querySelector(`input[name="${param}"]`);
|
||||||
if (!input) {
|
if (!input) {
|
||||||
input = document.createElement("input");
|
input = document.createElement("input");
|
||||||
input.type = "hidden";
|
input.type = "hidden";
|
||||||
input.name = param;
|
input.name = param;
|
||||||
form.appendChild(input);
|
form.appendChild(input);
|
||||||
}
|
}
|
||||||
input.value = urlParams[param] || "";
|
input.value = urlParams[param] || "";
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- Create Modal ---
|
||||||
function createModal() {
|
function createModal() {
|
||||||
const modal = document.createElement("div");
|
const modal = document.createElement("div");
|
||||||
modal.id = "buyNowModal";
|
modal.id = "buyNowModal";
|
||||||
modal.style.position = "fixed";
|
Object.assign(modal.style, {
|
||||||
modal.style.top = "0";
|
position: "fixed",
|
||||||
modal.style.left = "0";
|
top: "0",
|
||||||
modal.style.width = "100%";
|
left: "0",
|
||||||
modal.style.height = "100%";
|
width: "100%",
|
||||||
modal.style.backgroundColor = "rgba(0,0,0,0.6)";
|
height: "100%",
|
||||||
modal.style.display = "none";
|
backgroundColor: "rgba(0,0,0,0.6)",
|
||||||
modal.style.justifyContent = "center";
|
display: "none",
|
||||||
modal.style.alignItems = "center";
|
justifyContent: "center",
|
||||||
modal.style.zIndex = "1000";
|
alignItems: "center",
|
||||||
|
zIndex: "1000"
|
||||||
|
});
|
||||||
|
|
||||||
modal.innerHTML = `
|
modal.innerHTML = `
|
||||||
<div style="
|
<div style="background: #fff; padding: 40px 30px; border-radius: 16px; max-width: 500px; width: 90%; position: relative; font-family: 'Arial', sans-serif; box-shadow: 0 10px 25px rgba(0,0,0,0.2);">
|
||||||
background: #ffffff;
|
<span id="closeModal" style="position: absolute; top: 15px; right: 20px; cursor: pointer; font-weight: bold; font-size: 24px; color: #555;">×</span>
|
||||||
padding: 40px 30px;
|
<h2 style="margin-bottom: 15px; font-size: 24px; color: #333;">Order Details</h2>
|
||||||
border-radius: 16px;
|
<p id="productText" style="margin-bottom: 25px; font-weight: 500; color: #555;"></p>
|
||||||
max-width: 500px;
|
|
||||||
width: 90%;
|
|
||||||
position: relative;
|
|
||||||
font-family: 'Arial', sans-serif;
|
|
||||||
box-shadow: 0 10px 25px rgba(0,0,0,0.2);
|
|
||||||
">
|
|
||||||
<span id="closeModal" style="
|
|
||||||
position: absolute;
|
|
||||||
top: 15px;
|
|
||||||
right: 20px;
|
|
||||||
cursor: pointer;
|
|
||||||
font-weight: bold;
|
|
||||||
font-size: 24px;
|
|
||||||
color: #555;
|
|
||||||
">×</span>
|
|
||||||
|
|
||||||
<h2 style="margin-bottom: 15px; font-size: 24px; color: #333;">Order Details</h2>
|
|
||||||
<p id="productText" style="margin-bottom: 25px; font-weight: 500; color: #555;"></p>
|
|
||||||
|
|
||||||
<form id="buyForm" style="display: flex; flex-direction: column; gap: 15px;">
|
<form id="buyForm" style="display: flex; flex-direction: column; gap: 15px;">
|
||||||
|
<select name="location" id="locationSelect" required style="display: none; padding: 12px 15px; font-size: 16px; border: 1px solid #ccc; border-radius: 8px; outline: none;">
|
||||||
<!-- Hidden select, shown only if product starts with "_" -->
|
<option value="">Select Location</option>
|
||||||
<select name="location" id="locationSelect" required style="
|
<option value="Boston">Boston</option>
|
||||||
display: none;
|
<option value="Manchester">Manchester</option>
|
||||||
padding: 12px 15px;
|
</select>
|
||||||
font-size: 16px;
|
|
||||||
border: 1px solid #ccc;
|
|
||||||
border-radius: 8px;
|
|
||||||
outline: none;
|
|
||||||
">
|
|
||||||
<option value="">Select Location</option>
|
|
||||||
<option value="Boston">Boston</option>
|
|
||||||
<option value="Manchester">Manchester</option>
|
|
||||||
</select>
|
|
||||||
|
|
||||||
<input type="text" name="name" placeholder="Name" required style="padding: 12px 15px; font-size: 16px; border: 1px solid #ccc; border-radius: 8px; outline: none;">
|
<input type="text" name="name" placeholder="Name" required style="padding: 12px 15px; border: 1px solid #ccc; border-radius: 8px;">
|
||||||
<input type="text" name="company" placeholder="Company" required style="padding: 12px 15px; font-size: 16px; border: 1px solid #ccc; border-radius: 8px; outline: none;">
|
<input type="text" name="company" placeholder="Company" required style="padding: 12px 15px; border: 1px solid #ccc; border-radius: 8px;">
|
||||||
<input type="text" name="country" placeholder="Country" required style="padding: 12px 15px; font-size: 16px; border: 1px solid #ccc; border-radius: 8px; outline: none;">
|
<input type="text" name="country" placeholder="Country" required style="padding: 12px 15px; border: 1px solid #ccc; border-radius: 8px;">
|
||||||
<input type="text" name="street" placeholder="Street" required style="padding: 12px 15px; font-size: 16px; border: 1px solid #ccc; border-radius: 8px; outline: none;">
|
<input type="text" name="street" placeholder="Street" required style="padding: 12px 15px; border: 1px solid #ccc; border-radius: 8px;">
|
||||||
<div style="display: flex; gap: 10px;">
|
<div style="display: flex; gap: 10px;">
|
||||||
<input type="text" name="zip" placeholder="ZIP" required style="max-width: 100px; flex: 1 1 0; padding: 12px 10px; font-size: 16px; border: 1px solid #ccc; border-radius: 8px;">
|
<input type="text" name="zip" placeholder="ZIP" required style="max-width: 100px; flex: 1 1 0; padding: 12px 10px; border: 1px solid #ccc; border-radius: 8px;">
|
||||||
<input type="text" name="town" placeholder="Town" required style="flex: 2 1 0; padding: 12px 10px; font-size: 16px; border: 1px solid #ccc; border-radius: 8px;">
|
<input type="text" name="town" placeholder="Town" required style="flex: 2 1 0; padding: 12px 10px; border: 1px solid #ccc; border-radius: 8px;">
|
||||||
</div>
|
|
||||||
|
|
||||||
<input type="email" name="email" placeholder="Email" required style="padding: 12px 15px; font-size: 16px; border: 1px solid #ccc; border-radius: 8px; outline: none;">
|
|
||||||
|
|
||||||
<input type="hidden" name="product">
|
|
||||||
<input type="hidden" name="price">
|
|
||||||
|
|
||||||
<button id="submitBuy" type="submit" style="
|
|
||||||
padding: 14px;
|
|
||||||
background: #007BFF;
|
|
||||||
color: #fff;
|
|
||||||
border: none;
|
|
||||||
border-radius: 8px;
|
|
||||||
cursor: pointer;
|
|
||||||
font-size: 18px;
|
|
||||||
font-weight: bold;
|
|
||||||
transition: background 0.3s;
|
|
||||||
">Send Order</button>
|
|
||||||
</form>
|
|
||||||
|
|
||||||
<div id="confirmation" style="
|
|
||||||
display: none;
|
|
||||||
margin-top: 20px;
|
|
||||||
padding: 20px;
|
|
||||||
background-color: #e6ffed;
|
|
||||||
color: #056608;
|
|
||||||
border-radius: 12px;
|
|
||||||
text-align: center;
|
|
||||||
font-weight: 500;
|
|
||||||
">
|
|
||||||
<p>Thank you for your purchase! 🎉</p>
|
|
||||||
<button id="closeConfirmation" style="
|
|
||||||
margin-top: 10px;
|
|
||||||
padding: 10px 20px;
|
|
||||||
background: #007BFF;
|
|
||||||
color: #fff;
|
|
||||||
border: none;
|
|
||||||
border-radius: 8px;
|
|
||||||
cursor: pointer;
|
|
||||||
font-size: 16px;
|
|
||||||
transition: background 0.3s;
|
|
||||||
">Close</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
`;
|
<input type="email" name="email" placeholder="Email" required style="padding: 12px 15px; border: 1px solid #ccc; border-radius: 8px;">
|
||||||
|
<input type="hidden" name="product">
|
||||||
|
<input type="hidden" name="price">
|
||||||
|
<input type="hidden" name="id">
|
||||||
|
<input type="hidden" name="category">
|
||||||
|
|
||||||
document.body.appendChild(modal);
|
<button id="submitBuy" type="submit" style="padding: 14px; background: #007BFF; color: #fff; border: none; border-radius: 8px; cursor: pointer; font-size: 18px; font-weight: bold;">Send Order</button>
|
||||||
|
</form>
|
||||||
|
|
||||||
// Close modal
|
<div id="confirmation" style="display: none; margin-top: 20px; padding: 20px; background-color: #e6ffed; color: #056608; border-radius: 12px; text-align: center; font-weight: 500;">
|
||||||
document.getElementById("closeModal").onclick = () => { document.getElementById("buyNowModal").style.display = "none"; };
|
<p>Thank you for your purchase! 🎉</p>
|
||||||
document.getElementById("closeConfirmation").onclick = () => {
|
<button id="closeConfirmation" style="margin-top: 10px; padding: 10px 20px; background: #007BFF; color: #fff; border: none; border-radius: 8px; cursor: pointer;">Close</button>
|
||||||
document.getElementById("confirmation").style.display = "none";
|
</div>
|
||||||
document.getElementById("buyNowModal").style.display = "none";
|
</div>
|
||||||
};
|
`;
|
||||||
modal.onclick = (e) => { if (e.target === modal) modal.style.display = "none"; };
|
|
||||||
|
|
||||||
return modal;
|
document.body.appendChild(modal);
|
||||||
|
document.getElementById("closeModal").onclick = () => modal.style.display = "none";
|
||||||
|
document.getElementById("closeConfirmation").onclick = () => {
|
||||||
|
document.getElementById("confirmation").style.display = "none";
|
||||||
|
modal.style.display = "none";
|
||||||
|
};
|
||||||
|
modal.onclick = e => { if (e.target === modal) modal.style.display = "none"; };
|
||||||
|
return modal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- Open Modal ---
|
||||||
function openModal(productHref) {
|
function openModal(productHref) {
|
||||||
const modal = document.getElementById("buyNowModal");
|
const modal = document.getElementById("buyNowModal");
|
||||||
modal.style.display = "flex";
|
const form = modal.querySelector("#buyForm");
|
||||||
|
const confirmation = modal.querySelector("#confirmation");
|
||||||
|
modal.style.display = "flex";
|
||||||
|
form.style.display = "flex";
|
||||||
|
confirmation.style.display = "none";
|
||||||
|
|
||||||
// Reset form display for repeated orders
|
// Parse URL /<id>/<price>/<category>/<product>
|
||||||
const form = modal.querySelector("#buyForm");
|
const parts = productHref.split("/").filter(Boolean);
|
||||||
const confirmation = modal.querySelector("#confirmation");
|
const [id, price, category, ...productParts] = parts;
|
||||||
form.style.display = "flex";
|
const product = decodeURIComponent(productParts.join("/"));
|
||||||
confirmation.style.display = "none";
|
const categoryNum = parseInt(category, 10);
|
||||||
|
const config = CATEGORY_CONFIG[categoryNum] || { showLocation: false, webhook: "" };
|
||||||
|
|
||||||
// Parse productHref
|
// Handle location field visibility
|
||||||
let product = "Unknown";
|
const locationSelect = document.getElementById("locationSelect");
|
||||||
let price = "0";
|
if (config.showLocation) {
|
||||||
let id = "";
|
locationSelect.style.display = "block";
|
||||||
|
locationSelect.setAttribute("required", "true");
|
||||||
|
} else {
|
||||||
|
locationSelect.style.display = "none";
|
||||||
|
locationSelect.removeAttribute("required");
|
||||||
|
locationSelect.value = "";
|
||||||
|
}
|
||||||
|
|
||||||
if (productHref.includes("/")) {
|
// Update form fields
|
||||||
const parts = productHref.split("/").filter(Boolean);
|
form.querySelector('input[name="id"]').value = id || "";
|
||||||
id = parts[0] || "";
|
form.querySelector('input[name="price"]').value = price || "0";
|
||||||
price = parts[1] || "0";
|
form.querySelector('input[name="category"]').value = category || "";
|
||||||
product = parts.slice(2).join("/");
|
form.querySelector('input[name="product"]').value = product || "Unknown";
|
||||||
product = decodeURIComponent(product);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Show/hide location select if ID starts with "_"
|
// Store webhook dynamically for submission
|
||||||
const locationSelect = document.getElementById("locationSelect");
|
form.dataset.webhook = config.webhook || "";
|
||||||
if (id.startsWith("_")) {
|
|
||||||
locationSelect.style.display = "block";
|
|
||||||
locationSelect.setAttribute("required", "true");
|
|
||||||
} else {
|
|
||||||
locationSelect.style.display = "none";
|
|
||||||
locationSelect.removeAttribute("required");
|
|
||||||
locationSelect.value = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set hidden inputs
|
// Update display text
|
||||||
form.querySelector('input[name="product"]').value = product;
|
modal.querySelector("#productText").textContent = `You will buy ${product} for $${price}.`;
|
||||||
form.querySelector('input[name="price"]').value = price;
|
|
||||||
|
|
||||||
// Add or update hidden input for ID
|
// Add UTM fields
|
||||||
let idInput = form.querySelector('input[name="id"]');
|
addUtmFields(form);
|
||||||
if (!idInput) {
|
|
||||||
idInput = document.createElement("input");
|
|
||||||
idInput.type = "hidden";
|
|
||||||
idInput.name = "id";
|
|
||||||
form.appendChild(idInput);
|
|
||||||
}
|
|
||||||
idInput.value = id;
|
|
||||||
|
|
||||||
// Update product text
|
|
||||||
modal.querySelector("#productText").textContent = `You will buy ${product} for $${price}.`;
|
|
||||||
|
|
||||||
// Add/update UTM fields
|
|
||||||
addUtmFields(form);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- Handle Form Submit ---
|
||||||
function handleFormSubmit() {
|
function handleFormSubmit() {
|
||||||
const form = document.getElementById("buyForm");
|
const form = document.getElementById("buyForm");
|
||||||
const confirmation = document.getElementById("confirmation");
|
const confirmation = document.getElementById("confirmation");
|
||||||
|
|
||||||
if (!form) {
|
form.addEventListener("submit", async (e) => {
|
||||||
console.error("Form not found!");
|
e.preventDefault();
|
||||||
return;
|
addUtmFields(form);
|
||||||
|
|
||||||
|
const data = Object.fromEntries(new FormData(form).entries());
|
||||||
|
const webhookUrl = form.dataset.webhook;
|
||||||
|
|
||||||
|
if (!webhookUrl) {
|
||||||
|
alert("No webhook configured for this category.");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
form.addEventListener("submit", async (e) => {
|
try {
|
||||||
e.preventDefault();
|
const res = await fetch(webhookUrl, {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify(data),
|
||||||
|
});
|
||||||
|
|
||||||
// Refresh UTM fields just before submitting
|
if (res.ok) {
|
||||||
addUtmFields(form);
|
form.style.display = "none";
|
||||||
|
confirmation.style.display = "block";
|
||||||
const data = {};
|
form.reset();
|
||||||
new FormData(form).forEach((value, key) => (data[key] = value));
|
} else {
|
||||||
|
alert("Failed to submit form.");
|
||||||
try {
|
}
|
||||||
const res = await fetch(WEBHOOK_URL, {
|
} catch (err) {
|
||||||
method: "POST",
|
console.error(err);
|
||||||
headers: { "Content-Type": "application/json" },
|
alert("Error submitting form.");
|
||||||
body: JSON.stringify(data)
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (res.ok) {
|
|
||||||
form.style.display = "none";
|
|
||||||
confirmation.style.display = "block";
|
|
||||||
form.reset();
|
|
||||||
} else {
|
|
||||||
alert("Failed to submit form.");
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
console.error(err);
|
|
||||||
alert("Error submitting form.");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- Attach Buttons ---
|
||||||
function attachButtons() {
|
function attachButtons() {
|
||||||
const buttons = Array.from(document.querySelectorAll("button, a"));
|
const buttons = Array.from(document.querySelectorAll("button, a"));
|
||||||
buttons.forEach(btn => {
|
buttons.forEach(btn => {
|
||||||
const text = btn.textContent.trim();
|
const text = btn.textContent.trim();
|
||||||
if (text === "Buy Now" || text === "Book Now") {
|
if (text === "Buy Now" || text === "Book Now") {
|
||||||
btn.addEventListener("click", (e) => {
|
btn.addEventListener("click", (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
const href = btn.getAttribute("href") || btn.dataset.product || "Unknown/0";
|
const href = btn.getAttribute("href") || btn.dataset.product || "Unknown/0/0";
|
||||||
openModal(href);
|
openModal(href);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- Initialize ---
|
||||||
document.addEventListener("DOMContentLoaded", () => {
|
document.addEventListener("DOMContentLoaded", () => {
|
||||||
createModal();
|
createModal();
|
||||||
handleFormSubmit();
|
handleFormSubmit();
|
||||||
attachButtons();
|
attachButtons();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
1
public/buyModal.min.js
vendored
Normal file
1
public/buyModal.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
4
public/minify
Executable file
4
public/minify
Executable file
@@ -0,0 +1,4 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
npm install -g terser
|
||||||
|
terser buyModal.js -o buyModal.min.js -c -m
|
||||||
|
|
||||||
Reference in New Issue
Block a user