-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
157 lines (135 loc) · 4.88 KB
/
Copy pathscript.js
File metadata and controls
157 lines (135 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Load images from localStorage or use default placeholders
let images = JSON.parse(localStorage.getItem("images")) || [
{ src: "https://via.placeholder.com/400x250", title: "Sample 1", category: "Game" },
{ src: "https://via.placeholder.com/400x250", title: "Sample 2", category: "Nature" },
{ src: "https://via.placeholder.com/400x250", title: "Sample 3", category: "Sci-Fi" }
];
// Display gallery
function displayGallery() {
const grid = document.getElementById("galleryGrid");
if (!grid) return;
grid.innerHTML = "";
images.forEach(imgData => {
const card = document.createElement("div");
card.className = "image-card";
card.innerHTML = `
<img src="${imgData.src}" alt="${imgData.title}" onclick="openModal('${imgData.src}','${imgData.title}')">
<div class="overlay">
<h3>${imgData.title}</h3>
<p>Category: ${imgData.category}</p>
</div>
`;
grid.appendChild(card);
});
}
// Generate category buttons
function generateCategoryButtons() {
const container = document.getElementById("categoryButtons");
if (!container) return;
const categories = ["All", ...new Set(images.map(img => img.category))];
container.innerHTML = "";
categories.forEach(cat => {
const btn = document.createElement("button");
btn.textContent = cat;
btn.onclick = () => filterByCategory(cat, btn);
container.appendChild(btn);
});
}
// Filter gallery by category
function filterByCategory(category, btn) {
const grid = document.getElementById("galleryGrid");
const cards = grid.getElementsByClassName("image-card");
// Highlight active button
const allButtons = document.querySelectorAll(".category-buttons button");
allButtons.forEach(b => b.classList.remove("active"));
btn.classList.add("active");
Array.from(cards).forEach(card => {
const cardCategory = card.querySelector("p").innerText.replace("Category: ", "");
card.style.display = category === "All" || cardCategory === category ? "" : "none";
});
}
// Search/filter gallery
function filterGallery() {
const query = document.getElementById("search")?.value.toLowerCase();
if (!query) return;
const grid = document.getElementById("galleryGrid");
if (!grid) return;
const cards = grid.getElementsByClassName("image-card");
Array.from(cards).forEach(card => {
const title = card.querySelector("h3").innerText.toLowerCase();
const category = card.querySelector("p").innerText.toLowerCase();
card.style.display = title.includes(query) || category.includes(query) ? "" : "none";
});
}
// Modal
function openModal(src, title) {
const modal = document.getElementById("imageModal");
if (!modal) return;
document.getElementById("imageModal").style.display = "block";
document.getElementById("modalImage").src = src;
document.getElementById("modalTitle").innerText = title;
const downloadBtn = document.getElementById("downloadBtn");
downloadBtn.onclick = () => {
const a = document.createElement("a");
a.href = src;
a.download = title + ".jpg";
a.click();
};
}
function closeModal() {
const modal = document.getElementById("imageModal");
if (!modal) return;
modal.style.display = "none";
}
// Upload image
function uploadImage() {
const input = document.getElementById("imageInput");
const title = document.getElementById("imageTitle")?.value.trim();
const category = document.getElementById("imageCategory")?.value.trim();
const status = document.getElementById("uploadStatus");
if (!input?.files[0]) {
if (status) status.textContent = "Please select a JPEG file.";
return;
}
if (!title || !category) {
if (status) status.textContent = "Please enter both a title and category.";
return;
}
const file = input.files[0];
if (file.type !== "image/jpeg") {
if (status) status.textContent = "Only JPEG images are allowed.";
return;
}
const reader = new FileReader();
reader.onload = function(e) {
const newImage = {
src: e.target.result,
title: title,
category: category
};
images.push(newImage);
// Save to localStorage
localStorage.setItem("images", JSON.stringify(images));
// Update gallery immediately if exists
if (document.getElementById("galleryGrid")) {
displayGallery();
generateCategoryButtons();
}
if (status) status.textContent = "Upload successful!";
if (input) input.value = "";
const titleInput = document.getElementById("imageTitle");
if (titleInput) titleInput.value = "";
const categoryInput = document.getElementById("imageCategory");
if (categoryInput) categoryInput.value = "";
}
reader.readAsDataURL(file);
}
// Initialize gallery on page load (only if gallery exists)
window.onload = () => {
const storedImages = JSON.parse(localStorage.getItem("images"));
if (storedImages) images = storedImages;
if (document.getElementById("galleryGrid")) {
displayGallery();
generateCategoryButtons();
}
};