Skip to content

Commit e144d4d

Browse files
committed
Complete refactor of collapsibles.
This is now a completely self-contained feature, which can be used in the same way on every page. I've moved all the collapsible-related logics out of the resources.js file and into collapsibles.js, and added documentation and other quality-of-life features that will apply to other pages, as well. Changes: - The icon will now always change when the collapsible opens or closes. - By adding the "collapsed" class, you can tell the collapsible to be collapsed by default.
1 parent f3bccff commit e144d4d

File tree

4 files changed

+67
-32
lines changed

4 files changed

+67
-32
lines changed

pydis_site/apps/content/resources/guides/pydis-guides/contributing/bot.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ First things first, to run the bot's code and make changes to it, you need a loc
1414
<button type="button" class="card-header collapsible">
1515
<span class="card-header-title subtitle is-6 my-2 ml-2">Getting started with Git and GitHub</span>
1616
<span class="card-header-icon">
17-
<i class="fas fa-angle-down title is-5" aria-hidden="true"></i>
17+
<i class="fas fa-fw fa-angle-down title is-5" aria-hidden="true"></i>
1818
</span>
1919
</button>
20-
<div class="collapsible-content">
20+
<div class="collapsible-content collapsed">
2121
<div class="card-content">
2222
<p>If you don't have Git on your computer already, <a href="https://git-scm.com/downloads">install it</a>. You can additionally install a Git GUI such as <a href="https://www.gitkraken.com/download">GitKraken</a>, or the <a href="https://cli.github.com/manual/installation">GitHub CLI</a>.</p>
2323
<p>To learn more about Git, you can look into <a href="../working-with-git">our guides</a>, as well as <a href="https://education.github.com/git-cheat-sheet-education.pdf">this cheatsheet</a>, <a href="https://learngitbranching.js.org">Learn Git Branching</a>, and otherwise any guide you can find on the internet. Once you got the basic idea though, the best way to learn Git is to use it.</p>
@@ -78,10 +78,10 @@ See [here](../obtaining-discord-ids) for help with obtaining Discord IDs.
7878
<button type="button" class="card-header collapsible">
7979
<span class="card-header-title subtitle is-6 my-2 ml-2">Optional config.yml</span>
8080
<span class="card-header-icon">
81-
<i class="fas fa-angle-down title is-5" aria-hidden="true"></i>
81+
<i class="fas fa-fw fa-angle-down title is-5" aria-hidden="true"></i>
8282
</span>
8383
</button>
84-
<div class="collapsible-content">
84+
<div class="collapsible-content collapsed">
8585
<div class="card-content">
8686
<p>If you used the provided server template, and you're not sure which channels belong where in the config file, you can use the config below. Pay attention to the comments with several <code>#</code> symbols, and replace the <code>�</code> characters with the right IDs.</p>
8787
<pre>
@@ -458,10 +458,10 @@ We understand this is tedious and are working on a better solution for setting u
458458
<button type="button" class="card-header collapsible">
459459
<span class="card-header-title subtitle is-6 my-2 ml-2">Why do you need a separate config file?</span>
460460
<span class="card-header-icon">
461-
<i class="fas fa-angle-down title is-5" aria-hidden="true"></i>
461+
<i class="fas fa-fw fa-angle-down title is-5" aria-hidden="true"></i>
462462
</span>
463463
</button>
464-
<div class="collapsible-content">
464+
<div class="collapsible-content collapsed">
465465
<div class="card-content">
466466
While it's technically possible to edit <code>config-default.yml</code> to match your server, it is heavily discouraged.
467467
This file's purpose is to provide the configurations the Python bot needs to run in the Python server in production, and should remain as such.
@@ -487,10 +487,10 @@ You are now almost ready to run the Python bot. The simplest way to do so is wit
487487
<button type="button" class="card-header collapsible">
488488
<span class="card-header-title subtitle is-6 my-2 ml-2">Getting started with Docker</span>
489489
<span class="card-header-icon">
490-
<i class="fas fa-angle-down title is-5" aria-hidden="true"></i>
490+
<i class="fas fa-fw fa-angle-down title is-5" aria-hidden="true"></i>
491491
</span>
492492
</button>
493-
<div class="collapsible-content">
493+
<div class="collapsible-content collapsed">
494494
<div class="card-content">
495495
The requirements for Docker are:
496496
<ul>
@@ -541,10 +541,10 @@ With at least the site running in Docker already (see the previous section on ho
541541
<button type="button" class="card-header collapsible">
542542
<span class="card-header-title subtitle is-6 my-2 ml-2">Ways to run code</span>
543543
<span class="card-header-icon">
544-
<i class="fas fa-angle-down title is-5" aria-hidden="true"></i>
544+
<i class="fas fa-fw fa-angle-down title is-5" aria-hidden="true"></i>
545545
</span>
546546
</button>
547-
<div class="collapsible-content">
547+
<div class="collapsible-content collapsed">
548548
<div class="card-content">
549549
Notice that the bot is started as a module. There are several ways to do so:
550550
<ul>

pydis_site/static/css/collapsibles.css

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,5 @@
77

88
.collapsible-content {
99
transition: max-height 0.3s ease-out;
10-
}
11-
12-
.collapsible-content.collapsed {
1310
overflow: hidden;
14-
max-height: 0 !important;
1511
}

pydis_site/static/js/collapsibles.js

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,67 @@
1+
/*
2+
A utility for creating simple collapsible cards.
3+
4+
To see this in action, go to /resources or /pages/guides/pydis-guides/contributing/bot/
5+
6+
// HOW TO USE THIS //
7+
First, import this file and the corresponding css file into your template.
8+
9+
<link rel="stylesheet" href="{% static "css/collapsibles.css" %}">
10+
<script defer src="{% static "js/collapsibles.js" %}"></script>
11+
12+
Next, you'll need some HTML that these scripts can interact with.
13+
14+
<div class="card">
15+
<button type="button" class="card-header collapsible">
16+
<span class="card-header-title subtitle is-6 my-2 ml-2">Your headline</span>
17+
<span class="card-header-icon">
18+
<i class="fas fa-fw fa-angle-down title is-5" aria-hidden="true"></i>
19+
</span>
20+
</button>
21+
<div class="collapsible-content collapsed">
22+
<div class="card-content">
23+
You can put anything you want here. Lists, more divs, flexboxes, images, whatever.
24+
</div>
25+
</div>
26+
</div>
27+
28+
That's it! Collapsing stuff should now work.
29+
*/
30+
131
document.addEventListener("DOMContentLoaded", () => {
2-
// Set maxHeight to scroll height on all matching collapsibles
332
const contentContainers = document.getElementsByClassName("collapsible-content");
433
for (const container of contentContainers) {
5-
container.style.maxHeight = container.scrollHeight + "px";
34+
// Close any collapsibles that are marked as initially collapsed
35+
if (container.classList.contains("collapsed")) {
36+
container.style.maxHeight = "0px";
37+
// Set maxHeight to the size of the container on all other containers.
38+
} else {
39+
container.style.maxHeight = container.scrollHeight + "px";
40+
}
641
}
742

43+
// Listen for click events, and collapse or explode
844
const headers = document.getElementsByClassName("collapsible");
945
for (const header of headers) {
46+
const content = header.nextElementSibling;
47+
const icon = header.querySelector(".card-header-icon i");
48+
49+
// Any collapsibles that are not initially collapsed needs an icon switch.
50+
if (!content.classList.contains("collapsed")) {
51+
icon.classList.remove("fas", "fa-angle-down");
52+
icon.classList.add("far", "fa-window-minimize");
53+
}
54+
1055
header.addEventListener("click", () => {
11-
var content = header.nextElementSibling;
12-
content.classList.toggle('collapsed');
56+
if (content.style.maxHeight !== "0px"){
57+
content.style.maxHeight = "0px";
58+
icon.classList.remove("far", "fa-window-minimize");
59+
icon.classList.add("fas", "fa-angle-down");
60+
} else {
61+
content.style.maxHeight = content.scrollHeight + "px";
62+
icon.classList.remove("fas", "fa-angle-down");
63+
icon.classList.add("far", "fa-window-minimize");
64+
}
1365
});
1466
}
1567
});

pydis_site/static/js/resources/resources.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -217,20 +217,7 @@ document.addEventListener("DOMContentLoaded", function () {
217217
deserializeURLParams();
218218
updateUI();
219219

220-
// If you collapse or uncollapse a filter group, swap the icon.
221-
$('button.collapsible').on("click", function() {
222-
let icon = $(this).find(".card-header-icon i");
223-
224-
if ($(icon).hasClass("fa-window-minimize")) {
225-
$(icon).removeClass(["far", "fa-window-minimize"]);
226-
$(icon).addClass(["fas", "fa-angle-down"]);
227-
} else {
228-
$(icon).removeClass(["fas", "fa-angle-down"]);
229-
$(icon).addClass(["far", "fa-window-minimize"]);
230-
}
231-
});
232-
233-
// If this is a mobile device, collapse the categories to win back some screen real estate.
220+
// If this is a mobile device, collapse all the categories to win back some screen real estate.
234221
if (screen.width < 480) {
235222
let categoryHeaders = $(".filter-category-header .collapsible-content");
236223
let icons = $('.filter-category-header button .card-header-icon i');

0 commit comments

Comments
 (0)