/* DrCr: Web-based double-entry bookkeeping framework Copyright (C) 2022–2024 Lee Yingtong Li (RunasSudo) This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ function updateComboboxInputs(elCombobox, elInput) { elCombobox.querySelector('ul').querySelectorAll('li').forEach((elLi) => { const liText = elLi.querySelector('.combobox-text').innerText; if (liText.toLowerCase().startsWith(elInput.value.toLowerCase())) { elLi.classList.remove('hidden'); elLi.classList.add('block'); if (liText == elInput.value) { elLi.dataset.selected = 'selected'; } else { elLi.dataset.selected = ''; } } else { elLi.classList.remove('block'); elLi.classList.add('hidden'); elLi.dataset.selected = ''; } }); } function initCombobox(elCombobox) { const elInput = elCombobox.querySelector('input'); updateComboboxInputs(elCombobox, elInput); // Update combobox options on input elInput.addEventListener('input', (evt) => { updateComboboxInputs(elCombobox, elInput); }); // Open dropdown on click button elCombobox.querySelector('button').addEventListener('click', (evt) => { elInput.focus(); }); // Update combobox value on select elCombobox.querySelector('ul').querySelectorAll('li').forEach((elLi) => { elLi.addEventListener('mousedown', (evt) => { const liText = elLi.querySelector('.combobox-text').innerText; elInput.value = liText; updateComboboxInputs(elCombobox, elInput); }); }); // Disable autocomplete // Set this in Javascript rather than in HTML so that browser will continue to persist values after refresh elInput.autocomplete = 'off'; } // Init comboboxes document.querySelectorAll('.combobox').forEach(initCombobox);