Spaces:
Running
Running
File size: 4,284 Bytes
802d8d8 | 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 | // Shared functionality across all pages
document.addEventListener('DOMContentLoaded', () => {
// Initialize dark mode from localStorage or preference
if (localStorage.getItem('darkMode') === 'true' ||
(!localStorage.getItem('darkMode') && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
document.documentElement.classList.add('dark');
}
// Add event listeners for dark mode toggle
const darkModeToggle = document.getElementById('darkModeToggle');
if (darkModeToggle) {
darkModeToggle.checked = document.documentElement.classList.contains('dark');
darkModeToggle.addEventListener('change', (e) => {
if (e.target.checked) {
document.documentElement.classList.add('dark');
localStorage.setItem('darkMode', 'true');
} else {
document.documentElement.classList.remove('dark');
localStorage.setItem('darkMode', 'false');
}
});
}
// Mobile menu toggle
const mobileMenuButton = document.getElementById('mobileMenuButton');
const mobileMenu = document.getElementById('mobileMenu');
if (mobileMenuButton && mobileMenu) {
mobileMenuButton.addEventListener('click', () => {
mobileMenu.classList.toggle('hidden');
});
}
// Initialize all tooltips
const tooltips = document.querySelectorAll('[data-tooltip]');
tooltips.forEach(tooltip => {
new Tooltip(tooltip);
});
});
// Tooltip class for UI elements
class Tooltip {
constructor(element) {
this.element = element;
this.tooltipText = element.getAttribute('data-tooltip');
this.tooltip = null;
this.init();
}
init() {
this.element.addEventListener('mouseenter', this.show.bind(this));
this.element.addEventListener('mouseleave', this.hide.bind(this));
}
show() {
this.tooltip = document.createElement('div');
this.tooltip.className = 'absolute z-10 bg-gray-900 text-white text-xs rounded py-1 px-2 whitespace-nowrap';
this.tooltip.textContent = this.tooltipText;
const rect = this.element.getBoundingClientRect();
this.tooltip.style.top = `${rect.bottom + window.scrollY + 5}px`;
this.tooltip.style.left = `${rect.left + window.scrollX + rect.width / 2}px`;
this.tooltip.style.transform = 'translateX(-50%)';
document.body.appendChild(this.tooltip);
}
hide() {
if (this.tooltip) {
document.body.removeChild(this.tooltip);
this.tooltip = null;
}
}
}
// Shared file handling functions
class FileHandler {
static createFileList(files) {
const fileList = document.createElement('div');
fileList.className = 'file-list';
files.forEach(file => {
const fileItem = document.createElement('div');
fileItem.className = 'file-item';
const fileInfo = document.createElement('div');
fileInfo.innerHTML = `
<span class="font-medium">${file.name}</span>
<span class="text-xs text-gray-500">${this.formatFileSize(file.size)}</span>
`;
const removeBtn = document.createElement('button');
removeBtn.className = 'text-red-500 hover:text-red-700';
removeBtn.innerHTML = '<i data-feather="x"></i>';
removeBtn.addEventListener('click', () => {
fileItem.remove();
if (fileList.children.length === 0) {
fileList.remove();
}
});
fileItem.appendChild(fileInfo);
fileItem.appendChild(removeBtn);
fileList.appendChild(fileItem);
});
feather.replace();
return fileList;
}
static formatFileSize(bytes) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
}
// Export for use in tool pages
window.FileHandler = FileHandler; |