No intervals added yet.
";
}
appState.intervals.sort(function(a, b) { return a.top - b.top; }); // Keep sorted by depth
appState.intervals.forEach(function(interval) {
var item = document.createElement("div");
item.className = "scl-interval-item";
item.innerHTML = `
${interval.top.toFixed(1)} - ${interval.bottom.toFixed(1)} cm: ${interval.lithology} (${interval.color})
`;
intervalListDiv.appendChild(item);
});
}
function renderDashboardTab() {
// Update metadata
appState.projectName = projectNameInput.value;
appState.coreId = coreIdInput.value;
appState.dateLogged = dateLoggedInput.value;
appState.loggedBy = loggedByInput.value;
projectTitleDisplay.textContent = appState.projectName;
projectSubtitle.textContent = "Core ID: " + appState.coreId;
displayDate.textContent = appState.dateLogged;
displayLogger.textContent = appState.loggedBy;
appState.intervals.sort(function(a, b) { return a.top - b.top; });
// Calculate total depth
var totalDepth = appState.intervals.reduce(function(max, interval) {
return Math.max(max, interval.bottom);
}, 0);
displayTotalDepth.textContent = totalDepth.toFixed(1) + " cm";
displayTotalIntervals.textContent = appState.intervals.length;
// Render Table
tableBody.innerHTML = "";
if (appState.intervals.length === 0) {
placeholderText.style.display = 'block';
return;
}
placeholderText.style.display = 'none';
appState.intervals.forEach(function(interval) {
var thickness = interval.bottom - interval.top;
var tr = document.createElement("tr");
tr.innerHTML = `
${interval.top.toFixed(1)} |
${interval.bottom.toFixed(1)} |
${interval.lithology} |
${interval.color} |
${interval.notes} |
${thickness.toFixed(1)} |
`;
tableBody.appendChild(tr);
});
}
// --- Event Handlers ---
// Update Dashboard Button
updateBtn.addEventListener("click", function() {
renderDashboardTab();
showTab(1); // Switch to Dashboard
});
// Add Interval
addIntervalBtn.addEventListener("click", function() {
var top = parseFloat(topDepthInput.value);
var bottom = parseFloat(bottomDepthInput.value);
var lithology = lithologyInput.value.trim();
var color = colorInput.value.trim();
var notes = notesInput.value.trim();
if (isNaN(top) || isNaN(bottom) || !lithology || !color) {
alert("Please enter valid Top/Bottom Depths, Lithology, and Color.");
return;
}
if (top >= bottom) {
alert("Top Depth must be less than Bottom Depth.");
return;
}
var newInterval = {
id: Date.now(),
top: top,
bottom: bottom,
lithology: lithology,
color: color,
notes: notes
};
appState.intervals.push(newInterval);
renderConfigTab();
// Clear form fields
topDepthInput.value = bottom.toFixed(1); // Auto-set next top depth
bottomDepthInput.value = '';
lithologyInput.value = "";
colorInput.value = "";
notesInput.value = "";
});
// Remove Interval (Event Delegation)
intervalListDiv.addEventListener("click", function(e) {
if (e.target.classList.contains("scl-remove-btn")) {
var id = parseInt(e.target.dataset.id);
appState.intervals = appState.intervals.filter(function(i) { return i.id !== id; });
renderConfigTab();
}
});
// PDF Download
pdfBtn.addEventListener("click", function() {
var jsPDF = window.jspdf.jsPDF;
var titleSlug = projectNameInput.value.replace(/[^a-zA-Z0-9\s]/g, '').replace(/\s/g, '_').substring(0, 30) || 'Core_Log';
var fileName = `${titleSlug}_${coreIdInput.value}_Log.pdf`;
// Temporarily adjust minimum table width for PDF quality
var table = document.getElementById('scl-log-table');
var originalMinWidth = table.style.minWidth;
table.style.minWidth = '1100px';
html2canvas(exportArea, {
scale: 2,
useCORS: true,
backgroundColor: '#ffffff'
}).then(function(canvas) {
// Restore style
table.style.minWidth = originalMinWidth;
var imgData = canvas.toDataURL('image/png');
var doc = new jsPDF({
orientation: 'l', // Landscape is better for core logs
unit: 'pt',
format: 'a4'
});
var pdfWidth = doc.internal.pageSize.getWidth();
var pdfHeight = doc.internal.pageSize.getHeight();
var imgProps = doc.getImageProperties(imgData);
var imgWidth = imgProps.width;
var imgHeight = imgProps.height;
var margin = 30;
var usableWidth = pdfWidth - (2 * margin);
var ratio = usableWidth / imgWidth;
var scaledHeight = imgHeight * ratio;
if (scaledHeight > pdfHeight - (2 * margin)) {
var pageHeight = pdfHeight - (2 * margin);
var heightLeft = scaledHeight;
var position = 0;
while (heightLeft > 0) {
doc.addImage(imgData, 'PNG', margin, position + margin, usableWidth, scaledHeight);
heightLeft -= pageHeight;
position -= pageHeight;
if (heightLeft > 0) {
doc.addPage();
}
}
} else {
// Single page
doc.addImage(imgData, 'PNG', margin, margin, usableWidth, scaledHeight);
}
doc.save(fileName);
}).catch(function(err) {
table.style.minWidth = originalMinWidth; // Restore on error
console.error("SCL PDF Error:", err);
// alert("An error occurred while generating the PDF."); // Per spec
});
});
// --- Initial Load ---
renderConfigTab();
renderDashboardTab();
showTab(0);
});