|
| 1 | +import { identify } from "WoltLabSuite/Core/Dom/Util"; |
| 2 | +import { getPhrase } from "WoltLabSuite/Core/Language"; |
| 3 | +import { getValues, init as initI18n } from "WoltLabSuite/Core/Language/Input"; |
| 4 | +import Sortable from "sortablejs"; |
| 5 | + |
| 6 | +type Data = { |
| 7 | + key: string; |
| 8 | + value: Record<string, string>; |
| 9 | +}; |
| 10 | + |
| 11 | +type Languages = Record<string, string>; |
| 12 | + |
| 13 | +let _languages: Languages; |
| 14 | + |
| 15 | +export function setup(formField: HTMLInputElement, languages: Languages): void { |
| 16 | + _languages = languages; |
| 17 | + |
| 18 | + const ul = createUi(formField); |
| 19 | + |
| 20 | + formField.form?.addEventListener("submit", () => { |
| 21 | + setHiddenValue(formField); |
| 22 | + }); |
| 23 | + |
| 24 | + new Sortable(ul, { |
| 25 | + direction: "vertical", |
| 26 | + animation: 150, |
| 27 | + fallbackOnBody: true, |
| 28 | + draggable: "li", |
| 29 | + handle: ".selectOptionsListItem__handle", |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +function createUi(formField: HTMLInputElement): HTMLUListElement { |
| 34 | + const ul = document.createElement("ul"); |
| 35 | + ul.classList.add("selectOptionsList"); |
| 36 | + formField.parentElement?.append(ul); |
| 37 | + |
| 38 | + if (formField.value) { |
| 39 | + const data = JSON.parse(formField.value) as Data[]; |
| 40 | + data.forEach((option) => { |
| 41 | + createRow(ul, option); |
| 42 | + }); |
| 43 | + } else { |
| 44 | + createRow(ul); |
| 45 | + } |
| 46 | + |
| 47 | + return ul; |
| 48 | +} |
| 49 | + |
| 50 | +function createRow(ul: HTMLUListElement, option?: Data, autoFocus: boolean = false): void { |
| 51 | + const li = document.createElement("li"); |
| 52 | + li.classList.add("selectOptionsListItem"); |
| 53 | + ul.append(li); |
| 54 | + |
| 55 | + const addButton = getAddButton(); |
| 56 | + addButton.addEventListener("click", () => { |
| 57 | + createRow(ul, undefined, true); |
| 58 | + }); |
| 59 | + |
| 60 | + const deleteButton = getDeleteButton(); |
| 61 | + deleteButton.addEventListener("click", () => { |
| 62 | + li.remove(); |
| 63 | + |
| 64 | + if (!ul.childElementCount) { |
| 65 | + createRow(ul); |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | + const keyInput = getKeyInput(); |
| 70 | + keyInput.addEventListener("keydown", (event) => { |
| 71 | + if (event.key === "Enter") { |
| 72 | + event.preventDefault(); |
| 73 | + createRow(ul, undefined, true); |
| 74 | + } |
| 75 | + }); |
| 76 | + keyInput.value = option ? option.key : ""; |
| 77 | + |
| 78 | + const equalsIcon = document.createElement("fa-icon"); |
| 79 | + equalsIcon.setIcon("equals"); |
| 80 | + |
| 81 | + const valueInput = getValueInput(); |
| 82 | + valueInput.addEventListener("keydown", (event) => { |
| 83 | + if (event.key === "Enter") { |
| 84 | + event.preventDefault(); |
| 85 | + createRow(ul, undefined, true); |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + li.append(getSortableHandle(), addButton, deleteButton, keyInput, equalsIcon, valueInput); |
| 90 | + |
| 91 | + const hasI18nValues = option && !Object.hasOwn(option.value, 0); |
| 92 | + |
| 93 | + initI18n(identify(valueInput), hasI18nValues ? option.value : {}, _languages, false); |
| 94 | + |
| 95 | + if (!hasI18nValues) { |
| 96 | + valueInput.value = option?.value[0] ?? ""; |
| 97 | + } |
| 98 | + |
| 99 | + if (autoFocus) { |
| 100 | + keyInput.focus(); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +function getAddButton(): HTMLButtonElement { |
| 105 | + const addIcon = document.createElement("fa-icon"); |
| 106 | + addIcon.setIcon("plus"); |
| 107 | + |
| 108 | + const addButton = document.createElement("button"); |
| 109 | + addButton.type = "button"; |
| 110 | + addButton.append(addIcon); |
| 111 | + addButton.classList.add("jsTooltip"); |
| 112 | + addButton.title = getPhrase("wcf.global.button.add"); |
| 113 | + |
| 114 | + return addButton; |
| 115 | +} |
| 116 | + |
| 117 | +function getDeleteButton(): HTMLButtonElement { |
| 118 | + const deleteIcon = document.createElement("fa-icon"); |
| 119 | + deleteIcon.setIcon("xmark"); |
| 120 | + |
| 121 | + const deleteButton = document.createElement("button"); |
| 122 | + deleteButton.type = "button"; |
| 123 | + deleteButton.append(deleteIcon); |
| 124 | + deleteButton.classList.add("jsTooltip"); |
| 125 | + deleteButton.title = getPhrase("wcf.global.button.delete"); |
| 126 | + |
| 127 | + return deleteButton; |
| 128 | +} |
| 129 | + |
| 130 | +function getKeyInput(): HTMLInputElement { |
| 131 | + const keyInput = document.createElement("input"); |
| 132 | + keyInput.classList.add("selectOptionsListItem__key"); |
| 133 | + keyInput.placeholder = getPhrase("wcf.form.selectOptions.key"); |
| 134 | + keyInput.type = "text"; |
| 135 | + keyInput.required = true; |
| 136 | + |
| 137 | + return keyInput; |
| 138 | +} |
| 139 | + |
| 140 | +function getValueInput(): HTMLInputElement { |
| 141 | + const valueInput = document.createElement("input"); |
| 142 | + valueInput.classList.add("selectOptionsListItem__value"); |
| 143 | + valueInput.placeholder = getPhrase("wcf.form.selectOptions.value"); |
| 144 | + valueInput.type = "text"; |
| 145 | + valueInput.required = true; |
| 146 | + |
| 147 | + return valueInput; |
| 148 | +} |
| 149 | + |
| 150 | +function getSortableHandle(): HTMLElement { |
| 151 | + const icon = document.createElement("fa-icon"); |
| 152 | + icon.setIcon("up-down"); |
| 153 | + const handle = document.createElement("span"); |
| 154 | + handle.append(icon); |
| 155 | + handle.classList.add("selectOptionsListItem__handle"); |
| 156 | + |
| 157 | + return handle; |
| 158 | +} |
| 159 | + |
| 160 | +function setHiddenValue(formField: HTMLInputElement): void { |
| 161 | + const data: Data[] = []; |
| 162 | + |
| 163 | + formField.parentElement?.querySelectorAll(".selectOptionsListItem").forEach((li) => { |
| 164 | + const key = li.querySelector<HTMLInputElement>(".selectOptionsListItem__key")!.value; |
| 165 | + const valueInput = li.querySelector<HTMLInputElement>(".selectOptionsListItem__value")!; |
| 166 | + |
| 167 | + data.push({ |
| 168 | + key, |
| 169 | + value: Object.fromEntries(getValues(valueInput.id)), |
| 170 | + }); |
| 171 | + }); |
| 172 | + |
| 173 | + formField.value = JSON.stringify(data); |
| 174 | +} |
0 commit comments