Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature stopwatch #741

Merged
merged 15 commits into from
Jul 13, 2024
Merged
66 changes: 66 additions & 0 deletions common-theme/assets/scripts/stop-watch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
class StopWatch {
constructor() {
this.attachListener();
}

attachListener() {
const timeElements = document.querySelectorAll('.c-block__time');

timeElements.forEach(timeElement => {
timeElement.addEventListener('click', () => {
if (!timeElement.classList.contains('timer-active')) {
this.startTimer(timeElement);
}
});
});
}

startTimer(timeElement) {
const datetime = timeElement.getAttribute('datetime');
const duration = this.parseDuration(datetime); // Convert format PXM to milliseconds
if (isNaN(duration) || duration <= 0) {
console.error("Invalid duration specified in datetime attribute.");
return;
}
let startTime = Date.now();

timeElement.classList.add('timer-active');

const timerInterval = setInterval(() => {
const remainingTime = this.updateTime(timeElement, startTime, duration);

if (remainingTime <= 0) {
clearInterval(timerInterval);
timeElement.classList.remove('timer-active');
}
}, 1000);
}

parseDuration(datetime) {
const match = datetime.match(/P(\d+)M/);
if (!match) {
return NaN;
}
const minutes = parseInt(match[1]);
return minutes * 60 * 1000; // Convert minutes to milliseconds
}

updateTime(element, startTime, duration) {
const currentTime = Date.now();
const elapsedTime = currentTime - startTime;
const remainingTime = duration - elapsedTime;

if (remainingTime <= 0) {
element.textContent = "0 minutes 00 seconds ⏱";
return 0;
}

const minutes = Math.floor(remainingTime / 60000);
const seconds = Math.floor((remainingTime % 60000) / 1000);

element.textContent = `${minutes} minutes ${seconds < 10 ? '0' : ''}${seconds} seconds ⏱`;
return remainingTime;
}
}

new StopWatch();
8 changes: 8 additions & 0 deletions common-theme/assets/styles/04-components/block.scss
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,12 @@
transform: translate(0%, 100%);
position: absolute;
}

&__time {
cursor: pointer;
}

&__time:hover,.timer-active {
metinbaris marked this conversation as resolved.
Show resolved Hide resolved
color: var(--theme-color--pop)
}
}
2 changes: 2 additions & 0 deletions common-theme/layouts/partials/scripts.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
<script src="{{ $darkmode.RelPermalink }}"></script>
{{ $alertmessage := resources.Get "scripts/alert-message.js" | resources.Minify }}
<script src="{{ $alertmessage.RelPermalink }}" defer></script>
{{ $stopwatch := resources.GetMatch "scripts/stop-watch.js" | resources.Minify }}
<script src="{{ $stopwatch.RelPermalink }}" defer></script>
metinbaris marked this conversation as resolved.
Show resolved Hide resolved

{{/* if there is custom js to, load here, but if it's a component please contribute it back to the commons */}}

Expand Down
2 changes: 1 addition & 1 deletion common-theme/layouts/partials/time.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
{{ $blockData := .Page.Scratch.Get "blockData" }}
{{ $block := .Page.Site.GetPage $blockData.api }}
{{ $time := $blockData.time | default $block.Params.time | default 60 }}
<time class="c-block__time" datetime="P{{ $time }}M">{{ $time }} minutes</time>
<time class="c-block__time" datetime="P{{ $time }}M">{{ $time }} minutes</time>
SallyMcGrath marked this conversation as resolved.
Show resolved Hide resolved
Loading