From d15d54283e92b2d578429bef19dc45d1b8db0a67 Mon Sep 17 00:00:00 2001 From: Jean-Marie Favreau Date: Thu, 9 Nov 2023 11:50:28 +0100 Subject: [PATCH] =?UTF-8?q?les=20filtres=20sont=20maintenant=20adapt=C3=A9?= =?UTF-8?q?s=20suivant=20la=20s=C3=A9lection?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/agenda_culturel/static/js/filters.js | 91 +++++++++++++++++++ src/agenda_culturel/static/style.scss | 13 ++- .../forms/category-checkbox.html | 10 +- .../forms/checkbox-option.html | 2 +- .../agenda_culturel/forms/tag-checkbox.html | 10 +- 5 files changed, 113 insertions(+), 13 deletions(-) create mode 100644 src/agenda_culturel/static/js/filters.js diff --git a/src/agenda_culturel/static/js/filters.js b/src/agenda_culturel/static/js/filters.js new file mode 100644 index 0000000..fa8ddda --- /dev/null +++ b/src/agenda_culturel/static/js/filters.js @@ -0,0 +1,91 @@ +let underToggle = false; + +const toggleElement = (event) => { + console.log(event.currentTarget.getAttribute("data-target")); + container = document.getElementById(event.currentTarget.getAttribute("data-target")); + setFilterClasses(container); +}; + + +// Toggle select all elements +const toggleSelectAllFilterElements = (event) => { + event.preventDefault(); + + selectAllFilterElementsFromCheckbox(event.currentTarget); +}; + +const selectAllFilterElementsFromCheckbox = (checkbox) => { + const container = document.getElementById(checkbox.getAttribute("data-target")); + active = checkbox.checked; + + Array.prototype.forEach.call(container.children, function(child) { + Array.prototype.forEach.call(child.getElementsByTagName("input"), function(elem) { + elem.checked = active; + }); + }); + setFilterClasses(container); +}; + +const setFilterClasses = (container) => { + console.log("set filter"); + if (!underToggle) { + underToggle = true; + selectionButtom = document.getElementById(container.getAttribute("data-button")); + checkboxes = container.getElementsByTagName("input"); + // count the number of selected elements + nbElemsChecked = 0; + for (i = 0; i < checkboxes.length; i++) { + if (checkboxes[i].checked) { + nbElemsChecked++; + } + } + + // count the number of elements + nbElems = checkboxes.length; + + // if all elements are deselected, the class "no-selected" is removed and the all selection button is deselected + if (nbElemsChecked == 0) { + Array.prototype.forEach.call(checkboxes, function(checkbox) { + checkbox.parentNode.parentNode.classList.remove("no-selected"); + }); + selectionButtom.checked = false; + selectionButtom.indeterminate = false; + } + else { + // otherwise, for each filter element, set "no-selected" class if required + Array.prototype.forEach.call(checkboxes, function(checkbox) { + if (checkbox.checked) + checkbox.parentNode.parentNode.classList.remove("no-selected"); + else + checkbox.parentNode.parentNode.classList.add("no-selected"); + }); + + // if all elements are selected, the all selection button is selected, otherwise its set to "indeterminate" + if (nbElems == nbElemsChecked) { + selectionButtom.checked = true; + selectionButtom.indeterminate = false; + } else { + selectionButtom.indeterminate = true; + selectionButtom.checked = false; + } + } + + underToggle = false; + } +} + +document.addEventListener("DOMContentLoaded", function(e) { + Array.prototype.forEach.call(document.getElementsByClassName("buttons-filter"), function(buttongroup) { + buttongroup.style.display = "inline-block"; + }); + + const buttonsAll = document.getElementsByClassName("all-elements"); + + Array.prototype.forEach.call(buttonsAll, function(button) { + const container = document.getElementById(button.getAttribute("data-target")); + setFilterClasses(container); + }); + + +}); + \ No newline at end of file diff --git a/src/agenda_culturel/static/style.scss b/src/agenda_culturel/static/style.scss index f1c2c9c..da38545 100644 --- a/src/agenda_culturel/static/style.scss +++ b/src/agenda_culturel/static/style.scss @@ -267,14 +267,19 @@ article#filters { .cat { border: 0; } + + &.no-selected { + @extend .secondary, .outline; + + label { + color: var(--secondary); + } + } } [role="button"] input { margin-left: 0.3em; } + } } -.disabled { - pointer-events: none; - opacity: 0.4; -} \ No newline at end of file diff --git a/src/agenda_culturel/templates/agenda_culturel/forms/category-checkbox.html b/src/agenda_culturel/templates/agenda_culturel/forms/category-checkbox.html index d6140a3..08c33fb 100644 --- a/src/agenda_culturel/templates/agenda_culturel/forms/category-checkbox.html +++ b/src/agenda_culturel/templates/agenda_culturel/forms/category-checkbox.html @@ -1,12 +1,14 @@ {% load cat_extra %} -
+
-
{% for group, options, index in widget.optgroups %}{% if group %} +{% with container_id=widget.attrs.id %} +
{% for group, options, index in widget.optgroups %}{% if group %}
{% endif %}{% for widget in options %}
{{ widget.value.instance | circle_cat }}{% include widget.template_name %}
{% endfor %}{% if group %}
{% endif %}{% endfor %} -
\ No newline at end of file +
+{% endwith %} \ No newline at end of file diff --git a/src/agenda_culturel/templates/agenda_culturel/forms/checkbox-option.html b/src/agenda_culturel/templates/agenda_culturel/forms/checkbox-option.html index 088d1c4..4d4260c 100644 --- a/src/agenda_culturel/templates/agenda_culturel/forms/checkbox-option.html +++ b/src/agenda_culturel/templates/agenda_culturel/forms/checkbox-option.html @@ -1,4 +1,4 @@ diff --git a/src/agenda_culturel/templates/agenda_culturel/forms/tag-checkbox.html b/src/agenda_culturel/templates/agenda_culturel/forms/tag-checkbox.html index 636551d..c5d631a 100644 --- a/src/agenda_culturel/templates/agenda_culturel/forms/tag-checkbox.html +++ b/src/agenda_culturel/templates/agenda_culturel/forms/tag-checkbox.html @@ -1,12 +1,14 @@ {% load cat_extra %} -
+
-
{% for group, options, index in widget.optgroups %}{% if group %} +{% with container_id=widget.attrs.id %} +
{% for group, options, index in widget.optgroups %}{% if group %}
{% endif %}{% for widget in options %}
{% include widget.template_name %}
{% endfor %}{% if group %}
{% endif %}{% endfor %} -
\ No newline at end of file +
+{% endwith %}