refactor: move chart builder button event handling to Alpine, rename chart builder initialization, and improve new snippet creation.

This commit is contained in:
2025-12-09 23:18:57 +02:00
parent f13793031f
commit 00ccdd27d5
3 changed files with 21 additions and 23 deletions

View File

@@ -295,6 +295,7 @@
<button class="btn btn-modal primary" id="edit-dataset-btn" <button class="btn btn-modal primary" id="edit-dataset-btn"
title="Edit this dataset contents">Edit Contents</button> title="Edit this dataset contents">Edit Contents</button>
<button class="btn btn-modal primary" id="build-chart-btn" <button class="btn btn-modal primary" id="build-chart-btn"
@click="openChartBuilder($store.datasets.currentDatasetId)"
title="Build a chart using this dataset">Build Chart</button> title="Build a chart using this dataset">Build Chart</button>
<button class="btn btn-modal primary" id="new-snippet-btn" <button class="btn btn-modal primary" id="new-snippet-btn"
title="Create a new snippet using this dataset">New Snippet</button> title="Create a new snippet using this dataset">New Snippet</button>

View File

@@ -247,15 +247,7 @@ document.addEventListener('DOMContentLoaded', function () {
refreshMetadataBtn.addEventListener('click', refreshDatasetMetadata); refreshMetadataBtn.addEventListener('click', refreshDatasetMetadata);
} }
// Build chart from dataset button // Build chart from dataset button - now handled by Alpine @click in HTML
const buildChartBtn = document.getElementById('build-chart-btn');
if (buildChartBtn) {
buildChartBtn.addEventListener('click', async () => {
if (Alpine.store('datasets').currentDatasetId) {
openChartBuilder(Alpine.store('datasets').currentDatasetId);
}
});
}
// New snippet from dataset button // New snippet from dataset button
const newSnippetBtn = document.getElementById('new-snippet-btn'); const newSnippetBtn = document.getElementById('new-snippet-btn');

View File

@@ -62,12 +62,12 @@ function chartBuilder() {
return Object.values(this.encodings).some(enc => enc.field !== ''); return Object.values(this.encodings).some(enc => enc.field !== '');
}, },
// Initialize component with dataset // Load dataset and initialize chart builder
async init(datasetId) { async loadDataset(datasetId) {
try { try {
// Validate datasetId is provided // Validate datasetId is provided
if (!datasetId || isNaN(datasetId)) { if (!datasetId || isNaN(datasetId)) {
console.warn('Chart builder init called without valid datasetId'); console.warn('Chart builder loadDataset called without valid datasetId');
return false; return false;
} }
@@ -214,7 +214,9 @@ function chartBuilder() {
if (!this.isValid) return; if (!this.isValid) return;
try { try {
// Create snippet with auto-generated name // Capture current state before closing (important: do this BEFORE close())
const specToSave = JSON.parse(JSON.stringify(this.spec));
const datasetNameToSave = this.datasetName;
const snippetName = generateSnippetName(); const snippetName = generateSnippetName();
const now = new Date().toISOString(); const now = new Date().toISOString();
@@ -223,11 +225,11 @@ function chartBuilder() {
name: snippetName, name: snippetName,
created: now, created: now,
modified: now, modified: now,
spec: this.spec, spec: specToSave,
draftSpec: null, draftSpec: specToSave, // Initialize draft with same spec
comment: `Chart built from dataset: ${this.datasetName}`, comment: `Chart built from dataset: ${datasetNameToSave}`,
tags: [], tags: [],
datasetRefs: [this.datasetName], datasetRefs: [datasetNameToSave],
meta: {} meta: {}
}; };
@@ -239,7 +241,8 @@ function chartBuilder() {
const datasetModal = document.getElementById('dataset-modal'); const datasetModal = document.getElementById('dataset-modal');
if (datasetModal) datasetModal.style.display = 'none'; if (datasetModal) datasetModal.style.display = 'none';
// Select and open the new snippet // Refresh snippet list and select the new snippet
renderSnippetList();
selectSnippet(snippet.id); selectSnippet(snippet.id);
// Show success message // Show success message
@@ -320,11 +323,11 @@ async function openChartBuilder(datasetId) {
const modal = document.getElementById('chart-builder-modal'); const modal = document.getElementById('chart-builder-modal');
modal.style.display = 'flex'; modal.style.display = 'flex';
// Get Alpine component instance and initialize it // Get Alpine component instance and load dataset
const chartBuilderView = document.getElementById('chart-builder-view'); const chartBuilderView = document.getElementById('chart-builder-view');
if (chartBuilderView && chartBuilderView._x_dataStack) { if (chartBuilderView && chartBuilderView._x_dataStack) {
const component = chartBuilderView._x_dataStack[0]; const component = chartBuilderView._x_dataStack[0];
const success = await component.init(datasetId); const success = await component.loadDataset(datasetId);
if (!success) { if (!success) {
modal.style.display = 'none'; modal.style.display = 'none';
@@ -345,8 +348,6 @@ async function openChartBuilder(datasetId) {
} }
} }
// Populate field dropdowns with dataset columns (utility function) // Populate field dropdowns with dataset columns (utility function)
function populateFieldDropdowns(dataset) { function populateFieldDropdowns(dataset) {
const encodings = ['x', 'y', 'color', 'size']; const encodings = ['x', 'y', 'color', 'size'];
@@ -377,3 +378,7 @@ function closeChartBuilder() {
component.close(); component.close();
} }
} }
// Expose functions to global scope for Alpine access
window.openChartBuilder = openChartBuilder;
window.closeChartBuilder = closeChartBuilder;