Skip to content

Commit

Permalink
Support view all
Browse files Browse the repository at this point in the history
  • Loading branch information
TsayAdobe committed Sep 23, 2024
1 parent c3c764e commit edbe095
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
10 changes: 10 additions & 0 deletions acrobat/blocks/prompt-card/prompt-card.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
display: none
}

.prompt-card.hidden {
display: none;
}

.view-all {
grid-column: 1 / -1;
display: flex;
justify-content: center;
}

.prompt-toast {
font-family: "Adobe Clean", adobe-clean, "Trebuchet MS", sans-serif;
align-items: flex-start;
Expand Down
34 changes: 32 additions & 2 deletions acrobat/blocks/prompt-card/prompt-card.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable compat/compat */
import { setLibs } from '../../scripts/utils.js';

const miloLibs = setLibs('/libs');
Expand Down Expand Up @@ -70,12 +71,41 @@ async function createBlock(element, cfg) {

async function createBlocks(element, blockArray, templateCfg) {
const { parentNode } = element;
for (const cfg of blockArray) {
for (const [i, cfg] of blockArray.entries()) {
const blockEl = createTag('div', { class: 'prompt-card' });
if (templateCfg.rows && i > 0) blockEl.classList.add('hidden');
await createBlock(blockEl, { ...templateCfg, ...cfg });
parentNode.insertBefore(blockEl, element.previousSibling);
}
element.remove();

if (templateCfg.rows && parentNode.classList.contains('section')) {
const resizeObserver = new ResizeObserver(() => {
const computedStyle = window.getComputedStyle(parentNode);
if (computedStyle.gridTemplateColumns !== 'none') {
const promptcards = [...parentNode.querySelectorAll('.prompt-card')];
const visibleCnt = computedStyle.gridTemplateColumns.split(' ').length * templateCfg.rows;
promptcards.forEach(
(x, i) => (i < visibleCnt ? x.classList.remove('hidden') : x.classList.add('hidden')),
);
if (promptcards.length <= visibleCnt) {
parentNode.querySelector('.view-all')?.remove();
resizeObserver.unobserve(parentNode);
}
}
});
resizeObserver.observe(parentNode);

const viewMore = createTag('div', { class: 'view-all' });
const moreBtn = createTag('div', { class: 'con-button outline' }, getPlaceHolder('View all'));
moreBtn.addEventListener('click', (e) => {
resizeObserver.unobserve(parentNode);
[...parentNode.querySelectorAll('.prompt-card')].forEach((x) => x.classList.remove('hidden'));
e.target.parentNode.remove();
});
viewMore.appendChild(moreBtn);
parentNode.appendChild(viewMore);
}
}

async function processGroup(element, cfg, startIndex) {
Expand All @@ -95,7 +125,7 @@ async function processGroup(element, cfg, startIndex) {
return;
}
const json = await resp.json();
const keys = Object.keys(cfg).filter((k) => !['json'].includes(k));
const keys = Object.keys(cfg).filter((k) => !['json', 'rows'].includes(k));
blockArray = json.data.filter(
(x) => keys.reduce((a, k) => a && cfg[k] === x[k], true),
);
Expand Down

0 comments on commit edbe095

Please sign in to comment.