Skip to content
Open
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
6 changes: 3 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1382,12 +1382,12 @@ <h1>Material You New Tab</h1>
<div class="sectionInner">
<div class="ttcont" id="clockHidden">
<div class="texts">
<div class="bigText" id="hideClockBox">Hide Clock</div>
<div class="bigText" id="hideClockBox">Show Clock</div>
<div class="infoText" id="hideClockBoxInfo">Show or hide the clock and date
</div>
</div>
<label class="switch">
<input id="hideClock" type="checkbox">
<input id="showClock" type="checkbox">
<span class="toggle"></span>
</label>
</div>
Expand Down Expand Up @@ -1950,4 +1950,4 @@ <h1>Material You New Tab</h1>

</body>

</html>
</html>
116 changes: 69 additions & 47 deletions scripts/clock.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ let lastGreetingString = null;

// ---------------------- Hiding clock func ----------------------
// Select elements
const leftDiv = document.getElementById("leftDiv");
const rightDiv = document.getElementById("rightDiv");
const hideClockCheckbox = document.getElementById("hideClock");
const clockHidden = document.getElementById("clockHidden");
const clockOptions = document.querySelector(".clockOptions");
const leftDiv = document.getElementById("leftDiv");
const rightDiv = document.getElementById("rightDiv");
const showClockCheckbox = document.getElementById("showClock");
const clockHidden = document.getElementById("clockHidden");
const clockOptions = document.querySelector(".clockOptions");

function toggleHideState(isHidden) {
clockOptions.classList.toggle("not-applicable", isHidden);
Expand All @@ -37,42 +37,57 @@ function applyClockState(isHidden) {
rightDiv.classList.toggle("clock-padding-adjusted", isHidden);
}

function handleClockVisibility() {
if (window.matchMedia("(max-width: 500px)").matches) {
initializeClock();

clockOptions.classList.remove("not-applicable");
rightDiv.classList.remove("clock-padding-adjusted");
}
else {
// Retrieve saved state from localStorage
const isClockHidden = localStorage.getItem("hideClockVisible") === "true";
hideClockCheckbox.checked = isClockHidden;

// Apply initial state
applyClockState(isClockHidden);
toggleHideState(isClockHidden);

if (!isClockHidden) {
initializeClock();
}

hideClockCheckbox.addEventListener("change", function () {
const isChecked = hideClockCheckbox.checked;
localStorage.setItem("hideClockVisible", isChecked);
applyClockState(isChecked);
toggleHideState(isChecked);

if (!isChecked) {
initializeClock();
}
});
}
}

handleClockVisibility();
// Update on window resize
window.addEventListener("resize", handleClockVisibility);
function handleClockVisibility() {
if (localStorage.getItem("showClock") === null) {
localStorage.setItem("showClock", "true");
}

const isClockVisible = localStorage.getItem("showClock") !== "false";

if (window.matchMedia("(max-width: 500px)").matches) {
showClockCheckbox.checked = isClockVisible;
applyClockState(!isClockVisible);
toggleHideState(!isClockVisible);

if (isClockVisible) {
initializeClock();
}

clockOptions.classList.remove("not-applicable");
rightDiv.classList.remove("clock-padding-adjusted");
}
else {
showClockCheckbox.checked = isClockVisible;

// Apply initial state
applyClockState(!isClockVisible);
toggleHideState(!isClockVisible);

if (isClockVisible) {
initializeClock();
}
}
}

showClockCheckbox.addEventListener("change", function () {
const isChecked = showClockCheckbox.checked;
localStorage.setItem("showClock", isChecked);
applyClockState(!isChecked);
toggleHideState(!isChecked);

if (window.matchMedia("(max-width: 500px)").matches) {
clockOptions.classList.remove("not-applicable");
rightDiv.classList.remove("clock-padding-adjusted");
}

if (isChecked) {
initializeClock();
}
});

handleClockVisibility();
// Update on window resize
window.addEventListener("resize", handleClockVisibility);

// ---------------------- Clock func ----------------------
async function initializeClock() {
Expand Down Expand Up @@ -465,12 +480,19 @@ async function initializeClock() {
}
});

function displayClock() {
const analogClock = document.getElementById("analogClock");
const digitalClock = document.getElementById("digitalClock");

if (clocktype === "analog") {
analogClock.style.display = "block"; // Show the analog clock
function displayClock() {
const analogClock = document.getElementById("analogClock");
const digitalClock = document.getElementById("digitalClock");
const showClock = localStorage.getItem("showClock") !== "false";

if (!showClock) {
analogClock.style.display = "none";
digitalClock.style.display = "none";
return;
}

if (clocktype === "analog") {
analogClock.style.display = "block"; // Show the analog clock
digitalClock.style.display = "none"; // Hide the digital clock
} else if (clocktype === "digital") {
digitalClock.style.display = "block"; // Show the digital clock
Expand Down