-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3906eaf
commit e011064
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,65 @@ | ||
// dm-template-handler.js: js for html templates | ||
|
||
// used in auto-interaction | ||
const showRelevantFields = () => { | ||
const toolType = document.querySelector('[data-tool-type]').textContent.toLowerCase(); | ||
document.querySelectorAll('[data-if]').forEach(element => { | ||
element.style.display = element.dataset.if === toolType ? 'block' : 'none'; | ||
}); | ||
}; | ||
|
||
// used in demo-load-element-via-api.html | ||
document.addEventListener('DOMContentLoaded', showRelevantFields); | ||
|
||
async function fetchThumbnails() { | ||
const container = document.querySelector(".thumbnails-container"); | ||
const containerId = container.dataset.id; | ||
const apiUrl = containerId | ||
? `https://jsonplaceholder.typicode.com/photos?albumId=${containerId}` | ||
: "https://jsonplaceholder.typicode.com/photos?_limit=10"; | ||
|
||
container.innerHTML = "<p>Loading thumbnails...</p>"; | ||
|
||
try { | ||
const response = await fetch(apiUrl); | ||
if (!response.ok) { | ||
throw new Error("Failed to fetch thumbnails"); | ||
} | ||
|
||
const images = await response.json(); | ||
container.innerHTML = ""; // Clear loading message | ||
|
||
images.forEach(image => { | ||
const img = document.createElement("img"); | ||
img.src = image.thumbnailUrl; // API response structure | ||
img.alt = image.title || "Thumbnail"; | ||
img.classList.add("thumbnail"); | ||
img.dataset.fullImageUrl = image.url; // Store full image URL | ||
|
||
img.addEventListener("click", () => openModal(image.url)); | ||
container.appendChild(img); | ||
}); | ||
} catch (error) { | ||
console.error(error); | ||
container.innerHTML = "<p>Failed to load thumbnails.</p>"; | ||
} | ||
} | ||
|
||
function openModal(imageUrl) { | ||
const modal = document.querySelector(".modal"); | ||
const modalImage = modal.querySelector("img"); | ||
const downloadBtn = modal.querySelector(".download-btn"); | ||
|
||
modalImage.src = imageUrl; | ||
downloadBtn.href = imageUrl; | ||
downloadBtn.download = "image.jpg"; | ||
|
||
modal.classList.add("active"); | ||
} | ||
|
||
function closeModal() { | ||
const modal = document.querySelector(".modal"); | ||
modal.classList.remove("active"); | ||
} | ||
|
||
document.addEventListener("DOMContentLoaded", fetchThumbnails); |