mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-11 23:17:48 +00:00
- added a new Stimulus controller to display one line selects - that are to be populated when getting focus to avoid repeating - 10 times same option lists - on focus, select is populated by the data stored in template. - data that used to be source for each select (and so repeated) is - now stored once in a template
38 lines
996 B
JavaScript
38 lines
996 B
JavaScript
import TomSelect from "./tom_select_controller";
|
|
|
|
const minimumOptionsForSearchField = 11;
|
|
|
|
export default class extends TomSelect {
|
|
static values = { options: Object, placeholder: String };
|
|
|
|
connect(options = {}) {
|
|
const template = this.template();
|
|
let additional_options = {};
|
|
const lines_of_option = template.content.children.length;
|
|
|
|
if (lines_of_option < minimumOptionsForSearchField) {
|
|
additional_options.plugins = [];
|
|
}
|
|
|
|
super.connect(additional_options);
|
|
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();
|
|
Array.from(template.content.children).forEach((option) => {
|
|
this.control.addOption({value: option.value, text: option.text});
|
|
});
|
|
}
|
|
|
|
template() {
|
|
return document.querySelector("#producer_options");
|
|
}
|
|
}
|