Use es6 notation in module

This commit is contained in:
rejas 2021-03-23 22:40:24 +01:00
parent 5fe654c19d
commit 6eba8d681c

View File

@ -6,7 +6,6 @@
* *
* By Michael Teeuw https://michaelteeuw.nl * By Michael Teeuw https://michaelteeuw.nl
* MIT Licensed. * MIT Licensed.
*
*/ */
var Module = Class.extend({ var Module = Class.extend({
/********************************************************* /*********************************************************
@ -82,16 +81,15 @@ var Module = Class.extend({
* @returns {HTMLElement|Promise} The dom or a promise with the dom to display. * @returns {HTMLElement|Promise} The dom or a promise with the dom to display.
*/ */
getDom: function () { getDom: function () {
var self = this; return new Promise((resolve) => {
return new Promise(function (resolve) { const div = document.createElement("div");
var div = document.createElement("div"); const template = this.getTemplate();
var template = self.getTemplate(); const templateData = this.getTemplateData();
var templateData = self.getTemplateData();
// Check to see if we need to render a template string or a file. // Check to see if we need to render a template string or a file.
if (/^.*((\.html)|(\.njk))$/.test(template)) { if (/^.*((\.html)|(\.njk))$/.test(template)) {
// the template is a filename // the template is a filename
self.nunjucksEnvironment().render(template, templateData, function (err, res) { this.nunjucksEnvironment().render(template, templateData, function (err, res) {
if (err) { if (err) {
Log.error(err); Log.error(err);
} }
@ -102,7 +100,7 @@ var Module = Class.extend({
}); });
} else { } else {
// the template is a template string. // the template is a template string.
div.innerHTML = self.nunjucksEnvironment().renderString(template, templateData); div.innerHTML = this.nunjucksEnvironment().renderString(template, templateData);
resolve(div); resolve(div);
} }
@ -168,15 +166,13 @@ var Module = Class.extend({
return this._nunjucksEnvironment; return this._nunjucksEnvironment;
} }
var self = this;
this._nunjucksEnvironment = new nunjucks.Environment(new nunjucks.WebLoader(this.file(""), { async: true }), { this._nunjucksEnvironment = new nunjucks.Environment(new nunjucks.WebLoader(this.file(""), { async: true }), {
trimBlocks: true, trimBlocks: true,
lstripBlocks: true lstripBlocks: true
}); });
this._nunjucksEnvironment.addFilter("translate", function (str, variables) { this._nunjucksEnvironment.addFilter("translate", (str, variables) => {
return nunjucks.runtime.markSafe(self.translate(str, variables)); return nunjucks.runtime.markSafe(this.translate(str, variables));
}); });
return this._nunjucksEnvironment; return this._nunjucksEnvironment;
@ -192,14 +188,14 @@ var Module = Class.extend({
Log.log(this.name + " received a socket notification: " + notification + " - Payload: " + payload); Log.log(this.name + " received a socket notification: " + notification + " - Payload: " + payload);
}, },
/* /**
* Called when the module is hidden. * Called when the module is hidden.
*/ */
suspend: function () { suspend: function () {
Log.log(this.name + " is suspended."); Log.log(this.name + " is suspended.");
}, },
/* /**
* Called when the module is shown. * Called when the module is shown.
*/ */
resume: function () { resume: function () {
@ -213,7 +209,7 @@ var Module = Class.extend({
/** /**
* Set the module data. * Set the module data.
* *
* @param {Module} data The module data * @param {object} data The module data
*/ */
setData: function (data) { setData: function (data) {
this.data = data; this.data = data;
@ -245,9 +241,8 @@ var Module = Class.extend({
this._socket = new MMSocket(this.name); this._socket = new MMSocket(this.name);
} }
var self = this; this._socket.setNotificationCallback((notification, payload) => {
this._socket.setNotificationCallback(function (notification, payload) { this.socketNotificationReceived(notification, payload);
self.socketNotificationReceived(notification, payload);
}); });
return this._socket; return this._socket;
@ -288,13 +283,12 @@ var Module = Class.extend({
* @param {Function} callback Function called when done. * @param {Function} callback Function called when done.
*/ */
loadDependencies: function (funcName, callback) { loadDependencies: function (funcName, callback) {
var self = this; let dependencies = this[funcName]();
var dependencies = this[funcName]();
var loadNextDependency = function () { const loadNextDependency = () => {
if (dependencies.length > 0) { if (dependencies.length > 0) {
var nextDependency = dependencies[0]; const nextDependency = dependencies[0];
Loader.loadFile(nextDependency, self, function () { Loader.loadFile(nextDependency, this, () => {
dependencies = dependencies.slice(1); dependencies = dependencies.slice(1);
loadNextDependency(); loadNextDependency();
}); });
@ -400,12 +394,11 @@ var Module = Class.extend({
callback = callback || function () {}; callback = callback || function () {};
options = options || {}; options = options || {};
var self = this;
MM.hideModule( MM.hideModule(
self, this,
speed, speed,
function () { () => {
self.suspend(); this.suspend();
callback(); callback();
}, },
options options
@ -464,9 +457,9 @@ var Module = Class.extend({
* @returns {object} the merged config * @returns {object} the merged config
*/ */
function configMerge(result) { function configMerge(result) {
var stack = Array.prototype.slice.call(arguments, 1); const stack = Array.prototype.slice.call(arguments, 1);
var item; let item, key;
var key;
while (stack.length) { while (stack.length) {
item = stack.shift(); item = stack.shift();
for (key in item) { for (key in item) {
@ -494,11 +487,11 @@ Module.create = function (name) {
return; return;
} }
var moduleDefinition = Module.definitions[name]; const moduleDefinition = Module.definitions[name];
var clonedDefinition = cloneObject(moduleDefinition); const clonedDefinition = cloneObject(moduleDefinition);
// Note that we clone the definition. Otherwise the objects are shared, which gives problems. // Note that we clone the definition. Otherwise the objects are shared, which gives problems.
var ModuleClass = Module.extend(clonedDefinition); const ModuleClass = Module.extend(clonedDefinition);
return new ModuleClass(); return new ModuleClass();
}; };
@ -526,14 +519,13 @@ Module.register = function (name, moduleDefinition) {
* number if a is smaller and 0 if they are the same * number if a is smaller and 0 if they are the same
*/ */
function cmpVersions(a, b) { function cmpVersions(a, b) {
var i, diff; const regExStrip0 = /(\.0+)+$/;
var regExStrip0 = /(\.0+)+$/; const segmentsA = a.replace(regExStrip0, "").split(".");
var segmentsA = a.replace(regExStrip0, "").split("."); const segmentsB = b.replace(regExStrip0, "").split(".");
var segmentsB = b.replace(regExStrip0, "").split("."); const l = Math.min(segmentsA.length, segmentsB.length);
var l = Math.min(segmentsA.length, segmentsB.length);
for (i = 0; i < l; i++) { for (let i = 0; i < l; i++) {
diff = parseInt(segmentsA[i], 10) - parseInt(segmentsB[i], 10); let diff = parseInt(segmentsA[i], 10) - parseInt(segmentsB[i], 10);
if (diff) { if (diff) {
return diff; return diff;
} }