2022-12-24 14:04:46 +01:00
|
|
|
import {Controller} from "@hotwired/stimulus";
|
|
|
|
|
|
|
|
import "tom-select/dist/css/tom-select.bootstrap5.css";
|
|
|
|
import '../../css/components/tom-select_extensions.css';
|
|
|
|
import TomSelect from "tom-select";
|
|
|
|
import {marked} from "marked";
|
|
|
|
|
|
|
|
export default class extends Controller {
|
|
|
|
_tomSelect;
|
|
|
|
|
|
|
|
connect() {
|
|
|
|
|
|
|
|
let settings = {
|
|
|
|
allowEmptyOption: true,
|
2022-12-24 16:10:19 +01:00
|
|
|
plugins: ['dropdown_input'],
|
2022-12-24 16:25:29 +01:00
|
|
|
searchField: ["name", "description", "category", "footprint"],
|
2022-12-24 14:04:46 +01:00
|
|
|
valueField: "id",
|
2022-12-24 16:10:19 +01:00
|
|
|
labelField: "name",
|
2022-12-24 16:25:29 +01:00
|
|
|
preload: "focus",
|
2022-12-24 14:04:46 +01:00
|
|
|
render: {
|
|
|
|
item: (data, escape) => {
|
2022-12-24 16:25:29 +01:00
|
|
|
return '<span>' + (data.image ? "<img style='height: 1.5rem; margin-right: 5px;' ' src='" + data.image + "'/>" : "") + escape(data.name) + '</span>';
|
2022-12-24 14:04:46 +01:00
|
|
|
},
|
|
|
|
option: (data, escape) => {
|
2022-12-24 16:10:19 +01:00
|
|
|
if(data.text) {
|
|
|
|
return '<span>' + escape(data.text) + '</span>';
|
|
|
|
}
|
|
|
|
|
2022-12-24 14:04:46 +01:00
|
|
|
let tmp = '<div class="row m-0">' +
|
2022-12-24 16:10:19 +01:00
|
|
|
"<div class='col-2 p-0 d-flex align-items-center'>" +
|
|
|
|
(data.image ? "<img class='typeahead-image' src='" + data.image + "'/>" : "") +
|
|
|
|
"</div>" +
|
2022-12-24 14:04:46 +01:00
|
|
|
"<div class='col-10'>" +
|
|
|
|
'<h6 class="m-0">' + escape(data.name) + '</h6>' +
|
2022-12-24 16:10:19 +01:00
|
|
|
(data.description ? '<p class="m-0">' + marked.parseInline(data.description) + '</p>' : "") +
|
|
|
|
(data.category ? '<p class="m-0"><span class="fa-solid fa-tags fa-fw"></span> ' + escape(data.category) : "");
|
2022-12-24 14:04:46 +01:00
|
|
|
|
|
|
|
if (data.footprint) { //If footprint is defined for the part show it next to the category
|
|
|
|
tmp += ' <span class="fa-solid fa-microchip fa-fw"></span> ' + escape(data.footprint);
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmp + '</p>' +
|
|
|
|
'</div></div>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (this.element.dataset.autocomplete) {
|
|
|
|
const base_url = this.element.dataset.autocomplete;
|
|
|
|
settings.valueField = "id";
|
|
|
|
settings.load = (query, callback) => {
|
|
|
|
const url = base_url.replace('__QUERY__', encodeURIComponent(query));
|
|
|
|
|
|
|
|
fetch(url)
|
|
|
|
.then(response => response.json())
|
|
|
|
.then(json => {callback(json);})
|
|
|
|
.catch(() => {
|
|
|
|
callback()
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
this._tomSelect = new TomSelect(this.element, settings);
|
2022-12-24 16:10:19 +01:00
|
|
|
//this._tomSelect.clearOptions();
|
2022-12-24 14:04:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|