From d947c6efe1c4af3e265eb50ddc6953f334840b56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Mon, 20 Nov 2023 21:15:18 +0100 Subject: [PATCH] Remote JavaScript files --- games/static/js/add_edition.js | 30 +++++++++++++++--------------- games/static/js/add_game.js | 16 ++++++++-------- games/static/js/add_purchase.js | 14 +++++++------- games/static/js/add_session.js | 10 +++++++--- games/static/js/utils.js | 31 ++++++++++++++++++++----------- 5 files changed, 57 insertions(+), 44 deletions(-) diff --git a/games/static/js/add_edition.js b/games/static/js/add_edition.js index dabfca6..334f069 100644 --- a/games/static/js/add_edition.js +++ b/games/static/js/add_edition.js @@ -1,24 +1,24 @@ -import { syncSelectInputUntilChanged } from './utils.js'; +import { syncSelectInputUntilChanged } from "./utils.js"; let syncData = [ { - "source": "#id_game", - "source_value": "dataset.name", - "target": "#id_name", - "target_value": "value" + source: "#id_game", + source_value: "dataset.name", + target: "#id_name", + target_value: "value", }, { - "source": "#id_game", - "source_value": "textContent", - "target": "#id_sort_name", - "target_value": "value" + source: "#id_game", + source_value: "textContent", + target: "#id_sort_name", + target_value: "value", }, { - "source": "#id_game", - "source_value": "dataset.year", - "target": "#id_year_released", - "target_value": "value" - }, -] + source: "#id_game", + source_value: "dataset.year", + target: "#id_year_released", + target_value: "value", + }, +]; syncSelectInputUntilChanged(syncData, "form"); diff --git a/games/static/js/add_game.js b/games/static/js/add_game.js index 617e28a..d35ade7 100644 --- a/games/static/js/add_game.js +++ b/games/static/js/add_game.js @@ -1,12 +1,12 @@ -import { syncSelectInputUntilChanged } from './utils.js' +import { syncSelectInputUntilChanged } from "./utils.js"; let syncData = [ { - "source": "#id_name", - "source_value": "value", - "target": "#id_sort_name", - "target_value": "value" - } -] + source: "#id_name", + source_value: "value", + target: "#id_sort_name", + target_value: "value", + }, +]; -syncSelectInputUntilChanged(syncData, "form") +syncSelectInputUntilChanged(syncData, "form"); diff --git a/games/static/js/add_purchase.js b/games/static/js/add_purchase.js index cfacecc..e27e88c 100644 --- a/games/static/js/add_purchase.js +++ b/games/static/js/add_purchase.js @@ -30,14 +30,14 @@ getEl("#id_type").onchange = () => { setupElementHandlers(); }; -document.body.addEventListener('htmx:beforeRequest', function(event) { +document.body.addEventListener("htmx:beforeRequest", function (event) { // Assuming 'Purchase1' is the element that triggers the HTMX request - if (event.target.id === 'id_edition') { - var idEditionValue = document.getElementById('id_edition').value; + if (event.target.id === "id_edition") { + var idEditionValue = document.getElementById("id_edition").value; - // Condition to check - replace this with your actual logic - if (idEditionValue != '') { - event.preventDefault(); // This cancels the HTMX request - } + // Condition to check - replace this with your actual logic + if (idEditionValue != "") { + event.preventDefault(); // This cancels the HTMX request + } } }); diff --git a/games/static/js/add_session.js b/games/static/js/add_session.js index e190820..8effc41 100644 --- a/games/static/js/add_session.js +++ b/games/static/js/add_session.js @@ -7,10 +7,14 @@ for (let button of document.querySelectorAll("[data-target]")) { button.addEventListener("click", (event) => { event.preventDefault(); if (type == "now") { - targetElement.value = toISOUTCString(new Date); + targetElement.value = toISOUTCString(new Date()); } else if (type == "copy") { - const oppositeName = targetElement.name == "timestamp_start" ? "timestamp_end" : "timestamp_start"; - document.querySelector(`[name='${oppositeName}']`).value = targetElement.value; + const oppositeName = + targetElement.name == "timestamp_start" + ? "timestamp_end" + : "timestamp_start"; + document.querySelector(`[name='${oppositeName}']`).value = + targetElement.value; } else if (type == "toggle") { if (targetElement.type == "datetime-local") targetElement.type = "text"; else targetElement.type = "datetime-local"; diff --git a/games/static/js/utils.js b/games/static/js/utils.js index 5b7eddc..3a5ee1b 100644 --- a/games/static/js/utils.js +++ b/games/static/js/utils.js @@ -75,7 +75,10 @@ function syncSelectInputUntilChanged(syncData, parentSelector = document) { * @param {string} property - The property to retrieve the value from. */ function getValueFromProperty(sourceElement, property) { - let source = (sourceElement instanceof HTMLSelectElement) ? sourceElement.selectedOptions[0] : sourceElement + let source = + sourceElement instanceof HTMLSelectElement + ? sourceElement.selectedOptions[0] + : sourceElement; if (property.startsWith("dataset.")) { let datasetKey = property.slice(8); // Remove 'dataset.' part return source.dataset[datasetKey]; @@ -93,13 +96,11 @@ function getValueFromProperty(sourceElement, property) { */ function getEl(selector) { if (selector.startsWith("#")) { - return document.getElementById(selector.slice(1)) - } - else if (selector.startsWith(".")) { - return document.getElementsByClassName(selector) - } - else { - return document.getElementsByTagName(selector) + return document.getElementById(selector.slice(1)); + } else if (selector.startsWith(".")) { + return document.getElementsByClassName(selector); + } else { + return document.getElementsByTagName(selector); } } @@ -116,7 +117,7 @@ function getEl(selector) { function conditionalElementHandler(...configs) { configs.forEach(([condition, targetElements, callbackfn1, callbackfn2]) => { if (condition()) { - targetElements.forEach(elementName => { + targetElements.forEach((elementName) => { let el = getEl(elementName); if (el === null) { console.error(`Element ${elementName} doesn't exist.`); @@ -125,7 +126,7 @@ function conditionalElementHandler(...configs) { } }); } else { - targetElements.forEach(elementName => { + targetElements.forEach((elementName) => { let el = getEl(elementName); if (el === null) { console.error(`Element ${elementName} doesn't exist.`); @@ -167,4 +168,12 @@ function disableElementsWhenTrue(targetSelect, targetValue, elementList) { ]); } -export { toISOUTCString, syncSelectInputUntilChanged, getEl, conditionalElementHandler, disableElementsWhenFalse, disableElementsWhenTrue, getValueFromProperty }; +export { + toISOUTCString, + syncSelectInputUntilChanged, + getEl, + conditionalElementHandler, + disableElementsWhenFalse, + disableElementsWhenTrue, + getValueFromProperty, +};