vail

Internet morse code repeater
git clone https://git.woozle.org/neale/vail.git

vail / static / scripts
Neale Pickett  ·  2022-06-06

ui.mjs

 1/**
 2 * Set up repeater autofill list, and make dropdown active
 3 *
 4 * This fills the dataset from the dropdown, and make each dropdown element set
 5 * the value in the input field.
 6 */
 7function setRepeaterList() {
 8    let input = document.querySelector("#repeater")
 9    let datalist = document.querySelector("datalist#repeater-list")
10    let repeaterList = document.querySelector("#stock-repeaters .dropdown-content")
11    for (let a of repeaterList.children) {
12        if (a.tagName == "A") {
13            let opt = datalist.appendChild(document.createElement("option"))
14            if (a.dataset.value != undefined) {
15                opt.value = a.dataset.value
16            }
17            opt.textContent = a.textContent
18
19            a.addEventListener(
20                "click", 
21                () => {
22                    input.value = opt.value
23                    input.dispatchEvent(new Event("change"))
24                },
25            )
26        }
27    }
28}
29
30function init() {
31    setRepeaterList()
32}
33
34if (document.readyState === "loading") {
35	document.addEventListener("DOMContentLoaded", init)
36} else {
37	init()
38}