Web/scripts/blog.js
Ell 8cd72547f0
All checks were successful
Web/pipeline/head This commit looks good
added categories to the blog
2021-03-17 03:07:12 +01:00

57 lines
1.8 KiB
JavaScript

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