serdar779's picture
AŞAMA 1 — (UI Altyapısı Kurulumu)
2579cc6 verified
Raw History Blame Contribute Delete
6.68 kB
// Main JavaScript for PixelCraft UI Studio
// Mobile menu toggle
function toggleMobileMenu() {
const sidebar = document.querySelector('custom-sidebar');
if (sidebar) {
const isOpen = sidebar.getAttribute('data-mobile-open') === 'true';
sidebar.setAttribute('data-mobile-open', !isOpen);
}
}
// Theme toggle function
function toggleTheme() {
const html = document.documentElement;
const currentTheme = html.getAttribute('data-theme');
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
html.setAttribute('data-theme', newTheme);
localStorage.setItem('theme', newTheme);
// Update button icon
const themeButton = document.querySelector('[data-theme-toggle]');
if (themeButton) {
themeButton.innerHTML = newTheme === 'dark'
? '<i data-feather="moon"></i>'
: '<i data-feather="sun"></i>';
feather.replace();
}
}
// Initialize theme from localStorage
function initTheme() {
const savedTheme = localStorage.getItem('theme') || 'dark';
document.documentElement.setAttribute('data-theme', savedTheme);
// Update theme button if exists
const themeButton = document.querySelector('[data-theme-toggle]');
if (themeButton) {
themeButton.innerHTML = savedTheme === 'dark'
? '<i data-feather="moon"></i>'
: '<i data-feather="sun"></i>';
feather.replace();
}
}
// Smooth scroll to anchor links
function initSmoothScroll() {
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
target.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
});
});
}
// Form validation helper
function validateEmail(email) {
const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return re.test(email);
}
// Debounce function for performance
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
// Copy to clipboard utility
function copyToClipboard(text) {
navigator.clipboard.writeText(text).then(() => {
// Show success message
const toast = document.createElement('div');
toast.className = 'fixed bottom-4 right-4 bg-green-600 text-white px-4 py-2 rounded-lg shadow-lg';
toast.textContent = 'Copied to clipboard!';
document.body.appendChild(toast);
setTimeout(() => toast.remove(), 3000);
}).catch(err => {
console.error('Failed to copy: ', err);
});
}
// Responsive image loader
function loadResponsiveImages() {
const images = document.querySelectorAll('img[data-src]');
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const img = entry.target;
img.src = img.dataset.src;
img.removeAttribute('data-src');
observer.unobserve(img);
}
});
});
images.forEach(img => observer.observe(img));
}
// Initialize all functions when DOM is loaded
document.addEventListener('DOMContentLoaded', function() {
initTheme();
initSmoothScroll();
loadResponsiveImages();
// Add click event to theme toggle buttons
document.querySelectorAll('[data-theme-toggle]').forEach(button => {
button.addEventListener('click', toggleTheme);
});
// Initialize tooltips
const tooltips = document.querySelectorAll('[data-tooltip]');
tooltips.forEach(element => {
element.addEventListener('mouseenter', function() {
const tooltip = document.createElement('div');
tooltip.className = 'absolute z-50 px-2 py-1 text-sm bg-gray-800 text-white rounded shadow-lg';
tooltip.textContent = this.dataset.tooltip;
this.appendChild(tooltip);
// Position tooltip
const rect = this.getBoundingClientRect();
tooltip.style.top = `${rect.top - tooltip.offsetHeight - 10}px`;
tooltip.style.left = `${rect.left + rect.width / 2 - tooltip.offsetWidth / 2}px`;
});
element.addEventListener('mouseleave', function() {
const tooltip = this.querySelector('div');
if (tooltip) {
tooltip.remove();
}
});
});
// Initialize form submissions
document.querySelectorAll('form').forEach(form => {
form.addEventListener('submit', function(e) {
e.preventDefault();
// Add your form submission logic here
const submitBtn = this.querySelector('button[type="submit"]');
if (submitBtn) {
const originalText = submitBtn.textContent;
submitBtn.textContent = 'Processing...';
submitBtn.disabled = true;
// Simulate API call
setTimeout(() => {
submitBtn.textContent = originalText;
submitBtn.disabled = false;
// Show success message
const successMsg = document.createElement('div');
successMsg.className = 'mt-4 p-4 bg-green-900/50 border border-green-700 rounded-lg';
successMsg.innerHTML = `
<div class="flex items-center">
<i data-feather="check-circle" class="text-green-400 mr-2"></i>
<span class="text-green-300">Form submitted successfully!</span>
</div>
`;
this.appendChild(successMsg);
feather.replace();
// Remove message after 5 seconds
setTimeout(() => successMsg.remove(), 5000);
}, 1500);
}
});
});
});
// Window resize handler
window.addEventListener('resize', debounce(function() {
// Handle responsive behaviors
const isMobile = window.innerWidth < 768;
document.body.classList.toggle('mobile-view', isMobile);
}, 250));
// Export functions for use in components
window.PixelCraftUI = {
toggleTheme,
validateEmail,
copyToClipboard,
debounce
};