Web/scripts/blog.js

60 lines
2 KiB
JavaScript
Raw Normal View History

2020-09-30 00:24:37 +02:00
$("#blog-archive-button").on("click", function () {
let archive = $('#blog-archive');
archive.toggle();
$(this).html((archive.is(":visible") ? "Hide" : "Show") + " archived posts");
});
2019-02-23 16:30:23 +01:00
$.ajax({
dataType: "json",
2021-03-11 03:44:50 +01:00
url: "blog/src/posts.json",
2019-02-23 16:30:23 +01:00
cache: false,
success: function (json) {
populateBlog(json, "All");
2021-03-17 03:07:12 +01:00
forceToAnchor();
}
});
function populateBlog(json, cat) {
2021-03-17 03:07:12 +01:00
let archive = $('#blog-archive');
let list = $('#blog-list');
let cats = $('#blog-cats');
archive.html("");
list.html("");
cats.html("");
addCatButton(json, cats, "All", cat);
for (let i = json.length - 1; i >= 0; i--) {
2021-03-19 07:55:18 +01:00
let obj = json[i];
addCatButton(json, cats, obj.cat, cat);
2021-03-19 07:55:18 +01:00
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>
2021-03-24 19:54:14 +01:00
<div class="card-text blog-summary">${obj.summary}</div>
2021-03-19 07:55:18 +01:00
<span class="text-muted project-status">${obj.date}</span>
<span class="text-muted blog-cat">${obj.cat}</span>
</div>
</div>
`;
2020-09-30 00:24:37 +02:00
if (obj.archived) {
archive.append(p);
} else {
list.append(p);
}
}
}
if (!archive.html())
archive.html(`<em>There are no archived ${cat} posts.</em>`);
2021-03-17 03:07:12 +01:00
}
function addCatButton(json, cats, cat, currCat) {
2021-03-17 03:07:12 +01:00
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 === currCat ? "disabled" : ""}>${cat}</button>`);
2021-03-17 03:07:12 +01:00
$(`#${catAnchor}`).on('click', function () {
populateBlog(json, cat);
2021-03-17 03:07:12 +01:00
});
}
}