From 50f0dd83857d824ebf808ef252d3c614428c9dfb Mon Sep 17 00:00:00 2001 From: WriterV2 Date: Sat, 5 Nov 2022 18:04:22 +0100 Subject: Add stories filter form JS --- docs/index.html | 3 +++ docs/stories.html | 21 ++++++++++----------- docs/stories.js | 34 ++++++++++++++++++++++++++++++++++ templates/base.html | 2 ++ templates/stories.html | 13 ++++++------- 5 files changed, 55 insertions(+), 18 deletions(-) create mode 100644 docs/stories.js diff --git a/docs/index.html b/docs/index.html index cd7df22..4a16d7c 100644 --- a/docs/index.html +++ b/docs/index.html @@ -25,6 +25,9 @@ + + + diff --git a/docs/stories.html b/docs/stories.html index e48400d..720a6ee 100644 --- a/docs/stories.html +++ b/docs/stories.html @@ -22,7 +22,7 @@
- @@ -30,8 +30,8 @@ - - @@ -39,8 +39,6 @@ - -
@@ -52,8 +50,8 @@
7 Pages - German - Shortstory + German + Shortstory
@@ -64,8 +62,8 @@
2 Pages - German - Shortstory + German + Shortstory
@@ -76,8 +74,8 @@
2 Pages - English - Screenplay + English + Screenplay
@@ -87,6 +85,7 @@ + diff --git a/docs/stories.js b/docs/stories.js new file mode 100644 index 0000000..e5a57ec --- /dev/null +++ b/docs/stories.js @@ -0,0 +1,34 @@ +// every story card +const workCards = document.querySelectorAll(".work-card"); + +// story type select input +const storyTypeSelect = document.querySelector("#story-type-select"); +// language select input +const languageSelect = document.querySelector("#language-select"); +// copy of works with all work cards +const works = Array.from(document.querySelector(".works").children); + +// filter cards based on storytype and language filter +function filterWorkCards() { + // restore initial state with all cards + // (TODO: Explore more efficient solutions without another loop) + document.querySelector(".works").replaceChildren(); + works.forEach((card) => { + document.querySelector(".works").appendChild(card); + }) + + // filter card if language or story type filter doesn't match + workCards.forEach((card) => { + if (languageSelect.value != "*" && card.querySelector(".story-language").innerHTML != languageSelect.value) { + document.querySelector(".works").removeChild(card); + } else if (storyTypeSelect.value != "*" && card.querySelector(".story-type").innerHTML != storyTypeSelect.value) { + document.querySelector(".works").removeChild(card); + } + }) +} + +// filter cards when +// story type selection changes +storyTypeSelect.addEventListener("change", filterWorkCards); +// language selection changes +languageSelect.addEventListener("change", filterWorkCards); diff --git a/templates/base.html b/templates/base.html index ab83cfb..025f1d9 100644 --- a/templates/base.html +++ b/templates/base.html @@ -23,6 +23,8 @@ + {% block jsfile %} + {% endblock jsfile %} diff --git a/templates/stories.html b/templates/stories.html index e342c02..431453c 100644 --- a/templates/stories.html +++ b/templates/stories.html @@ -4,21 +4,19 @@
- {% for work in works | unique(attribute="language") %} {% endfor %} - - {% for work in works | unique(attribute="story_type") %} {% endfor %} - -
@@ -30,10 +28,11 @@
{{work.number_of_pages}} Pages - {{work.language}} - {{work.story_type}} + {{work.language}} + {{work.story_type}}
{% endfor %} {% endblock content %} +{% block jsfile %}{% endblock jsfile %} -- cgit v1.2.3