From 4c9c3b48db02bcc27a89da694cd9ef5aad1c14c5 Mon Sep 17 00:00:00 2001 From: Javier Sánchez Parra Date: Tue, 5 Apr 2022 16:52:05 +0200 Subject: Add indeterminate checkboxes to scopes tree MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Each checkbox may have child checkboxes. If all those children are checked, it be checked. If none are checked, it is unchecked. If some of them are checked, then it’s in an indeterminate state (in this case symbolically meaning “partially” checked). --- ogcp/static/js/ogcp.js | 20 ++++++++++++++++++++ ogcp/templates/macros.html | 1 + 2 files changed, 21 insertions(+) diff --git a/ogcp/static/js/ogcp.js b/ogcp/static/js/ogcp.js index ac31c65..86a9282 100644 --- a/ogcp/static/js/ogcp.js +++ b/ogcp/static/js/ogcp.js @@ -37,6 +37,25 @@ function storeCheckboxStatus(checkbox) { localStorage.removeItem(checkbox.name); } +function checkParentsCheckboxes() { + const checkboxes = $('input:checkbox[form|="scopesForm"]'); + const reversedCheckboxes = $(checkboxes.get().reverse()) + + reversedCheckboxes.each(function() { + const checkbox = this; + const checkboxChildren = $('input:checkbox', this.parentNode).not(this); + + if (checkboxChildren.length == 0) return; + + const unCheckedChildren = checkboxChildren.filter(":not(:checked)"); + + checkbox.indeterminate = + unCheckedChildren.length > 0 && + unCheckedChildren.length < checkboxChildren.length; + checkbox.checked = unCheckedChildren.length === 0; + }); +} + function checkChildrenCheckboxes() { const checkboxes = $('input:checkbox[form|="scopesForm"]') @@ -48,6 +67,7 @@ function checkChildrenCheckboxes() { storeCheckboxStatus(this); $(this).trigger('show-client'); }); + checkParentsCheckboxes(); }); } diff --git a/ogcp/templates/macros.html b/ogcp/templates/macros.html index 6f19a30..ba500b3 100644 --- a/ogcp/templates/macros.html +++ b/ogcp/templates/macros.html @@ -13,6 +13,7 @@ keepScopesTreeState(); keepSelectedClients(); checkChildrenCheckboxes(); + checkParentsCheckboxes(); } }); -- cgit v1.2.3-18-g5258