cylinderflow-mapper / script.js
dcm23's picture
Can you redesing the front for better UX?
12e06d8 verified
Raw History Blame Contribute Delete
9.72 kB
// Handle mobile menu toggle
document.addEventListener('DOMContentLoaded', function() {
const mobileMenuButton = document.querySelector('.user-menu button');
if (mobileMenuButton) {
mobileMenuButton.addEventListener('click', function() {
const navLinks = document.querySelector('.nav-links');
navLinks.classList.toggle('hidden');
});
}
// Handle user dropdown
const userAvatar = document.querySelector('.user-avatar');
if (userAvatar) {
userAvatar.addEventListener('click', function() {
const dropdown = this.nextElementSibling;
dropdown.classList.toggle('hidden');
});
}
// Close dropdowns when clicking outside
document.addEventListener('click', function(event) {
if (!event.target.closest('.user-menu')) {
const dropdowns = document.querySelectorAll('.user-menu .dropdown-menu');
dropdowns.forEach(dropdown => {
if (!dropdown.classList.contains('hidden')) {
dropdown.classList.add('hidden');
}
});
}
});
// Initialize tooltips
document.addEventListener('DOMContentLoaded', function() {
// Initialize any tooltips if needed
const tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
tooltipTriggerList.map(function (tooltipTriggerEl) {
return new bootstrap.Tooltip(tooltipTriggerEl);
});
// Handle modal toggles
const modalTriggers = document.querySelectorAll('[data-modal-toggle]');
modalTriggers.forEach(trigger => {
trigger.addEventListener('click', function() {
const modalId = this.getAttribute('data-modal-toggle');
const modal = document.getElementById(modalId);
modal.classList.toggle('hidden');
});
});
// Close modals when clicking outside
const modals = document.querySelectorAll('.modal');
modals.forEach(modal => {
modal.addEventListener('click', function(e) {
if (e.target === this) {
this.classList.add('hidden');
}
});
});
// Close modals with close button
const closeButtons = document.querySelectorAll('[data-modal-hide]');
closeButtons.forEach(button => {
button.addEventListener('click', function() {
const modalId = this.getAttribute('data-modal-hide');
const modal = document.getElementById(modalId);
modal.classList.add('hidden');
});
});
// Tab functionality
const tabButtons = document.querySelectorAll('[data-tab-toggle]');
tabButtons.forEach(button => {
button.addEventListener('click', function() {
const tabId = this.getAttribute('data-tab-toggle');
const tabContent = document.getElementById(tabId);
// Hide all tab contents
document.querySelectorAll('.tab-content').forEach(content => {
content.classList.add('hidden');
});
// Deactivate all tab buttons
document.querySelectorAll('[data-tab-toggle]').forEach(btn => {
btn.classList.remove('bg-blue-100', 'text-blue-700');
btn.classList.add('text-gray-600', 'hover:text-gray-800');
});
// Show selected tab content
tabContent.classList.remove('hidden');
// Activate current tab button
this.classList.remove('text-gray-600', 'hover:text-gray-800');
this.classList.add('bg-blue-100', 'text-blue-700');
});
});
// Accordion functionality
const accordionButtons = document.querySelectorAll('[data-accordion-toggle]');
accordionButtons.forEach(button => {
button.addEventListener('click', function() {
const accordionId = this.getAttribute('data-accordion-toggle');
const accordionContent = document.getElementById(accordionId);
const icon = this.querySelector('i');
// Toggle content
accordionContent.classList.toggle('hidden');
// Toggle icon
if (accordionContent.classList.contains('hidden')) {
icon.setAttribute('data-feather', 'plus');
} else {
icon.setAttribute('data-feather', 'minus');
}
feather.replace();
});
});
});
// Format numbers with commas
function formatNumber(num) {
return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
// Calculate percentage difference
function calculatePercentageDifference(actual, expected) {
if (expected === 0) return 0;
return ((actual - expected) / expected) * 100;
}
// Validate POD entries
function validatePODEntry(entry) {
const { format5, format11, format15, format45 } = entry;
const totalKg = (format5 * 5) + (format11 * 11) + (format15 * 15) + (format45 * 45);
return {
totalKg,
isValid: totalKg > 0
};
}
// View toggle functionality
function toggleView(viewType) {
const cardView = document.querySelector('.grid');
const tableView = document.querySelector('.hidden > div');
if (viewType === 'table') {
cardView.classList.add('hidden');
tableView.classList.remove('hidden');
} else if (viewType === 'card') {
cardView.classList.remove('hidden');
tableView.classList.add('hidden');
}
}
// Initialize view toggle buttons
document.querySelectorAll('[data-view-toggle]').forEach(button => {
button.addEventListener('click', function() {
const viewType = this.getAttribute('data-view-toggle');
toggleView(viewType);
// Update active state
document.querySelectorAll('[data-view-toggle]').forEach(btn => {
btn.classList.remove('active');
});
this.classList.add('active');
});
});
// Map functions
function openMapWithTruck(vehicleId) {
// In a real app, this would open a modal with live map tracking
console.log(`Opening map for vehicle ${vehicleId}`);
alert(`Opening live map tracking for vehicle ${vehicleId}`);
}
// Add click handlers for map buttons
document.addEventListener('DOMContentLoaded', function() {
const mapButtons = document.querySelectorAll('[data-feather="map"]');
mapButtons.forEach(button => {
button.addEventListener('click', function() {
const vehicleCell = this.closest('tr').querySelector('td:nth-child(8)');
const vehicleId = vehicleCell.textContent.trim();
if (vehicleId && vehicleId !== '-') {
openMapWithTruck(vehicleId);
}
});
});
});
// Enhanced API functions with loading states
async function fetchTransfers(filters = {}) {
// Show loading state
document.getElementById('transfers-container').innerHTML = `
<div class="flex justify-center items-center h-64">
<div class="animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-blue-500"></div>
</div>
`;
try {
// In a real app, this would call your backend API
const response = await mockApiCall(filters);
renderTransfers(response);
} catch (error) {
showError(error);
}
}
async function mockApiCall(filters) {
return new Promise(resolve => {
setTimeout(() => {
// Mock data
const mockData = [
// Your mock transfer data here
];
resolve(mockData);
}, 1000);
});
}
function renderTransfers(transfers) {
const container = document.getElementById('transfers-container');
if (transfers.length === 0) {
container.innerHTML = `
<div class="text-center py-12">
<i data-feather="package" class="mx-auto h-12 w-12 text-gray-400"></i>
<h3 class="mt-2 text-sm font-medium text-gray-900">No transfers found</h3>
<p class="mt-1 text-sm text-gray-500">Try adjusting your search or filter criteria.</p>
</div>
`;
feather.replace();
return;
}
// Render transfers
// ...
}
function showError(error) {
const container = document.getElementById('transfers-container');
container.innerHTML = `
<div class="rounded-md bg-red-50 p-4">
<div class="flex">
<div class="flex-shrink-0">
<i data-feather="alert-triangle" class="h-5 w-5 text-red-400"></i>
</div>
<div class="ml-3">
<h3 class="text-sm font-medium text-red-800">Error loading transfers</h3>
<div class="mt-2 text-sm text-red-700">
${error.message || 'Please try again later.'}
</div>
</div>
</div>
</div>
`;
feather.replace();
}
// Refresh data every 30 seconds
setInterval(() => {
fetchTransfers();
}, 30000);
async function fetchTransfers(filters = {}) {
// In a real app, this would call your backend API
console.log('Fetching transfers with filters:', filters);
return [];
}
async function createTransfer(transferData) {
// In a real app, this would call your backend API
console.log('Creating transfer:', transferData);
return { success: true, transferId: 'TRF-' + Math.floor(Math.random() * 10000) };
}
async function registerHandover(handoverData) {
// In a real app, this would call your backend API
console.log('Registering handover:', handoverData);
return { success: true };
}