From f67d98b8392a760dd44efbd8131d037d94f52815 Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Fri, 14 Mar 2025 17:41:48 +0100 Subject: [PATCH] WIP --- changedetectionio/static/js/conditions.js | 68 +++++++++++++++++++++++ changedetectionio/templates/_helpers.html | 7 ++- changedetectionio/templates/edit.html | 32 +---------- 3 files changed, 73 insertions(+), 34 deletions(-) create mode 100644 changedetectionio/static/js/conditions.js diff --git a/changedetectionio/static/js/conditions.js b/changedetectionio/static/js/conditions.js new file mode 100644 index 000000000..1b983b514 --- /dev/null +++ b/changedetectionio/static/js/conditions.js @@ -0,0 +1,68 @@ +$(document).ready(function () { + // Function to set up button event handlers + function setupButtonHandlers() { + // Unbind existing handlers first to prevent duplicates + $(".addRuleRow, .removeRuleRow").off("click"); + + // Add row button handler + $(".addRuleRow").on("click", function(e) { + e.preventDefault(); + + let currentRow = $(this).closest("tr"); + + // Clone without events + let newRow = currentRow.clone(false); + + // Reset input values in the cloned row + newRow.find("input").val(""); + newRow.find("select").prop("selectedIndex", 0); + + // Insert the new row after the current one + currentRow.after(newRow); + + // Reindex all rows + reindexRules(); + }); + + // Remove row button handler + $(".removeRuleRow").on("click", function(e) { + e.preventDefault(); + + // Only remove if there's more than one row + if ($("#rulesTable tbody tr").length > 1) { + $(this).closest("tr").remove(); + reindexRules(); + } + }); + } + + // Function to reindex form elements and re-setup event handlers + function reindexRules() { + // Unbind all button handlers first + $(".addRuleRow, .removeRuleRow").off("click"); + + // Reindex all form elements + $("#rulesTable tbody tr").each(function(index) { + $(this).find("select, input").each(function() { + let oldName = $(this).attr("name"); + let oldId = $(this).attr("id"); + + if (oldName) { + let newName = oldName.replace(/\d+/, index); + $(this).attr("name", newName); + } + + if (oldId) { + let newId = oldId.replace(/\d+/, index); + $(this).attr("id", newId); + } + }); + }); + + // Reattach event handlers after reindexing + setupButtonHandlers(); + } + + // Initial setup of button handlers + setupButtonHandlers(); +}); diff --git a/changedetectionio/templates/_helpers.html b/changedetectionio/templates/_helpers.html index ab66f6332..d0eec6dfe 100644 --- a/changedetectionio/templates/_helpers.html +++ b/changedetectionio/templates/_helpers.html @@ -61,8 +61,8 @@ {{ field(**kwargs)|safe }} {% endmacro %} -{% macro render_fieldlist_of_formfields_as_table(fieldlist) %} - +{% macro render_fieldlist_of_formfields_as_table(fieldlist, table_id="rulesTable") %} +
{% for subfield in fieldlist[0] %} @@ -87,7 +87,8 @@ {% endfor %} {% endfor %} diff --git a/changedetectionio/templates/edit.html b/changedetectionio/templates/edit.html index c7bae7048..ae82c9df8 100644 --- a/changedetectionio/templates/edit.html +++ b/changedetectionio/templates/edit.html @@ -6,37 +6,7 @@ - - - +
- + +