From 74ec1760d5b5bb933d5d70bff28ef118edb104d0 Mon Sep 17 00:00:00 2001 From: Jan Hermann Date: Tue, 7 Nov 2023 01:18:46 +0100 Subject: [PATCH] Added views --- examples/index.html | 8 ++++---- src/countdown.js | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/examples/index.html b/examples/index.html index 2c47823..5ecc787 100644 --- a/examples/index.html +++ b/examples/index.html @@ -7,18 +7,18 @@

With text

Source:

-
<time class="countdown" data-lang="en" datetime="2021-12-21T15:16" data-remains="Remains" data-after="." data-ended="The event is over.">2021-12-21T15:16</time>
+
<time class="countdown" data-lang="en" datetime="2024-12-21T15:16" data-remains="Remains" data-after="." data-ended="The event is over.">2024-12-21T15:16</time>

Rendered:

- +

Without text

Source:

-
<time class="countdown" datetime="2021-12-21T15:16" data-ended="The event is over.">2021-12-21T15:16</time>
+
<time class="countdown" datetime="2024-12-21T15:16" data-ended="The event is over.">2024-12-21T15:16</time>

Rendered:

- + diff --git a/src/countdown.js b/src/countdown.js index 265471c..a866a0d 100644 --- a/src/countdown.js +++ b/src/countdown.js @@ -32,7 +32,7 @@ function inflection(count, what, lang = "en") { return words[lang][what][0]; } - if(0 < count && count < 5) { + if(2 <= count && count < 5) { return words[lang][what][1]; } return words[lang][what][2]; @@ -66,9 +66,39 @@ function countdown(element) { lang = element.getAttribute("data-lang"); } + let view = "blank"; + if(element.hasAttribute("data-view")) { + view = element.getAttribute("data-view"); + } + + let visible = []; + let showRemains = false; + for(let what in remain) { + switch(view) { + case "all": + visible[what] = true; + break; + case "rest": + if(remain[what] > 0) { + visible[what] = true; + } + break; + default: + case "blank": + if(showRemains === true || remain[what] > 0) { + visible[what] = true; + showRemains = true; + } else { + visible[what] = false; + } + break; + } + } + for(let what in remain) { let count = remain[what]; - if(count > 0) { + + if(visible[what] === true) { if(element.hasAttribute("data-lang")) { output += " " + count + " " + inflection(count, what, lang); } else {