/* =========================================================
روائع الخليجية للعود
JavaScript منظم للهيدر والتصنيفات
========================================================= */
(function () {
/* =====================================================
1) تحديد الصفحة الرئيسية والصفحات الداخلية
===================================================== */
function setPageClasses() {
const path = window.location.pathname.replace(/\/+$/, "");
const parts = path
.split("/")
.filter(Boolean)
.filter(function (part) {
return part !== "ar" && part !== "en";
});
const isSallaPreview =
window.location.hostname === "salla.sa" ||
window.location.hostname.endsWith(".salla.sa");
const isHomePage = isSallaPreview
? parts.length === 1
: parts.length === 0;
document.body.classList.toggle("store-real-home", isHomePage);
document.body.classList.toggle("store-inner-page", !isHomePage);
}
/* =====================================================
2) إنشاء الروابط العلوية
===================================================== */
function createTopLinks() {
if (document.querySelector(".custom-top-links")) {
return true;
}
const header = document.querySelector("header");
if (!header) {
return false;
}
const topLinks = document.createElement("div");
topLinks.className = "custom-top-links";
topLinks.innerHTML = `
من نحن
|
سياسة الاستبدال والاسترجاع
|
التوصيل والشحن
`;
header.prepend(topLinks);
return true;
}
/* =====================================================
3) إنشاء صف واحد جديد للتصنيفات
===================================================== */
function createOneRowCategories() {
const quicklinks = document.querySelector(".quicklinks");
if (!quicklinks) {
return false;
}
if (quicklinks.querySelector(".custom-one-row-categories")) {
return true;
}
const originalLinks = Array.from(
quicklinks.querySelectorAll(
"li > a, .main-menu-item"
)
);
if (!originalLinks.length) {
return false;
}
const uniqueLinks = [];
const seen = new Set();
originalLinks.forEach(function (link) {
const text = (link.textContent || "").trim();
const href = link.getAttribute("href") || "";
if (!text) {
return;
}
const key = text + "|" + href;
if (!seen.has(key)) {
seen.add(key);
uniqueLinks.push({
text: text,
href: href
});
}
});
if (!uniqueLinks.length) {
return false;
}
const row = document.createElement("div");
row.className = "custom-one-row-categories";
uniqueLinks.forEach(function (linkData) {
const item = document.createElement("a");
item.className = "custom-one-row-category";
item.textContent = linkData.text;
item.href = linkData.href || "#";
row.appendChild(item);
});
Array.from(quicklinks.children).forEach(function (child) {
if (!child.classList.contains("custom-one-row-categories")) {
child.style.display = "none";
}
});
quicklinks.appendChild(row);
quicklinks.classList.add("custom-categories-ready");
return true;
}
/* =====================================================
4) تشغيل جميع وظائف الهيدر
===================================================== */
function initializeHeader() {
setPageClasses();
createTopLinks();
createOneRowCategories();
}
/* =====================================================
5) انتظار عناصر سلة إذا تأخر تحميلها
===================================================== */
function waitForHeaderElements() {
let attempts = 0;
const maxAttempts = 50;
const timer = setInterval(function () {
attempts++;
setPageClasses();
const topLinksReady = createTopLinks();
const categoriesReady = createOneRowCategories();
if (
(topLinksReady && categoriesReady) ||
attempts >= maxAttempts
) {
clearInterval(timer);
}
}, 200);
}
/* =====================================================
6) التشغيل
===================================================== */
if (document.readyState === "loading") {
document.addEventListener(
"DOMContentLoaded",
function () {
initializeHeader();
waitForHeaderElements();
}
);
} else {
initializeHeader();
waitForHeaderElements();
}
})();
/* =========================================================
تحويل القوائم المنسدلة إلى أزرار
========================================================= */
document.addEventListener("DOMContentLoaded", function () {
function convertSelectsToButtons() {
const selects = document.querySelectorAll("select");
selects.forEach(function (select) {
if (select.dataset.buttonsConverted === "true") return;
const validOptions = Array.from(select.options).filter(function (option) {
return option.value && option.textContent.trim();
});
if (validOptions.length < 2) return;
select.dataset.buttonsConverted = "true";
const buttonsWrapper = document.createElement("div");
buttonsWrapper.style.display = "flex";
buttonsWrapper.style.flexWrap = "wrap";
buttonsWrapper.style.gap = "8px";
buttonsWrapper.style.marginTop = "10px";
buttonsWrapper.style.marginBottom = "10px";
validOptions.forEach(function (option) {
const button = document.createElement("button");
button.type = "button";
button.textContent = option.textContent.trim();
button.style.background = "#ffffff";
button.style.color = "#222";
button.style.border = "1px solid #d6d6d6";
button.style.borderRadius = "7px";
button.style.padding = "8px 13px";
button.style.cursor = "pointer";
button.style.fontSize = "14px";
button.style.fontWeight = "600";
button.style.transition = "0.2s";
button.style.fontFamily = "inherit";
if (select.value === option.value) {
activateButton(button);
}
button.addEventListener("click", function () {
select.value = option.value;
select.dispatchEvent(
new Event("change", {
bubbles: true
})
);
select.dispatchEvent(
new Event("input", {
bubbles: true
})
);
buttonsWrapper
.querySelectorAll("button")
.forEach(function (btn) {
resetButton(btn);
});
activateButton(button);
});
buttonsWrapper.appendChild(button);
});
select.insertAdjacentElement("afterend", buttonsWrapper);
select.style.position = "absolute";
select.style.opacity = "0";
select.style.pointerEvents = "none";
select.style.width = "1px";
select.style.height = "1px";
});
}
function activateButton(button) {
button.style.background = "#111111";
button.style.color = "#ffffff";
button.style.borderColor = "#111111";
}
function resetButton(button) {
button.style.background = "#ffffff";
button.style.color = "#222222";
button.style.borderColor = "#d6d6d6";
}
convertSelectsToButtons();
const observer = new MutationObserver(function () {
convertSelectsToButtons();
});
observer.observe(document.body, {
childList: true,
subtree: true
});
});
/* =========================================================
أيقونات التصنيفات داخل نافذة العرض
+ إضافة أيقونة خاصة للعطور
========================================================= */
document.addEventListener("DOMContentLoaded", function () {
const incenseIcon = `
`;
const iconMap = {
"البخورات": incenseIcon,
"البخور": incenseIcon,
"العطور": `
`,
"معطرات اجواء شرقية": "🕌",
"معطرات اجواء فرنسية": "🌸",
"فواحات منزلية": "🏠",
"خمريات الجسم والشعر": "🧴"
};
function normalizeText(text) {
return (text || "")
.replace(/\s+/g, " ")
.trim();
}
function applyIcons() {
const popupCards = document.querySelectorAll(
'[role="dialog"] a,' +
'[role="dialog"] .offer-item,' +
'[role="dialog"] .category-item,' +
'.modal a,' +
'.modal .offer-item,' +
'.modal .category-item'
);
popupCards.forEach(function (card) {
if (card.dataset.iconApplied === "true") return;
const text = normalizeText(card.textContent);
for (const [name, icon] of Object.entries(iconMap)) {
if (!text.includes(name)) continue;
const elements = card.querySelectorAll("*");
let target = null;
elements.forEach(function (el) {
const t = normalizeText(el.textContent);
if (!target && t === name) {
target = el;
}
});
if (!target) {
target = card;
}
const iconBox = document.createElement("span");
iconBox.style.display = "flex";
iconBox.style.alignItems = "center";
iconBox.style.justifyContent = "center";
iconBox.style.width = "100%";
iconBox.style.height = "auto";
iconBox.style.marginBottom = "6px";
iconBox.style.textAlign = "center";
iconBox.style.color = "#d8a35d";
if (icon.trim().startsWith("