// Homepage Editor JavaScript let homepageData = {}; let quillEditors = {}; let currentMediaPicker = null; // Initialize Quill editors function initializeQuillEditors() { // Check if Quill is loaded if (typeof Quill === "undefined") { console.error("Quill.js is not loaded!"); alert("Text editor failed to load. Please refresh the page."); return; } const toolbarOptions = [ ["bold", "italic", "underline", "strike"], ["blockquote", "code-block"], [{ header: 1 }, { header: 2 }], [{ list: "ordered" }, { list: "bullet" }], [{ script: "sub" }, { script: "super" }], [{ indent: "-1" }, { indent: "+1" }], [{ direction: "rtl" }], [{ size: ["small", false, "large", "huge"] }], [{ header: [1, 2, 3, 4, 5, 6, false] }], [{ color: [] }, { background: [] }], [{ font: [] }], [{ align: [] }], ["link"], ["clean"], ]; try { // Initialize Quill for each description field quillEditors.hero = new Quill("#heroDescription", { theme: "snow", modules: { toolbar: toolbarOptions }, placeholder: "Enter hero section description...", }); quillEditors.promotion = new Quill("#promotionDescription", { theme: "snow", modules: { toolbar: toolbarOptions }, placeholder: "Enter promotion description...", }); quillEditors.portfolio = new Quill("#portfolioDescription", { theme: "snow", modules: { toolbar: toolbarOptions }, placeholder: "Enter portfolio description...", }); console.log("Quill editors initialized successfully"); } catch (error) { console.error("Error initializing Quill editors:", error); alert( "Failed to initialize text editors. Please check the console for errors." ); } } document.addEventListener("DOMContentLoaded", function () { initializeQuillEditors(); checkAuth().then((authenticated) => { if (authenticated) { loadHomepageSettings(); setupMediaLibraryListener(); } }); }); // Setup media library selection listener function setupMediaLibraryListener() { window.addEventListener("message", function (event) { // Security: verify origin if needed if ( event.data && event.data.type === "mediaSelected" && currentMediaPicker ) { const { section, field } = currentMediaPicker; handleMediaSelection(section, field, event.data.media); currentMediaPicker = null; } }); } async function loadHomepageSettings() { try { const response = await fetch("/api/admin/homepage/settings", { credentials: "include", }); const data = await response.json(); if (data.success) { homepageData = data.settings || {}; // If no data exists, load defaults from the frontend if (Object.keys(homepageData).length === 0) { console.log("No homepage data found, loading defaults from frontend"); await loadDefaultsFromFrontend(); } populateFields(); } } catch (error) { console.error("Failed to load homepage settings:", error); // Load defaults if API fails await loadDefaultsFromFrontend(); populateFields(); } } // Load default content from the current homepage async function loadDefaultsFromFrontend() { homepageData = { hero: { enabled: true, headline: "Welcome to Sky Art Shop", subheading: "Your destination for creative stationery and supplies", description: "
Discover our curated collection of scrapbooking, journaling, cardmaking, and collaging supplies. Express your creativity and bring your artistic vision to life.
", ctaText: "Shop Now", ctaLink: "/shop.html", backgroundUrl: "", layout: "text-left", }, promotion: { enabled: true, title: "Get Inspired", description: "At Sky Art Shop, we believe in the power of creativity to transform and inspire. Whether you're an experienced crafter or just beginning your creative journey, we have everything you need to bring your ideas to life.
Explore our collection of washi tapes, stickers, stamps, and more. Each item is carefully selected to help you create something beautiful and meaningful.
", imageUrl: "", imagePosition: "left", textAlignment: "left", }, portfolio: { enabled: true, title: "Featured Products", description: "Discover our most popular items
", count: 6, }, }; } function populateFields() { console.log("Populating fields with data:", homepageData); // Hero Section if (homepageData.hero) { document.getElementById("heroEnabled").checked = homepageData.hero.enabled !== false; document.getElementById("heroHeadline").value = homepageData.hero.headline || ""; document.getElementById("heroSubheading").value = homepageData.hero.subheading || ""; if (homepageData.hero.description) { quillEditors.hero.root.innerHTML = homepageData.hero.description; } document.getElementById("heroCtaText").value = homepageData.hero.ctaText || ""; document.getElementById("heroCtaLink").value = homepageData.hero.ctaLink || ""; if (homepageData.hero.backgroundUrl) { document.getElementById("heroBackgroundUrl").value = homepageData.hero.backgroundUrl; displayMediaPreview( "hero", "background", homepageData.hero.backgroundUrl ); } if (homepageData.hero.layout) { const heroSection = document.getElementById("heroSection"); heroSection.setAttribute("data-layout", homepageData.hero.layout); setActiveButton(`heroSection`, `layout-${homepageData.hero.layout}`); } toggleSection("hero"); } // Promotion Section if (homepageData.promotion) { document.getElementById("promotionEnabled").checked = homepageData.promotion.enabled !== false; document.getElementById("promotionTitle").value = homepageData.promotion.title || ""; if (homepageData.promotion.description) { quillEditors.promotion.root.innerHTML = homepageData.promotion.description; } if (homepageData.promotion.imageUrl) { document.getElementById("promotionImageUrl").value = homepageData.promotion.imageUrl; displayMediaPreview( "promotion", "image", homepageData.promotion.imageUrl ); } if (homepageData.promotion.imagePosition) { const promotionSection = document.getElementById("promotionSection"); promotionSection.setAttribute( "data-image-position", homepageData.promotion.imagePosition ); setActiveButton( `promotionSection`, `position-${homepageData.promotion.imagePosition}` ); } if (homepageData.promotion.textAlignment) { const promotionSection = document.getElementById("promotionSection"); promotionSection.setAttribute( "data-text-alignment", homepageData.promotion.textAlignment ); setActiveButton( `promotionSection`, `align-${homepageData.promotion.textAlignment}` ); } toggleSection("promotion"); } // Portfolio Section if (homepageData.portfolio) { document.getElementById("portfolioEnabled").checked = homepageData.portfolio.enabled !== false; document.getElementById("portfolioTitle").value = homepageData.portfolio.title || ""; if (homepageData.portfolio.description) { quillEditors.portfolio.root.innerHTML = homepageData.portfolio.description; } document.getElementById("portfolioCount").value = homepageData.portfolio.count || 6; toggleSection("portfolio"); } // Show success message showSuccess( "Homepage content loaded! You can now edit and preview your changes." ); } function setActiveButton(sectionId, className) { const section = document.getElementById(sectionId); if (section) { const buttons = section.querySelectorAll(".alignment-btn"); buttons.forEach((btn) => { if (btn.classList.contains(className)) { btn.classList.add("active"); } }); } } function toggleSection(sectionName) { const enabled = document.getElementById(`${sectionName}Enabled`).checked; const section = document.getElementById(`${sectionName}Section`); const content = section.querySelector(".section-content"); if (enabled) { section.classList.remove("disabled"); content.querySelectorAll("input, button, select").forEach((el) => { el.disabled = false; }); // Enable Quill editor if (quillEditors[sectionName]) { quillEditors[sectionName].enable(); } } else { section.classList.add("disabled"); content.querySelectorAll("input, button, select").forEach((el) => { el.disabled = true; }); // Disable Quill editor if (quillEditors[sectionName]) { quillEditors[sectionName].disable(); } } } // Open media library in a modal function openMediaLibrary(section, field) { currentMediaPicker = { section, field }; // Create modal backdrop const backdrop = document.createElement("div"); backdrop.id = "mediaLibraryBackdrop"; backdrop.style.cssText = ` position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.7); z-index: 9998; display: flex; align-items: center; justify-content: center; `; // Create modal container const modal = document.createElement("div"); modal.id = "mediaLibraryModal"; modal.style.cssText = ` position: relative; width: 90%; max-width: 1200px; height: 85vh; background: white; border-radius: 12px; overflow: hidden; box-shadow: 0 10px 40px rgba(0,0,0,0.3); `; // Create close button const closeBtn = document.createElement("button"); closeBtn.innerHTML = ''; closeBtn.style.cssText = ` position: absolute; top: 15px; right: 15px; z-index: 10000; background: #dc3545; color: white; border: none; width: 40px; height: 40px; border-radius: 50%; cursor: pointer; font-size: 18px; display: flex; align-items: center; justify-content: center; `; closeBtn.onclick = closeMediaLibrary; // Create iframe const iframe = document.createElement("iframe"); iframe.id = "mediaLibraryFrame"; iframe.src = "/admin/media-library.html?selectMode=true"; iframe.style.cssText = ` width: 100%; height: 100%; border: none; `; // Setup iframe message listener iframe.onload = function () { iframe.contentWindow.postMessage( { type: "initSelectMode", section: section, field: field, }, "*" ); }; modal.appendChild(closeBtn); modal.appendChild(iframe); backdrop.appendChild(modal); document.body.appendChild(backdrop); // Close on backdrop click backdrop.onclick = function (e) { if (e.target === backdrop) { closeMediaLibrary(); } }; } function closeMediaLibrary() { const backdrop = document.getElementById("mediaLibraryBackdrop"); if (backdrop) { backdrop.remove(); } currentMediaPicker = null; } function handleMediaSelection(section, field, media) { closeMediaLibrary(); const urlField = document.getElementById( `${section}${field === "background" ? "Background" : "Image"}Url` ); if (urlField) { urlField.value = media.url; } displayMediaPreview(section, field, media.url); showSuccess(`Media selected successfully!`); } function displayMediaPreview(section, field, url) { const previewId = `${section}Preview`; const preview = document.getElementById(previewId); const clearBtnId = `${section}${ field === "background" ? "Background" : "Image" }Clear`; const clearBtn = document.getElementById(clearBtnId); if (preview) { preview.classList.remove("empty"); // Check if it's a video const isVideo = url.match(/\.(mp4|webm|ogg)$/i); if (isVideo) { preview.innerHTML = ``; } else { preview.innerHTML = `