get rid of global variables because js is bad
All checks were successful
Web/pipeline/head This commit looks good
All checks were successful
Web/pipeline/head This commit looks good
This commit is contained in:
parent
8cd72547f0
commit
54282c31bb
1 changed files with 8 additions and 10 deletions
|
@ -4,19 +4,17 @@ $("#blog-archive-button").on("click", function () {
|
|||
$(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");
|
||||
populateBlog(json, "All");
|
||||
forceToAnchor();
|
||||
}
|
||||
});
|
||||
|
||||
function populateBlog(cat) {
|
||||
function populateBlog(json, cat) {
|
||||
let archive = $('#blog-archive');
|
||||
let list = $('#blog-list');
|
||||
let cats = $('#blog-cats');
|
||||
|
@ -24,10 +22,10 @@ function populateBlog(cat) {
|
|||
list.html("");
|
||||
cats.html("");
|
||||
|
||||
addCatButton(cats, "All");
|
||||
for (let i = blogData.length - 1; i >= 0; i--) {
|
||||
var obj = blogData[i];
|
||||
addCatButton(cats, obj.cat);
|
||||
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">';
|
||||
|
@ -46,12 +44,12 @@ function populateBlog(cat) {
|
|||
}
|
||||
}
|
||||
|
||||
function addCatButton(cats, cat) {
|
||||
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(cat);
|
||||
populateBlog(json, cat);
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue