Files
once-campfire/app/javascript/lib/autocomplete/autocomplete_handler.js
Kevin McConnell df76a227dc Hello world
First open source release of Campfire 🎉
2025-08-21 09:31:59 +01:00

47 lines
1.1 KiB
JavaScript

import BaseAutocompleteHandler from "lib/autocomplete/base_autocomplete_handler"
import Selection from "lib/autocomplete/selection"
export default class extends BaseAutocompleteHandler {
#selection
constructor(element, select, url) {
super(element, url)
this.#selection = new Selection(select)
}
disconnect() {
this.#selection.disconnect()
}
insertAutocompletable(autocompletable) {
this.#selection.add(autocompletable.value, autocompletable.name, { avatarUrl: autocompletable.avatar_url })
this.element.value = ""
}
get pattern() {
return new RegExp(`^(.*?)$`)
}
remove(value) {
this.#selection.remove(value)
}
removeLastSelection() {
this.#selection.removeLast()
}
search(term) {
super.updateWithContentAndPosition(term, 0)
}
setAutocompletables(autocompletables) {
super.setAutocompletables(this.#filterSelectedAutocompletables(autocompletables))
}
#filterSelectedAutocompletables(autocompletables) {
const selectedValues = this.#selection.values.concat(Current.user.id)
return autocompletables.filter(autocompletable => !selectedValues.includes(autocompletable.value))
}
}