artec13's picture
tlačítka nefungujú, nič nerobia
ffde63f verified
Raw History Blame Contribute Delete
15.4 kB
// Main application JavaScript for FotoSphere Collective
class PhotoGallery {
constructor() {
this.photos = [];
this.currentPage = 1;
this.isLoggedIn = false;
this.currentUser = null;
this.sortBy = 'latest';
this.init();
}
init() {
this.loadSamplePhotos();
this.setupEventListeners();
this.checkAuthStatus();
this.setupGlobalEventListeners();
}
setupEventListeners() {
// Auth buttons
const registerBtn = document.getElementById('registerBtn');
const loginBtn = document.getElementById('loginBtn');
const closeModal = document.getElementById('closeModal');
const switchAuth = document.getElementById('switchAuth');
const authForm = document.getElementById('authForm');
if (registerBtn) {
registerBtn.addEventListener('click', () => this.showAuthModal('register'));
}
if (loginBtn) {
loginBtn.addEventListener('click', () => this.showAuthModal('login'));
}
if (closeModal) {
closeModal.addEventListener('click', () => this.hideAuthModal());
}
if (switchAuth) {
switchAuth.addEventListener('click', () => this.switchAuthMode());
}
if (authForm) {
authForm.addEventListener('submit', (e) => this.handleAuthSubmit(e));
}
// Sort buttons
const sortLatest = document.getElementById('sortLatest');
const sortPopular = document.getElementById('sortPopular');
if (sortLatest) {
sortLatest.addEventListener('click', () => this.sortPhotos('latest'));
}
if (sortPopular) {
sortPopular.addEventListener('click', () => this.sortPhotos('popular'));
}
// Load more button
const loadMore = document.getElementById('loadMore');
if (loadMore) {
loadMore.addEventListener('click', () => this.loadMorePhotos());
}
// Upload modal
const closeUploadModal = document.getElementById('closeUploadModal');
if (closeUploadModal) {
closeUploadModal.addEventListener('click', () => this.hideUploadModal());
}
// File upload
const dropZone = document.getElementById('dropZone');
const fileInput = document.getElementById('fileInput');
if (dropZone && fileInput) {
dropZone.addEventListener('click', () => fileInput.click());
dropZone.addEventListener('dragover', (e) => this.handleDragOver(e));
dropZone.addEventListener('drop', (e) => this.handleFileDrop(e));
fileInput.addEventListener('change', (e) => this.handleFileSelect(e));
}
// Close modals on outside click
const authModal = document.getElementById('authModal');
const uploadModal = document.getElementById('uploadModal');
if (authModal) {
authModal.addEventListener('click', (e) => {
if (e.target.id === 'authModal') this.hideAuthModal();
}
});
if (uploadModal) {
uploadModal.addEventListener('click', (e) => {
if (e.target.id === 'uploadModal') this.hideUploadModal();
}
});
}
setupGlobalEventListeners() {
// Listen for events from navbar
document.addEventListener('showAuthModal', (e) => {
this.showAuthModal(e.detail.mode);
});
document.addEventListener('showUploadModal', () => {
this.showUploadModal();
});
}
loadSamplePhotos() {
// Using Picsum API for sample photos
const categories = ['nature', 'cityscape', 'travel', 'aerial', 'minimal'];
const samplePhotos = [];
for (let i = 1; i <= 12; i++) {
const category = categories[Math.floor(Math.random() * categories.length)];
samplePhotos.push({
id: i,
title: `Fotografia ${i}`,
description: 'Krásny záber zachytený našou komunitou',
url: `http://static.photos/${category}/640x360/${i}`,
author: `Fotograf ${i}`,
likes: Math.floor(Math.random() * 100),
date: new Date(Date.now() - Math.random() * 7 * 24 * 60 * 60 * 1000),
views: Math.floor(Math.random() * 500)
});
}
this.photos = samplePhotos.sort((a, b) => new Date(b.date) - new Date(a.date));
this.renderGallery();
}
renderGallery() {
const galleryGrid = document.getElementById('galleryGrid');
galleryGrid.innerHTML = '';
this.photos.forEach(photo => {
const photoCard = this.createPhotoCard(photo);
galleryGrid.appendChild(photoCard);
});
}
createPhotoCard(photo) {
const card = document.createElement('div');
card.className = 'photo-card bg-gray-800 rounded-xl overflow-hidden fade-in';
card.innerHTML = `
<div class="relative group cursor-pointer">
<img src="${photo.url}" alt="${photo.title}" class="w-full h-64 object-cover">
<div class="absolute inset-0 bg-black bg-opacity-0 group-hover:bg-opacity-40 transition-all duration-300">
<div class="absolute bottom-0 left-0 right-0 p-4 photo-overlay">
<h3 class="font-semibold text-white truncate">${photo.title}</h3>
<p class="text-gray-300 text-sm truncate">${photo.description}</p>
<div class="flex items-center justify-between mt-2">
<div class="flex items-center gap-2 text-gray-300">
<i data-feather="eye" class="w-4 h-4"></i>
<span class="text-sm">${photo.views}</span>
</div>
<div class="flex items-center gap-2">
<button class="text-gray-300 hover:text-red-500 transition-colors">
<i data-feather="heart" class="w-4 h-4"></i>
</button>
<span class="text-sm text-gray-300">${photo.likes}</span>
</div>
</div>
</div>
</div>
<div class="p-4">
<div class="flex items-center justify-between">
<span class="text-cyan-400 text-sm">${photo.author}</span>
<span class="text-gray-400 text-sm">${this.formatDate(photo.date)}</span>
</div>
</div>
`;
// Add event listeners for interactive elements
const likeBtn = card.querySelector('button');
likeBtn.addEventListener('click', (e) => {
e.stopPropagation();
this.handleLike(photo.id);
});
card.addEventListener('click', () => this.showPhotoDetail(photo));
return card;
}
formatDate(date) {
return new Date(date).toLocaleDateString('sk-SK');
}
showAuthModal(mode) {
const modal = document.getElementById('authModal');
const title = document.getElementById('modalTitle');
const nameField = document.getElementById('nameField');
const switchText = document.getElementById('switchAuthText');
const switchBtn = document.getElementById('switchAuth');
if (modal && title && nameField && switchText && switchBtn) {
if (mode === 'register') {
title.textContent = 'Registrácia';
nameField.classList.remove('hidden');
switchText.textContent = 'Už máte účet?';
switchBtn.textContent = 'Prihlásiť sa';
} else {
title.textContent = 'Prihlásenie';
nameField.classList.add('hidden');
switchText.textContent = 'Nemáte účet?';
switchBtn.textContent = 'Zaregistrovať sa';
}
modal.classList.remove('hidden');
setTimeout(() => feather.replace(), 100);
}
}
hideAuthModal() {
document.getElementById('authModal').classList.add('hidden');
}
switchAuthMode() {
const currentTitle = document.getElementById('modalTitle').textContent;
if (currentTitle === 'Prihlásenie') {
this.showAuthModal('register');
} else {
this.showAuthModal('login');
}
}
handleAuthSubmit(e) {
e.preventDefault();
// Simulate auth process
this.isLoggedIn = true;
this.currentUser = {
name: 'Fotograf',
email: 'user@example.com'
};
this.hideAuthModal();
this.updateUIForAuth();
// Show success message
this.showNotification('Úspešne prihlásený!', 'success');
}
showUploadModal() {
const uploadModal = document.getElementById('uploadModal');
if (uploadModal) {
uploadModal.classList.remove('hidden');
setTimeout(() => feather.replace(), 100);
}
}
hideUploadModal() {
const uploadModal = document.getElementById('uploadModal');
if (uploadModal) {
uploadModal.classList.add('hidden');
}
}
handleDragOver(e) {
e.preventDefault();
document.getElementById('dropZone').classList.add('dragover');
}
handleFileDrop(e) {
e.preventDefault();
document.getElementById('dropZone').classList.remove('dragover');
this.handleFiles(e.dataTransfer.files);
}
handleFileSelect(e) {
this.handleFiles(e.target.files);
}
handleFiles(files) {
if (files.length > 0) {
const file = files[0];
if (file.type.startsWith('image/')) {
// Simulate file upload
this.showNotification('Fotografia bola úspešne nahraná!', 'success');
this.hideUploadModal();
}
}
handleLike(photoId) {
const photo = this.photos.find(p => p.id === photoId);
if (photo) {
photo.likes += 1;
this.renderGallery();
setTimeout(() => feather.replace(), 100);
}
}
sortPhotos(method) {
this.sortBy = method;
if (method === 'latest') {
this.photos.sort((a, b) => new Date(b.date) - new Date(a.date));
} else {
this.photos.sort((a, b) => b.likes - a.likes);
}
this.renderGallery();
this.updateSortButtons();
}
updateSortButtons() {
const latestBtn = document.getElementById('sortLatest');
const popularBtn = document.getElementById('sortPopular');
if (this.sortBy === 'latest') {
latestBtn.classList.add('bg-cyan-600');
latestBtn.classList.remove('bg-gray-700');
popularBtn.classList.remove('bg-cyan-600');
popularBtn.classList.add('bg-gray-700');
} else {
popularBtn.classList.add('bg-cyan-600');
popularBtn.classList.remove('bg-gray-700');
latestBtn.classList.add('bg-gray-700');
latestBtn.classList.remove('bg-cyan-600');
}
}
loadMorePhotos() {
// Simulate loading more photos
const btn = document.getElementById('loadMore');
btn.textContent = 'Načítava sa...';
btn.disabled = true;
setTimeout(() => {
const newPhotos = [];
const categories = ['nature', 'cityscape', 'travel', 'aerial', 'minimal'];
for (let i = this.photos.length + 1; i <= this.photos.length + 4; i++) {
const category = categories[Math.floor(Math.random() * categories.length)];
newPhotos.push({
id: i,
title: `Fotografia ${i}`,
description: 'Ďalší úžasný záber od našej komunity',
url: `http://static.photos/${category}/640x360/${i}`,
author: `Fotograf ${i}`,
likes: Math.floor(Math.random() * 100),
date: new Date(Date.now() - Math.random() * 14 * 24 * 60 * 60 * 1000),
views: Math.floor(Math.random() * 500)
});
}
this.photos = [...this.photos, ...newPhotos];
if (this.sortBy === 'latest') {
this.photos.sort((a, b) => new Date(b.date) - new Date(a.date));
this.renderGallery();
btn.textContent = 'Načítať viac fotografií';
btn.disabled = false;
setTimeout(() => feather.replace(), 100);
this.showNotification('Nové fotografie načítané!', 'info');
}, 1000);
}
showPhotoDetail(photo) {
// In a real app, this would show a detailed view or lightbox
this.showNotification(`Zobrazuje sa: ${photo.title}`, 'info');
}
checkAuthStatus() {
// Check if user is logged in (in real app, check localStorage/tokens)
this.updateUIForAuth();
}
updateUIForAuth() {
const navbar = document.querySelector('custom-navbar');
if (navbar && navbar.shadowRoot) {
const uploadBtn = navbar.shadowRoot.querySelector('#uploadBtn');
const authSection = navbar.shadowRoot.querySelector('#authSection');
if (this.isLoggedIn) {
if (uploadBtn) {
uploadBtn.classList.remove('hidden');
}
if (authSection) {
authSection.classList.add('hidden');
}
}
}
}
showNotification(message, type = 'info') {
// Create notification element
const notification = document.createElement('div');
const bgColor = type === 'success' ? 'bg-green-600' :
type === 'error' ? 'bg-red-600' : 'bg-cyan-600';
notification.className = `fixed top-4 right-4 ${bgColor} text-white px-6 py-3 rounded-lg shadow-lg z-50 fade-in';
notification.textContent = message;
document.body.appendChild(notification);
// Remove after 3 seconds
setTimeout(() => {
notification.classList.add('opacity-0', 'transition-opacity', 'duration-300');
setTimeout(() => {
if (notification.parentNode) {
notification.parentNode.removeChild(notification);
}
}, 3000);
}, 100);
}
}
// Initialize the application when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
new PhotoGallery();
});
// Theme management
function toggleTheme() {
const html = document.documentElement;
if (html.classList.contains('dark')) {
html.classList.remove('dark');
html.classList.add('light');
localStorage.setItem('theme', 'light');
} else {
html.classList.remove('light');
html.classList.add('dark');
localStorage.setItem('theme', 'dark');
}
}
// Check for saved theme preference
if (localStorage.getItem('theme') === 'light') {
document.documentElement.classList.remove('dark');
document.documentElement.classList.add('light');
}