Skip to content

Commit 35cd4d8

Browse files
authored
weather: add possibility to override njk's and css (#4051)
This is an approach for #2909 It adds 2 values to the config: ```js themeDir: "./", themeCustomScripts: [] ``` `themeDir` must be specified relative to the weather dir. Example config: ```js { module: "weather", position: "top_center", config: { weatherProvider: "openmeteo", type: "current", lat: 40.776676, lon: -73.971321, themeDir: "../../../modules/MyWeatherTemplate/", themeCustomScripts: [ "skycons.js", "weathertheme.js" ], } }, ``` The `themeDir` must contain the 4 files - current.njk - forecast.njk - hourly.njk - weather.css You can add more files (if needed) and add them to the `themeCustomScripts` so they are loaded as script. There are 2 methods inserted which are called if defined: - initWeatherTheme: For doing special things when starting the module - updateWeatherTheme: For doing special things before updating the dom I see this as a simple approach for overriding the default njk templates and css. I did already convert my [MMM-WeatherBridge](https://gitlab.com/khassel/MMM-WeatherBridge) into a template.
1 parent cb61aeb commit 35cd4d8

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

defaultmodules/weather/weather.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ Module.register("weather", {
4242
colored: false,
4343
absoluteDates: false,
4444
forecastDateFormat: "ddd", // format for forecast date display, e.g., "ddd" = Mon, "dddd" = Monday, "D MMM" = 18 Oct
45-
hourlyForecastIncrements: 1
45+
hourlyForecastIncrements: 1,
46+
themeDir: "./",
47+
themeCustomScripts: []
4648
},
4749

4850
// Module properties (all providers run server-side)
@@ -57,14 +59,18 @@ Module.register("weather", {
5759

5860
// Define required scripts.
5961
getStyles () {
60-
return ["font-awesome.css", "weather-icons.css", "weather.css"];
62+
return ["font-awesome.css", "weather-icons.css", `${this.config.themeDir}weather.css`];
6163
},
6264

6365
// Return the scripts that are necessary for the weather module.
6466
getScripts () {
6567
// Only load client-side dependencies for rendering
6668
// All providers run server-side via node_helper
67-
return ["moment.js", "weatherutils.js", "weatherobject.js", "suncalc.js"];
69+
const resArr = ["moment.js", "weatherutils.js", "weatherobject.js", "suncalc.js"];
70+
this.config.themeCustomScripts.forEach((element) => {
71+
resArr.push(`${this.config.themeDir}${element}`);
72+
});
73+
return resArr;
6874
},
6975

7076
// Override getHeader method.
@@ -98,6 +104,8 @@ Module.register("weather", {
98104
// All providers run server-side: generate unique instance ID and initialize via node_helper
99105
this.instanceId = `${this.identifier}_${Date.now()}`;
100106

107+
if (window.initWeatherTheme) window.initWeatherTheme(this);
108+
101109
Log.log(`[weather] Initializing server-side provider with instance ID: ${this.instanceId}`);
102110

103111
this.sendSocketNotification("INIT_WEATHER", {
@@ -211,15 +219,15 @@ Module.register("weather", {
211219
getTemplate () {
212220
switch (this.config.type.toLowerCase()) {
213221
case "current":
214-
return "current.njk";
222+
return `${this.config.themeDir}current.njk`;
215223
case "hourly":
216-
return "hourly.njk";
224+
return `${this.config.themeDir}hourly.njk`;
217225
case "daily":
218226
case "forecast":
219-
return "forecast.njk";
227+
return `${this.config.themeDir}forecast.njk`;
220228
//Make the invalid values use the "Loading..." from forecast
221229
default:
222-
return "forecast.njk";
230+
return `${this.config.themeDir}forecast.njk`;
223231
}
224232
},
225233

@@ -242,7 +250,11 @@ Module.register("weather", {
242250
// What to do when the weather provider has new information available?
243251
updateAvailable () {
244252
Log.log("[weather] New weather information available.");
245-
this.updateDom(300);
253+
if (window.updateWeatherTheme) {
254+
window.updateWeatherTheme(this);
255+
} else {
256+
this.updateDom(300);
257+
}
246258

247259
const currentWeather = this.currentWeatherObject;
248260

0 commit comments

Comments
 (0)