File size: 8,350 Bytes
01297e0 | 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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
// Enhanced UI Interactions
document.addEventListener('DOMContentLoaded', function() {
// Initialize tooltips
initTooltips();
// Initialize form validations
initForms();
// Initialize modals
initModals();
// Initialize datepickers
initDatepickers();
});
function initTooltips() {
const tooltipElements = document.querySelectorAll('[data-tooltip]');
tooltipElements.forEach(el => {
const tooltipText = el.getAttribute('data-tooltip');
const tooltip = document.createElement('div');
tooltip.className = 'tooltip hidden absolute z-50 px-3 py-2 text-sm text-white bg-gray-800 rounded-md shadow-lg';
tooltip.textContent = tooltipText;
document.body.appendChild(tooltip);
el.addEventListener('mouseenter', () => {
const rect = el.getBoundingClientRect();
tooltip.style.left = `${rect.left + rect.width/2 - tooltip.offsetWidth/2}px`;
tooltip.style.top = `${rect.top - tooltip.offsetHeight - 5}px`;
tooltip.classList.remove('hidden');
});
el.addEventListener('mouseleave', () => {
tooltip.classList.add('hidden');
});
});
}
function initForms() {
const forms = document.querySelectorAll('form');
forms.forEach(form => {
form.addEventListener('submit', async (e) => {
e.preventDefault();
const submitBtn = form.querySelector('[type="submit"]');
submitBtn.disabled = true;
submitBtn.innerHTML = 'Processing...';
try {
// Simulate form submission
await new Promise(resolve => setTimeout(resolve, 1500));
showAlert('Form submitted successfully!', 'success');
} catch (error) {
showAlert('Error submitting form. Please try again.', 'danger');
} finally {
submitBtn.disabled = false;
submitBtn.innerHTML = 'Submit';
}
});
});
}
function initModals() {
const modalTriggers = document.querySelectorAll('[data-modal]');
modalTriggers.forEach(trigger => {
const modalId = trigger.getAttribute('data-modal');
const modal = document.getElementById(modalId);
if (!modal) return;
trigger.addEventListener('click', () => {
modal.classList.remove('hidden');
});
const closeButtons = modal.querySelectorAll('[data-dismiss]');
closeButtons.forEach(btn => {
btn.addEventListener('click', () => {
modal.classList.add('hidden');
});
});
});
}
function initDatepickers() {
const dateInputs = document.querySelectorAll('[type="date"]');
dateInputs.forEach(input => {
if (!input.value) {
const today = new Date().toISOString().split('T')[0];
input.value = today;
}
});
}
function showAlert(message, type = 'info') {
const alert = document.createElement('div');
alert.className = `alert alert-${type} fixed top-4 right-4 max-w-md z-50`;
alert.innerHTML = `
<div class="flex items-start">
<div class="flex-shrink-0">
${type === 'success' ? '<i data-feather="check-circle"></i>' : ''}
${type === 'danger' ? '<i data-feather="alert-circle"></i>' : ''}
${type === 'warning' ? '<i data-feather="alert-triangle"></i>' : ''}
</div>
<div class="ml-3">
<p class="text-sm">${message}</p>
</div>
<button onclick="this.parentElement.remove()" class="ml-auto -mx-1.5 -my-1.5 p-1.5 rounded-md hover:bg-opacity-30 focus:outline-none">
<i data-feather="x"></i>
</button>
</div>
`;
document.body.appendChild(alert);
feather.replace();
setTimeout(() => {
alert.remove();
}, 5000);
}
// Toast notification
function showToast(message, type = 'info') {
const toast = document.createElement('div');
toast.className = `toast toast-${type} fixed bottom-4 right-4 max-w-xs z-50`;
toast.innerHTML = `
<div class="flex items-center p-4 rounded-md shadow-lg bg-white border-l-4 ${type === 'success' ? 'border-green-500' : type === 'danger' ? 'border-red-500' : 'border-blue-500'}">
<div class="mr-3">
${type === 'success' ? '<i data-feather="check-circle" class="text-green-500"></i>' : ''}
${type === 'danger' ? '<i data-feather="alert-circle" class="text-red-500"></i>' : ''}
${type === 'info' ? '<i data-feather="info" class="text-blue-500"></i>' : ''}
</div>
<div class="text-sm">${message}</div>
</div>
`;
document.body.appendChild(toast);
feather.replace();
setTimeout(() => {
toast.remove();
}, 3000);
}
document.addEventListener('DOMContentLoaded', function() {
// Initialize any tooltips or popovers
const tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
tooltipTriggerList.map(function (tooltipTriggerEl) {
return new bootstrap.Tooltip(tooltipTriggerEl);
});
// Sample data for charts (would be replaced with real data)
const revenueData = {
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
datasets: [{
label: 'Revenue',
data: [6500, 5900, 8000, 8100, 5600, 5500, 4000, 6200, 7800, 8200, 9100, 9500],
backgroundColor: 'rgba(79, 70, 229, 0.1)',
borderColor: 'rgba(79, 70, 229, 1)',
borderWidth: 2,
tension: 0.3,
fill: true
}]
};
// Chart initialization would go here
// const ctx = document.getElementById('revenueChart').getContext('2d');
// new Chart(ctx, {
// type: 'line',
// data: revenueData,
// options: {
// responsive: true,
// plugins: {
// legend: {
// display: false
// }
// },
// scales: {
// y: {
// beginAtZero: true
// }
// }
// }
// });
});
// Toggle mobile menu
function toggleMobileMenu() {
const mobileMenu = document.getElementById('mobile-menu');
mobileMenu.classList.toggle('hidden');
}
// Search functionality
function handleSearch() {
const searchInput = document.getElementById('search-input');
const searchValue = searchInput.value.toLowerCase();
// Implement search logic
console.log('Searching for:', searchValue);
}
// Dark mode toggle
function toggleDarkMode() {
const html = document.documentElement;
html.classList.toggle('dark');
localStorage.setItem('darkMode', html.classList.contains('dark'));
}
// Check for saved dark mode preference
if (localStorage.getItem('darkMode') === 'true') {
document.documentElement.classList.add('dark');
}
// Notification dropdown toggle
function toggleNotifications() {
const notificationDropdown = document.getElementById('notification-dropdown');
notificationDropdown.classList.toggle('hidden');
}
// User dropdown toggle
function toggleUserDropdown() {
const userDropdown = document.getElementById('user-dropdown');
userDropdown.classList.toggle('hidden');
}
// Close dropdowns when clicking outside
document.addEventListener('click', function(event) {
const notificationDropdown = document.getElementById('notification-dropdown');
const notificationButton = document.getElementById('notification-button');
const userDropdown = document.getElementById('user-dropdown');
const userButton = document.getElementById('user-button');
if (!notificationButton.contains(event.target) && !notificationDropdown.contains(event.target)) {
notificationDropdown.classList.add('hidden');
}
if (!userButton.contains(event.target) && !userDropdown.contains(event.target)) {
userDropdown.classList.add('hidden');
}
});
// Initialize datepicker
function initDatepicker() {
// Datepicker initialization would go here
console.log('Initializing datepicker');
} |