Files
MagicMirror/js/module.js
sam detweiler b742e839be Release 2.34.0 (#3999)
Thanks to: @Blackspirits, @Crazylegstoo, @jarnoml, @jboucly, @JHWelch,
@khassel, @KristjanESPERANTO, @rejas, @sdetweil, @xsorifc28

⚠️ This release needs nodejs version >=22.21.1 <23 || >=24

[Compare to previous Release
v2.33.0](https://github.com/MagicMirrorOrg/MagicMirror/compare/v2.33.0...develop)

[core]
Prepare Release 2.34.0
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3998)
dependency update + adjust Playwright setup + fix linter issue
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3994)
demo with gif (https://github.com/MagicMirrorOrg/MagicMirror/pull/3995)
[core] fix: allow browser globals in config files
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3992)
[core] fix: restore --ozone-platform=wayland flag for reliable Wayland
support (https://github.com/MagicMirrorOrg/MagicMirror/pull/3989)
[core] auto create release notes with every push on develop
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3985)
[core] chore: simplify Wayland start script
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3974)
[gitignore] restore the old Git behavior for the default modules
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3968)
[core] configure cspell to check default modules only and fix typos
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3955)
[gitignore] restoring the old Git behavior for the CSS directory
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3954)
feat(core): add server:watch script with automatic restart on file
changes (https://github.com/MagicMirrorOrg/MagicMirror/pull/3920)
[check_config] refactor: improve error handling
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3927)
refactor: replace express-ipfilter with lightweight custom middleware
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3917)
refactor: replace module-alias dependency with internal alias resolver
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3893)

[dependencies]

[chore] update dependencies and min. node version
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3986)
[core] bump dependencies into december
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3982)
Bump actions/checkout from 5 to 6
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3972)
Update deps, unpin parse5
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3934)
[core] Update deps and pin jsdom to v27.0.0
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3925)
chore: update dependencies
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3921)
update deps, exclude node v23
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3916)
remove eslint warnings, add npm publish process to Collaboration.md
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3913)
feat: add ESlint rule no-sparse-arrays for config check
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3911)
chore: bump dependencies into october
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3909)

[logging]

logger: add calling filename as prefix on server side
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3926)
[logger] Add prefixes to most Log messages
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3923)

[modules/alert]
Add new pt and pt-BR translations for Alert module and update global PT
strings (https://github.com/MagicMirrorOrg/MagicMirror/pull/3965)

[modules/calendar]
add checksum to test whether calendar event list changed
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3988)
[calendar] fix: prevent excessive fetching on client reload and refactor
calendarfetcherutils.js
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3976)
[calendar] refactor: migrate CalendarFetcher to ES6 class and improve
error handling (https://github.com/MagicMirrorOrg/MagicMirror/pull/3959)
[calendar] Show repeatingCountTitle only if yearDiff > 0
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3949)
[tests] suppress debug logs in CI environment + improve calendar symbol
test stability (https://github.com/MagicMirrorOrg/MagicMirror/pull/3941)
[calendar] chore: remove requiresVersion: "2.1.0"
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3932)
[calendar] test: remove "Recurring event per timezone" test
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3929)

[modules/compliments]
[compliments] refactor: optimize loadComplimentFile method and add unit
tests (https://github.com/MagicMirrorOrg/MagicMirror/pull/3969)
fix: set compliments remote file minimum delay to 15 minutes
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3970)
[compliments] fix: duplicate query param "?" in compliments module
refresh url (https://github.com/MagicMirrorOrg/MagicMirror/pull/3967)

[modules/newsfeed]
[newsfeed] fix header layout issue
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3946)

[modules/weather]
[weatherprovider] update subclass language use override
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3914)
[weather] fix wind-icon not showing in pirateweather
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3957)
[weather] add error handling to weather fetch functions, including cors
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3791)
remove deprecated ukmetoffice datapoint provider, cleanup .gitignore
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3952)
fixes problems with daylight-saving-time in weather provider openmeteo
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3931)
Fix for envcanada Provider to use updated Env Canada URL
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3919)
[weather] feat: add configurable forecast date format option
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3918)

[testing]
testing: update "Enforce Pull-Request Rules" workflow
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3987)
[core] refactor: replace XMLHttpRequest with fetch and migrate e2e tests
to Playwright (https://github.com/MagicMirrorOrg/MagicMirror/pull/3950)
[test] replace node-libgpiod with serialport in electron-rebuild
workflow (https://github.com/MagicMirrorOrg/MagicMirror/pull/3945)
[ci] Add concurrency to automated tests workflow to cancel outdated runs
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3943)
[tests] migrate from jest to vitest
(https://github.com/MagicMirrorOrg/MagicMirror/pull/3940)

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Karsten Hassel <hassel@gmx.de>
Co-authored-by: Kristjan ESPERANTO <35647502+KristjanESPERANTO@users.noreply.github.com>
Co-authored-by: Ryan Williams <65094007+ryan-d-williams@users.noreply.github.com>
Co-authored-by: Veeck <github@veeck.de>
Co-authored-by: Bugsounet - Cédric <github@bugsounet.fr>
Co-authored-by: Marc Landis <dirk.rettschlag@gmail.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: HeikoGr <20295490+HeikoGr@users.noreply.github.com>
Co-authored-by: Pedro Lamas <pedrolamas@gmail.com>
Co-authored-by: veeck <gitkraken@veeck.de>
Co-authored-by: Magnus <34011212+MagMar94@users.noreply.github.com>
Co-authored-by: Ikko Eltociear Ashimine <eltociear@gmail.com>
Co-authored-by: DevIncomin <56730075+Developer-Incoming@users.noreply.github.com>
Co-authored-by: Nathan <n8nyoung@gmail.com>
Co-authored-by: mixasgr <mixasgr@users.noreply.github.com>
Co-authored-by: Savvas Adamtziloglou <savvas-gr@greeklug.gr>
Co-authored-by: Konstantinos <geraki@gmail.com>
Co-authored-by: OWL4C <124401812+OWL4C@users.noreply.github.com>
Co-authored-by: BugHaver <43462320+bughaver@users.noreply.github.com>
Co-authored-by: BugHaver <43462320+lsaadeh@users.noreply.github.com>
Co-authored-by: Koen Konst <koenspero@gmail.com>
Co-authored-by: Koen Konst <c.h.konst@avisi.nl>
Co-authored-by: dathbe <github@beffa.us>
Co-authored-by: Marcel <m-idler@users.noreply.github.com>
Co-authored-by: Kevin G. <crazylegstoo@gmail.com>
Co-authored-by: Jboucly <33218155+jboucly@users.noreply.github.com>
Co-authored-by: Jboucly <contact@jboucly.fr>
Co-authored-by: Jarno <54169345+jarnoml@users.noreply.github.com>
Co-authored-by: Jordan Welch <JordanHWelch@gmail.com>
Co-authored-by: Blackspirits <blackspirits@gmail.com>
Co-authored-by: Samed Ozdemir <samed@xsor.io>
2026-01-01 08:45:36 -06:00

515 lines
14 KiB
JavaScript

/* global Class, cloneObject, Loader, MMSocket, nunjucks, Translator */
/*
* Module Blueprint.
* @typedef {Object} Module
*/
const Module = Class.extend({
/**
*********************************************************
* All methods (and properties) below can be overridden. *
*********************************************************
*/
// Set the minimum MagicMirror² module version for this module.
requiresVersion: "2.0.0",
// Module config defaults.
defaults: {},
// Timer reference used for showHide animation callbacks.
showHideTimer: null,
/*
* Array to store lockStrings. These strings are used to lock
* visibility when hiding and showing module.
*/
lockStrings: [],
/*
* Storage of the nunjucks Environment,
* This should not be referenced directly.
* Use the nunjucksEnvironment() to get it.
*/
_nunjucksEnvironment: null,
/**
* Called when the module is instantiated.
*/
init () {
},
/**
* Called when the module is started.
*/
async start () {
Log.info(`Starting module: ${this.name}`);
},
/**
* Returns a list of scripts the module requires to be loaded.
* @returns {string[]} An array with filenames.
*/
getScripts () {
return [];
},
/**
* Returns a list of stylesheets the module requires to be loaded.
* @returns {string[]} An array with filenames.
*/
getStyles () {
return [];
},
/**
* Returns a map of translation files the module requires to be loaded.
*
* return Map<String, String> -
* @returns {Map} A map with langKeys and filenames.
*/
getTranslations () {
return false;
},
/**
* Generates the dom which needs to be displayed. This method is called by the MagicMirror² core.
* This method can to be overridden if the module wants to display info on the mirror.
* Alternatively, the getTemplate method could be overridden.
* @returns {HTMLElement|Promise} The dom or a promise with the dom to display.
*/
getDom () {
return new Promise((resolve) => {
const div = document.createElement("div");
const template = this.getTemplate();
const templateData = this.getTemplateData();
// Check to see if we need to render a template string or a file.
if ((/^.*((\.html)|(\.njk))$/).test(template)) {
// the template is a filename
this.nunjucksEnvironment().render(template, templateData, function (err, res) {
if (err) {
Log.error(err);
}
div.innerHTML = res;
resolve(div);
});
} else {
// the template is a template string.
div.innerHTML = this.nunjucksEnvironment().renderString(template, templateData);
resolve(div);
}
});
},
/**
* Generates the header string which needs to be displayed if a user has a header configured for this module.
* This method is called by the MagicMirror² core, but only if the user has configured a default header for the module.
* This method needs to be overridden if the module wants to display modified headers on the mirror.
* @returns {string} The header to display above the header.
*/
getHeader () {
return this.data.header;
},
/**
* Returns the template for the module which is used by the default getDom implementation.
* This method needs to be overridden if the module wants to use a template.
* It can either return a template string, or a template filename.
* If the string ends with '.html' it's considered a file from within the module's folder.
* @returns {string} The template string of filename.
*/
getTemplate () {
return `<div class="normal">${this.name}</div><div class="small dimmed">${this.identifier}</div>`;
},
/**
* Returns the data to be used in the template.
* This method needs to be overridden if the module wants to use a custom data.
* @returns {object} The data for the template
*/
getTemplateData () {
return {};
},
/**
* Called by the MagicMirror² core when a notification arrives.
* @param {string} notification The identifier of the notification.
* @param {object} payload The payload of the notification.
* @param {Module} sender The module that sent the notification.
*/
notificationReceived (notification, payload, sender) {
if (sender) {
Log.debug(`${this.name} received a module notification: ${notification} from sender: ${sender.name}`);
} else {
Log.debug(`${this.name} received a system notification: ${notification}`);
}
},
/**
* Returns the nunjucks environment for the current module.
* The environment is checked in the _nunjucksEnvironment instance variable.
* @returns {object} The Nunjucks Environment
*/
nunjucksEnvironment () {
if (this._nunjucksEnvironment !== null) {
return this._nunjucksEnvironment;
}
this._nunjucksEnvironment = new nunjucks.Environment(new nunjucks.WebLoader(this.file(""), { async: true }), {
trimBlocks: true,
lstripBlocks: true
});
this._nunjucksEnvironment.addFilter("translate", (str, variables) => {
return nunjucks.runtime.markSafe(this.translate(str, variables));
});
return this._nunjucksEnvironment;
},
/**
* Called when a socket notification arrives.
* @param {string} notification The identifier of the notification.
* @param {object} payload The payload of the notification.
*/
socketNotificationReceived (notification, payload) {
Log.log(`${this.name} received a socket notification: ${notification} - Payload: ${payload}`);
},
/**
* Called when the module is hidden.
*/
suspend () {
Log.log(`${this.name} is suspended.`);
},
/**
* Called when the module is shown.
*/
resume () {
Log.log(`${this.name} is resumed.`);
},
/**
***********************************************
* The methods below should not be overridden. *
***********************************************
*/
/**
* Set the module data.
* @param {object} data The module data
*/
setData (data) {
this.data = data;
this.name = data.name;
this.identifier = data.identifier;
this.hidden = false;
this.hasAnimateIn = false;
this.hasAnimateOut = false;
this.setConfig(data.config, data.configDeepMerge);
},
/**
* Set the module config and combine it with the module defaults.
* @param {object} config The combined module config.
* @param {boolean} deep Merge module config in deep.
*/
setConfig (config, deep) {
this.config = deep ? configMerge({}, this.defaults, config) : Object.assign({}, this.defaults, config);
},
/**
* Returns a socket object. If it doesn't exist, it's created.
* It also registers the notification callback.
* @returns {MMSocket} a socket object
*/
socket () {
if (typeof this._socket === "undefined") {
this._socket = new MMSocket(this.name);
}
this._socket.setNotificationCallback((notification, payload) => {
this.socketNotificationReceived(notification, payload);
});
return this._socket;
},
/**
* Retrieve the path to a module file.
* @param {string} file Filename
* @returns {string} the file path
*/
file (file) {
return `${this.data.path}/${file}`.replace("//", "/");
},
/**
* Load all required stylesheets by requesting the MM object to load the files.
* @returns {Promise<void>}
*/
loadStyles () {
return this.loadDependencies("getStyles");
},
/**
* Load all required scripts by requesting the MM object to load the files.
* @returns {Promise<void>}
*/
loadScripts () {
return this.loadDependencies("getScripts");
},
/**
* Helper method to load all dependencies.
* @param {string} funcName Function name to call to get scripts or styles.
* @returns {Promise<void>}
*/
async loadDependencies (funcName) {
let dependencies = this[funcName]();
const loadNextDependency = async () => {
if (dependencies.length > 0) {
const nextDependency = dependencies[0];
await Loader.loadFileForModule(nextDependency, this);
dependencies = dependencies.slice(1);
await loadNextDependency();
} else {
return Promise.resolve();
}
};
await loadNextDependency();
},
/**
* Load all translations.
* @returns {Promise<void>}
*/
async loadTranslations () {
const translations = this.getTranslations() || {};
const language = config.language.toLowerCase();
const languages = Object.keys(translations);
const fallbackLanguage = languages[0];
if (languages.length === 0) {
return;
}
const translationFile = translations[language];
const translationsFallbackFile = translations[fallbackLanguage];
if (!translationFile) {
return Translator.load(this, translationsFallbackFile, true);
}
await Translator.load(this, translationFile, false);
if (translationFile !== translationsFallbackFile) {
return Translator.load(this, translationsFallbackFile, true);
}
},
/**
* Request the translation for a given key with optional variables and default value.
* @param {string} key The key of the string to translate
* @param {string|object} [defaultValueOrVariables] The default value or variables for translating.
* @param {string} [defaultValue] The default value with variables.
* @returns {string} the translated key
*/
translate (key, defaultValueOrVariables, defaultValue) {
if (typeof defaultValueOrVariables === "object") {
return Translator.translate(this, key, defaultValueOrVariables) || defaultValue || "";
}
return Translator.translate(this, key) || defaultValueOrVariables || "";
},
/**
* Request an (animated) update of the module.
* @param {number|object} [updateOptions] The speed of the animation or object with for updateOptions (speed/animates)
*/
updateDom (updateOptions) {
MM.updateDom(this, updateOptions);
},
/**
* Send a notification to all modules.
* @param {string} notification The identifier of the notification.
* @param {object} payload The payload of the notification.
*/
sendNotification (notification, payload) {
MM.sendNotification(notification, payload, this);
},
/**
* Send a socket notification to the node helper.
* @param {string} notification The identifier of the notification.
* @param {object} payload The payload of the notification.
*/
sendSocketNotification (notification, payload) {
this.socket().sendNotification(notification, payload);
},
/**
* Hide this module.
* @param {number} speed The speed of the hide animation.
* @param {Promise} callback Called when the animation is done.
* @param {object} [options] Optional settings for the hide method.
*/
hide (speed, callback, options = {}) {
let usedCallback = callback || function () {};
let usedOptions = options;
if (typeof callback === "object") {
Log.error("Parameter mismatch in module.hide: callback is not an optional parameter!");
usedOptions = callback;
usedCallback = function () {};
}
MM.hideModule(
this,
speed,
() => {
this.suspend();
usedCallback();
},
usedOptions
);
},
/**
* Show this module.
* @param {number} speed The speed of the show animation.
* @param {Promise} callback Called when the animation is done.
* @param {object} [options] Optional settings for the show method.
*/
show (speed, callback, options) {
let usedCallback = callback || function () {};
let usedOptions = options;
if (typeof callback === "object") {
Log.error("Parameter mismatch in module.show: callback is not an optional parameter!");
usedOptions = callback;
usedCallback = function () {};
}
MM.showModule(
this,
speed,
() => {
this.resume();
usedCallback();
},
usedOptions
);
}
});
/**
* Merging MagicMirror² (or other) default/config script by `@bugsounet`
* Merge 2 objects or/with array
*
* Usage:
* -------
* this.config = configMerge({}, this.defaults, this.config)
* -------
* arg1: initial object
* arg2: config model
* arg3: config to merge
* -------
* why using it ?
* Object.assign() function don't to all job
* it don't merge all thing in deep
* -> object in object and array is not merging
* -------
*
* Todo: idea of Mich determinate what do you want to merge or not
* @param {object} result the initial object
* @returns {object} the merged config
*/
function configMerge (result) {
const stack = Array.prototype.slice.call(arguments, 1);
let item, key;
while (stack.length) {
item = stack.shift();
for (key in item) {
if (item.hasOwnProperty(key)) {
if (typeof result[key] === "object" && result[key] && Object.prototype.toString.call(result[key]) !== "[object Array]") {
if (typeof item[key] === "object" && item[key] !== null) {
result[key] = configMerge({}, result[key], item[key]);
} else {
result[key] = item[key];
}
} else {
result[key] = item[key];
}
}
}
}
return result;
}
Module.definitions = {};
Module.create = function (name) {
// Make sure module definition is available.
if (!Module.definitions[name]) {
return;
}
const moduleDefinition = Module.definitions[name];
const clonedDefinition = cloneObject(moduleDefinition);
// Note that we clone the definition. Otherwise the objects are shared, which gives problems.
const ModuleClass = Module.extend(clonedDefinition);
return new ModuleClass();
};
Module.register = function (name, moduleDefinition) {
if (moduleDefinition.requiresVersion) {
Log.log(`Check MagicMirror² version for module '${name}' - Minimum version: ${moduleDefinition.requiresVersion} - Current version: ${window.mmVersion}`);
if (cmpVersions(window.mmVersion, moduleDefinition.requiresVersion) >= 0) {
Log.log("Version is ok!");
} else {
Log.warn(`Version is incorrect. Skip module: '${name}'`);
return;
}
}
Log.log(`Module registered: ${name}`);
Module.definitions[name] = moduleDefinition;
};
window.Module = Module;
/**
* Compare two semantic version numbers and return the difference.
* @param {string} a Version number a.
* @param {string} b Version number b.
* @returns {number} A positive number if a is larger than b, a negative
* number if a is smaller and 0 if they are the same
*/
function cmpVersions (a, b) {
const regExStrip0 = /(\.0+)+$/;
const segmentsA = a.replace(regExStrip0, "").split(".");
const segmentsB = b.replace(regExStrip0, "").split(".");
const l = Math.min(segmentsA.length, segmentsB.length);
for (let i = 0; i < l; i++) {
let diff = parseInt(segmentsA[i], 10) - parseInt(segmentsB[i], 10);
if (diff) {
return diff;
}
}
return segmentsA.length - segmentsB.length;
}