35 lines
1.8 KiB
JavaScript
35 lines
1.8 KiB
JavaScript
// js/utils.js
|
|
|
|
// Note: We are 'exporting' each function so other files can import them.
|
|
export const showLoading = (show) => {
|
|
const loadingSpinner = document.getElementById('loading-spinner');
|
|
loadingSpinner.innerHTML = show ? `<div class="inline-block animate-spin rounded-full h-16 w-16 border-t-2 border-b-2 border-blue-500"></div>` : '';
|
|
};
|
|
|
|
export const showMessage = (message, type = 'success') => {
|
|
const messageBox = document.getElementById('message-box');
|
|
messageBox.innerHTML = `<div class="p-4 mb-4 text-sm rounded-lg flex items-center justify-between ${type === 'error' ? 'bg-red-100 text-red-700' : 'bg-green-100 text-green-700'}" role="alert"><span>${message}</span><button onclick="this.parentElement.style.display='none'" class="font-bold text-lg">×</button></div>`;
|
|
messageBox.classList.remove('hidden');
|
|
};
|
|
|
|
export const formatDecimal = (ms) => ms ? (ms / 3600000).toFixed(2) : '0.00';
|
|
|
|
export const formatDateTime = (s) => s ? new Date(s).toLocaleString(undefined, { month: '2-digit', day: '2-digit', year: '2-digit', hour: 'numeric', minute: '2-digit' }) : 'N/A';
|
|
|
|
export const formatDate = (s) => s ? new Date(s).toLocaleDateString(undefined, { month: '2-digit', day: '2-digit', year: '2-digit' }) : 'N/A';
|
|
|
|
export const toLocalISO = (d) => {
|
|
if (!d) return '';
|
|
const date = new Date(d);
|
|
return new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString().slice(0, 16);
|
|
};
|
|
|
|
export const formatDuration = (ms) => {
|
|
if (!ms || ms < 0) return '00:00:00';
|
|
const totalSeconds = Math.floor(ms / 1000);
|
|
const h = Math.floor(totalSeconds / 3600);
|
|
const m = Math.floor((totalSeconds % 3600) / 60);
|
|
const s = totalSeconds % 60;
|
|
return `${String(h).padStart(2, '0')}:${String(m).padStart(2, '0')}:${String(s).padStart(2, '0')}`;
|
|
};
|