Web/scripts/blog.js
Ell 54282c31bb
All checks were successful
Web/pipeline/head This commit looks good
get rid of global variables because js is bad
2021-03-17 03:10:25 +01:00

55 lines
No EOL
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");
});
$.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--) {
var obj = json[i];
addCatButton(json, 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(json, 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(json, cat);
});
}
}