|
| 1 | +import { TooltipManager } from "./tooltip_manager"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Creates a parameter editor with a label and an input. |
| 5 | + * @param label - The text of the label. |
| 6 | + * @param initialValue - The initial value of the input. |
| 7 | + * @param onChange - Function that is executed when the value changes. |
| 8 | + * @returns An HTML element representing the parameter editor. |
| 9 | + */ |
| 10 | +export function createParameterEditor( |
| 11 | + label: string, |
| 12 | + initialValue: number | string, |
| 13 | + onChange: (newValue: number | string) => void, |
| 14 | +): HTMLElement { |
| 15 | + // Create the editor container |
| 16 | + const container = document.createElement("div"); |
| 17 | + container.className = "parameter-editor"; |
| 18 | + |
| 19 | + // Create the label |
| 20 | + const labelElement = document.createElement("label"); |
| 21 | + labelElement.textContent = label; |
| 22 | + labelElement.className = "parameter-editor-label"; |
| 23 | + TooltipManager.getInstance().attachTooltip(labelElement, label); |
| 24 | + |
| 25 | + // Create the input |
| 26 | + const input = document.createElement("input"); |
| 27 | + input.type = typeof initialValue === "number" ? "number" : "text"; |
| 28 | + input.value = initialValue.toString(); |
| 29 | + input.className = "parameter-editor-input"; |
| 30 | + |
| 31 | + // Add the change event |
| 32 | + let previousValue = initialValue; |
| 33 | + |
| 34 | + input.addEventListener("change", () => { |
| 35 | + const newValue = |
| 36 | + input.type === "number" |
| 37 | + ? (parseFloat(input.value) as number) |
| 38 | + : (input.value as string); |
| 39 | + |
| 40 | + if (input.value === "") { |
| 41 | + input.value = previousValue.toString(); |
| 42 | + } else { |
| 43 | + previousValue = newValue; |
| 44 | + onChange(newValue); |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + // Add the label and input to the container |
| 49 | + container.appendChild(labelElement); |
| 50 | + container.appendChild(input); |
| 51 | + |
| 52 | + return container; |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Creates a parameter group with a toggle button. |
| 57 | + * @param groupName - The name of the group. |
| 58 | + * @param parameters - List of parameters with label, initial value, and onChange function. |
| 59 | + * @returns An HTML container that includes the toggle button and the parameters. |
| 60 | + */ |
| 61 | +export function createParameterGroup( |
| 62 | + groupName: string, |
| 63 | + parameters: { |
| 64 | + label: string; |
| 65 | + initialValue: number | string; |
| 66 | + onChange: (newValue: number | string) => void; |
| 67 | + }[], |
| 68 | +): { toggleButton: HTMLElement; borderedContainer: HTMLElement } { |
| 69 | + // Create a bordered container for the parameter group |
| 70 | + const borderedContainer = document.createElement("div"); |
| 71 | + borderedContainer.className = "parameter-group"; |
| 72 | + |
| 73 | + // Create a container for the parameters |
| 74 | + const parametersContainer = document.createElement("div"); |
| 75 | + parametersContainer.className = "parameter-group-parameters"; |
| 76 | + |
| 77 | + // Add each parameter to the container |
| 78 | + parameters.forEach(({ label, initialValue, onChange }) => { |
| 79 | + const parameterEditor = createParameterEditor( |
| 80 | + label, |
| 81 | + initialValue, |
| 82 | + onChange, |
| 83 | + ); |
| 84 | + parametersContainer.appendChild(parameterEditor); |
| 85 | + }); |
| 86 | + |
| 87 | + // Create a toggle button |
| 88 | + const toggleButton = document.createElement("button"); |
| 89 | + toggleButton.className = "right-bar-toggle-button"; |
| 90 | + TooltipManager.getInstance().attachTooltip(toggleButton, groupName); |
| 91 | + toggleButton.textContent = groupName; |
| 92 | + toggleButton.addEventListener("click", () => { |
| 93 | + const isHidden = borderedContainer.style.display === "none"; |
| 94 | + borderedContainer.style.display = isHidden ? "block" : "none"; |
| 95 | + }); |
| 96 | + |
| 97 | + // Hide the parameters by default |
| 98 | + borderedContainer.style.display = "none"; |
| 99 | + |
| 100 | + // Add the parameter container to the bordered container |
| 101 | + borderedContainer.appendChild(parametersContainer); |
| 102 | + |
| 103 | + return { toggleButton, borderedContainer }; |
| 104 | +} |
0 commit comments