From 7997f9bbb20357124fa56d7337d741100c71b898 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Sun, 19 Feb 2023 22:31:13 +0100 Subject: [PATCH] Sort games by name on edition form --- games/static/main.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 games/static/main.js diff --git a/games/static/main.js b/games/static/main.js new file mode 100644 index 0000000..d668f5b --- /dev/null +++ b/games/static/main.js @@ -0,0 +1,43 @@ +function elt(type, props, ...children) { + let dom = document.createElement(type); + if (props) Object.assign(dom, props); + for (let child of children) { + if (typeof child != "string") dom.appendChild(child); + else dom.appendChild(document.createTextNode(child)); + } + return dom; +} + +/** + * @param {Node} targetNode + */ +function addToggleButton(targetNode) { + let manualToggleButton = elt( + "td", + {}, + elt( + "div", + { className: "basic-button" }, + elt( + "button", + { + onclick: (event) => { + let textInputField = elt("input", { type: "text", id: targetNode.id }); + targetNode.replaceWith(textInputField); + event.target.addEventListener("click", (event) => { + textInputField.replaceWith(targetNode); + }); + }, + }, + "Toggle manual" + ) + ) + ); + targetNode.parentElement.appendChild(manualToggleButton); +} + +const toggleableFields = ["#id_game", "#id_edition", "#id_platform"]; + +toggleableFields.map((selector) => { + addToggleButton(document.querySelector(selector)); +});