From 74e01206adc28c262ecdd5fad4d34ba3b2a3948d Mon Sep 17 00:00:00 2001 From: bery Date: Wed, 28 Feb 2024 07:58:43 +0100 Subject: [PATCH] added IommiBase.getContainer in JS --- iommi/static/js/iommi.js | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/iommi/static/js/iommi.js b/iommi/static/js/iommi.js index 474abf13..8d0839c7 100644 --- a/iommi/static/js/iommi.js +++ b/iommi/static/js/iommi.js @@ -65,6 +65,20 @@ class IommiBase { window.history.replaceState(null, null, `${window.location.pathname}?${params.toString()}`); } + /** + * returns iommi table container or null if none found + * @param {Element} element filter form or any other element with data-iommi-id-of-table attribute or any element inside container + * @return {(Element|null)} container element or null + */ + getContainer(element) { + if(element.hasAttribute('data-iommi-id-of-table')) { + return document.querySelector( + `.iommi-table-container[data-iommi-id="${element.getAttribute('data-iommi-id-of-table')}"]` + ); + } + return element.closest(".iommi-table-container"); + } + debounce(func, wait) { let timeout; @@ -279,9 +293,7 @@ class IommiBase { } } - const tableIommiID = form.getAttribute('data-iommi-id-of-table'); - const table = document.querySelector(`[data-iommi-id="${tableIommiID}"]`); - const container = table.closest('.iommi-table-container'); + const container = this.getContainer(form); // remove "page" for this table to always jump to the first page after filtering const paginator = container.querySelector('[data-iommi-page-parameter]'); @@ -304,10 +316,7 @@ class IommiBase { } enhanceFilterForm(form) { - let table = document.querySelector( - `[data-iommi-id="${form.getAttribute('data-iommi-id-of-table')}"]` - ) - const container = table.closest('.iommi-table-container'); + const container = this.getContainer(form); form.setAttribute('autocomplete', 'off'); const debouncedPopulate = this.debounce(this.queryPopulate, 400);