Cell Structure & Function Identifier

Loading cell diagram...

Structure Information

Select a cell type and click on a structure in the diagram, or choose one from the dropdown above.

Error: Invalid cell type selected.

'; return; } currentCellType = type; currentStructures = cellData[type].structures; // Load SVG diagramContainer.innerHTML = cellData[type].svgString; // Populate dropdown populateStructureDropdown(currentStructures); // Clear previous info and selection displayInfo(null); // Clear info panel selectedStructureId = null; downloadBtn.disabled = true; // Add event listener to the NEW SVG content attachSvgListener(); } // --- Attach Listener to SVG --- function attachSvgListener() { const svgElement = diagramContainer.querySelector('svg'); if (svgElement) { svgElement.addEventListener('click', handleSvgClick); } } // --- Handle Clicks on SVG --- function handleSvgClick(event) { const target = event.target; // Check if the clicked element or its parent has the 'cell-structure' class and an ID let structureElement = target.closest('.cell-structure[id]'); if (structureElement) { const structureId = structureElement.id; if (currentStructures[structureId]) { selectStructure(structureId); // Update dropdown selection to match diagram click structureSelect.value = structureId; } else { // Clicked on a styled part without data, maybe clear selection? // console.log("Clicked structure without data:", structureId); // deselectStructure(); // Optional: deselect if clicking background/unknown } } else { // Clicked outside interactive elements // deselectStructure(); // Optional: deselect if clicking background } } // --- Function to Select a Structure (by ID) --- function selectStructure(structureId) { if (!structureId || !currentStructures[structureId]) { deselectStructure(); return; } // Remove highlight from previous if (selectedStructureId) { const previousElement = diagramContainer.querySelector(`svg #${selectedStructureId}`); if (previousElement) { previousElement.classList.remove('highlight'); } } // Highlight new const currentElement = diagramContainer.querySelector(`svg #${structureId}`); if (currentElement) { currentElement.classList.add('highlight'); selectedStructureId = structureId; displayInfo(currentStructures[structureId]); downloadBtn.disabled = false; } else { // Structure ID exists in data but not found in SVG? Error. console.error("Structure ID in data but not found in SVG:", structureId); deselectStructure(); } } // --- Function to Deselect --- function deselectStructure() { if (selectedStructureId) { const previousElement = diagramContainer.querySelector(`svg #${selectedStructureId}`); if (previousElement) { previousElement.classList.remove('highlight'); } } selectedStructureId = null; displayInfo(null); structureSelect.value = ""; // Reset dropdown downloadBtn.disabled = true; } // --- Populate Structure Dropdown --- function populateStructureDropdown(structures) { structureSelect.innerHTML = ''; // Clear existing // Sort structure names alphabetically for dropdown const sortedNames = Object.entries(structures) .map(([id, data]) => ({ id: id, name: data.name })) .sort((a, b) => a.name.localeCompare(b.name)); sortedNames.forEach(item => { const option = document.createElement('option'); option.value = item.id; option.textContent = item.name; structureSelect.appendChild(option); }); } // --- Display Structure Info --- function displayInfo(structureData) { if (structureData) { infoPanel.innerHTML = `

${structureData.name}

Function: ${structureData.function || 'N/A'}

`; } else { infoPanel.innerHTML = `

Structure Information

Select a cell type and click on a structure in the diagram, or choose one from the dropdown above.

`; } } // --- Event Listeners --- cellTypeSelect.addEventListener('change', (e) => { loadCellData(e.target.value); }); structureSelect.addEventListener('change', (e) => { const selectedId = e.target.value; if (selectedId) { selectStructure(selectedId); } else { deselectStructure(); } }); // --- PDF Download --- downloadBtn.addEventListener('click', () => { if (!selectedStructureId || !currentStructures[selectedStructureId]) { console.error("PDF download clicked but no valid structure selected."); return; // Should be disabled, but check anyway } const structureInfo = currentStructures[selectedStructureId]; const { jsPDF } = window.jspdf; const doc = new jsPDF(); const margin = 15; const pageWidth = doc.internal.pageSize.getWidth(); let currentY = margin; // --- PDF Content --- // Title doc.setFontSize(16); doc.setTextColor(pdfColors.primary); doc.text("Cell Structure Information", pageWidth / 2, currentY, { align: 'center' }); currentY += 10; // Cell Type doc.setFontSize(11); doc.setTextColor(pdfColors.secondary); doc.text(`Cell Type: ${currentCellType.charAt(0).toUpperCase() + currentCellType.slice(1)}`, margin, currentY); currentY += 10; // Structure Name (with a colored underline maybe) doc.setFontSize(14); doc.setTextColor(pdfColors.primary); // Use primary color for name doc.text(structureInfo.name, margin, currentY); // Add an accent underline const nameWidth = doc.getTextWidth(structureInfo.name); doc.setDrawColor(pdfColors.highlightStroke); // Use highlight color for accent doc.setLineWidth(0.5); doc.line(margin, currentY + 1, margin + nameWidth, currentY + 1); currentY += 10; // Function doc.setFontSize(11); doc.setTextColor(pdfColors.text); // Reset to default text color doc.setFont('helvetica', 'bold'); doc.text("Function:", margin, currentY); currentY += 5; doc.setFont('helvetica', 'normal'); const functionLines = doc.splitTextToSize(structureInfo.function || 'N/A', pageWidth - 2 * margin); doc.text(functionLines, margin, currentY); currentY += (functionLines.length * 5) + 5; // Adjust spacing // --- Save PDF --- const safeName = structureInfo.name.replace(/[^a-z0-9]/gi, '_').toLowerCase(); doc.save(`cell_structure_${safeName}.pdf`); }); // --- Initialize --- loadCellData(currentCellType); // Load default cell type on start });
Scroll to Top