Fix HTMX swapped DOM element selection latency and race conditions in Add Asset wizard

This commit is contained in:
lucio committed 2026-05-21 02:37:14 -07:00
1 parent e721c5a3fc
commit 96338e6dce
1 file changed
+254 -227
+254 -227
View File
@@ -435,261 +435,288 @@
</style>
<script>
// Parse catalog JSON data dynamically injected by handler
const catalog = {{safeJS .CatalogJSON}};
(function() {
// Parse catalog JSON data dynamically injected by handler
const catalog = {{safeJS .CatalogJSON}};
let currentStep = 1;
let selectedCategory = null;
let selectedItem = null;
let selectedBrand = null;
let currentStep = 1;
let selectedCategory = null;
let selectedItem = null;
let selectedBrand = null;
// DOM Elements
const screenCategory = document.getElementById('screen-category');
const screenItem = document.getElementById('screen-item');
const screenBrand = document.getElementById('screen-brand');
// Helper to get element by ID at runtime
const getEl = (id) => document.getElementById(id);
const categoryGrid = document.getElementById('category-grid-container');
const itemGrid = document.getElementById('item-grid-container');
const brandGrid = document.getElementById('brand-grid-container');
// 1. Initial render of Categories screen
function initWizard() {
const categoryGrid = getEl('category-grid-container');
if (!categoryGrid) {
console.warn("category-grid-container not in DOM yet. Retrying...");
setTimeout(initWizard, 30);
return;
}
const inputCategory = document.getElementById('input-category');
const inputItem = document.getElementById('input-item');
const inputBrand = document.getElementById('input-brand');
const wizardForm = document.getElementById('wizard-form');
const backBtnWrapper = document.getElementById('wizard-back-wrapper');
const backBtn = document.getElementById('btn-wizard-back');
// Set up steps DOM markers
const step1 = document.getElementById('progress-step-1');
const step2 = document.getElementById('progress-step-2');
const step3 = document.getElementById('progress-step-3');
const line1 = document.getElementById('progress-line-1');
const line2 = document.getElementById('progress-line-2');
// 1. Initial render of Categories screen
function initWizard() {
categoryGrid.innerHTML = '';
catalog.forEach(cat => {
// Create beautiful HSL tints using the custom category color
const color = cat.color || '#d97706';
categoryGrid.innerHTML = '';
// Simple helper to convert HEX to RGBA / transparent tint
const r = parseInt(color.slice(1, 3), 16);
const g = parseInt(color.slice(3, 5), 16);
const b = parseInt(color.slice(5, 7), 16);
const card = document.createElement('div');
card.className = 'category-card';
card.style.setProperty('--cat-color', color);
card.style.setProperty('--cat-color-bg', `rgba(${r}, ${g}, ${b}, 0.04)`);
card.style.setProperty('--cat-color-badge-bg', `rgba(${r}, ${g}, ${b}, 0.1)`);
// Pick beautiful stylized icon or stylized letter badge
const initial = cat.name.charAt(0);
card.innerHTML = `
<div class="category-badge">${initial}</div>
<div class="category-name">${cat.name}</div>
`;
card.addEventListener('click', () => {
selectCategory(cat);
catalog.forEach(cat => {
// Create beautiful HSL tints using the custom category color
const color = cat.color || '#d97706';
// Simple helper to convert HEX to RGBA / transparent tint
const r = parseInt(color.slice(1, 3), 16);
const g = parseInt(color.slice(3, 5), 16);
const b = parseInt(color.slice(5, 7), 16);
const card = document.createElement('div');
card.className = 'category-card';
card.style.setProperty('--cat-color', color);
card.style.setProperty('--cat-color-bg', `rgba(${r}, ${g}, ${b}, 0.04)`);
card.style.setProperty('--cat-color-badge-bg', `rgba(${r}, ${g}, ${b}, 0.1)`);
// Pick beautiful stylized icon or stylized letter badge
const initial = cat.name.charAt(0);
card.innerHTML = `
<div class="category-badge">${initial}</div>
<div class="category-name">${cat.name}</div>
`;
card.addEventListener('click', () => {
selectCategory(cat);
});
categoryGrid.appendChild(card);
});
categoryGrid.appendChild(card);
});
// Add "Other" selection Card at first layer
const otherCard = document.createElement('div');
otherCard.className = 'category-card';
otherCard.style.setProperty('--cat-color', 'var(--color-text-muted)');
otherCard.style.setProperty('--cat-color-bg', 'var(--color-bg-base)');
otherCard.style.setProperty('--cat-color-badge-bg', 'var(--color-border)');
otherCard.style.borderStyle = 'dashed';
otherCard.style.borderWidth = '2px';
otherCard.innerHTML = `
<div class="category-badge" style="font-weight: 500;">+</div>
<div class="category-name">Other / Unlisted</div>
`;
otherCard.addEventListener('click', () => {
selectCategory({ id: 'Other', name: 'Other', items: [] });
});
categoryGrid.appendChild(otherCard);
}
// 2. Category selection logic
function selectCategory(cat) {
selectedCategory = cat.id;
inputCategory.value = cat.id;
if (cat.id === 'Other') {
// Instantly submit with Other fallback
inputItem.value = 'Other';
inputBrand.value = 'Other';
wizardForm.submit();
return;
// Add "Other" selection Card at first layer
const otherCard = document.createElement('div');
otherCard.className = 'category-card';
otherCard.style.setProperty('--cat-color', 'var(--color-text-muted)');
otherCard.style.setProperty('--cat-color-bg', 'var(--color-bg-base)');
otherCard.style.setProperty('--cat-color-badge-bg', 'var(--color-border)');
otherCard.style.borderStyle = 'dashed';
otherCard.style.borderWidth = '2px';
otherCard.innerHTML = `
<div class="category-badge" style="font-weight: 500;">+</div>
<div class="category-name">Other / Unlisted</div>
`;
otherCard.addEventListener('click', () => {
selectCategory({ id: 'Other', name: 'Other', items: [] });
});
categoryGrid.appendChild(otherCard);
}
renderItems(cat);
goToStep(2);
}
// 2. Category selection logic
function selectCategory(cat) {
selectedCategory = cat.id;
const inputCategory = getEl('input-category');
if (inputCategory) inputCategory.value = cat.id;
// 3. Render Items Screen
function renderItems(cat) {
const heading = document.getElementById('item-screen-heading');
heading.textContent = `What kind of ${cat.name.toLowerCase()} is it?`;
if (cat.id === 'Other') {
// Instantly submit with Other fallback
const inputItem = getEl('input-item');
const inputBrand = getEl('input-brand');
const wizardForm = getEl('wizard-form');
if (inputItem) inputItem.value = 'Other';
if (inputBrand) inputBrand.value = 'Other';
if (wizardForm) wizardForm.submit();
return;
}
itemGrid.innerHTML = '';
cat.items.forEach(item => {
const btn = document.createElement('button');
btn.type = 'button';
btn.className = 'pill-button';
btn.innerHTML = `
<span>${item.name}</span>
renderItems(cat);
goToStep(2);
}
// 3. Render Items Screen
function renderItems(cat) {
const heading = getEl('item-screen-heading');
if (heading) heading.textContent = `What kind of ${cat.name.toLowerCase()} is it?`;
const itemGrid = getEl('item-grid-container');
if (!itemGrid) return;
itemGrid.innerHTML = '';
cat.items.forEach(item => {
const btn = document.createElement('button');
btn.type = 'button';
btn.className = 'pill-button';
btn.innerHTML = `
<span>${item.name}</span>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="M8.25 4.5l7.5 7.5-7.5 7.5" />
</svg>
`;
btn.addEventListener('click', () => {
selectItem(cat, item);
});
itemGrid.appendChild(btn);
});
// Add "Other" option in Layer 2
const otherBtn = document.createElement('button');
otherBtn.type = 'button';
otherBtn.className = 'pill-button other-pill';
otherBtn.innerHTML = `
<span>Other Item</span>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="M8.25 4.5l7.5 7.5-7.5 7.5" />
<path stroke-linecap="round" stroke-linejoin="round" d="M12 4.5v15m7.5-7.5h-15" />
</svg>
`;
btn.addEventListener('click', () => {
selectItem(cat, item);
otherBtn.addEventListener('click', () => {
selectItem(cat, { name: 'Other', brands: [] });
});
itemGrid.appendChild(btn);
});
// Add "Other" option in Layer 2
const otherBtn = document.createElement('button');
otherBtn.type = 'button';
otherBtn.className = 'pill-button other-pill';
otherBtn.innerHTML = `
<span>Other Item</span>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="M12 4.5v15m7.5-7.5h-15" />
</svg>
`;
otherBtn.addEventListener('click', () => {
selectItem(cat, { name: 'Other', brands: [] });
});
itemGrid.appendChild(otherBtn);
}
// 4. Item selection logic
function selectItem(cat, item) {
selectedItem = item.name;
inputItem.value = item.name;
if (item.name === 'Other') {
// Instantly submit with Other fallback
inputBrand.value = 'Other';
wizardForm.submit();
return;
itemGrid.appendChild(otherBtn);
}
renderBrands(item);
goToStep(3);
}
// 4. Item selection logic
function selectItem(cat, item) {
selectedItem = item.name;
const inputItem = getEl('input-item');
if (inputItem) inputItem.value = item.name;
// 5. Render Brands Screen
function renderBrands(item) {
const heading = document.getElementById('brand-screen-heading');
heading.textContent = `What brand is the ${item.name.toLowerCase()}?`;
if (item.name === 'Other') {
// Instantly submit with Other fallback
const inputBrand = getEl('input-brand');
const wizardForm = getEl('wizard-form');
if (inputBrand) inputBrand.value = 'Other';
if (wizardForm) wizardForm.submit();
return;
}
brandGrid.innerHTML = '';
item.brands.forEach(brand => {
const btn = document.createElement('button');
btn.type = 'button';
btn.className = 'pill-button';
btn.innerHTML = `
<span>${brand}</span>
renderBrands(item);
goToStep(3);
}
// 5. Render Brands Screen
function renderBrands(item) {
const heading = getEl('brand-screen-heading');
if (heading) heading.textContent = `What brand is the ${item.name.toLowerCase()}?`;
const brandGrid = getEl('brand-grid-container');
if (!brandGrid) return;
brandGrid.innerHTML = '';
item.brands.forEach(brand => {
const btn = document.createElement('button');
btn.type = 'button';
btn.className = 'pill-button';
btn.innerHTML = `
<span>${brand}</span>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
`;
btn.addEventListener('click', () => {
selectBrand(brand);
});
brandGrid.appendChild(btn);
});
// Add "Other" option in Layer 3
const otherBtn = document.createElement('button');
otherBtn.type = 'button';
otherBtn.className = 'pill-button other-pill';
otherBtn.innerHTML = `
<span>Other Brand</span>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
<path stroke-linecap="round" stroke-linejoin="round" d="M12 4.5v15m7.5-7.5h-15" />
</svg>
`;
btn.addEventListener('click', () => {
selectBrand(brand);
otherBtn.addEventListener('click', () => {
selectBrand('Other');
});
brandGrid.appendChild(otherBtn);
}
// 6. Brand selection logic (final layer)
function selectBrand(brand) {
selectedBrand = brand;
const inputBrand = getEl('input-brand');
if (inputBrand) inputBrand.value = brand;
brandGrid.appendChild(btn);
});
// Add "Other" option in Layer 3
const otherBtn = document.createElement('button');
otherBtn.type = 'button';
otherBtn.className = 'pill-button other-pill';
otherBtn.innerHTML = `
<span>Other Brand</span>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="M12 4.5v15m7.5-7.5h-15" />
</svg>
`;
otherBtn.addEventListener('click', () => {
selectBrand('Other');
});
brandGrid.appendChild(otherBtn);
}
// 6. Brand selection logic (final layer)
function selectBrand(brand) {
selectedBrand = brand;
inputBrand.value = brand;
// Submit the form immediately to complete creation
wizardForm.submit();
}
// 7. Navigation Step Controller
function goToStep(step) {
currentStep = step;
// Hide all screens
screenCategory.classList.remove('active');
screenItem.classList.remove('active');
screenBrand.classList.remove('active');
// Update steps indicator class
step1.className = 'wizard-step';
step2.className = 'wizard-step';
step3.className = 'wizard-step';
line1.className = 'wizard-line';
line2.className = 'wizard-line';
if (step === 1) {
screenCategory.classList.add('active');
step1.classList.add('active');
backBtnWrapper.style.display = 'none';
} else if (step === 2) {
screenItem.classList.add('active');
step1.classList.add('completed');
step2.classList.add('active');
line1.classList.add('completed');
backBtnWrapper.style.display = 'block';
} else if (step === 3) {
screenBrand.classList.add('active');
step1.classList.add('completed');
step2.classList.add('completed');
step3.classList.add('active');
line1.classList.add('completed');
line2.classList.add('completed');
backBtnWrapper.style.display = 'block';
// Submit the form immediately to complete creation
const wizardForm = getEl('wizard-form');
if (wizardForm) wizardForm.submit();
}
}
// Back button hook
backBtn.addEventListener('click', () => {
if (currentStep > 1) {
goToStep(currentStep - 1);
// 7. Navigation Step Controller
function goToStep(step) {
currentStep = step;
const screenCategory = getEl('screen-category');
const screenItem = getEl('screen-item');
const screenBrand = getEl('screen-brand');
const step1 = getEl('progress-step-1');
const step2 = getEl('progress-step-2');
const step3 = getEl('progress-step-3');
const line1 = getEl('progress-line-1');
const line2 = getEl('progress-line-2');
const backBtnWrapper = getEl('wizard-back-wrapper');
if (!screenCategory || !screenItem || !screenBrand || !step1 || !step2 || !step3 || !line1 || !line2 || !backBtnWrapper) {
console.warn("Some wizard elements missing during goToStep. Retrying step set...");
setTimeout(() => goToStep(step), 20);
return;
}
// Hide all screens
screenCategory.classList.remove('active');
screenItem.classList.remove('active');
screenBrand.classList.remove('active');
// Update steps indicator class
step1.className = 'wizard-step';
step2.className = 'wizard-step';
step3.className = 'wizard-step';
line1.className = 'wizard-line';
line2.className = 'wizard-line';
if (step === 1) {
screenCategory.classList.add('active');
step1.classList.add('active');
backBtnWrapper.style.display = 'none';
} else if (step === 2) {
screenItem.classList.add('active');
step1.classList.add('completed');
step2.classList.add('active');
line1.classList.add('completed');
backBtnWrapper.style.display = 'block';
} else if (step === 3) {
screenBrand.classList.add('active');
step1.classList.add('completed');
step2.classList.add('completed');
step3.classList.add('active');
line1.classList.add('completed');
line2.classList.add('completed');
backBtnWrapper.style.display = 'block';
}
}
});
// Run initialization
initWizard();
// Bind back button hook
function initBackBtn() {
const backBtn = getEl('btn-wizard-back');
if (!backBtn) {
setTimeout(initBackBtn, 30);
return;
}
backBtn.onclick = () => {
if (currentStep > 1) {
goToStep(currentStep - 1);
}
};
}
// Run initialization
initWizard();
initBackBtn();
})();
</script>
{{end}}
{{else if .ViewMode}}