Skip to content

Commit

Permalink
Fix bug with collapsible grids inside of group widget
Browse files Browse the repository at this point in the history
Also move utils to new file
  • Loading branch information
svilenmarkov committed Sep 29, 2024
1 parent d5fa642 commit b0c4d70
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
37 changes: 8 additions & 29 deletions internal/assets/static/js/main.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
import { setupPopovers } from './popover.js';

function throttledDebounce(callback, maxDebounceTimes, debounceDelay) {
let debounceTimeout;
let timesDebounced = 0;

return function () {
if (timesDebounced == maxDebounceTimes) {
clearTimeout(debounceTimeout);
timesDebounced = 0;
callback();
return;
}

clearTimeout(debounceTimeout);
timesDebounced++;

debounceTimeout = setTimeout(() => {
timesDebounced = 0;
callback();
}, debounceDelay);
};
};

import { throttledDebounce, isElementVisible } from './utils.js';

async function fetchPageContent(pageData) {
// TODO: handle non 200 status codes/time outs
Expand Down Expand Up @@ -427,7 +405,7 @@ function setupCollapsibleGrids() {

const button = attachExpandToggleButton(gridElement);

let cardsPerRow = 2;
let cardsPerRow;

const resolveCollapsibleItems = () => {
const hideItemsAfterIndex = cardsPerRow * collapseAfterRows;
Expand Down Expand Up @@ -457,12 +435,11 @@ function setupCollapsibleGrids() {
}
};

afterContentReady(() => {
cardsPerRow = getCardsPerRow();
resolveCollapsibleItems();
});
const observer = new ResizeObserver(() => {
if (!isElementVisible(gridElement)) {
return;
}

window.addEventListener("resize", () => {
const newCardsPerRow = getCardsPerRow();

if (cardsPerRow == newCardsPerRow) {
Expand All @@ -472,6 +449,8 @@ function setupCollapsibleGrids() {
cardsPerRow = newCardsPerRow;
resolveCollapsibleItems();
});

afterContentReady(() => observer.observe(gridElement));
}
}

Expand Down
25 changes: 25 additions & 0 deletions internal/assets/static/js/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export function throttledDebounce(callback, maxDebounceTimes, debounceDelay) {
let debounceTimeout;
let timesDebounced = 0;

return function () {
if (timesDebounced == maxDebounceTimes) {
clearTimeout(debounceTimeout);
timesDebounced = 0;
callback();
return;
}

clearTimeout(debounceTimeout);
timesDebounced++;

debounceTimeout = setTimeout(() => {
timesDebounced = 0;
callback();
}, debounceDelay);
};
};

export function isElementVisible(element) {
return !!(element.offsetWidth || element.offsetHeight || element.getClientRects().length);
}

0 comments on commit b0c4d70

Please sign in to comment.