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) {
|
2021-03-31 02:48:13 +02:00
|
|
|
populateBlog(json, getCookie("category") || "Featured");
|
2021-03-17 03:07:12 +01:00
|
|
|
forceToAnchor();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-03-31 02:48:13 +02:00
|
|
|
function populateBlog(json, currCat) {
|
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("");
|
|
|
|
|
2021-03-31 02:48:13 +02:00
|
|
|
// force all and featured categories to be first
|
|
|
|
addCatButton(json, cats, "All", currCat);
|
|
|
|
addCatButton(json, cats, "Featured", currCat);
|
|
|
|
|
2021-03-17 03:10:25 +01:00
|
|
|
for (let i = json.length - 1; i >= 0; i--) {
|
2021-03-31 02:48:13 +02:00
|
|
|
let post = json[i];
|
|
|
|
for (let cat of post.cat)
|
|
|
|
addCatButton(json, cats, cat, currCat);
|
|
|
|
if (currCat === "All" || post.cat.includes(currCat)) {
|
2021-03-19 07:55:18 +01:00
|
|
|
let p = /*html*/ `
|
|
|
|
<div class="card bg-light blog-entry rounded-0">
|
|
|
|
<div class="card-body">
|
2021-03-31 02:48:13 +02:00
|
|
|
<h4 class="card-title blog-title"><a class="blog-button" href="./blog/${post.id}">${post.name}</a></h4>
|
|
|
|
<div class="card-text blog-summary">${post.summary}</div>
|
|
|
|
<span class="text-muted project-status">${post.date}</span>
|
|
|
|
<span class="text-muted blog-cat">${post.cat.join(", ")}</span>
|
2021-03-19 07:55:18 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`;
|
2021-03-31 02:48:13 +02:00
|
|
|
if (post.archived) {
|
2020-09-30 00:24:37 +02:00
|
|
|
archive.append(p);
|
|
|
|
} else {
|
|
|
|
list.append(p);
|
|
|
|
}
|
2019-02-17 01:56:31 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-20 16:31:03 +01:00
|
|
|
|
|
|
|
if (!archive.html())
|
2021-03-31 02:48:13 +02:00
|
|
|
archive.html(`<em>There are no archived ${currCat} posts.</em>`);
|
2021-03-17 03:07:12 +01:00
|
|
|
}
|
|
|
|
|
2021-03-24 17:38:02 +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) {
|
2021-03-24 17:38:02 +01:00
|
|
|
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 () {
|
2021-03-17 03:10:25 +01:00
|
|
|
populateBlog(json, cat);
|
2021-03-31 02:48:13 +02:00
|
|
|
setCookie("category", cat, 365);
|
2021-03-17 03:07:12 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|