MagicMirror/js/loader.js

262 lines
7.2 KiB
JavaScript
Raw Normal View History

/* global defaultModules, vendor */
2020-04-21 10:41:21 +02: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 () {
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
/**
* Loops through all modules and requests start for every module.
2016-03-24 17:19:32 +01:00
*/
const startModules = async function () {
const modulePromises = [];
2021-03-23 21:41:21 +01:00
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-24 17:19:32 +01:00
}
const results = await Promise.allSettled(modulePromises);
// Log errors that happened during async node_helper startup
results.forEach((result) => {
if (result.status === "rejected") {
Log.error(result.reason);
}
});
// 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) {
Log.info(`Initially hiding ${thisModule.name}`);
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];
let moduleFolder = `${config.paths.modules}/${module}`;
2016-04-01 17:35:29 +02:00
if (defaultModules.indexOf(moduleName) !== -1) {
moduleFolder = `${config.paths.modules}/default/${module}`;
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,
identifier: `module_${index}_${module}`,
2016-04-01 17:35:29 +02:00
name: moduleName,
path: `${moduleFolder}/`,
file: `${moduleName}.js`,
2016-03-24 17:19:32 +01:00
position: moduleData.position,
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,
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
/**
* 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.
* @returns {Promise<void>} resolved when module is loaded
2016-03-24 17:19:32 +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
/**
* @returns {Promise<void>}
*/
const afterLoad = async function () {
2021-03-23 21:41:21 +01:00
const moduleObject = Module.create(module.name);
if (moduleObject) {
await bootstrapModule(module, moduleObject);
}
2016-03-24 17:19:32 +01:00
};
2016-03-31 13:05:23 +02:00
if (loadedModuleFiles.indexOf(url) !== -1) {
await afterLoad();
2016-03-31 13:05:23 +02:00
} else {
await loadFile(url);
loadedModuleFiles.push(url);
await afterLoad();
2016-03-31 13:05:23 +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
*/
const bootstrapModule = async function (module, mObj) {
Log.info(`Bootstrapping module: ${module.name}`);
2016-03-24 17:19:32 +01:00
mObj.setData(module);
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.
* @returns {Promise} resolved when the file is loaded
2016-03-24 17:19:32 +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":
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);
resolve();
};
document.getElementsByTagName("body")[0].appendChild(script);
});
2020-05-25 18:57:15 +02:00
case "css":
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);
resolve();
};
document.getElementsByTagName("head")[0].appendChild(stylesheet);
});
2016-03-24 17:19:32 +01:00
}
};
/* Public Methods */
return {
2020-08-01 16:38:32 +02:00
/**
2016-03-24 17:19:32 +01:00
* Load all modules as defined in the config.
*/
async loadModules () {
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.
* @returns {Promise} resolved when the file is loaded
2016-03-24 17:19:32 +01:00
*/
async loadFileForModule (fileName, module) {
2016-03-31 13:05:23 +02:00
if (loadedFiles.indexOf(fileName.toLowerCase()) !== -1) {
Log.log(`File already loaded: ${fileName}`);
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());
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(`${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());
return loadFile(module.file(fileName));
2016-03-24 17:19:32 +01:00
}
};
}());