Use es6 notation in main.js

This commit is contained in:
rejas 2021-04-18 15:29:10 +02:00
parent 7accb84eb9
commit de93b3294f

View File

@ -6,25 +6,25 @@
* By Michael Teeuw https://michaelteeuw.nl * By Michael Teeuw https://michaelteeuw.nl
* MIT Licensed. * MIT Licensed.
*/ */
var MM = (function () { const MM = (function () {
var modules = []; let modules = [];
/* Private Methods */ /* Private Methods */
/** /**
* Create dom objects for all modules that are configured for a specific position. * Create dom objects for all modules that are configured for a specific position.
*/ */
var createDomObjects = function () { const createDomObjects = function () {
var domCreationPromises = []; const domCreationPromises = [];
modules.forEach(function (module) { modules.forEach(function (module) {
if (typeof module.data.position !== "string") { if (typeof module.data.position !== "string") {
return; return;
} }
var wrapper = selectWrapper(module.data.position); const wrapper = selectWrapper(module.data.position);
var dom = document.createElement("div"); const dom = document.createElement("div");
dom.id = module.identifier; dom.id = module.identifier;
dom.className = module.name; dom.className = module.name;
@ -35,7 +35,7 @@ var MM = (function () {
dom.opacity = 0; dom.opacity = 0;
wrapper.appendChild(dom); wrapper.appendChild(dom);
var moduleHeader = document.createElement("header"); const moduleHeader = document.createElement("header");
moduleHeader.innerHTML = module.getHeader(); moduleHeader.innerHTML = module.getHeader();
moduleHeader.className = "module-header"; moduleHeader.className = "module-header";
dom.appendChild(moduleHeader); dom.appendChild(moduleHeader);
@ -46,11 +46,11 @@ var MM = (function () {
moduleHeader.style.display = "block;"; moduleHeader.style.display = "block;";
} }
var moduleContent = document.createElement("div"); const moduleContent = document.createElement("div");
moduleContent.className = "module-content"; moduleContent.className = "module-content";
dom.appendChild(moduleContent); dom.appendChild(moduleContent);
var domCreationPromise = updateDom(module, 0); const domCreationPromise = updateDom(module, 0);
domCreationPromises.push(domCreationPromise); domCreationPromises.push(domCreationPromise);
domCreationPromise domCreationPromise
.then(function () { .then(function () {
@ -73,11 +73,11 @@ var MM = (function () {
* *
* @returns {HTMLElement} the wrapper element * @returns {HTMLElement} the wrapper element
*/ */
var selectWrapper = function (position) { const selectWrapper = function (position) {
var classes = position.replace("_", " "); const classes = position.replace("_", " ");
var parentWrapper = document.getElementsByClassName(classes); const parentWrapper = document.getElementsByClassName(classes);
if (parentWrapper.length > 0) { if (parentWrapper.length > 0) {
var wrapper = parentWrapper[0].getElementsByClassName("container"); const wrapper = parentWrapper[0].getElementsByClassName("container");
if (wrapper.length > 0) { if (wrapper.length > 0) {
return wrapper[0]; return wrapper[0];
} }
@ -92,9 +92,9 @@ var MM = (function () {
* @param {Module} sender The module that sent the notification. * @param {Module} sender The module that sent the notification.
* @param {Module} [sendTo] The (optional) module to send the notification to. * @param {Module} [sendTo] The (optional) module to send the notification to.
*/ */
var sendNotification = function (notification, payload, sender, sendTo) { const sendNotification = function (notification, payload, sender, sendTo) {
for (var m in modules) { for (const m in modules) {
var module = modules[m]; const module = modules[m];
if (module !== sender && (!sendTo || module === sendTo)) { if (module !== sender && (!sendTo || module === sendTo)) {
module.notificationReceived(notification, payload, sender); module.notificationReceived(notification, payload, sender);
} }
@ -109,10 +109,10 @@ var MM = (function () {
* *
* @returns {Promise} Resolved when the dom is fully updated. * @returns {Promise} Resolved when the dom is fully updated.
*/ */
var updateDom = function (module, speed) { const updateDom = function (module, speed) {
return new Promise(function (resolve) { return new Promise(function (resolve) {
var newContentPromise = module.getDom(); const newHeader = module.getHeader();
var newHeader = module.getHeader(); let newContentPromise = module.getDom();
if (!(newContentPromise instanceof Promise)) { if (!(newContentPromise instanceof Promise)) {
// convert to a promise if not already one to avoid if/else's everywhere // convert to a promise if not already one to avoid if/else's everywhere
@ -121,7 +121,7 @@ var MM = (function () {
newContentPromise newContentPromise
.then(function (newContent) { .then(function (newContent) {
var updatePromise = updateDomWithContent(module, speed, newHeader, newContent); const updatePromise = updateDomWithContent(module, speed, newHeader, newContent);
updatePromise.then(resolve).catch(Log.error); updatePromise.then(resolve).catch(Log.error);
}) })
@ -139,7 +139,7 @@ var MM = (function () {
* *
* @returns {Promise} Resolved when the module dom has been updated. * @returns {Promise} Resolved when the module dom has been updated.
*/ */
var updateDomWithContent = function (module, speed, newHeader, newContent) { const updateDomWithContent = function (module, speed, newHeader, newContent) {
return new Promise(function (resolve) { return new Promise(function (resolve) {
if (module.hidden || !speed) { if (module.hidden || !speed) {
updateModuleContent(module, newHeader, newContent); updateModuleContent(module, newHeader, newContent);
@ -177,23 +177,23 @@ var MM = (function () {
* *
* @returns {boolean} True if the module need an update, false otherwise * @returns {boolean} True if the module need an update, false otherwise
*/ */
var moduleNeedsUpdate = function (module, newHeader, newContent) { const moduleNeedsUpdate = function (module, newHeader, newContent) {
var moduleWrapper = document.getElementById(module.identifier); const moduleWrapper = document.getElementById(module.identifier);
if (moduleWrapper === null) { if (moduleWrapper === null) {
return false; return false;
} }
var contentWrapper = moduleWrapper.getElementsByClassName("module-content"); const contentWrapper = moduleWrapper.getElementsByClassName("module-content");
var headerWrapper = moduleWrapper.getElementsByClassName("module-header"); const headerWrapper = moduleWrapper.getElementsByClassName("module-header");
var headerNeedsUpdate = false; let headerNeedsUpdate = false;
var contentNeedsUpdate = false; let contentNeedsUpdate;
if (headerWrapper.length > 0) { if (headerWrapper.length > 0) {
headerNeedsUpdate = newHeader !== headerWrapper[0].innerHTML; headerNeedsUpdate = newHeader !== headerWrapper[0].innerHTML;
} }
var tempContentWrapper = document.createElement("div"); const tempContentWrapper = document.createElement("div");
tempContentWrapper.appendChild(newContent); tempContentWrapper.appendChild(newContent);
contentNeedsUpdate = tempContentWrapper.innerHTML !== contentWrapper[0].innerHTML; contentNeedsUpdate = tempContentWrapper.innerHTML !== contentWrapper[0].innerHTML;
@ -207,13 +207,13 @@ var MM = (function () {
* @param {string} newHeader The new header that is generated. * @param {string} newHeader The new header that is generated.
* @param {HTMLElement} newContent The new content that is generated. * @param {HTMLElement} newContent The new content that is generated.
*/ */
var updateModuleContent = function (module, newHeader, newContent) { const updateModuleContent = function (module, newHeader, newContent) {
var moduleWrapper = document.getElementById(module.identifier); const moduleWrapper = document.getElementById(module.identifier);
if (moduleWrapper === null) { if (moduleWrapper === null) {
return; return;
} }
var headerWrapper = moduleWrapper.getElementsByClassName("module-header"); const headerWrapper = moduleWrapper.getElementsByClassName("module-header");
var contentWrapper = moduleWrapper.getElementsByClassName("module-content"); const contentWrapper = moduleWrapper.getElementsByClassName("module-content");
contentWrapper[0].innerHTML = ""; contentWrapper[0].innerHTML = "";
contentWrapper[0].appendChild(newContent); contentWrapper[0].appendChild(newContent);
@ -234,7 +234,7 @@ var MM = (function () {
* @param {Function} callback Called when the animation is done. * @param {Function} callback Called when the animation is done.
* @param {object} [options] Optional settings for the hide method. * @param {object} [options] Optional settings for the hide method.
*/ */
var hideModule = function (module, speed, callback, options) { const hideModule = function (module, speed, callback, options) {
options = options || {}; options = options || {};
// set lockString if set in options. // set lockString if set in options.
@ -245,7 +245,7 @@ var MM = (function () {
} }
} }
var moduleWrapper = document.getElementById(module.identifier); const moduleWrapper = document.getElementById(module.identifier);
if (moduleWrapper !== null) { if (moduleWrapper !== null) {
moduleWrapper.style.transition = "opacity " + speed / 1000 + "s"; moduleWrapper.style.transition = "opacity " + speed / 1000 + "s";
moduleWrapper.style.opacity = 0; moduleWrapper.style.opacity = 0;
@ -280,12 +280,12 @@ var MM = (function () {
* @param {Function} callback Called when the animation is done. * @param {Function} callback Called when the animation is done.
* @param {object} [options] Optional settings for the show method. * @param {object} [options] Optional settings for the show method.
*/ */
var showModule = function (module, speed, callback, options) { const showModule = function (module, speed, callback, options) {
options = options || {}; options = options || {};
// remove lockString if set in options. // remove lockString if set in options.
if (options.lockString) { if (options.lockString) {
var index = module.lockStrings.indexOf(options.lockString); const index = module.lockStrings.indexOf(options.lockString);
if (index !== -1) { if (index !== -1) {
module.lockStrings.splice(index, 1); module.lockStrings.splice(index, 1);
} }
@ -309,7 +309,7 @@ var MM = (function () {
module.lockStrings = []; module.lockStrings = [];
} }
var moduleWrapper = document.getElementById(module.identifier); const moduleWrapper = document.getElementById(module.identifier);
if (moduleWrapper !== null) { if (moduleWrapper !== null) {
moduleWrapper.style.transition = "opacity " + speed / 1000 + "s"; moduleWrapper.style.transition = "opacity " + speed / 1000 + "s";
// Restore the position. See hideModule() for more info. // Restore the position. See hideModule() for more info.
@ -318,7 +318,7 @@ var MM = (function () {
updateWrapperStates(); updateWrapperStates();
// Waiting for DOM-changes done in updateWrapperStates before we can start the animation. // Waiting for DOM-changes done in updateWrapperStates before we can start the animation.
var dummy = moduleWrapper.parentElement.parentElement.offsetHeight; const dummy = moduleWrapper.parentElement.parentElement.offsetHeight;
moduleWrapper.style.opacity = 1; moduleWrapper.style.opacity = 1;
clearTimeout(module.showHideTimer); clearTimeout(module.showHideTimer);
@ -346,14 +346,14 @@ var MM = (function () {
* an ugly top margin. By using this function, the top bar will be hidden if the * an ugly top margin. By using this function, the top bar will be hidden if the
* update notification is not visible. * update notification is not visible.
*/ */
var updateWrapperStates = function () { const updateWrapperStates = function () {
var positions = ["top_bar", "top_left", "top_center", "top_right", "upper_third", "middle_center", "lower_third", "bottom_left", "bottom_center", "bottom_right", "bottom_bar", "fullscreen_above", "fullscreen_below"]; const positions = ["top_bar", "top_left", "top_center", "top_right", "upper_third", "middle_center", "lower_third", "bottom_left", "bottom_center", "bottom_right", "bottom_bar", "fullscreen_above", "fullscreen_below"];
positions.forEach(function (position) { positions.forEach(function (position) {
var wrapper = selectWrapper(position); const wrapper = selectWrapper(position);
var moduleWrappers = wrapper.getElementsByClassName("module"); const moduleWrappers = wrapper.getElementsByClassName("module");
var showWrapper = false; let showWrapper = false;
Array.prototype.forEach.call(moduleWrappers, function (moduleWrapper) { Array.prototype.forEach.call(moduleWrappers, function (moduleWrapper) {
if (moduleWrapper.style.position === "" || moduleWrapper.style.position === "static") { if (moduleWrapper.style.position === "" || moduleWrapper.style.position === "static") {
showWrapper = true; showWrapper = true;
@ -367,7 +367,7 @@ var MM = (function () {
/** /**
* Loads the core config and combines it with the system defaults. * Loads the core config and combines it with the system defaults.
*/ */
var loadConfig = function () { const loadConfig = function () {
// FIXME: Think about how to pass config around without breaking tests // FIXME: Think about how to pass config around without breaking tests
/* eslint-disable */ /* eslint-disable */
if (typeof config === "undefined") { if (typeof config === "undefined") {
@ -385,7 +385,7 @@ var MM = (function () {
* *
* @param {Module[]} modules Array of modules. * @param {Module[]} modules Array of modules.
*/ */
var setSelectionMethodsForModules = function (modules) { const setSelectionMethodsForModules = function (modules) {
/** /**
* Filter modules with the specified classes. * Filter modules with the specified classes.
* *
@ -393,7 +393,7 @@ var MM = (function () {
* *
* @returns {Module[]} Filtered collection of modules. * @returns {Module[]} Filtered collection of modules.
*/ */
var withClass = function (className) { const withClass = function (className) {
return modulesByClass(className, true); return modulesByClass(className, true);
}; };
@ -404,7 +404,7 @@ var MM = (function () {
* *
* @returns {Module[]} Filtered collection of modules. * @returns {Module[]} Filtered collection of modules.
*/ */
var exceptWithClass = function (className) { const exceptWithClass = function (className) {
return modulesByClass(className, false); return modulesByClass(className, false);
}; };
@ -416,17 +416,16 @@ var MM = (function () {
* *
* @returns {Module[]} Filtered collection of modules. * @returns {Module[]} Filtered collection of modules.
*/ */
var modulesByClass = function (className, include) { const modulesByClass = function (className, include) {
var searchClasses = className; let searchClasses = className;
if (typeof className === "string") { if (typeof className === "string") {
searchClasses = className.split(" "); searchClasses = className.split(" ");
} }
var newModules = modules.filter(function (module) { const newModules = modules.filter(function (module) {
var classes = module.data.classes.toLowerCase().split(" "); const classes = module.data.classes.toLowerCase().split(" ");
for (var c in searchClasses) { for (const searchClass of searchClasses) {
var searchClass = searchClasses[c];
if (classes.indexOf(searchClass.toLowerCase()) !== -1) { if (classes.indexOf(searchClass.toLowerCase()) !== -1) {
return include; return include;
} }
@ -445,8 +444,8 @@ var MM = (function () {
* @param {object} module The module instance to remove from the collection. * @param {object} module The module instance to remove from the collection.
* @returns {Module[]} Filtered collection of modules. * @returns {Module[]} Filtered collection of modules.
*/ */
var exceptModule = function (module) { const exceptModule = function (module) {
var newModules = modules.filter(function (mod) { const newModules = modules.filter(function (mod) {
return mod.identifier !== module.identifier; return mod.identifier !== module.identifier;
}); });
@ -459,7 +458,7 @@ var MM = (function () {
* *
* @param {Function} callback The function to execute with the module as an argument. * @param {Function} callback The function to execute with the module as an argument.
*/ */
var enumerate = function (callback) { const enumerate = function (callback) {
modules.map(function (module) { modules.map(function (module) {
callback(module); callback(module);
}); });
@ -604,11 +603,11 @@ if (typeof Object.assign !== "function") {
if (target === undefined || target === null) { if (target === undefined || target === null) {
throw new TypeError("Cannot convert undefined or null to object"); throw new TypeError("Cannot convert undefined or null to object");
} }
var output = Object(target); const output = Object(target);
for (var index = 1; index < arguments.length; index++) { for (let index = 1; index < arguments.length; index++) {
var source = arguments[index]; const source = arguments[index];
if (source !== undefined && source !== null) { if (source !== undefined && source !== null) {
for (var nextKey in source) { for (const nextKey in source) {
if (source.hasOwnProperty(nextKey)) { if (source.hasOwnProperty(nextKey)) {
output[nextKey] = source[nextKey]; output[nextKey] = source[nextKey];
} }