Files
MagicMirror/js/loader.js
T

279 lines
7.9 KiB
JavaScript
Raw Normal View History

2020-05-05 14:55:15 +02:00
/* global defaultModules, vendor */
2020-04-21 10:41:21 +02:00
2021-03-23 21:41:21 +01:00
const Loader = (function () {
2024-01-01 15:38:08 +01:00
2019-06-05 09:46:59 +02:00
/* Create helper variables */
2016-03-24 17:19:32 +01:00
2021-03-23 21:41:21 +01:00
const loadedModuleFiles = [];
const loadedFiles = [];
const moduleObjects = [];
2016-03-24 17:19:32 +01:00
/* Private Methods */
/**
* Retrieve object of env variables.
* @returns {object} with key: values as assembled in js/server_functions.js
*/
const getEnvVars = async function () {
2024-11-02 08:22:27 +01:00
const res = await fetch(`${location.protocol}//${location.host}${config.basePath}env`);
return JSON.parse(await res.text());
};
2020-08-01 16:38:32 +02:00
/**
2023-04-04 20:44:32 +02:00
* Loops through all modules and requests start for every module.
2016-03-24 17:19:32 +01:00
*/
2023-04-04 20:44:32 +02:00
const startModules = async function () {
const modulePromises = [];
for (const module of moduleObjects) {
try {
modulePromises.push(module.start());
} catch (error) {
Log.error(`Error when starting node_helper for module ${module.name}:`);
Log.error(error);
2016-03-31 13:05:23 +02:00
}
2023-04-04 20:44:32 +02:00
}
2016-03-31 13:05:23 +02:00
2023-04-04 20:44:32 +02:00
const results = await Promise.allSettled(modulePromises);
2016-03-24 17:19:32 +01:00
2023-04-04 20:44:32 +02:00
// Log errors that happened during async node_helper startup
results.forEach((result) => {
if (result.status === "rejected") {
Log.error(result.reason);
}
});
2016-03-24 17:19:32 +01:00
2019-06-05 09:46:59 +02:00
// Notify core of loaded modules.
2016-03-24 17:19:32 +01:00
MM.modulesStarted(moduleObjects);
2021-03-07 11:23:52 +00:00
// Starting modules also hides any modules that have requested to be initially hidden
2021-03-23 21:41:21 +01:00
for (const thisModule of moduleObjects) {
2021-03-07 11:23:52 +00:00
if (thisModule.data.hiddenOnStartup) {
2023-04-04 20:44:32 +02:00
Log.info(`Initially hiding ${thisModule.name}`);
2021-03-07 11:23:52 +00:00
thisModule.hide();
}
}
2016-03-24 17:19:32 +01:00
};
2020-08-01 16:38:32 +02:00
/**
2016-03-24 17:19:32 +01:00
* Retrieve list of all modules.
2020-08-01 16:38:32 +02:00
* @returns {object[]} module data as configured in config
2016-03-24 17:19:32 +01:00
*/
2021-03-23 21:41:21 +01:00
const getAllModules = function () {
const AllModules = config.modules.filter((module) => (module.module !== undefined) && (MM.getAvailableModulePositions.indexOf(module.position) > -1 || typeof (module.position) === "undefined"));
return AllModules;
2016-03-24 17:19:32 +01:00
};
2020-08-01 16:38:32 +02:00
/**
2016-03-24 17:19:32 +01:00
* Generate array with module information including module paths.
2020-08-01 16:38:32 +02:00
* @returns {object[]} Module information.
2016-03-24 17:19:32 +01:00
*/
const getModuleData = async function () {
2021-03-23 21:41:21 +01:00
const modules = getAllModules();
const moduleFiles = [];
const envVars = await getEnvVars();
2016-03-24 17:19:32 +01:00
2021-03-23 21:41:21 +01:00
modules.forEach(function (moduleData, index) {
const module = moduleData.module;
2016-03-24 17:19:32 +01:00
2021-03-23 21:41:21 +01:00
const elements = module.split("/");
const moduleName = elements[elements.length - 1];
let moduleFolder = `${envVars.modulesDir}/${module}`;
2016-04-03 19:52:13 +02:00
2016-04-01 17:35:29 +02:00
if (defaultModules.indexOf(moduleName) !== -1) {
const defaultModuleFolder = `modules/default/${module}`;
if (window.name !== "jsdom") {
moduleFolder = defaultModuleFolder;
} else {
// running in Jest, allow defaultModules placed under moduleDir for testing
if (envVars.modulesDir === "modules") {
moduleFolder = defaultModuleFolder;
}
}
2016-04-01 17:35:29 +02:00
}
if (moduleData.disabled === true) {
2021-03-23 21:41:21 +01:00
return;
}
2016-03-24 17:19:32 +01:00
moduleFiles.push({
2021-03-23 21:41:21 +01:00
index: index,
2023-04-04 20:44:32 +02:00
identifier: `module_${index}_${module}`,
2016-04-01 17:35:29 +02:00
name: moduleName,
2023-04-04 20:44:32 +02:00
path: `${moduleFolder}/`,
file: `${moduleName}.js`,
2016-03-24 17:19:32 +01:00
position: moduleData.position,
2023-10-01 20:13:41 +02:00
animateIn: moduleData.animateIn,
animateOut: moduleData.animateOut,
hiddenOnStartup: moduleData.hiddenOnStartup,
2016-03-29 13:28:15 +02:00
header: moduleData.header,
2020-09-22 00:04:05 +02:00
configDeepMerge: typeof moduleData.configDeepMerge === "boolean" ? moduleData.configDeepMerge : false,
2016-03-31 17:05:35 +02:00
config: moduleData.config,
2023-04-04 20:44:32 +02:00
classes: typeof moduleData.classes !== "undefined" ? `${moduleData.classes} ${module}` : module
2016-03-24 17:19:32 +01:00
});
2021-03-23 21:41:21 +01:00
});
2016-03-24 17:19:32 +01:00
return moduleFiles;
};
2020-08-01 16:38:32 +02:00
/**
2023-04-04 20:44:32 +02:00
* Load modules via ajax request and create module objects.
2020-08-01 16:38:32 +02:00
* @param {object} module Information about the module we want to load.
2023-04-04 20:44:32 +02:00
* @returns {Promise<void>} resolved when module is loaded
2016-03-24 17:19:32 +01:00
*/
2023-04-04 20:44:32 +02:00
const loadModule = async function (module) {
2021-03-23 21:41:21 +01:00
const url = module.path + module.file;
2016-03-31 13:05:23 +02:00
2023-04-04 20:44:32 +02:00
/**
* @returns {Promise<void>}
*/
const afterLoad = async function () {
2021-03-23 21:41:21 +01:00
const moduleObject = Module.create(module.name);
2016-10-13 16:42:15 +02:00
if (moduleObject) {
2023-04-04 20:44:32 +02:00
await bootstrapModule(module, moduleObject);
2016-10-13 16:42:15 +02:00
}
2016-03-24 17:19:32 +01:00
};
2016-03-31 13:05:23 +02:00
if (loadedModuleFiles.indexOf(url) !== -1) {
2023-04-04 20:44:32 +02:00
await afterLoad();
2016-03-31 13:05:23 +02:00
} else {
2023-04-04 20:44:32 +02:00
await loadFile(url);
loadedModuleFiles.push(url);
await afterLoad();
2016-03-31 13:05:23 +02:00
}
2016-04-03 19:52:13 +02:00
};
2016-03-24 17:19:32 +01:00
2020-08-01 16:38:32 +02:00
/**
2016-03-24 17:19:32 +01:00
* Bootstrap modules by setting the module data and loading the scripts & styles.
2020-08-01 16:38:32 +02:00
* @param {object} module Information about the module we want to load.
* @param {Module} mObj Modules instance.
2016-03-24 17:19:32 +01:00
*/
2023-04-04 20:44:32 +02:00
const bootstrapModule = async function (module, mObj) {
Log.info(`Bootstrapping module: ${module.name}`);
2016-03-24 17:19:32 +01:00
mObj.setData(module);
2023-04-04 20:44:32 +02:00
await mObj.loadScripts();
Log.log(`Scripts loaded for: ${module.name}`);
await mObj.loadStyles();
Log.log(`Styles loaded for: ${module.name}`);
await mObj.loadTranslations();
Log.log(`Translations loaded for: ${module.name}`);
moduleObjects.push(mObj);
2016-03-24 17:19:32 +01:00
};
2020-08-01 16:38:32 +02:00
/**
2016-03-24 17:19:32 +01:00
* Load a script or stylesheet by adding it to the dom.
2020-08-01 16:38:32 +02:00
* @param {string} fileName Path of the file we want to load.
2023-04-04 20:44:32 +02:00
* @returns {Promise} resolved when the file is loaded
2016-03-24 17:19:32 +01:00
*/
2023-04-04 20:44:32 +02:00
const loadFile = async function (fileName) {
2021-03-23 21:41:21 +01:00
const extension = fileName.slice((Math.max(0, fileName.lastIndexOf(".")) || Infinity) + 1);
let script, stylesheet;
2016-03-24 17:19:32 +01:00
switch (extension.toLowerCase()) {
2020-05-25 18:57:15 +02:00
case "js":
2023-04-04 20:44:32 +02:00
return new Promise((resolve) => {
Log.log(`Load script: ${fileName}`);
script = document.createElement("script");
script.type = "text/javascript";
script.src = fileName;
script.onload = function () {
resolve();
};
script.onerror = function () {
Log.error("Error on loading script:", fileName);
script.remove();
2023-04-04 20:44:32 +02:00
resolve();
};
document.getElementsByTagName("body")[0].appendChild(script);
});
2020-05-25 18:57:15 +02:00
case "css":
2023-04-04 20:44:32 +02:00
return new Promise((resolve) => {
Log.log(`Load stylesheet: ${fileName}`);
stylesheet = document.createElement("link");
stylesheet.rel = "stylesheet";
stylesheet.type = "text/css";
stylesheet.href = fileName;
stylesheet.onload = function () {
resolve();
};
stylesheet.onerror = function () {
Log.error("Error on loading stylesheet:", fileName);
stylesheet.remove();
2023-04-04 20:44:32 +02:00
resolve();
};
document.getElementsByTagName("head")[0].appendChild(stylesheet);
});
2016-03-24 17:19:32 +01:00
}
};
/* Public Methods */
return {
2024-01-01 15:38:08 +01:00
2020-08-01 16:38:32 +02:00
/**
2016-03-24 17:19:32 +01:00
* Load all modules as defined in the config.
*/
2024-01-01 15:38:08 +01:00
async loadModules () {
let moduleData = await getModuleData();
const envVars = await getEnvVars();
const customCss = envVars.customCss;
2023-04-04 20:44:32 +02:00
/**
* @returns {Promise<void>} when all modules are loaded
*/
const loadNextModule = async function () {
if (moduleData.length > 0) {
const nextModule = moduleData[0];
await loadModule(nextModule);
moduleData = moduleData.slice(1);
await loadNextModule();
} else {
// All modules loaded. Load custom.css
// This is done after all the modules so we can
// overwrite all the defined styles.
await loadFile(customCss);
2023-04-04 20:44:32 +02:00
// custom.css loaded. Start all modules.
await startModules();
}
};
await loadNextModule();
2016-03-24 17:19:32 +01:00
},
2020-08-01 16:38:32 +02:00
/**
2016-03-24 17:19:32 +01:00
* Load a file (script or stylesheet).
* Prevent double loading and search for files in the vendor folder.
2020-08-01 16:38:32 +02:00
* @param {string} fileName Path of the file we want to load.
* @param {Module} module The module that calls the loadFile function.
2023-04-04 20:44:32 +02:00
* @returns {Promise} resolved when the file is loaded
2016-03-24 17:19:32 +01:00
*/
2024-01-01 15:38:08 +01:00
async loadFileForModule (fileName, module) {
2016-03-31 13:05:23 +02:00
if (loadedFiles.indexOf(fileName.toLowerCase()) !== -1) {
2023-04-04 20:44:32 +02:00
Log.log(`File already loaded: ${fileName}`);
2016-03-24 17:19:32 +01:00
return;
}
2016-04-05 14:35:11 -04:00
if (fileName.indexOf("http://") === 0 || fileName.indexOf("https://") === 0 || fileName.indexOf("/") !== -1) {
2016-03-31 13:05:23 +02:00
// This is an absolute or relative path.
2016-03-24 17:19:32 +01:00
// Load it and then return.
2016-03-31 13:05:23 +02:00
loadedFiles.push(fileName.toLowerCase());
2023-04-04 20:44:32 +02:00
return loadFile(fileName);
2016-03-24 17:19:32 +01:00
}
if (vendor[fileName] !== undefined) {
// This file is available in the vendor folder.
// Load it from this vendor folder.
loadedFiles.push(fileName.toLowerCase());
return loadFile(`vendor/${vendor[fileName]}`);
2016-03-24 17:19:32 +01:00
}
2016-03-31 13:05:23 +02:00
// File not loaded yet.
// Load it based on the module path.
loadedFiles.push(fileName.toLowerCase());
2023-04-04 20:44:32 +02:00
return loadFile(module.file(fileName));
2016-03-24 17:19:32 +01:00
}
};
2024-01-01 15:38:08 +01:00
}());