/** * Moderation Transition History JavaScript * Handles AJAX loading and display of FSM transition history */ let currentPage = 1; let nextPageUrl = null; let previousPageUrl = null; /** * Format timestamp to human-readable format */ function formatTimestamp(timestamp) { const date = new Date(timestamp); return date.toLocaleString('en-US', { year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' }); } /** * Get CSRF token from cookie */ function getCookie(name) { let cookieValue = null; if (document.cookie && document.cookie !== '') { const cookies = document.cookie.split(';'); for (let i = 0; i < cookies.length; i++) { const cookie = cookies[i].trim(); if (cookie.substring(0, name.length + 1) === (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; } /** * Fetch and display transition history */ function loadHistory(url = null, filters = {}) { const tbody = document.getElementById('history-tbody'); tbody.innerHTML = '
No log entry found with this ID.
'; } }) .catch(error => { console.error('Error loading details:', error); modalBody.innerHTML = 'Error loading details.
'; }); } /** * Close modal */ function closeModal() { const modal = document.getElementById('details-modal'); modal.style.display = 'none'; } /** * Get current filters */ function getCurrentFilters() { return { model_type: document.getElementById('model-filter').value, state: document.getElementById('state-filter').value, start_date: document.getElementById('start-date').value, end_date: document.getElementById('end-date').value, user_id: document.getElementById('user-filter').value, }; } /** * Event listeners */ document.addEventListener('DOMContentLoaded', function() { // Apply filters button document.getElementById('apply-filters').addEventListener('click', () => { currentPage = 1; const filters = getCurrentFilters(); loadHistory(null, filters); }); // Clear filters button document.getElementById('clear-filters').addEventListener('click', () => { document.getElementById('model-filter').value = ''; document.getElementById('state-filter').value = ''; document.getElementById('start-date').value = ''; document.getElementById('end-date').value = ''; document.getElementById('user-filter').value = ''; currentPage = 1; loadHistory(); }); // Pagination buttons document.getElementById('prev-page').addEventListener('click', () => { if (previousPageUrl) { currentPage--; loadHistory(previousPageUrl); } }); document.getElementById('next-page').addEventListener('click', () => { if (nextPageUrl) { currentPage++; loadHistory(nextPageUrl); } }); // Close modal on background click document.getElementById('details-modal').addEventListener('click', (e) => { if (e.target.id === 'details-modal') { closeModal(); } }); // Initial load loadHistory(); }); // Additional CSS for badges (inline styles) const style = document.createElement('style'); style.textContent = ` .badge { display: inline-block; padding: 4px 8px; border-radius: 4px; font-size: 0.75rem; font-weight: 600; text-transform: uppercase; } .badge-model { background-color: #e7f3ff; color: #0066cc; } .badge-transition { background-color: #fff3cd; color: #856404; } .badge-state { background-color: #d4edda; color: #155724; } .badge-state-PENDING { background-color: #fff3cd; color: #856404; } .badge-state-APPROVED { background-color: #d4edda; color: #155724; } .badge-state-REJECTED { background-color: #f8d7da; color: #721c24; } .badge-state-IN_PROGRESS { background-color: #d1ecf1; color: #0c5460; } .badge-state-COMPLETED { background-color: #d4edda; color: #155724; } .badge-state-ESCALATED { background-color: #f8d7da; color: #721c24; } .object-link { color: #007bff; text-decoration: none; } .object-link:hover { text-decoration: underline; } .btn-view { background-color: #007bff; color: white; border: none; padding: 4px 12px; border-radius: 4px; cursor: pointer; } .btn-view:hover { background-color: #0056b3; } .detail-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 15px; } .detail-item { padding: 10px; background-color: #f8f9fa; border-radius: 4px; } .detail-item.full-width { grid-column: 1 / -1; } .detail-item strong { display: block; margin-bottom: 5px; color: #666; font-size: 0.875rem; } `; document.head.appendChild(style);