refactor: extract inline scripts to dedupe and sort_filter modules
This commit is contained in:
18
static/dedupe.ts
Normal file
18
static/dedupe.ts
Normal 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
23
static/sort_filter.ts
Normal 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)
|
||||
Reference in New Issue
Block a user