Skip to content

Commit

Permalink
Merge pull request #11 from Silvyjson/play-pause-function
Browse files Browse the repository at this point in the history
Created a  Restart logic
  • Loading branch information
Archlight001 authored Dec 30, 2023
2 parents b727117 + 4a38325 commit 6e734f9
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 100 deletions.
File renamed without changes
File renamed without changes
7 changes: 6 additions & 1 deletion index.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
--main-color: #00C2FF;
--circle-color: #d3d3d3;
--input-color: #f9f9f9;
--hover-bg-color: #00C2FF3D;
--box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}

Expand Down Expand Up @@ -69,6 +70,10 @@ body {
background-color: var(--main-color);
}

.action-bar--icon:hover{
background-color: var(--hover-bg-color);
}

.img2 {
cursor: pointer;
width: 20px;
Expand Down Expand Up @@ -97,7 +102,7 @@ body {
}

.sand-timer {
background-color: #00C2FF3D;
background-color: var(--hover-bg-color);
}

.img3 {
Expand Down
10 changes: 5 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ <h1 class="time" id="time-text">00:00</h1>
</div>
</div>
<div class="action-bar">
<div class="action-bar--container">
<img src="./image/heroicons-solid_trash.png" alt="trash-icon" class="img2" onclick="toggleRestart()">
<div class="action-bar--container action-bar--icon">
<img src="./image/heroicons-solid_trash.png" alt="trash-icon" class="img2" onclick="toggleDelete()">
</div>
<div class="action-bar--container pause-icon">
<img src="./image/Group 21.png" alt="pause-icon" class="img2">
<img src="./image/pause-icon.png" alt="pause-icon" class="img2" id="pause" onclick="togglePlay()">
</div>
<div class="action-bar--container">
<img src="./image/Group 629424.png" alt="refreash-icon" class="img2">
<div class="action-bar--container action-bar--icon">
<img src="./image/Group 629424.png" alt="refreash-icon" class="img2" onclick="toggleRefresh()">
</div>
</div>
</section>
Expand Down
242 changes: 148 additions & 94 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,131 +1,185 @@
const listElement = document.getElementById("menu");
const minuteInput = document.getElementById('minuteInput');
const secondInput = document.getElementById('secondInput');
const startButton = document.getElementById('startButton');
const minuteInput = document.getElementById("minuteInput");
const secondInput = document.getElementById("secondInput");
const startButton = document.getElementById("startButton");
const body = document.body;
const statusBarImg1 = document.getElementById("status-bar--img1");
const statusBarImg2 = document.getElementById("status-bar--img2");
let intervalValue = null;
let remainingSeconds = 0;
const listElement = document.getElementById("menu");
const pauseIcon = document.getElementById("pause");

const toggleList = () => {

if (listElement.style.display === "block") {
listElement.style.display = "none";
}

else {
listElement.style.display = "block";
}
}

function hideMenuList() {
if (listElement.style.display === "block") {
listElement.style.display = "none";
} else {
listElement.style.display = "block";
}
};

document.body.addEventListener('click', function (event) {
function hideMenuList() {
listElement.style.display = "none";
}

if (!event.target.closest('.menu') && !event.target.closest('.menu-list')) {
hideMenuList();
}
document.body.addEventListener("click", function (event) {
if (!event.target.closest(".menu") && !event.target.closest(".menu-list")) {
hideMenuList();
}
});


function toggleMode() {

const body = document.body;
body.classList.toggle('dark_mode');

listElement.innerHTML = 'Switch to light mode'

const statusBarImg1 = document.getElementById("status-bar--img1");
const statusBarImg2 = document.getElementById("status-bar--img2");

if (statusBarImg1.style.display === "none") {
statusBarImg1.style.display = "block";
statusBarImg2.style.display = "none";
}

else {
statusBarImg1.style.display = "none";
statusBarImg2.style.display = "block";
}

hideMenuList();
body.classList.toggle("dark_mode");

if (body.classList.contains("dark_mode")) {
listElement.innerHTML = "Switch to light mode";
localStorage.setItem("currentMode", "darkMode");
} else {
listElement.innerHTML = "Switch to dark mode";
localStorage.setItem("currentMode", "lightMode");
}

if (statusBarImg1.style.display === "none") {
statusBarImg1.style.display = "block";
statusBarImg2.style.display = "none";
} else {
statusBarImg1.style.display = "none";
statusBarImg2.style.display = "block";
}

hideMenuList();
}

function toggleStart() {
document.addEventListener("DOMContentLoaded", function () {
const storedMode = localStorage.getItem("currentMode");

if (storedMode === "darkMode") {
body.classList.toggle("dark_mode");
listElement.innerHTML = "Switch to light mode";
statusBarImg1.style.display = "none";
statusBarImg2.style.display = "block";
} else {
listElement.innerHTML = "Switch to dark mode";
statusBarImg1.style.display = "block";
statusBarImg2.style.display = "none";
}
});

toggleRestart();
function toggleStart() {
toggleDelete();

const minutes = parseInt(minuteInput.value) || 0;
const seconds = parseInt(secondInput.value) || 0;
totalTimer();

const totalSeconds = minutes * 60 + seconds;
updateTimer(totalSeconds);
playTimer();
}

let remainingSeconds = totalSeconds;
const totalTimer = () => {
const minutes = parseInt(minuteInput.value) || 0;
const seconds = parseInt(secondInput.value) || 0;

if (totalSeconds > 0) {
let totalSeconds = minutes * 60 + seconds;

intervalValue = setInterval(() => {
remainingSeconds--;
remainingSeconds = totalSeconds;

updateTimer(remainingSeconds);
if (remainingSeconds === 0) {
clearInterval(intervalValue);
}
}, 1000);
}
}
updateTimer(totalSeconds);
};

const updateTimer = (seconds = 0) => {
const timeTextElement = document.getElementById("time-text");

const timeTextElement = document.getElementById("time-text");

timeTextElement.innerHTML = `${Math.floor(seconds / 60)}:${seconds % 60}`;
timeTextElement.innerHTML = `${Math.floor(seconds / 60)}:${seconds % 60}`;
};

const toggleDelete = () => {
const timerStart = document.getElementById("timer--start");
const timerCircle = document.getElementById("timer-count");

const toggleRestart = () => {
if (timerStart.style.display === "none") {
timerStart.style.display = "block";
timerCircle.style.display = "none";
} else {
timerStart.style.display = "none";
timerCircle.style.display = "block";
}

const timerStart = document.getElementById("timer--start");
const timerCircle = document.getElementById("timer-count");
clearInterval(intervalValue);
};

if (timerStart.style.display === "none") {
timerStart.style.display = "block";
timerCircle.style.display = "none";
} else {
timerStart.style.display = "none";
timerCircle.style.display = "block";
}
const digitInput = (event, inputElement) => {
const allowedKeys = [
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"Backspace",
"ArrowLeft",
"ArrowRight",
];
const exceptionKeys = ["Backspace", "ArrowLeft", "ArrowRight"];

if (
!allowedKeys.includes(event.key) ||
(!exceptionKeys.includes(event.key) && inputElement.value.length === 2)
) {
event.preventDefault();
}
if (minuteInput.value.trim() !== "" || secondInput.value.trim() !== "") {
startButton.removeAttribute("disabled");
} else {
startButton.setAttribute("disabled", "true");
}
};

clearInterval(intervalValue);
const inputHandler = (inputElement) => {
inputElement.addEventListener("keydown", function (event) {
digitInput(event, inputElement);
});

}
inputElement.addEventListener("input", function (event) {
digitInput(event, inputElement);
});
};

const digitInput = (event, inputElement) => {
const allowedKeys = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'Backspace', 'ArrowLeft', 'ArrowRight'];
const exceptionKeys = ["Backspace", "ArrowLeft", "ArrowRight"];
inputHandler(minuteInput);
inputHandler(secondInput);

if (!allowedKeys.includes(event.key) || (!exceptionKeys.includes(event.key) && inputElement.value.length === 2)) {
event.preventDefault();
}
const playTimer = () => {
if (remainingSeconds > 0) {
intervalValue = setInterval(() => {
remainingSeconds--;

updateTimer(remainingSeconds);
if (remainingSeconds === 0) {
pauseTimer();
}
}, 1000);
}
};

if (minuteInput.value.trim() !== '' || secondInput.value.trim() !== '') {
startButton.removeAttribute('disabled');
}
const pauseTimer = () => {
clearInterval(intervalValue);
intervalValue = null;
};

else {
startButton.setAttribute('disabled', 'true');
}
function togglePlay() {
if (pauseIcon.src.endsWith("pause-icon.png")) {
pauseIcon.src = "./image/play-icon.png";
pauseTimer();
} else {
pauseIcon.src = "./image/pause-icon.png";
playTimer();
}
}

const inputHandler = (inputElement) => {
inputElement.addEventListener('keydown', function (event) {
digitInput(event, inputElement);
});
function toggleRefresh() {
pauseTimer();

inputElement.addEventListener('input', function (event) {
digitInput(event, inputElement);
});
totalTimer();
pauseIcon.src = "./image/pause-icon.png";
playTimer();
}

inputHandler(minuteInput);
inputHandler(secondInput);

0 comments on commit 6e734f9

Please sign in to comment.