From 0f9c658d105d6cfea0b54d40a4bc88b45a20766a Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Mon, 24 Oct 2022 23:06:24 +0400 Subject: [PATCH] Reduced a size of a code for a mindflow. Removed check for empty name property because there's no plans to add more buttons. Click handler function defined outside to eliminate definition of many same functions. Used for ... of instead of traditional for for code reduce and it doesn't drastically affect performance actually. Especially at such low array sizes. Used force arg of a toggle method to reduce code. --- homepage/static/assets/js/mindflow.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/homepage/static/assets/js/mindflow.js b/homepage/static/assets/js/mindflow.js index 37bbae9..9d33f8d 100644 --- a/homepage/static/assets/js/mindflow.js +++ b/homepage/static/assets/js/mindflow.js @@ -1,13 +1,12 @@ -const buttons = document.getElementsByTagName("button"); const articles = document.getElementsByTagName("article"); document.getElementById("filter").classList.remove("hidden"); -for (let i = 0; i < buttons.length; ++i) - if (buttons[i].name !== "") - buttons[i].addEventListener("click", e => { - for (let j = 0; j < articles.length; ++j) - if (articles[j].id.startsWith(e.target.name) || e.target.name === "all") - articles[j].classList.remove("hidden"); - else - articles[j].classList.add("hidden"); }); \ No newline at end of file +function filter(e) { + for (const a of articles) + a.classList.toggle("hidden", + !(e.target.name === "all" || a.id.startsWith(e.target.name))); +} + +for (const b of document.getElementsByTagName("button")) + b.addEventListener("click", filter); \ No newline at end of file