refactor: extract inline scripts to dedupe and sort_filter modules

This commit is contained in:
2026-04-21 01:22:09 +02:00
parent bece1a970a
commit 28cacdd7c5
5 changed files with 46 additions and 28 deletions

18
static/dedupe.ts Normal file
View File

@@ -0,0 +1,18 @@
const dedupe = (): void => {
const script = document.currentScript as HTMLScriptElement | null
if (!script) return
const containerId = script.getAttribute('data-container')
const container = containerId ? document.getElementById(containerId) : document
if (!container) return
const seen = new Set<string>()
container.querySelectorAll('[data-id]').forEach((item) => {
const id = item.getAttribute('data-id')
if (id && seen.has(id)) {
item.remove()
} else if (id) {
seen.add(id)
}
})
}
dedupe()

23
static/sort_filter.ts Normal file
View File

@@ -0,0 +1,23 @@
const initSortFilter = (): void => {
const sortSelect = document.getElementById('sort-select') as HTMLSelectElement | null
const orderSelect = document.getElementById('order-select') as HTMLSelectElement | null
const submitForm = (): void => {
const form = document.getElementById('sort-form') as HTMLFormElement | null
if (form) form.submit()
}
sortSelect?.addEventListener('change', () => {
const input = document.getElementById('sort-input') as HTMLInputElement | null
if (input) input.value = sortSelect.value
submitForm()
})
orderSelect?.addEventListener('change', () => {
const input = document.getElementById('order-input') as HTMLInputElement | null
if (input) input.value = orderSelect.value
submitForm()
})
}
document.addEventListener('DOMContentLoaded', initSortFilter)