// Initialize PWA function initPWA() { // Check if the browser supports service workers if ('serviceWorker' in navigator) { navigator.serviceWorker.register('/sw.js') .then(registration => { console.log('Service Worker registered with scope:', registration.scope); // Request permission for notifications if ('Notification' in window) { Notification.requestPermission().then(permission => { if (permission === 'granted') { console.log('Notification permission granted.'); } }); } }) .catch(error => { console.log('Service Worker registration failed:', error); }); } } // Ticket storage using localStorage const TICKETS_KEY = 'bugtracker_tickets'; // Initialize with sample tickets if none exist function initTickets() { if (!localStorage.getItem(TICKETS_KEY)) { const sampleTickets = [ { title: "Login Page Bug", status: "open", priority: "high", assigned: "John D.", date: "2023-11-15", description: "Users unable to login with Google auth" }, { title: "Mobile Responsiveness", status: "in-progress", priority: "medium", assigned: "Sarah K.", date: "2023-11-10", description: "Dashboard breaks on mobile devices" }, { title: "API Timeout", status: "resolved", priority: "low", assigned: "Mike T.", date: "2023-11-05", description: "Product API times out after 5 seconds" } ]; localStorage.setItem(TICKETS_KEY, JSON.stringify(sampleTickets)); } } // Get all tickets function getTickets() { initTickets(); return JSON.parse(localStorage.getItem(TICKETS_KEY)); } // Save tickets to localStorage function saveTickets(tickets) { localStorage.setItem(TICKETS_KEY, JSON.stringify(tickets)); } // Function to create a new ticket function createNewTicket(ticketData) { const tickets = getTickets(); tickets.push(ticketData); saveTickets(tickets); // Show notification if ('Notification' in window && Notification.permission === 'granted') { new Notification('New Ticket Created', { body: `Ticket "${ticketData.title}" has been created.`, icon: '/icons/icon-192x192.png' }); } // Refresh ticket display if on tickets page if (window.location.pathname.includes('tickets.html') || window.location.pathname.includes('index.html')) { loadTickets(); } } // Function to render tickets function loadTickets(filters = {}) { const container = document.querySelector('.tickets-container') || document.querySelector('.grid.grid-cols-1'); if (!container) return; container.innerHTML = ''; const tickets = getTickets(); let filteredTickets = [...tickets]; // Apply filters if (filters.status) { filteredTickets = filteredTickets.filter(t => t.status === filters.status); } if (filters.priority) { filteredTickets = filteredTickets.filter(t => t.priority === filters.priority); } if (filters.assigned) { filteredTickets = filteredTickets.filter(t => t.assigned === filters.assigned); } if (filters.date) { const now = new Date(); filteredTickets = filteredTickets.filter(t => { const ticketDate = new Date(t.date); switch(filters.date) { case 'today': return ticketDate.toDateString() === now.toDateString(); case 'week': const weekStart = new Date(now); weekStart.setDate(now.getDate() - now.getDay()); return ticketDate >= weekStart; case 'month': return ticketDate.getMonth() === now.getMonth() && ticketDate.getFullYear() === now.getFullYear(); default: return true; } }); } filteredTickets.forEach(ticket => { const card = document.createElement('custom-ticket-card'); card.setAttribute('title', ticket.title); card.setAttribute('status', ticket.status); card.setAttribute('priority', ticket.priority); card.setAttribute('assigned', ticket.assigned); card.setAttribute('date', ticket.date); card.innerHTML = ticket.description; container.appendChild(card); }); feather.replace(); } // Initialize the ticket filters function initTicketFilters() { const filterElement = document.querySelector('ticket-filters'); if (!filterElement) return; filterElement.shadowRoot.querySelector('.apply').addEventListener('click', () => { const filters = { status: filterElement.shadowRoot.querySelector('#status').value, priority: filterElement.shadowRoot.querySelector('#priority').value, assigned: filterElement.shadowRoot.querySelector('#assigned').value, date: filterElement.shadowRoot.querySelector('#date').value }; loadTickets(filters); }); filterElement.shadowRoot.querySelector('.reset').addEventListener('click', () => { filterElement.shadowRoot.querySelectorAll('select').forEach(select => { select.value = ''; }); loadTickets(); }); } // Initialize charts for reports page function initCharts() { if (!window.location.pathname.includes('reports.html')) return; const tickets = getTickets(); // Status Chart const statusCounts = { open: 0, 'in-progress': 0, resolved: 0 }; tickets.forEach(ticket => { statusCounts[ticket.status]++; }); new Chart(document.getElementById('statusChart').getContext('2d'), { type: 'doughnut', data: { labels: ['Open', 'In Progress', 'Resolved'], datasets: [{ data: [statusCounts.open, statusCounts['in-progress'], statusCounts.resolved], backgroundColor: ['#f59e0b', '#3b82f6', '#10b981'] }] } }); // Priority Chart const priorityCounts = { high: 0, medium: 0, low: 0 }; tickets.forEach(ticket => { priorityCounts[ticket.priority]++; }); new Chart(document.getElementById('priorityChart').getContext('2d'), { type: 'pie', data: { labels: ['High', 'Medium', 'Low'], datasets: [{ data: [priorityCounts.high, priorityCounts.medium, priorityCounts.low], backgroundColor: ['#ef4444', '#f97316', '#22c55e'] }] } }); // Timeline Chart - Group by month const monthlyCounts = {}; tickets.forEach(ticket => { const date = new Date(ticket.date); const monthYear = `${date.getFullYear()}-${date.getMonth()}`; monthlyCounts[monthYear] = (monthlyCounts[monthYear] || 0) + 1; }); const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; const currentYear = new Date().getFullYear(); new Chart(document.getElementById('timelineChart').getContext('2d'), { type: 'line', data: { labels: months, datasets: [{ label: 'Tickets Created', data: months.map((_, i) => { const key = `${currentYear}-${i}`; return monthlyCounts[key] || 0; }), borderColor: '#3b82f6', backgroundColor: 'rgba(59, 130, 246, 0.1)', fill: true, tension: 0.3 }] }, options: { responsive: true, scales: { y: { beginAtZero: true } } } }); } // Function to export reports data as PDF function exportReports() { const { jsPDF } = window.jspdf; const doc = new jsPDF(); const tickets = getTickets(); const date = new Date().toLocaleDateString(); // Add title doc.setFontSize(20); doc.text('RapidAction - Reports Export', 15, 20); // Add metadata doc.setFontSize(12); doc.text(`Generated on: ${date}`, 15, 30); doc.text(`Total Tickets: ${tickets.length}`, 15, 37); // Status report const statusCounts = { open: 0, 'in-progress': 0, resolved: 0 }; tickets.forEach(ticket => { statusCounts[ticket.status]++; }); doc.setFontSize(16); doc.text('Tickets by Status', 15, 55); doc.setFontSize(12); doc.text(`Open: ${statusCounts.open}`, 15, 65); doc.text(`In Progress: ${statusCounts['in-progress']}`, 15, 75); doc.text(`Resolved: ${statusCounts.resolved}`, 15, 85); // Priority report const priorityCounts = { high: 0, medium: 0, low: 0 }; tickets.forEach(ticket => { priorityCounts[ticket.priority]++; }); doc.setFontSize(16); doc.text('Tickets by Priority', 15, 105); doc.setFontSize(12); doc.text(`High: ${priorityCounts.high}`, 15, 115); doc.text(`Medium: ${priorityCounts.medium}`, 15, 125); doc.text(`Low: ${priorityCounts.low}`, 15, 135); // Timeline data doc.setFontSize(16); doc.text('Monthly Ticket Activity', 15, 155); doc.setFontSize(12); const monthlyCounts = {}; tickets.forEach(ticket => { const date = new Date(ticket.date); const monthYear = `${date.getFullYear()}-${date.getMonth()}`; monthlyCounts[monthYear] = (monthlyCounts[monthYear] || 0) + 1; }); const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; const currentYear = new Date().getFullYear(); let yPos = 165; months.forEach((month, i) => { const key = `${currentYear}-${i}`; const count = monthlyCounts[key] || 0; doc.text(`${month}: ${count}`, 15, yPos); yPos += 10; }); // Save PDF doc.save(`bugtracker-reports-${date.replace(/\//g, '-')}.pdf`); } // Function to export project data as PDF function exportProject() { const { jsPDF } = window.jspdf; const doc = new jsPDF(); const tickets = getTickets(); const date = new Date().toLocaleDateString(); // Add title doc.setFontSize(20); doc.text('RapidAction - Tickets Export', 15, 20); // Add metadata doc.setFontSize(12); doc.text(`Generated on: ${date}`, 15, 30); doc.text(`Total Tickets: ${tickets.length}`, 15, 37); // Prepare table data const tableData = tickets.map(ticket => [ ticket.title, ticket.status, ticket.priority, ticket.assigned, ticket.date, ticket.description.substring(0, 50) + '...' ]); // Add table doc.autoTable({ head: [['Title', 'Status', 'Priority', 'Assigned', 'Date', 'Description']], body: tableData, startY: 45, theme: 'grid', headStyles: { fillColor: [79, 70, 229], // Purple color textColor: 255 }, columnStyles: { 0: { cellWidth: 40 }, 1: { cellWidth: 20 }, 2: { cellWidth: 20 }, 3: { cellWidth: 25 }, 4: { cellWidth: 20 }, 5: { cellWidth: 'auto' } } }); // Save PDF doc.save(`bugtracker-export-${date.replace(/\//g, '-')}.pdf`); } // Ticket creation modal function showNewTicketModal() { const modal = document.createElement('div'); modal.className = 'fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50'; modal.innerHTML = `