2019-02-04 21:04:05 +01:00
|
|
|
import { delegate } from '@utils';
|
2018-07-12 11:50:47 +02:00
|
|
|
|
2019-02-04 21:04:05 +01:00
|
|
|
const PRIMARY_SELECTOR = 'select[data-secondary-options]';
|
|
|
|
const SECONDARY_SELECTOR = 'select[data-secondary]';
|
|
|
|
const CHAMP_SELECTOR = '.editable-champ';
|
2018-07-12 11:50:47 +02:00
|
|
|
|
2019-02-04 21:04:05 +01:00
|
|
|
delegate('change', PRIMARY_SELECTOR, evt => {
|
|
|
|
const primary = evt.target;
|
|
|
|
const secondary = primary
|
|
|
|
.closest(CHAMP_SELECTOR)
|
|
|
|
.querySelector(SECONDARY_SELECTOR);
|
|
|
|
const options = JSON.parse(primary.dataset.secondaryOptions);
|
2018-07-12 11:50:47 +02:00
|
|
|
|
2019-02-04 21:04:05 +01:00
|
|
|
selectOptions(secondary, options[primary.value]);
|
|
|
|
});
|
2018-07-12 11:50:47 +02:00
|
|
|
|
2019-02-04 21:04:05 +01:00
|
|
|
function selectOptions(selectElement, options) {
|
|
|
|
selectElement.innerHTML = '';
|
2018-07-12 11:50:47 +02:00
|
|
|
|
2019-02-04 21:04:05 +01:00
|
|
|
for (let option of options) {
|
|
|
|
let element = document.createElement('option');
|
|
|
|
element.textContent = option;
|
|
|
|
element.value = option;
|
|
|
|
selectElement.appendChild(element);
|
2018-07-12 11:50:47 +02:00
|
|
|
}
|
2019-02-04 21:04:05 +01:00
|
|
|
|
|
|
|
selectElement.selectedIndex = 0;
|
|
|
|
}
|