aboutsummaryrefslogtreecommitdiff
path: root/http/src/js/feed-condense-content.js
blob: 02361b516df01097a861bcf31639227ce4b8e7c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const expandedClass = "e-content--expanded";

function toggleContentExpanded(id) {
  const content = document.querySelector(`[data-content-id=${id}`);
  if (!content) {
    throw new Error(`No content found with ID: ${id}`);
  }
  const classes = content.getAttribute("class").split(" ");
  if (classes.includes(expandedClass)) {
    const newClasses = classes.filter((c) => c != expandedClass);
    content.setAttribute("class", newClasses.join(" "));
  } else {
    content.setAttribute("class", [...classes, expandedClass].join(" "));
  }
}

function addExpandItemButtons() {
  for (const content of document.querySelectorAll(".e-content")) {
    const contentId = crypto.randomUUID();
    content.setAttribute("data-content-id", contentId);
    const button = new HTMLButtonElement();
    button.onclick = `toggleContentExpanded(${contentId})`;
    content.insertAdjacentElement("afterend", button);
  }
}