|
| 1 | +const images = [ |
| 2 | + "#2B2B2B", // Dark Gray |
| 3 | + "#36454F", // Charcoal |
| 4 | + "#001F3F", // Navy Blue |
| 5 | + "#004D40", // Deep Teal |
| 6 | + "#808000", // Olive Green |
| 7 | + "#F0F0F0", // Light Gray |
| 8 | + "#F5F5F5", // Off-White |
| 9 | + "#E0F7FA", // Pale Blue |
| 10 | + "#F5DEB3", // Warm Beige |
| 11 | + "#FFFACD", // Pale Yellow |
| 12 | + "https://images.unsplash.com/photo-1464822759023-fed622ff2c3b?q=80&w=1470&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D", |
| 13 | + "https://images.unsplash.com/photo-1727197093259-e89dc8ccd8ee?q=80&w=1469&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D", |
| 14 | + "https://images.unsplash.com/photo-1470723710355-95304d8aece4?q=80&w=1470&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D", |
| 15 | + "https://images.unsplash.com/photo-1492144534655-ae79c964c9d7?q=80&w=1583&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D", |
| 16 | + "https://images.unsplash.com/photo-1449553728176-2ba1d6062517?q=80&w=1470&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D", |
| 17 | + "https://images.pexels.com/photos/919073/pexels-photo-919073.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1", |
| 18 | + "https://images.pexels.com/photos/409701/pexels-photo-409701.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1", |
| 19 | + "https://images.pexels.com/photos/33109/fall-autumn-red-season.jpg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1", |
| 20 | + "https://images.pexels.com/photos/206359/pexels-photo-206359.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1", |
| 21 | + "https://images.pexels.com/photos/1097456/pexels-photo-1097456.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1", |
| 22 | +]; |
| 23 | + |
| 24 | +const apiUrls = { |
| 25 | + all: "https://quotes-api-chi.vercel.app/quotes", |
| 26 | + success: "https://quotes-api-chi.vercel.app/quotes/random?type=success", |
| 27 | + inspirational: |
| 28 | + "https://quotes-api-chi.vercel.app/quotes/random?type=inspirational", |
| 29 | + selfConfidence: |
| 30 | + "https://quotes-api-chi.vercel.app/quotes/random?type=selfconfidence", |
| 31 | +}; |
| 32 | + |
| 33 | +document.addEventListener("DOMContentLoaded", () => { |
| 34 | + const categorySelect = document.querySelector("#category-select"); |
| 35 | + const theme = document.querySelector(".theme"); |
| 36 | + const themeToggle = document.querySelector("#theme-toggle"); |
| 37 | + const searchForm = document.querySelector(".search-container"); |
| 38 | + |
| 39 | + // Theme Setup |
| 40 | + initializeTheme(); |
| 41 | + addThemeSelectionListeners(); |
| 42 | + let category = localStorage.getItem("quote") || "all"; |
| 43 | + categorySelect.value = category; |
| 44 | + fetchRandomQuote(); |
| 45 | + categorySelect.addEventListener("change", () => { |
| 46 | + localStorage.setItem("quote", categorySelect.value); |
| 47 | + fetchRandomQuote(); |
| 48 | + }); |
| 49 | + |
| 50 | + // Search Form Submission Handler |
| 51 | + searchForm.addEventListener("submit", searchQuotesOnGoogle); |
| 52 | + |
| 53 | + // Theme Toggle Button |
| 54 | + themeToggle.addEventListener("click", () => { |
| 55 | + theme.classList.toggle("active"); |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
| 59 | +// Functions |
| 60 | +const initializeTheme = () => { |
| 61 | + let background = localStorage.getItem("theme") || "#2b2b2b"; |
| 62 | + if (background.startsWith("#")) { |
| 63 | + document.querySelector("body").style.background = background; |
| 64 | + } else { |
| 65 | + document.querySelector( |
| 66 | + "body" |
| 67 | + ).style.background = `center / cover no-repeat url(${background})`; |
| 68 | + } |
| 69 | +}; |
| 70 | + |
| 71 | +const addThemeSelectionListeners = () => { |
| 72 | + const theme = document.querySelector(".theme"); |
| 73 | + images.forEach((img) => { |
| 74 | + let div = document.createElement("div"); |
| 75 | + div.classList.add("theme-options"); |
| 76 | + if (img.startsWith("#")) { |
| 77 | + div.style.background = img; |
| 78 | + } else { |
| 79 | + div.style.backgroundImage = `url(${img})`; |
| 80 | + } |
| 81 | + div.addEventListener("click", () => { |
| 82 | + localStorage.setItem("theme", img); |
| 83 | + initializeTheme(); |
| 84 | + }); |
| 85 | + theme.appendChild(div); |
| 86 | + }); |
| 87 | +}; |
| 88 | + |
| 89 | +const fetchRandomQuote = async () => { |
| 90 | + let category = localStorage.getItem("quote") || "all"; |
| 91 | + try { |
| 92 | + const response = await fetch(apiUrls[category]); |
| 93 | + const data = await response.json(); |
| 94 | + const { quote, author } = data.response; |
| 95 | + displayQuote(quote, author); |
| 96 | + } catch (error) { |
| 97 | + document.querySelector(".header .tab").textContent = |
| 98 | + "Failed to load quote. Please try again."; |
| 99 | + } |
| 100 | +}; |
| 101 | + |
| 102 | +const displayQuote = (quote, author) => { |
| 103 | + const headerTab = document.querySelector(".header .tab"); |
| 104 | + const authorTab = document.querySelector(".header .author"); |
| 105 | + let displayedText = ""; |
| 106 | + let authorText = "- "; |
| 107 | + |
| 108 | + // Reset content |
| 109 | + headerTab.textContent = ""; |
| 110 | + authorTab.textContent = ""; |
| 111 | + |
| 112 | + // Animate Quote Text |
| 113 | + quote.split("").forEach((char, index) => { |
| 114 | + setTimeout(() => { |
| 115 | + displayedText += char; |
| 116 | + headerTab.textContent = displayedText; |
| 117 | + }, 50 * index); |
| 118 | + }); |
| 119 | + |
| 120 | + // Animate Author Text |
| 121 | + setTimeout(() => { |
| 122 | + author.split("").forEach((char, index) => { |
| 123 | + setTimeout(() => { |
| 124 | + authorText += char; |
| 125 | + authorTab.textContent = authorText; |
| 126 | + }, 50 * index); |
| 127 | + }); |
| 128 | + }, 50 * (quote.length + 1)); |
| 129 | +}; |
| 130 | + |
| 131 | +const searchQuotesOnGoogle = (event) => { |
| 132 | + event.preventDefault(); |
| 133 | + const searchBar = document.querySelector(".search-bar"); |
| 134 | + const query = searchBar.value.trim(); |
| 135 | + if (query) { |
| 136 | + const googleSearchUrl = `https://www.google.com/search?q=${encodeURIComponent( |
| 137 | + query |
| 138 | + )}`; |
| 139 | + window.location.href = googleSearchUrl; |
| 140 | + } |
| 141 | +}; |
0 commit comments