|
1 |
| - document.addEventListener("DOMContentLoaded", function () { |
2 |
| - const contributorsList = document.querySelector("#contributorsList"); |
| 1 | +document.addEventListener("DOMContentLoaded", function () { |
3 | 2 | const toggleContributorsLink = document.getElementById("toggleContributors");
|
| 3 | + const contributorsList = document.getElementById("contributorsList"); |
4 | 4 | const maxVisibleContributors = 5;
|
| 5 | + |
| 6 | + // Only proceed if there are more than maxVisibleContributors |
| 7 | + if (toggleContributorsLink) { |
| 8 | + const contributors = Array.from(contributorsList.getElementsByClassName("contributor-item")); |
| 9 | + const totalContributors = contributors.length; |
5 | 10 |
|
6 |
| - const contributors = Array.from(contributorsList.getElementsByClassName("contributor-item")); |
7 |
| - const totalContributors = contributors.length; |
| 11 | + // Set the initial state to show only the first 5 contributors |
| 12 | + function toggleContributors() { |
| 13 | + const isShowingAll = toggleContributorsLink.textContent === "View Less"; |
| 14 | + |
| 15 | + // Show or hide contributors beyond the first 5 |
| 16 | + contributors.forEach((contributor, index) => { |
| 17 | + if (isShowingAll || index < maxVisibleContributors) { |
| 18 | + contributor.classList.remove("d-none"); |
| 19 | + } else { |
| 20 | + contributor.classList.add("d-none"); |
| 21 | + } |
| 22 | + }); |
8 | 23 |
|
9 |
| - // Function to toggle between showing more or fewer contributors |
10 |
| - function toggleContributors() { |
11 |
| - const isShowingAll = toggleContributorsLink.textContent === "View Less"; |
12 |
| - |
13 |
| - // Show or hide contributors based on the toggle state |
14 |
| - contributors.forEach((contributor, index) => { |
15 |
| - if (isShowingAll || index < maxVisibleContributors) { |
16 |
| - contributor.classList.remove("d-none"); |
| 24 | + // Toggle the link text based on the current state |
| 25 | + if (isShowingAll) { |
| 26 | + toggleContributorsLink.textContent = `+${totalContributors - maxVisibleContributors} more Contributors`; |
17 | 27 | } else {
|
18 |
| - contributor.classList.add("d-none"); |
| 28 | + toggleContributorsLink.textContent = "View Less"; |
19 | 29 | }
|
20 |
| - }); |
21 |
| - |
22 |
| - // Toggle the link text based on the current state |
23 |
| - if (isShowingAll) { |
24 |
| - toggleContributorsLink.textContent = `+${totalContributors - maxVisibleContributors} more Contributors`; |
25 |
| - } else { |
26 |
| - toggleContributorsLink.textContent = "View Less"; |
27 | 30 | }
|
28 |
| - } |
29 | 31 |
|
30 |
| - // Click event for the "View More / View Less" link |
31 |
| - if (toggleContributorsLink) { |
| 32 | + // Initial call to ensure the first 5 contributors are shown |
| 33 | + toggleContributors(); |
| 34 | + |
| 35 | + // Event listener for clicking the toggle link |
32 | 36 | toggleContributorsLink.addEventListener("click", function (e) {
|
33 | 37 | e.preventDefault();
|
34 |
| - toggleContributors(); // Toggle the contributors and text |
| 38 | + toggleContributors(); // Toggle contributors and update link text |
35 | 39 | });
|
36 | 40 | }
|
37 | 41 | });
|
0 commit comments