feat: add category/subcategory selectors to link add/edit modal for moving links

This commit is contained in:
Einar 2026-05-11 15:46:04 +02:00
parent 68a9e44363
commit b052ccf82c
3 changed files with 149 additions and 27 deletions

View file

@ -183,8 +183,8 @@ async def create_link(link: LinkIn):
async def update_link(link_id: int, link: LinkIn): async def update_link(link_id: int, link: LinkIn):
async with aiosqlite.connect(DB_PATH) as db: async with aiosqlite.connect(DB_PATH) as db:
await db.execute( await db.execute(
"UPDATE links SET label=?, url=?, favicon=?, position=? WHERE id=?", "UPDATE links SET subcategory_id=?, label=?, url=?, favicon=?, position=? WHERE id=?",
(link.label, link.url, link.favicon, link.position, link_id) (link.subcategory_id, link.label, link.url, link.favicon, link.position, link_id)
) )
await db.commit() await db.commit()
return {"id": link_id, **link.dict()} return {"id": link_id, **link.dict()}

View file

@ -15,7 +15,6 @@ const globeSVG = `<svg class="globe-icon" viewBox="0 0 20 20" fill="none" xmlns=
<line x1="10" y1="1" x2="10" y2="19" stroke="#e8722a" stroke-width="1"/> <line x1="10" y1="1" x2="10" y2="19" stroke="#e8722a" stroke-width="1"/>
</svg>`; </svg>`;
// Sortable instances — kept so we can destroy/recreate on re-render
let categorySortable = null; let categorySortable = null;
let subcategorySortable = null; let subcategorySortable = null;
let linkSortable = null; let linkSortable = null;
@ -30,7 +29,6 @@ async function api(method, path, body) {
if (!res.ok) throw new Error(await res.text()); if (!res.ok) throw new Error(await res.text());
return res.json(); return res.json();
} }
const get = (path) => api('GET', path); const get = (path) => api('GET', path);
const post = (path, body) => api('POST', path, body); const post = (path, body) => api('POST', path, body);
const put = (path, body) => api('PUT', path, body); const put = (path, body) => api('PUT', path, body);
@ -55,7 +53,6 @@ async function loadCategories() {
function renderCategoryNav() { function renderCategoryNav() {
const nav = document.getElementById('category-nav'); const nav = document.getElementById('category-nav');
nav.innerHTML = ''; nav.innerHTML = '';
state.categories.forEach(cat => { state.categories.forEach(cat => {
const btn = document.createElement('button'); const btn = document.createElement('button');
btn.className = 'cat-tab' + (cat.id === state.activeCategoryId ? ' active' : ''); btn.className = 'cat-tab' + (cat.id === state.activeCategoryId ? ' active' : '');
@ -65,8 +62,6 @@ function renderCategoryNav() {
btn.oncontextmenu = (e) => { e.preventDefault(); openEditCategoryModal(cat); }; btn.oncontextmenu = (e) => { e.preventDefault(); openEditCategoryModal(cat); };
nav.appendChild(btn); nav.appendChild(btn);
}); });
// Destroy old instance before creating new one
if (categorySortable) categorySortable.destroy(); if (categorySortable) categorySortable.destroy();
categorySortable = Sortable.create(nav, { categorySortable = Sortable.create(nav, {
animation: 150, animation: 150,
@ -101,7 +96,6 @@ async function selectCategory(id) {
function renderSubcategoryBar() { function renderSubcategoryBar() {
const bar = document.getElementById('subcategory-bar'); const bar = document.getElementById('subcategory-bar');
bar.innerHTML = ''; bar.innerHTML = '';
state.subcategories.forEach(sub => { state.subcategories.forEach(sub => {
const btn = document.createElement('button'); const btn = document.createElement('button');
btn.className = 'sub-tab' + (sub.id === state.activeSubcategoryId ? ' active' : ''); btn.className = 'sub-tab' + (sub.id === state.activeSubcategoryId ? ' active' : '');
@ -111,14 +105,12 @@ function renderSubcategoryBar() {
btn.oncontextmenu = (e) => { e.preventDefault(); openEditSubcategoryModal(sub); }; btn.oncontextmenu = (e) => { e.preventDefault(); openEditSubcategoryModal(sub); };
bar.appendChild(btn); bar.appendChild(btn);
}); });
const addBtn = document.createElement('button'); const addBtn = document.createElement('button');
addBtn.className = 'btn-add-sub'; addBtn.className = 'btn-add-sub';
addBtn.id = 'add-sub-btn'; addBtn.id = 'add-sub-btn';
addBtn.innerHTML = '<i class="ti ti-plus"></i> Add subcategory'; addBtn.innerHTML = '<i class="ti ti-plus"></i> Add subcategory';
addBtn.onclick = () => openAddSubcategoryModal(); addBtn.onclick = () => openAddSubcategoryModal();
bar.appendChild(addBtn); bar.appendChild(addBtn);
if (subcategorySortable) subcategorySortable.destroy(); if (subcategorySortable) subcategorySortable.destroy();
subcategorySortable = Sortable.create(bar, { subcategorySortable = Sortable.create(bar, {
animation: 150, animation: 150,
@ -147,7 +139,6 @@ async function selectSubcategory(id) {
function renderLinkGrid() { function renderLinkGrid() {
const grid = document.getElementById('link-grid'); const grid = document.getElementById('link-grid');
grid.innerHTML = ''; grid.innerHTML = '';
state.links.forEach(link => { state.links.forEach(link => {
const a = document.createElement('a'); const a = document.createElement('a');
a.className = 'link-card'; a.className = 'link-card';
@ -155,7 +146,6 @@ function renderLinkGrid() {
a.target = '_blank'; a.target = '_blank';
a.rel = 'noopener noreferrer'; a.rel = 'noopener noreferrer';
a.dataset.id = link.id; a.dataset.id = link.id;
const favicon = document.createElement('div'); const favicon = document.createElement('div');
favicon.className = 'link-favicon'; favicon.className = 'link-favicon';
if (link.favicon) { if (link.favicon) {
@ -168,11 +158,9 @@ function renderLinkGrid() {
} else { } else {
favicon.innerHTML = globeSVG; favicon.innerHTML = globeSVG;
} }
const label = document.createElement('span'); const label = document.createElement('span');
label.className = 'link-label'; label.className = 'link-label';
label.textContent = link.label; label.textContent = link.label;
const actions = document.createElement('div'); const actions = document.createElement('div');
actions.className = 'link-actions'; actions.className = 'link-actions';
actions.innerHTML = ` actions.innerHTML = `
@ -183,20 +171,17 @@ function renderLinkGrid() {
<i class="ti ti-trash"></i> <i class="ti ti-trash"></i>
</button> </button>
`; `;
a.appendChild(favicon); a.appendChild(favicon);
a.appendChild(label); a.appendChild(label);
a.appendChild(actions); a.appendChild(actions);
grid.appendChild(a); grid.appendChild(a);
}); });
const addCard = document.createElement('button'); const addCard = document.createElement('button');
addCard.className = 'add-link-card'; addCard.className = 'add-link-card';
addCard.id = 'add-link-card'; addCard.id = 'add-link-card';
addCard.innerHTML = '<i class="ti ti-plus"></i> Add link'; addCard.innerHTML = '<i class="ti ti-plus"></i> Add link';
addCard.onclick = () => openAddLinkModal(); addCard.onclick = () => openAddLinkModal();
grid.appendChild(addCard); grid.appendChild(addCard);
if (linkSortable) linkSortable.destroy(); if (linkSortable) linkSortable.destroy();
linkSortable = Sortable.create(grid, { linkSortable = Sortable.create(grid, {
animation: 150, animation: 150,
@ -320,7 +305,44 @@ function openEditSubcategoryModal(sub) {
}); });
} }
// Build the category+subcategory select HTML for link modals.
// selectedCatId / selectedSubId pre-select the dropdowns (used in edit mode).
function buildLocationFields(selectedCatId, selectedSubId) {
const catOptions = state.categories.map(c =>
`<option value="${c.id}" ${c.id === selectedCatId ? 'selected' : ''}>${c.name}</option>`
).join('');
return `
<div class="field">
<label>Category</label>
<select id="f-cat">${catOptions}</select>
</div>
<div class="field">
<label>Subcategory</label>
<select id="f-sub"><option value="">Loading</option></select>
</div>
`;
}
// Populate the subcategory dropdown for the given category id.
// If targetSubId is provided, pre-select it.
async function populateSubSelect(catId, targetSubId) {
const subEl = document.getElementById('f-sub');
if (!subEl) return;
subEl.innerHTML = '<option value="">Loading…</option>';
const subs = await get('/subcategories/' + catId);
if (subs.length === 0) {
subEl.innerHTML = '<option value="">(no subcategories)</option>';
return;
}
subEl.innerHTML = subs.map(s =>
`<option value="${s.id}" ${s.id === targetSubId ? 'selected' : ''}>${s.name}</option>`
).join('');
}
function openAddLinkModal() { function openAddLinkModal() {
const defaultCatId = state.activeCategoryId || (state.categories[0] && state.categories[0].id);
const defaultSubId = state.activeSubcategoryId;
openModal('Add link', ` openModal('Add link', `
<div class="field"> <div class="field">
<label>Label</label> <label>Label</label>
@ -330,6 +352,7 @@ function openAddLinkModal() {
<label>URL</label> <label>URL</label>
<input type="text" id="f-url" placeholder="https://..." /> <input type="text" id="f-url" placeholder="https://..." />
</div> </div>
${buildLocationFields(defaultCatId, defaultSubId)}
<div class="field favicon-field"> <div class="field favicon-field">
<label>Favicon</label> <label>Favicon</label>
<div class="favicon-preview-row"> <div class="favicon-preview-row">
@ -346,23 +369,66 @@ function openAddLinkModal() {
const label = document.getElementById('f-label').value.trim(); const label = document.getElementById('f-label').value.trim();
const url = document.getElementById('f-url').value.trim(); const url = document.getElementById('f-url').value.trim();
const favicon = document.getElementById('f-favicon').value.trim(); const favicon = document.getElementById('f-favicon').value.trim();
if (!label || !url) return; const subId = parseInt(document.getElementById('f-sub').value);
if (!label || !url || !subId) return;
// Find position: end of the target subcategory's links
const targetLinks = await get('/links/' + subId);
await post('/links', { await post('/links', {
subcategory_id: state.activeSubcategoryId, subcategory_id: subId,
label, label,
url, url,
favicon: favicon || null, favicon: favicon || null,
position: state.links.length position: targetLinks.length
}); });
closeModal(); closeModal();
// If we added to the currently-viewed subcategory, refresh it; otherwise navigate there
if (subId === state.activeSubcategoryId) {
await selectSubcategory(state.activeSubcategoryId); await selectSubcategory(state.activeSubcategoryId);
} else {
// Find the category for this subcategory and navigate
const allCats = state.categories;
for (const cat of allCats) {
const subs = await get('/subcategories/' + cat.id);
if (subs.find(s => s.id === subId)) {
await selectCategory(cat.id);
await selectSubcategory(subId);
break;
}
}
}
}); });
// Populate subcategory dropdown for the initial category
populateSubSelect(defaultCatId, defaultSubId);
// When category changes, reload subcategory dropdown
setTimeout(() => {
const catEl = document.getElementById('f-cat');
if (catEl) {
catEl.addEventListener('change', () => {
populateSubSelect(parseInt(catEl.value), null);
});
}
}, 0);
setupFaviconFetch('f-url', 'f-favicon', 'f-favicon-preview', 'f-favicon-status', 'f-favicon-upload-btn', 'f-favicon-file'); setupFaviconFetch('f-url', 'f-favicon', 'f-favicon-preview', 'f-favicon-status', 'f-favicon-upload-btn', 'f-favicon-file');
} }
function openEditLinkModal(link, e) { async function openEditLinkModal(link, e) {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
// Determine which category owns this link's subcategory so we can pre-select it
let ownerCatId = state.activeCategoryId;
// Search all categories for the one that contains link.subcategory_id
for (const cat of state.categories) {
const subs = await get('/subcategories/' + cat.id);
if (subs.find(s => s.id === link.subcategory_id)) {
ownerCatId = cat.id;
break;
}
}
openModal('Edit link', ` openModal('Edit link', `
<div class="field"> <div class="field">
<label>Label</label> <label>Label</label>
@ -372,6 +438,7 @@ function openEditLinkModal(link, e) {
<label>URL</label> <label>URL</label>
<input type="text" id="f-url" value="${link.url}" /> <input type="text" id="f-url" value="${link.url}" />
</div> </div>
${buildLocationFields(ownerCatId, link.subcategory_id)}
<div class="field favicon-field"> <div class="field favicon-field">
<label>Favicon</label> <label>Favicon</label>
<div class="favicon-preview-row"> <div class="favicon-preview-row">
@ -392,17 +459,49 @@ function openEditLinkModal(link, e) {
const label = document.getElementById('f-label').value.trim(); const label = document.getElementById('f-label').value.trim();
const url = document.getElementById('f-url').value.trim(); const url = document.getElementById('f-url').value.trim();
const favicon = document.getElementById('f-favicon').value.trim(); const favicon = document.getElementById('f-favicon').value.trim();
if (!label || !url) return; const newSubId = parseInt(document.getElementById('f-sub').value);
if (!label || !url || !newSubId) return;
const isMoving = newSubId !== link.subcategory_id;
let position = link.position;
if (isMoving) {
// Place at end of the destination subcategory
const targetLinks = await get('/links/' + newSubId);
position = targetLinks.length;
}
await put('/links/' + link.id, { await put('/links/' + link.id, {
subcategory_id: state.activeSubcategoryId, subcategory_id: newSubId,
label, label,
url, url,
favicon: favicon || null, favicon: favicon || null,
position: link.position position
}); });
closeModal(); closeModal();
if (!isMoving) {
// Same subcategory — just refresh current view
await selectSubcategory(state.activeSubcategoryId); await selectSubcategory(state.activeSubcategoryId);
} else {
// Link moved away — refresh current subcategory (link is gone from here)
// Then optionally navigate to the destination
await selectSubcategory(state.activeSubcategoryId);
}
}); });
// Populate subcategory dropdown for the owner category
populateSubSelect(ownerCatId, link.subcategory_id);
// When category changes, reload subcategory dropdown
setTimeout(() => {
const catEl = document.getElementById('f-cat');
if (catEl) {
catEl.addEventListener('change', () => {
populateSubSelect(parseInt(catEl.value), null);
});
}
}, 0);
setupFaviconFetch('f-url', 'f-favicon', 'f-favicon-preview', 'f-favicon-status', 'f-favicon-upload-btn', 'f-favicon-file', link.favicon); setupFaviconFetch('f-url', 'f-favicon', 'f-favicon-preview', 'f-favicon-status', 'f-favicon-upload-btn', 'f-favicon-file', link.favicon);
} }
@ -502,7 +601,6 @@ function setupFaviconFetch(urlId, faviconId, previewId, statusId, uploadBtnId, f
} }
} }
// Show existing favicon immediately if editing
if (existingFavicon) { if (existingFavicon) {
showPreview(existingFavicon); showPreview(existingFavicon);
statusEl.textContent = existingFavicon; statusEl.textContent = existingFavicon;

View file

@ -488,3 +488,27 @@ body {
padding: 4px 8px; padding: 4px 8px;
font-size: 12px; font-size: 12px;
} }
.field select {
background: var(--bg);
border: 1px solid var(--border);
border-radius: var(--radius);
color: var(--text);
font-size: 13px;
padding: 8px 10px;
outline: none;
appearance: none;
-webkit-appearance: none;
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%23888' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
background-repeat: no-repeat;
background-position: right 10px center;
padding-right: 30px;
cursor: pointer;
}
.field select:focus {
border-color: var(--accent);
}
.field select option {
background: #1a1a1a;
color: var(--text);
}