get rid of global variables because js is bad
All checks were successful
Web/pipeline/head This commit looks good

This commit is contained in:
Ell 2021-03-17 03:10:25 +01:00
parent 8cd72547f0
commit 54282c31bb

View file

@ -4,19 +4,17 @@ $("#blog-archive-button").on("click", function () {
$(this).html((archive.is(":visible") ? "Hide" : "Show") + " archived posts"); $(this).html((archive.is(":visible") ? "Hide" : "Show") + " archived posts");
}); });
let blogData;
$.ajax({ $.ajax({
dataType: "json", dataType: "json",
url: "blog/src/posts.json", url: "blog/src/posts.json",
cache: false, cache: false,
success: function (json) { success: function (json) {
blogData = json; populateBlog(json, "All");
populateBlog("All");
forceToAnchor(); forceToAnchor();
} }
}); });
function populateBlog(cat) { function populateBlog(json, cat) {
let archive = $('#blog-archive'); let archive = $('#blog-archive');
let list = $('#blog-list'); let list = $('#blog-list');
let cats = $('#blog-cats'); let cats = $('#blog-cats');
@ -24,10 +22,10 @@ function populateBlog(cat) {
list.html(""); list.html("");
cats.html(""); cats.html("");
addCatButton(cats, "All"); addCatButton(json, cats, "All");
for (let i = blogData.length - 1; i >= 0; i--) { for (let i = json.length - 1; i >= 0; i--) {
var obj = blogData[i]; var obj = json[i];
addCatButton(cats, obj.cat); addCatButton(json, cats, obj.cat);
if (cat == "All" || obj.cat == cat) { if (cat == "All" || obj.cat == cat) {
let p = ""; let p = "";
p += '<div class="card bg-light blog-entry rounded-0">'; p += '<div class="card bg-light blog-entry rounded-0">';
@ -46,12 +44,12 @@ function populateBlog(cat) {
} }
} }
function addCatButton(cats, cat) { function addCatButton(json, cats, cat) {
let catAnchor = `blog-cat-${cat.toLowerCase().replace(" ", "_")}`; let catAnchor = `blog-cat-${cat.toLowerCase().replace(" ", "_")}`;
if (!$(`#${catAnchor}`).length) { if (!$(`#${catAnchor}`).length) {
cats.append(`<button type="button" class="btn btn-link blog-cat-button" id=${catAnchor}>${cat}</button>`); cats.append(`<button type="button" class="btn btn-link blog-cat-button" id=${catAnchor}>${cat}</button>`);
$(`#${catAnchor}`).on('click', function () { $(`#${catAnchor}`).on('click', function () {
populateBlog(cat); populateBlog(json, cat);
}); });
} }
} }