Sort subcategories alphabetically with Muut always last

Applied sortSubcategories() to submission form picker, admin category
list, and admin subcategory checkboxes. Added the helper function to
admin.html since it's a standalone file.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-08 12:59:41 +02:00
parent 74c31f898c
commit d9ecfe4c97
2 changed files with 25 additions and 4 deletions

View File

@@ -576,6 +576,15 @@
// ===========================
// CATEGORY HELPERS
// ===========================
function sortSubcategories(subs) {
return [...subs].sort((a, b) => {
const aM = a.fi.toLowerCase() === 'muut' ? 1 : 0;
const bM = b.fi.toLowerCase() === 'muut' ? 1 : 0;
if (aM !== bM) return aM - bM;
return a.fi.localeCompare(b.fi, 'fi');
});
}
function getCatLabel(id) {
const cat = ADMIN.categories.find(c => c.id === id);
return cat ? cat.fi : id;
@@ -589,7 +598,7 @@
const el = document.getElementById('catList');
if (!cats.length) { el.innerHTML = `<p class="empty-state">${at('cat_empty')}</p>`; return; }
el.innerHTML = cats.map(c => {
const subs = (c.subcategories || []).map(s =>
const subs = sortSubcategories(c.subcategories || []).map(s =>
`<span class="subcat-tag">${s.fi} <button onclick="removeSubcat('${c.id}','${s.id}')" class="subcat-remove">✕</button></span>`
).join('');
return `
@@ -670,7 +679,7 @@
function updateAdminSubcategories(checked = []) {
const catId = document.getElementById('postCategory').value;
const cat = ADMIN.categories.find(c => c.id === catId);
const subs = cat?.subcategories || [];
const subs = sortSubcategories(cat?.subcategories || []);
const group = document.getElementById('adminSubcatGroup');
const box = document.getElementById('adminSubcatCheckboxes');
if (!subs.length) { group.style.display = 'none'; box.innerHTML = ''; return; }

View File

@@ -81,6 +81,18 @@ function applyTranslations() {
document.querySelectorAll('[data-i18n-ph]').forEach(el => { el.placeholder = t(el.dataset.i18nPh); });
}
// ===========================
// HELPERS
// ===========================
function sortSubcategories(subs) {
return [...subs].sort((a, b) => {
const aM = a.fi.toLowerCase() === 'muut' ? 1 : 0;
const bM = b.fi.toLowerCase() === 'muut' ? 1 : 0;
if (aM !== bM) return aM - bM;
return a.fi.localeCompare(b.fi, 'fi');
});
}
// ===========================
// API
// ===========================
@@ -256,7 +268,7 @@ function renderSubFilters() {
if (!container) return;
if (currentFilter === 'all') { container.innerHTML = ''; return; }
const cat = APP.categories.find(c => c.id === currentFilter);
const subs = (cat?.subcategories || []).filter(s => countPostsInSubcategory(currentFilter, s.id) > 0);
const subs = sortSubcategories((cat?.subcategories || []).filter(s => countPostsInSubcategory(currentFilter, s.id) > 0));
if (!subs.length) { container.innerHTML = ''; return; }
container.innerHTML =
`<button class="sub-filter-btn ${currentSubFilter === 'all' ? 'active' : ''}" onclick="setSubFilter('all',this)">Kaikki</button>` +
@@ -453,7 +465,7 @@ function setSubmitType(type) {
function updateSubcategoryPicker() {
const catId = document.getElementById('sub-category').value;
const cat = APP.categories.find(c => c.id === catId);
const subs = cat?.subcategories || [];
const subs = sortSubcategories(cat?.subcategories || []);
const group = document.getElementById('sub-subcategory-group');
const sel = document.getElementById('sub-subcategory');
if (!subs.length) {