highlighter = () => {
const cities = Array.from(new Set(incidents.map(d => `${d.city}, ${d.state}`)))
const options = [
{ label: 'All', value: 'all' },
{
label: 'Assailant',
options: Object.entries(ASSAILANTS).map(([value, label]) => {
const incidentCount = incidents.filter(inc => inc.assailant === value).length
if (incidentCount === 0) return
return { value, label: `${label} (${incidentCount})` }
}).filter(d => d)
},
{
label: 'City',
options: cities.sort().map(c => {
const incidentCount = incidents.filter(inc => `${inc.city}, ${inc.state}` === c).length
return { label: `${c} (${incidentCount})`, value: c }
})
}
]
const defaultValue = 'all'
const option = ({ label, value }) => html`<option value="${value}">${label}</option>`
const optGroup = ({ label, options }) => html`<optgroup label="${label}">${options.map(option)}</optgroup>`
const select = html`<select value="${defaultValue}">
${options.map(opt => {
if ('value' in opt) return option(opt);
return optGroup(opt);
})}
</select>`
const output = html`
<div>
<div style="font: 700 0.9rem sans-serif; margin-bottom: 3px;">Highlight</div>
${select}
</div>
`
output.value = select.value
select.addEventListener('change', (e) => {
output.value = select.value
output.dispatchEvent(new CustomEvent("input"))
})
return output
}