Skip to content

Commit

Permalink
Merge pull request #3919 from open-formulieren/feature/3885-admin-for…
Browse files Browse the repository at this point in the history
…m-list

✨ [#3885] add open/collapsed for form categories
  • Loading branch information
sergei-maertens authored Feb 27, 2024
2 parents ffd32bc + 5b207d6 commit ac70173
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@
{% for category in categories %}
<tbody>
<tr
class="form-category {% if not category.is_leaf or category.form_count %}form-category--has-children{% endif %} {% if category.is_leaf %}form-category--leaf{% endif %} {% if category.is_leaf and category.depth > 1 %}form-category--collapsed{% endif %}"
class="form-category {% if not category.is_leaf or category.form_count %}form-category--has-children{% endif %} {% if category.is_leaf %}form-category--leaf{% endif %}"
data-id="{{ category.id }}"
data-parent-id="{{ category.get_parent.id }}"
data-depth="{{ category.get_depth }}">
{# span action checkbox + first column #}
<th colspan="{{ result_headers|length }}" scope="col" class="form-category__name">
Expand Down
62 changes: 46 additions & 16 deletions src/openforms/js/components/admin/form-category.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,50 @@ const init = async () => {
}
};

const checkSiblings = (node, depth) => {
const collapsedState = JSON.parse(localStorage.getItem('collapsedState')) || {};
let nodeIsCollapsed = false;
if (collapsedState && (nodeIsCollapsed = node.classList.contains('form-category--collapsed')))
collapsedState[node.dataset.id] = nodeIsCollapsed;

// check the siblings and extract the rows that are children of the current node
let tableBody = node.parentNode.nextElementSibling;

// loop over all next table bodies and check the form category rows for their depth.
// as soon as the depth value falls below our own depth value, we are done collapsing
// child nodes.
while (tableBody) {
const categoryRow = tableBody.querySelector('.form-category');
if (parseInt(categoryRow.dataset.depth) <= depth) break;

const parent = document.querySelector(`[data-id="${categoryRow.dataset.parentId}"]`);
categoryRow.classList.toggle(
'form-category--hidden',
nodeIsCollapsed || parent.classList.contains('form-category--collapsed')
);
categoryRow.classList.add('form-category--collapsed');
tableBody = tableBody.nextElementSibling;
}
};

const saveCollapsedState = (node, depth) => {
const collapsedState = {};
document.querySelectorAll('.form-category').forEach(category => {
const categoryId = category.dataset.id;
if (category.classList.contains('form-category--collapsed')) collapsedState[categoryId] = true;
});
localStorage.setItem('collapsedState', JSON.stringify(collapsedState));
checkSiblings(node, depth);
};

const restoreCollapsedState = (node, depth) => {
const collapsedState = JSON.parse(localStorage.getItem('collapsedState')) || {};
if (collapsedState[node.dataset.id]) {
node.classList.add('form-category--collapsed');
checkSiblings(node, depth);
}
};

const loadFormsForCategory = async (node, GETParams) => {
// node is a table row, after which we have to inject the forms.
const {id, depth: _depth} = node.dataset;
Expand All @@ -28,26 +72,12 @@ const loadFormsForCategory = async (node, GETParams) => {
_async: 1,
category: id,
};
restoreCollapsedState(node, depth);

node.addEventListener('click', event => {
event.preventDefault();
node.classList.toggle('form-category--collapsed');

// check the siblings and extract the rows that are children of the current node
let tableBody = node.parentNode.nextElementSibling;

// loop over all next table bodies and check the form category rows for their depth.
// as soon as the depth value falls below our own depth value, we are done collapsing
// child nodes.
while (tableBody) {
const categoryRow = tableBody.querySelector('.form-category');
if (parseInt(categoryRow.dataset.depth) <= depth) {
break;
}
categoryRow.classList.toggle('form-category--hidden');
categoryRow.classList.add('form-category--collapsed');
tableBody = tableBody.nextElementSibling;
}
saveCollapsedState(node, depth);
});

// TODO: handle pagination
Expand Down

0 comments on commit ac70173

Please sign in to comment.