Skip to content

Commit 8d20af5

Browse files
committed
Update clock.js
1 parent e1a53ef commit 8d20af5

File tree

4 files changed

+65
-14
lines changed

4 files changed

+65
-14
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
planned for 2025-07-01
1313

14+
### Added
15+
16+
- [clock] Added option showSunNextEvent to show/hide next sun event.
17+
1418
### Changed
1519

1620
- [refactor] Simplify module loading process

modules/default/clock/clock.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Module.register("clock", {
2626
secondsColor: "#888888", // DEPRECATED, use CSS instead. Class "clock-second-digital" for digital clock, "clock-second" for analog clock.
2727

2828
showSunTimes: false,
29+
showSunNextEvent: true,
2930
showMoonTimes: false, // options: false, 'times' (rise/set), 'percent' (lit percent), 'phase' (current phase), or 'both' (percent & phase)
3031
lat: 47.630539,
3132
lon: -122.344147
@@ -171,21 +172,28 @@ Module.register("clock", {
171172
if (this.config.showSunTimes) {
172173
const sunTimes = SunCalc.getTimes(now, this.config.lat, this.config.lon);
173174
const isVisible = now.isBetween(sunTimes.sunrise, sunTimes.sunset);
174-
let nextEvent;
175-
if (now.isBefore(sunTimes.sunrise)) {
176-
nextEvent = sunTimes.sunrise;
177-
} else if (now.isBefore(sunTimes.sunset)) {
178-
nextEvent = sunTimes.sunset;
179-
} else {
180-
const tomorrowSunTimes = SunCalc.getTimes(now.clone().add(1, "day"), this.config.lat, this.config.lon);
181-
nextEvent = tomorrowSunTimes.sunrise;
175+
let sunWrapperInnerHTML = "";
176+
177+
if (this.config.showSunNextEvent) {
178+
let nextEvent;
179+
if (now.isBefore(sunTimes.sunrise)) {
180+
nextEvent = sunTimes.sunrise;
181+
} else if (now.isBefore(sunTimes.sunset)) {
182+
nextEvent = sunTimes.sunset;
183+
} else {
184+
const tomorrowSunTimes = SunCalc.getTimes(now.clone().add(1, "day"), this.config.lat, this.config.lon);
185+
nextEvent = tomorrowSunTimes.sunrise;
186+
}
187+
const untilNextEvent = moment.duration(moment(nextEvent).diff(now));
188+
const untilNextEventString = `${untilNextEvent.hours()}h ${untilNextEvent.minutes()}m`;
189+
190+
sunWrapperInnerHTML = `<span class="${isVisible ? "bright" : ""}"><i class="fas fa-sun" aria-hidden="true"></i> ${untilNextEventString}</span>`;
182191
}
183-
const untilNextEvent = moment.duration(moment(nextEvent).diff(now));
184-
const untilNextEventString = `${untilNextEvent.hours()}h ${untilNextEvent.minutes()}m`;
185-
sunWrapper.innerHTML
186-
= `<span class="${isVisible ? "bright" : ""}"><i class="fas fa-sun" aria-hidden="true"></i> ${untilNextEventString}</span>`
187-
+ `<span><i class="fas fa-arrow-up" aria-hidden="true"></i> ${formatTime(this.config, sunTimes.sunrise)}</span>`
188-
+ `<span><i class="fas fa-arrow-down" aria-hidden="true"></i> ${formatTime(this.config, sunTimes.sunset)}</span>`;
192+
193+
sunWrapperInnerHTML += `<span><i class="fas fa-arrow-up" aria-hidden="true"></i> ${formatTime(this.config, sunTimes.sunrise)}</span>`
194+
+ `<span><i class="fas fa-arrow-down" aria-hidden="true"></i> ${formatTime(this.config, sunTimes.sunset)}</span>`;
195+
196+
sunWrapper.innerHTML = sunWrapperInnerHTML;
189197
digitalWrapper.appendChild(sunWrapper);
190198
}
191199

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
let config = {
2+
address: "0.0.0.0",
3+
ipWhitelist: [],
4+
timeFormat: 12,
5+
6+
modules: [
7+
{
8+
module: "clock",
9+
position: "middle_center",
10+
config: {
11+
showSunTimes: true,
12+
showSunNextEvent: false
13+
}
14+
}
15+
]
16+
};
17+
18+
/*************** DO NOT EDIT THE LINE BELOW ***************/
19+
if (typeof module !== "undefined") {
20+
module.exports = config;
21+
}

tests/e2e/modules/clock_spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ describe("Clock module", () => {
9292
it("should show the sun times", async () => {
9393
const elem = await helpers.waitForElement(".clock .digital .sun");
9494
expect(elem).not.toBeNull();
95+
96+
const elem2 = await helpers.waitForElement(".clock .digital .sun .fas.fa-sun");
97+
expect(elem2).not.toBeNull();
9598
});
9699

97100
it("should show the moon times", async () => {
@@ -100,6 +103,21 @@ describe("Clock module", () => {
100103
});
101104
});
102105

106+
describe("with showSunNextEvent disabled", () => {
107+
beforeAll(async () => {
108+
await helpers.startApplication("tests/configs/modules/clock/clock_showSunNoEvent.js");
109+
await helpers.getDocument();
110+
});
111+
112+
it("should show the sun times", async () => {
113+
const elem = await helpers.waitForElement(".clock .digital .sun");
114+
expect(elem).not.toBeNull();
115+
116+
const elem2 = document.querySelector(".clock .digital .sun .fas.fa-sun");
117+
expect(elem2).toBeNull();
118+
});
119+
});
120+
103121
describe("with showWeek config enabled", () => {
104122
beforeAll(async () => {
105123
await helpers.startApplication("tests/configs/modules/clock/clock_showWeek.js");

0 commit comments

Comments
 (0)