diff --git a/flaskr/static/collapse.js b/flaskr/static/collapse.js new file mode 100644 index 00000000..ea8145cd --- /dev/null +++ b/flaskr/static/collapse.js @@ -0,0 +1,24 @@ +document.addEventListener("DOMContentLoaded", function() { + const toggleAllButton = document.getElementById("toggle-all"); + const postHeaders = document.querySelectorAll(".post-header"); + const posts = document.querySelectorAll(".post"); + + toggleAllButton.addEventListener("click", function() { + posts.forEach(post => { + post.classList.toggle("collapsed"); + }); + }); + + postHeaders.forEach(header => { + header.addEventListener("click", function(event) { + const postId = event.currentTarget.getAttribute("onclick").match(/\d+/)[0]; + togglePostBody(postId); + }); + }); +}); + +function togglePostBody(postId) { + const postBody = document.getElementById(`post-body-${postId}`); + const post = postBody.closest(".post"); + post.classList.toggle("collapsed"); +} diff --git a/flaskr/static/style.css b/flaskr/static/style.css index 0b22aba7..f4c1c098 100644 --- a/flaskr/static/style.css +++ b/flaskr/static/style.css @@ -88,6 +88,7 @@ nav ul li a, nav ul li span, header .action { display: flex; align-items: flex-end; font-size: 0.85em; + cursor: pointer; } .post > header > div:first-of-type { @@ -106,6 +107,7 @@ nav ul li a, nav ul li span, header .action { .post .body { white-space: pre-line; + display: block; } .content:last-child { @@ -228,3 +230,7 @@ body.dark-mode .content textarea { color: #e0e0e0; border: 1px solid #555; } + +.post.collapsed .body { + display: none; +} diff --git a/flaskr/templates/blog/index.html b/flaskr/templates/blog/index.html index 3481b8e1..f257bec6 100644 --- a/flaskr/templates/blog/index.html +++ b/flaskr/templates/blog/index.html @@ -1,7 +1,7 @@ {% extends 'base.html' %} {% block header %} -

{% block title %}Posts{% endblock %}

+

Posts

{% if g.user %} New {% endif %} @@ -10,19 +10,20 @@ {% block content %} {% for post in posts %}
-
+
-

{{ post['title'] }}

+

{{ post['title'] }}

by {{ post['username'] }} on {{ post['created'].strftime('%Y-%m-%d') }}
{% if g.user['id'] == post['author_id'] %} Edit {% endif %}
-

{{ post['body'] }}

+

{{ post['body'] }}

{% if not loop.last %}
{% endif %} {% endfor %} + {% endblock %}