|
| 1 | +let socket = null; |
| 2 | + |
| 3 | +export function openConfigModal(config, send_socket) { |
| 4 | + socket = send_socket; |
| 5 | + document.getElementById("configModalTitle").textContent = config.title; |
| 6 | + |
| 7 | + document.getElementById("configModalBody").innerHTML = ""; |
| 8 | + |
| 9 | + const form = document.createElement("div"); |
| 10 | + form.classList.add("grid", "grid-cols-[30%_67%]", "gap-4", "items-center"); |
| 11 | + |
| 12 | + Object.entries(config.properties).forEach(([key, property]) => { |
| 13 | + const label = document.createElement("label"); |
| 14 | + label.textContent = property.title; |
| 15 | + label.classList.add("font-medium", "text-grey-color", "text-left"); |
| 16 | + label.setAttribute("key", key); |
| 17 | + |
| 18 | + const fieldContainer = document.createElement("div"); |
| 19 | + fieldContainer.classList.add("flex", "items-center", "gap-2"); |
| 20 | + |
| 21 | + if (property.type === "boolean") { |
| 22 | + const checkbox = document.createElement("input"); |
| 23 | + checkbox.type = "checkbox"; |
| 24 | + checkbox.checked = property.value; |
| 25 | + checkbox.classList.add("form-checkbox", "h-5", "w-5", "checked:accent-red-cogip", "checked:border-red-cogip"); |
| 26 | + checkbox.addEventListener("change", () => { |
| 27 | + sendSocketUpdate( |
| 28 | + config.sio_event, |
| 29 | + key, |
| 30 | + checkbox.checked, |
| 31 | + config.namespace |
| 32 | + ); |
| 33 | + }); |
| 34 | + fieldContainer.appendChild(checkbox); |
| 35 | + } else if (property.type === "integer" || property.type === "number") { |
| 36 | + const input = document.createElement("input"); |
| 37 | + input.type = "number"; |
| 38 | + input.value = property.value; |
| 39 | + input.min = property.minimum; |
| 40 | + input.max = property.maximum; |
| 41 | + input.style.width = '75px'; |
| 42 | + input.disabled = true; |
| 43 | + input.classList.add( |
| 44 | + "text-grey-color", |
| 45 | + "p-2", |
| 46 | + "bg-black", |
| 47 | + "rounded-md", |
| 48 | + "border", |
| 49 | + "border-slate-950", |
| 50 | + "use-keyboard-input", |
| 51 | + "focus:outline-none", |
| 52 | + "focus:caret-red-cogip", |
| 53 | + "focus:ring-2", |
| 54 | + "focus:ring-red-cogip" |
| 55 | + ); |
| 56 | + |
| 57 | + const slider = document.createElement("input"); |
| 58 | + slider.type = "range"; |
| 59 | + slider.min = property.minimum; |
| 60 | + slider.max = property.maximum; |
| 61 | + slider.value = property.value; |
| 62 | + slider.step = property.multipleOf || 1; |
| 63 | + slider.classList.add( |
| 64 | + "w-full", |
| 65 | + "appearance-none", |
| 66 | + "h-4", |
| 67 | + "bg-gray-300", |
| 68 | + "rounded-full", |
| 69 | + "thumb-red-cogip" |
| 70 | + ); |
| 71 | + |
| 72 | + const decrementBtn = document.createElement("button"); |
| 73 | + decrementBtn.textContent = "-"; |
| 74 | + decrementBtn.classList.add( |
| 75 | + "px-4", |
| 76 | + "py-2", |
| 77 | + "text-2xl", |
| 78 | + "bg-zinc-800", |
| 79 | + "text-grey-color", |
| 80 | + "rounded", |
| 81 | + "hover:bg-gray-600", |
| 82 | + "focus:outline-none" |
| 83 | + ); |
| 84 | + decrementBtn.addEventListener("click", () => { |
| 85 | + let newValue = Math.max( |
| 86 | + parseFloat(input.value) - (property.multipleOf || 1), |
| 87 | + property.minimum |
| 88 | + ); |
| 89 | + newValue = parseFloat(newValue.toFixed(3)); |
| 90 | + input.value = slider.value = newValue; |
| 91 | + sendSocketUpdate(config.sio_event, key, newValue, config.namespace); |
| 92 | + }); |
| 93 | + |
| 94 | + const incrementBtn = document.createElement("button"); |
| 95 | + incrementBtn.textContent = "+"; |
| 96 | + incrementBtn.classList.add("px-4", "py-2", "text-2xl", "bg-zinc-800", "text-grey-color", "rounded", "hover:bg-gray-600", "focus:outline-none"); |
| 97 | + incrementBtn.addEventListener("click", () => { |
| 98 | + let newValue = Math.min( |
| 99 | + parseFloat(input.value) + (property.multipleOf || 1), |
| 100 | + property.maximum |
| 101 | + ); |
| 102 | + newValue = parseFloat(newValue.toFixed(3)); |
| 103 | + input.value = slider.value = newValue; |
| 104 | + sendSocketUpdate(config.sio_event, key, newValue, config.namespace); |
| 105 | + }); |
| 106 | + |
| 107 | + input.addEventListener("input", () => { |
| 108 | + slider.value = input.value; |
| 109 | + sendSocketUpdate(config.sio_event, key, input.value, config.namespace); |
| 110 | + }); |
| 111 | + slider.addEventListener("input", () => { |
| 112 | + input.value = slider.value; |
| 113 | + sendSocketUpdate(config.sio_event, key, slider.value, config.namespace); |
| 114 | + }); |
| 115 | + fieldContainer.appendChild(input); |
| 116 | + fieldContainer.appendChild(slider); |
| 117 | + fieldContainer.appendChild(decrementBtn); |
| 118 | + fieldContainer.appendChild(incrementBtn); |
| 119 | + } |
| 120 | + |
| 121 | + form.appendChild(label); |
| 122 | + form.appendChild(fieldContainer); |
| 123 | + }); |
| 124 | + |
| 125 | + document.getElementById("configModalBody").appendChild(form); |
| 126 | + |
| 127 | + // Show the modal |
| 128 | + const configModal = document.getElementById("configModal"); |
| 129 | + configModal.classList.remove("hidden"); |
| 130 | + configModal.classList.add("flex"); |
| 131 | +} |
| 132 | + |
| 133 | +function sendSocketUpdate(event, name, value, namespace) { |
| 134 | + const socketUpdate = { |
| 135 | + name: name, |
| 136 | + namespace: namespace, |
| 137 | + sio_event: event, |
| 138 | + value: +value, |
| 139 | + }; |
| 140 | + socket.emit(event, socketUpdate); |
| 141 | +} |
0 commit comments