import { Controller } from '@hotwired/stimulus'; export default class extends Controller { initialize() { this._onPreConnect = this._onPreConnect.bind(this); this._onConnect = this._onConnect.bind(this); } connect() { document.querySelector("#search").onsubmit = (event) => { event.preventDefault(); const autocompleteController = this.application.getControllerForElementAndIdentifier(this.element, 'symfony--ux-autocomplete--autocomplete') window.location.href = `/search?term=${autocompleteController.tomSelect.lastValue}` } this.element.addEventListener('autocomplete:pre-connect', this._onPreConnect); this.element.addEventListener('autocomplete:connect', this._onConnect); } disconnect() { // You should always remove listeners when the controller is disconnected to avoid side-effects this.element.removeEventListener('autocomplete:connect', this._onConnect); this.element.removeEventListener('autocomplete:pre-connect', this._onPreConnect); } _onPreConnect(event) { // TomSelect has not been initialized - options can be changed // console.log(event.detail); // Options that will be used to initialize TomSelect event.detail.options.onItemAdd = (value, $item) => { const params = value.split('|') window.location.href = `/result/${params[0]}/${params[1]}` }; event.detail.options.render.loading = (data, escape) => { return ` `; } event.detail.options.render.option = (data, escape) => { if (data.data.description.length > 60) { data.data.description = data.data.description.substring(0, 107) + "..."; } return `
${data.data.description}