2020-05-05 14:55:15 +02:00
|
|
|
/* global defaultModules, vendor */
|
2020-04-21 10:41:21 +02:00
|
|
|
|
2022-01-26 23:09:26 +01:00
|
|
|
/* MagicMirror²
|
2016-03-24 17:19:32 +01:00
|
|
|
* Module and File loaders.
|
|
|
|
*
|
2020-04-28 23:05:28 +02:00
|
|
|
* By Michael Teeuw https://michaelteeuw.nl
|
2016-03-24 17:19:32 +01:00
|
|
|
* MIT Licensed.
|
|
|
|
*/
|
2021-03-23 21:41:21 +01:00
|
|
|
const Loader = (function () {
|
2023-12-25 08:17:11 +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 */
|
|
|
|
|
2020-08-01 16:38:32 +02:00
|
|
|
/**
|
2023-02-26 18:36:47 +01:00
|
|
|
* Loops through all modules and requests start for every module.
|
2016-03-24 17:19:32 +01:00
|
|
|
*/
|
2023-02-26 18:36:47 +01:00
|
|
|
const startModules = async function () {
|
2023-02-22 18:58:29 +01:00
|
|
|
const modulePromises = [];
|
2021-03-23 21:41:21 +01:00
|
|
|
for (const module of moduleObjects) {
|
2023-02-22 18:58:29 +01:00
|
|
|
try {
|
|
|
|
modulePromises.push(module.start());
|
|
|
|
} catch (error) {
|
|
|
|
Log.error(`Error when starting node_helper for module ${module.name}:`);
|
|
|
|
Log.error(error);
|
|
|
|
}
|
2016-03-24 17:19:32 +01:00
|
|
|
}
|
|
|
|
|
2023-02-26 18:36:47 +01:00
|
|
|
const results = await Promise.allSettled(modulePromises);
|
2021-03-07 11:05:29 +00:00
|
|
|
|
2023-02-26 18:36:47 +01:00
|
|
|
// Log errors that happened during async node_helper startup
|
|
|
|
results.forEach((result) => {
|
|
|
|
if (result.status === "rejected") {
|
|
|
|
Log.error(result.reason);
|
2021-03-07 11:05:29 +00:00
|
|
|
}
|
2023-02-22 18:58:29 +01:00
|
|
|
});
|
2023-02-26 18:36:47 +01:00
|
|
|
|
|
|
|
// Notify core of loaded modules.
|
|
|
|
MM.modulesStarted(moduleObjects);
|
|
|
|
|
|
|
|
// Starting modules also hides any modules that have requested to be initially hidden
|
|
|
|
for (const thisModule of moduleObjects) {
|
|
|
|
if (thisModule.data.hiddenOnStartup) {
|
2023-03-19 14:32:23 +01:00
|
|
|
Log.info(`Initially hiding ${thisModule.name}`);
|
2023-02-26 18:36:47 +01: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 () {
|
2016-03-24 17:19:32 +01:00
|
|
|
return config.modules;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
*/
|
2021-03-23 21:41:21 +01:00
|
|
|
const getModuleData = function () {
|
|
|
|
const modules = getAllModules();
|
|
|
|
const moduleFiles = [];
|
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];
|
2023-03-19 14:32:23 +01:00
|
|
|
let moduleFolder = `${config.paths.modules}/${module}`;
|
2016-04-03 19:52:13 +02:00
|
|
|
|
2016-04-01 17:35:29 +02:00
|
|
|
if (defaultModules.indexOf(moduleName) !== -1) {
|
2023-03-19 14:32:23 +01:00
|
|
|
moduleFolder = `${config.paths.modules}/default/${module}`;
|
2016-04-01 17:35:29 +02:00
|
|
|
}
|
|
|
|
|
2016-11-14 14:43:30 -03:00
|
|
|
if (moduleData.disabled === true) {
|
2021-03-23 21:41:21 +01:00
|
|
|
return;
|
2016-11-14 14:43:30 -03:00
|
|
|
}
|
|
|
|
|
2016-03-24 17:19:32 +01:00
|
|
|
moduleFiles.push({
|
2021-03-23 21:41:21 +01:00
|
|
|
index: index,
|
2023-03-19 14:32:23 +01:00
|
|
|
identifier: `module_${index}_${module}`,
|
2016-04-01 17:35:29 +02:00
|
|
|
name: moduleName,
|
2023-03-19 14:32:23 +01:00
|
|
|
path: `${moduleFolder}/`,
|
|
|
|
file: `${moduleName}.js`,
|
2016-03-24 17:19:32 +01:00
|
|
|
position: moduleData.position,
|
2023-09-08 07:43:39 +02:00
|
|
|
animateIn: moduleData.animateIn,
|
|
|
|
animateOut: moduleData.animateOut,
|
2021-03-07 11:05:29 +00:00
|
|
|
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-03-19 14:32:23 +01: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-02-26 18:36:47 +01: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-02-26 18:36:47 +01:00
|
|
|
* @returns {Promise<void>} resolved when module is loaded
|
2016-03-24 17:19:32 +01:00
|
|
|
*/
|
2023-02-26 18:36:47 +01: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-02-26 18:36:47 +01: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-02-26 18:36:47 +01: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-02-26 18:36:47 +01:00
|
|
|
await afterLoad();
|
2016-03-31 13:05:23 +02:00
|
|
|
} else {
|
2023-02-26 18:36:47 +01: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-02-26 18:36:47 +01:00
|
|
|
const bootstrapModule = async function (module, mObj) {
|
2023-03-19 14:32:23 +01:00
|
|
|
Log.info(`Bootstrapping module: ${module.name}`);
|
2016-03-24 17:19:32 +01:00
|
|
|
mObj.setData(module);
|
|
|
|
|
2023-02-26 18:36:47 +01:00
|
|
|
await mObj.loadScripts();
|
2023-03-19 14:32:23 +01:00
|
|
|
Log.log(`Scripts loaded for: ${module.name}`);
|
2023-02-26 18:36:47 +01:00
|
|
|
|
|
|
|
await mObj.loadStyles();
|
2023-03-19 14:32:23 +01:00
|
|
|
Log.log(`Styles loaded for: ${module.name}`);
|
2023-02-26 18:36:47 +01:00
|
|
|
|
|
|
|
await mObj.loadTranslations();
|
2023-03-19 14:32:23 +01:00
|
|
|
Log.log(`Translations loaded for: ${module.name}`);
|
2023-02-26 18:36:47 +01:00
|
|
|
|
|
|
|
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-02-22 20:03:05 +01:00
|
|
|
* @returns {Promise} resolved when the file is loaded
|
2016-03-24 17:19:32 +01:00
|
|
|
*/
|
2023-02-22 20:03:05 +01: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-02-22 20:03:05 +01:00
|
|
|
return new Promise((resolve) => {
|
2023-03-19 14:32:23 +01:00
|
|
|
Log.log(`Load script: ${fileName}`);
|
2023-02-22 20:03:05 +01:00
|
|
|
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);
|
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
document.getElementsByTagName("body")[0].appendChild(script);
|
|
|
|
});
|
2020-05-25 18:57:15 +02:00
|
|
|
case "css":
|
2023-02-22 20:03:05 +01:00
|
|
|
return new Promise((resolve) => {
|
2023-03-19 14:32:23 +01:00
|
|
|
Log.log(`Load stylesheet: ${fileName}`);
|
2023-02-22 20:03:05 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
document.getElementsByTagName("head")[0].appendChild(stylesheet);
|
|
|
|
});
|
2016-03-24 17:19:32 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Public Methods */
|
|
|
|
return {
|
2023-12-25 08:17:11 +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.
|
|
|
|
*/
|
2023-12-25 08:17:11 +01:00
|
|
|
async loadModules () {
|
2023-02-26 18:36:47 +01:00
|
|
|
let moduleData = getModuleData();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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(config.customCss);
|
|
|
|
// 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-02-22 20:03:05 +01:00
|
|
|
* @returns {Promise} resolved when the file is loaded
|
2016-03-24 17:19:32 +01:00
|
|
|
*/
|
2023-12-25 08:17:11 +01:00
|
|
|
async loadFileForModule (fileName, module) {
|
2016-03-31 13:05:23 +02:00
|
|
|
if (loadedFiles.indexOf(fileName.toLowerCase()) !== -1) {
|
2023-03-19 14:32:23 +01:00
|
|
|
Log.log(`File already loaded: ${fileName}`);
|
2023-02-26 18:36:47 +01:00
|
|
|
return;
|
2016-03-24 17:19:32 +01:00
|
|
|
}
|
|
|
|
|
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-02-22 20:03:05 +01: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());
|
2023-03-19 14:32:23 +01:00
|
|
|
return loadFile(`${config.paths.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-02-22 20:03:05 +01:00
|
|
|
return loadFile(module.file(fileName));
|
2016-03-24 17:19:32 +01:00
|
|
|
}
|
|
|
|
};
|
2023-12-25 08:17:11 +01:00
|
|
|
}());
|