Web/scripts/blog.js
Ell d3ebe92ed7
All checks were successful
Web/pipeline/head This commit looks good
display an info for categories with no archive
2021-03-20 16:31:03 +01:00

60 lines
No EOL
2 KiB
JavaScript

$("#blog-archive-button").on("click", function () {
let archive = $('#blog-archive');
archive.toggle();
$(this).html((archive.is(":visible") ? "Hide" : "Show") + " archived posts");
});
$.ajax({
dataType: "json",
url: "blog/src/posts.json",
cache: false,
success: function (json) {
populateBlog(json, "All");
forceToAnchor();
}
});
function populateBlog(json, cat) {
let archive = $('#blog-archive');
let list = $('#blog-list');
let cats = $('#blog-cats');
archive.html("");
list.html("");
cats.html("");
addCatButton(json, cats, "All");
for (let i = json.length - 1; i >= 0; i--) {
let obj = json[i];
addCatButton(json, cats, obj.cat);
if (cat === "All" || obj.cat === cat) {
let p = /*html*/ `
<div class="card bg-light blog-entry rounded-0">
<div class="card-body">
<h4 class="card-title blog-title"><a class="blog-button" href="/blog/${obj.id}">${obj.name}</a></h4>
<div class="card-text text-muted blog-summary">${obj.summary}</div>
<span class="text-muted project-status">${obj.date}</span>
<span class="text-muted blog-cat">${obj.cat}</span>
</div>
</div>
`;
if (obj.archived) {
archive.append(p);
} else {
list.append(p);
}
}
}
if (!archive.html())
archive.html(`<em>There are no archived ${cat} posts.</em>`);
}
function addCatButton(json, cats, cat) {
let catAnchor = `blog-cat-${cat.toLowerCase().replace(" ", "_")}`;
if (!$(`#${catAnchor}`).length) {
cats.append( /*html*/ `<button type="button" class="btn btn-link blog-cat-button" id=${catAnchor}>${cat}</button>`);
$(`#${catAnchor}`).on('click', function () {
populateBlog(json, cat);
});
}
}