mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-17 04:34:24 +00:00
40 lines
1.1 KiB
JavaScript
40 lines
1.1 KiB
JavaScript
import TomSelect from "./tom_select_controller";
|
|
|
|
const minimumOptionsForSearchField = 11;
|
|
|
|
export default class extends TomSelect {
|
|
static values = { templateId: String, placeholder: String };
|
|
|
|
connect() {
|
|
const template = this.template(this.templateIdValue );
|
|
let additional_options = {};
|
|
const lines_of_option = template.content.children.length;
|
|
|
|
// Disable search input ("dropdown_input" plugin) from tom-select
|
|
if (lines_of_option < minimumOptionsForSearchField) {
|
|
additional_options.plugins = [];
|
|
}
|
|
|
|
super.connect(additional_options);
|
|
// focus event available on tom-select , not on html select
|
|
this.control.on('focus', this.fetchProducers.bind(this));
|
|
}
|
|
|
|
disconnect() {
|
|
if (this.control) this.control.destroy();
|
|
}
|
|
|
|
fetchProducers() {
|
|
if ((Object.keys(this.control.options).length) > 1) return false;
|
|
|
|
const template = this.template(this.templateIdValue );
|
|
Array.from(template.content.children).forEach((option) => {
|
|
this.control.addOption({value: option.value, text: option.text});
|
|
});
|
|
}
|
|
|
|
template(id) {
|
|
return document.querySelector(`#${id}`);
|
|
}
|
|
}
|