Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>Cronometro</title>
<title>Cronômetro</title>
</head>
<body>
<div class="input-stopwatch">
<input type="number" min="00" value="00" id="hour" />
<input type="number" min="00" id="hour" disabled />
:
<input type="number" min="00" value="00" id="min" />
<input type="number" min="00" id="min" disabled />
:
<input type="number" min="00" value="30" id="sec" />
<input type="number" min="00" id="sec" disabled />
</div>
<div class="button-wrapper js-stopwatch-button">
<button class="js-active-fullscreen">
<img src="/images/full-screen.svg" alt="Colocar em tela cheia" />
</button>
<button class="js-edit-stopwatch">
<button class="js-edit-button">
<img src="/images/edit.svg" alt="Editar" />
</button>
<button class="js-stop-button" id="toZeroBtn">
Expand All @@ -40,7 +40,7 @@
</button>
</div>
<div id="countdown" class="hide">
<span id="countdown-number">10</span>
<span id="countdown-number"></span>
<button class="js-restart-button" id="restartBtn">
<img src="/images/cancel.svg" alt="Reiniciar" />
</button>
Expand Down
323 changes: 174 additions & 149 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,70 @@
let timerStarted = false;
let seconds = 30;
let minutes = 0;
let hours = 0;
let intervalInstance;
const TimerStatus = {
STOPPED: "STOPPED",
PAUSED: "PAUSED",
COUNTDOWN: "COUNTDOWN",
RUNNING: "RUNNING",
EDITING: "EDITING",
isRunning: (status) => status === TimerStatus.RUNNING,
isCountdown: (status) => status === TimerStatus.COUNTDOWN,
};

const DEFAULT_INTERVAL = 1000;
const DEFAULT_SECONDS = 30;
let TIMER_STATUS = TimerStatus.STOPPED;

setInputValues(DEFAULT_SECONDS);

document.getElementById("startBtn").addEventListener("click", start);
document.getElementById("pauseBtn").addEventListener("click", pause);
document.getElementById("toZeroBtn").addEventListener("click", stop);
document.getElementById("restartBtn").addEventListener("click", stop);
document
.querySelector(".js-edit-button")
.addEventListener("click", openEditInput);
document
.querySelector(".js-finish-edit-button")
.addEventListener("click", closeEditInput);
document
.querySelector(".js-cancel-button")
.addEventListener("click", cancelEditInput);
document
.querySelector(".js-active-fullscreen")
.addEventListener("click", handleFullscreen);

document.getElementById("hour").addEventListener("input", function () {
validateInput(this, 99);
});
document.getElementById("min").addEventListener("input", function () {
validateInput(this, 59);
});
document.getElementById("sec").addEventListener("input", function () {
validateInput(this, 59);
});

function timer() {
let seconds = getInputSeconds();

setTimeout(function () {
if (
TimerStatus.isRunning(TIMER_STATUS) ||
TimerStatus.isCountdown(TIMER_STATUS)
) {
seconds--;

function formatTime(value) {
return value < 10 ? `0${value}` : value.toString();
if (seconds <= 10) {
TIMER_STATUS = TimerStatus.COUNTDOWN;
styleCountdown(seconds);
}

setInputValues(seconds);
if (seconds == 0) {
document.body.classList.add("zero");
return;
} else {
timer();
}
}
}, DEFAULT_INTERVAL);
}

function validateInput(input, maxValue) {
Expand All @@ -14,176 +73,142 @@ function validateInput(input, maxValue) {
if (value < 0) value = 0;
if (value > maxValue) value = maxValue;

input.value = formatTime(value);
input.value = formatInput(value);
}

document.getElementById("hour").disabled = true;
document.getElementById("min").disabled = true;
document.getElementById("sec").disabled = true;
function formatInput(value = 0) {
return value.toString().padStart(2, "0").slice(-2);
}

document.getElementById("hour").value = formatTime(hours);
document.getElementById("min").value = formatTime(minutes);
document.getElementById("sec").value = formatTime(seconds);
function getInputSeconds() {
const { seconds, minutes, hours } = getInputValues();

document.getElementById("hour").addEventListener("input", function () {
validateInput(this, 99);
});
const sumMinutes = hours * 60 + minutes;
const sumSeconds = sumMinutes * 60 + seconds;

document.getElementById("min").addEventListener("input", function () {
validateInput(this, 59);
});

document.getElementById("sec").addEventListener("input", function () {
validateInput(this, 59);
});
return sumSeconds;
}

document.getElementById("startBtn").addEventListener("click", function () {
if (!timerStarted) {
timerStarted = true;
run();
}
});
function getInputValues() {
const hours = parseInt(document.getElementById("hour").value) || 0;
const minutes = parseInt(document.getElementById("min").value) || 0;
const seconds = parseInt(document.getElementById("sec").value) || 0;

document.getElementById("pauseBtn").addEventListener("click", function () {
pause();
});
return { seconds, minutes, hours };
}

document.getElementById("toZeroBtn").addEventListener("click", function () {
pause();
hours = 0;
minutes = 0;
seconds = 30;
function setInputValues(fromSeconds = 0) {
const hours = Math.floor(fromSeconds / 3600);
const minutes = Math.floor((fromSeconds % 3600) / 60);
const seconds = fromSeconds % 60;

document.getElementById("hour").value = formatTime(hours);
document.getElementById("min").value = formatTime(minutes);
document.getElementById("sec").value = formatTime(seconds);
});
document.getElementById(`sec`).value = formatInput(seconds);
document.getElementById(`min`).value = formatInput(minutes);
document.getElementById(`hour`).value = formatInput(hours);
}

document
.querySelector(".js-edit-stopwatch")
.addEventListener("click", function () {
document.getElementById("hour").disabled = false;
document.getElementById("min").disabled = false;
document.getElementById("sec").disabled = false;

document.querySelector(".js-stopwatch-button").classList.add("hide");
document
.querySelector(".js-edit-container-stopwatch")
.classList.remove("hide");
});
function start() {
TIMER_STATUS = TimerStatus.RUNNING;
applyStyles();
timer();
}

document
.querySelector(".js-cancel-button")
.addEventListener("click", function () {
document.getElementById("hour").value = formatTime(hours);
document.getElementById("min").value = formatTime(minutes);
document.getElementById("sec").value = formatTime(seconds);
function pause() {
TIMER_STATUS = TimerStatus.PAUSED;
applyStyles();
}

document.getElementById("hour").disabled = true;
document.getElementById("min").disabled = true;
document.getElementById("sec").disabled = true;
function stop() {
TIMER_STATUS = TimerStatus.STOPPED;
applyStyles();
setInputValues(DEFAULT_SECONDS);
}

document.querySelector(".js-stopwatch-button").classList.remove("hide");
document
.querySelector(".js-edit-container-stopwatch")
.classList.add("hide");
});
function handleFullscreen() {
if (!document.fullscreenElement) {
document.documentElement.requestFullscreen();
document.querySelector(".js-active-fullscreen").style =
"background-color: rgba(255, 255, 255, 0.25)";
} else {
document.exitFullscreen();
document.querySelector(".js-active-fullscreen").style = "";
}
}

document
.querySelector(".js-finish-edit-button")
.addEventListener("click", function () {
hours = parseInt(document.getElementById("hour").value) || 0;
minutes = parseInt(document.getElementById("min").value) || 0;
seconds = parseInt(document.getElementById("sec").value) || 0;
let PRE_EDIT_SECONDS = DEFAULT_SECONDS;

document.getElementById("hour").disabled = true;
document.getElementById("min").disabled = true;
document.getElementById("sec").disabled = true;
function cancelEditInput() {
setInputValues(PRE_EDIT_SECONDS);
closeEditInput();
}

document.querySelector(".js-stopwatch-button").classList.remove("hide");
document
.querySelector(".js-edit-container-stopwatch")
.classList.add("hide");
});
function openEditInput() {
TIMER_STATUS = TimerStatus.EDITING;
PRE_EDIT_SECONDS = getInputSeconds();
document.getElementById("hour").disabled = false;
document.getElementById("min").disabled = false;
document.getElementById("sec").disabled = false;

document.querySelector(".js-stopwatch-button").classList.add("hide");
document
.querySelector(".js-edit-container-stopwatch")
.classList.remove("hide");
}

document
.querySelector(".js-active-fullscreen")
.addEventListener("click", function () {
if (document.documentElement.requestFullscreen) {
document.documentElement.requestFullscreen();
}
});
function closeEditInput() {
TIMER_STATUS = TimerStatus.PAUSED;
document.getElementById("hour").disabled = true;
document.getElementById("min").disabled = true;
document.getElementById("sec").disabled = true;

document.getElementById("restartBtn").addEventListener("click", function () {
hours = 0;
minutes = 0;
seconds = 30;
document.querySelector(".js-stopwatch-button").classList.remove("hide");
document.querySelector(".js-edit-container-stopwatch").classList.add("hide");

document.getElementById("hour").value = formatTime(hours);
document.getElementById("min").value = formatTime(minutes);
document.getElementById("sec").value = formatTime(seconds);
applyStyles();
}

function resetStyles() {
document.querySelector(".js-play-button").classList.remove("press-start");
document.querySelector(".js-stop-button").classList.remove("press-stop");
document.querySelector(".js-pause-button").classList.remove("press-pause");
document.querySelector(".js-edit-button").classList.remove("press-edit");
document.querySelector(".input-stopwatch").classList.remove("hide");
document.querySelector(".js-stopwatch-button").classList.remove("hide");
document.getElementById("countdown").classList.add("hide");

document.body.classList.remove("zero");
});
}

function run() {
hours = parseInt(document.getElementById("hour").value) || 0;
minutes = parseInt(document.getElementById("min").value) || 0;
seconds = parseInt(document.getElementById("sec").value) || 0;

intervalInstance = setInterval(function () {
if (timerStarted) {
if (seconds === 0) {
if (minutes === 0) {
if (hours === 0) {
timerStarted = false;
clearInterval(intervalInstance);

document.getElementById("countdown").classList.remove("hide");
document.getElementById("countdown-number").textContent = "0";

document.body.classList.add("zero");

document.querySelector(".input-stopwatch").classList.add("hide");
document
.querySelector(".js-stopwatch-button")
.classList.add("hide");
} else {
hours--;
minutes = 59;
seconds = 59;
}
} else {
minutes--;
seconds = 59;
}
} else {
seconds--;
}
const styles = {
RUNNING: styleRunning,
PAUSED: stylePause,
STOPED: styleStop,
EDITING: styleEditing,
};

function applyStyles() {
resetStyles();
styles[TIMER_STATUS]?.();
}

if (hours === 0 && minutes === 0 && seconds <= 10) {
document.querySelector(".input-stopwatch").classList.add("hide");
document.querySelector(".js-stopwatch-button").classList.add("hide");
function styleCountdown(seconds = 0) {
document.querySelector(".input-stopwatch").classList.add("hide");
document.querySelector(".js-stopwatch-button").classList.add("hide");
document.getElementById("countdown").classList.remove("hide");
document.getElementById("countdown-number").textContent = seconds;
}

document.getElementById("countdown").classList.remove("hide");
document.getElementById("countdown-number").textContent = seconds;
}
function styleRunning() {
document.querySelector(".js-play-button").classList.add("press-start");
}

document.getElementById("sec").value = formatTime(seconds);
document.getElementById("min").value = formatTime(minutes);
document.getElementById("hour").value = formatTime(hours);
}
}, 1000);
function styleStop() {
document.querySelector(".js-stop-button").classList.add("press-stop");
}

function pause() {
timerStarted = false;
if (intervalInstance) {
clearInterval(intervalInstance);
intervalInstance = null;
}
function stylePause() {
document.querySelector(".js-pause-button").classList.add("press-pause");
}

function styleEditing() {
document.querySelector(".js-edit-button").classList.add("press-edit");
}
Loading