diff --git a/static/js/matrix-builder.js b/static/js/matrix-builder.js index 4e90f97..5cb9879 100644 --- a/static/js/matrix-builder.js +++ b/static/js/matrix-builder.js @@ -4,6 +4,45 @@ function generateMatrixInput(rows, cols, containerId, label, gridId) { const section = document.createElement('div'); section.className = 'matrix-section'; section.innerHTML = `

${label}

`; + + const header = document.createElement('div'); + const importBtn = document.createElement('button'); + const fileInput = document.createElement('input'); + importBtn.textContent = 'Importar CSV'; + importBtn.type = 'button'; + fileInput.type = 'file'; + fileInput.accept = '.csv,text/csv'; + fileInput.style.display = 'none'; + fileInput.dataset.matrixId = gridId; + + importBtn.addEventListener('click', function() { + fileInput.click(); + }); + + fileInput.addEventListener('change', function(event) { + const file = event.target.files[0]; + if (!file) return; + + const reader = new FileReader(); + + reader.onload = function(e) { + const csvText = e.target.result; + importMatrixFromCSV(csvText, gridId, rows, cols); + }; + + reader.onerror = function() { + showError('Erro ao ler o ficheiro CSV.'); + }; + + reader.readAsText(file); + + event.target.value = ''; + }); + + header.appendChild(importBtn); + header.appendChild(fileInput); + + section.appendChild(header); const grid = document.createElement('div'); grid.className = 'matrix-input-grid'; diff --git a/static/js/utils.js b/static/js/utils.js index b075911..4281f7e 100644 --- a/static/js/utils.js +++ b/static/js/utils.js @@ -84,4 +84,76 @@ function prettyHTML(matrix, title) { html += ''; return html; +} + +function importMatrixFromCSV(csvText, matrixId, expectedRows, expectedCols) { + try { + // Divide o CSV em linhas + const lines = csvText.trim().split('\n').filter(line => line.trim() !== ''); + + if (lines.length === 0) { + throw new Error('Arquivo CSV vazio.'); + } + + // Parse da matriz + const matrix = lines.map(line => + line.split(',').map(num => { + const val = num.trim(); + return val === '' ? 0 : parseFloat(val); + }) + ); + + // Verifica se todas as linhas têm o mesmo número de colunas + const cols = matrix[0].length; + if (!matrix.every(row => row.length === cols)) { + throw new Error('Todas as linhas devem ter o mesmo número de elementos.'); + } + + // Verifica se as dimensões correspondem às esperadas + if (expectedRows && expectedCols) { + if (matrix.length !== expectedRows || cols !== expectedCols) { + throw new Error(`Dimensões incorretas. Esperado: ${expectedRows}x${expectedCols}, Obtido: ${matrix.length}x${cols}`); + } + } + + // Preenche os inputs da matriz + for (let i = 0; i < matrix.length; i++) { + for (let j = 0; j < matrix[i].length; j++) { + const input = document.querySelector(`#${matrixId} [data-row="${i}"][data-col="${j}"]`); + if (input) { + input.value = matrix[i][j]; + input.dispatchEvent(new Event('input')); + } + } + } + + showSuccess(`Matriz ${matrixId === 'matrix-a' ? 'A' : 'B'} importada com sucesso!`); + + } catch (error) { + console.error('Erro ao importar CSV:', error); + showError('Erro ao importar CSV: ' + error.message); + } +} + +function showSuccess(message) { + hideError(); + const successDiv = document.createElement('div'); + successDiv.className = 'success-message'; + successDiv.textContent = message; + successDiv.style.margin = '0.5rem 0'; + successDiv.style.padding = '0.5rem'; + successDiv.style.backgroundColor = '#d4edda'; + successDiv.style.color = '#155724'; + successDiv.style.border = '1px solid #c3e6cb'; + successDiv.style.borderRadius = '0.25rem'; + successDiv.style.fontSize = '0.9rem'; + + setTimeout(() => { + if (successDiv.parentNode) { + successDiv.parentNode.removeChild(successDiv); + } + }, 3000); + + const form = document.getElementById('matrix-form'); + form.parentNode.insertBefore(successDiv, form.nextSibling); } \ No newline at end of file