// report-table.js class ReportTable extends HTMLElement { constructor() { super(); this.attachShadow({ mode: "open" }); this._reports = []; } /** * Set the reports data and render the table */ set reports(value) { this._reports = value; this.render(); } /** * Get the reports data */ get reports() { return this._reports; } connectedCallback() { this.render(); } /** * Get badge color class based on report type */ getTypeClass(type) { switch (type) { case "nuisance": return "bg-danger"; case "quick": return "bg-primary"; case "water": return "bg-success"; default: return "bg-secondary"; } } /** * Get badge color class based on report status */ getStatusClass(status) { switch (status) { case "Reported": return "bg-warning text-dark"; case "Assigned": return "bg-info text-dark"; case "On-Hold": return "bg-secondary"; case "Complete": return "bg-success"; default: return "bg-secondary"; } } /** * Format the report ID with hyphens */ formatId(id) { if (id.length === 12) { return `${id.substring(0, 4)}-${id.substring(4, 8)}-${id.substring(8)}`; } return id; } render() { // Create the styles const style = ` `; // Create the table let tableHTML = ` `; // Generate rows for each report if (this._reports.length > 0) { this._reports.forEach((report) => { const typeClass = this.getTypeClass(report.type); const statusClass = this.getStatusClass(report.status); const formattedId = this.formatId(report.id); tableHTML += ` `; }); } else { tableHTML += ` `; } tableHTML += `
Report ID Reported Type Address Status
${formattedId} ${report.type} ${report.address || "N/A"} ${report.status}
No reports
`; // Set the shadow DOM content this.shadowRoot.innerHTML = style + tableHTML; // Add click handlers for the rows this.shadowRoot.querySelectorAll("tr.clickable-row").forEach((el) => { el.addEventListener("click", (e) => { let element = e.target; while (element.nodeName != "TR") { element = element.parentElement; } this.dispatchEvent( new CustomEvent("row-clicked", { bubbles: true, composed: true, // Allows event to cross shadow DOM boundary detail: { reportId: element.dataset.reportId, }, }), ); }); }); } } // Register the custom element customElements.define("report-table", ReportTable);