From e3e3e7fd0ea50f0fd4284c426ff7e81cff646eb1 Mon Sep 17 00:00:00 2001 From: veeck Date: Sat, 5 Nov 2022 20:36:24 +0100 Subject: [PATCH 1/7] Only render something when there is a compliment --- modules/default/compliments/compliments.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/modules/default/compliments/compliments.js b/modules/default/compliments/compliments.js index 23c1a6831e..07cc3bbffc 100644 --- a/modules/default/compliments/compliments.js +++ b/modules/default/compliments/compliments.js @@ -164,19 +164,19 @@ Module.register("compliments", { const complimentText = this.randomCompliment(); // split it into parts on newline text const parts = complimentText.split("\n"); - // create a span to hold it all - const compliment = document.createElement("span"); // process all the parts of the compliment text for (const part of parts) { - // create a text element for each part - compliment.appendChild(document.createTextNode(part)); - // add a break ` - compliment.appendChild(document.createElement("BR")); + if (part !== "") { + // create a span to hold the part of the compliment + const compliment = document.createElement("span"); + // create a text element for each part + compliment.appendChild(document.createTextNode(part)); + // add a break + compliment.appendChild(document.createElement("BR")); + // add compliment part to wrapper + wrapper.appendChild(compliment); + } } - // remove the last break - compliment.lastElementChild.remove(); - wrapper.appendChild(compliment); - return wrapper; }, From 619b354d4ede9fbdb0be83b2374aee1c05b4b9d0 Mon Sep 17 00:00:00 2001 From: veeck Date: Sat, 5 Nov 2022 21:36:05 +0100 Subject: [PATCH 2/7] Cleanup naming, add comments, simplify --- modules/default/compliments/compliments.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/modules/default/compliments/compliments.js b/modules/default/compliments/compliments.js index 07cc3bbffc..8eef403906 100644 --- a/modules/default/compliments/compliments.js +++ b/modules/default/compliments/compliments.js @@ -87,6 +87,7 @@ Module.register("compliments", { const date = moment().format("YYYY-MM-DD"); let compliments; + // Add time of day compliments if (hour >= this.config.morningStartTime && hour < this.config.morningEndTime && this.config.compliments.hasOwnProperty("morning")) { compliments = this.config.compliments.morning.slice(0); } else if (hour >= this.config.afternoonStartTime && hour < this.config.afternoonEndTime && this.config.compliments.hasOwnProperty("afternoon")) { @@ -99,12 +100,15 @@ Module.register("compliments", { compliments = []; } + // Add compliments based on weather if (this.currentWeatherType in this.config.compliments) { compliments.push.apply(compliments, this.config.compliments[this.currentWeatherType]); } + // Add compliments for anytime compliments.push.apply(compliments, this.config.compliments.anytime); + // Add compliments for special days for (let entry in this.config.compliments) { if (new RegExp(entry).test(date)) { compliments.push.apply(compliments, this.config.compliments[entry]); @@ -138,7 +142,7 @@ Module.register("compliments", { * * @returns {string} a compliment */ - randomCompliment: function () { + getRandomCompliment: function () { // get the current time of day compliments list const compliments = this.complimentArray(); // variable for index to next message to display @@ -161,7 +165,7 @@ Module.register("compliments", { const wrapper = document.createElement("div"); wrapper.className = this.config.classes ? this.config.classes : "thin xlarge bright pre-line"; // get the compliment text - const complimentText = this.randomCompliment(); + const complimentText = this.getRandomCompliment(); // split it into parts on newline text const parts = complimentText.split("\n"); // process all the parts of the compliment text @@ -180,15 +184,10 @@ Module.register("compliments", { return wrapper; }, - // From data currentweather set weather type - setCurrentWeatherType: function (type) { - this.currentWeatherType = type; - }, - // Override notification handler. notificationReceived: function (notification, payload, sender) { if (notification === "CURRENTWEATHER_TYPE") { - this.setCurrentWeatherType(payload.type); + this.currentWeatherType = payload.type; } } }); From 841dd7a34df3bd0d587afddcfedf7e78d84767b0 Mon Sep 17 00:00:00 2001 From: veeck Date: Sat, 5 Nov 2022 22:03:50 +0100 Subject: [PATCH 3/7] Update to modern es6 notation --- modules/default/compliments/compliments.js | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/modules/default/compliments/compliments.js b/modules/default/compliments/compliments.js index 8eef403906..a94d713c87 100644 --- a/modules/default/compliments/compliments.js +++ b/modules/default/compliments/compliments.js @@ -85,33 +85,29 @@ Module.register("compliments", { complimentArray: function () { const hour = moment().hour(); const date = moment().format("YYYY-MM-DD"); - let compliments; + let compliments = []; // Add time of day compliments if (hour >= this.config.morningStartTime && hour < this.config.morningEndTime && this.config.compliments.hasOwnProperty("morning")) { - compliments = this.config.compliments.morning.slice(0); + compliments = [...this.config.compliments.morning]; } else if (hour >= this.config.afternoonStartTime && hour < this.config.afternoonEndTime && this.config.compliments.hasOwnProperty("afternoon")) { - compliments = this.config.compliments.afternoon.slice(0); + compliments = [...this.config.compliments.afternoon]; } else if (this.config.compliments.hasOwnProperty("evening")) { - compliments = this.config.compliments.evening.slice(0); - } - - if (typeof compliments === "undefined") { - compliments = []; + compliments = [...this.config.compliments.evening]; } // Add compliments based on weather if (this.currentWeatherType in this.config.compliments) { - compliments.push.apply(compliments, this.config.compliments[this.currentWeatherType]); + Array.prototype.push.apply(compliments, this.config.compliments[this.currentWeatherType]); } // Add compliments for anytime - compliments.push.apply(compliments, this.config.compliments.anytime); + Array.prototype.push.apply(compliments, this.config.compliments.anytime); // Add compliments for special days for (let entry in this.config.compliments) { if (new RegExp(entry).test(date)) { - compliments.push.apply(compliments, this.config.compliments[entry]); + Array.prototype.push.apply(compliments, this.config.compliments[entry]); } } From e5367a88b51d1e2acb12a4b54a96ef83625696fa Mon Sep 17 00:00:00 2001 From: veeck Date: Fri, 30 Sep 2022 21:41:53 +0200 Subject: [PATCH 4/7] Use fetch instead of XMLHttpRequest in compliments --- modules/default/compliments/compliments.js | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/modules/default/compliments/compliments.js b/modules/default/compliments/compliments.js index a94d713c87..a846c3a551 100644 --- a/modules/default/compliments/compliments.js +++ b/modules/default/compliments/compliments.js @@ -39,7 +39,7 @@ Module.register("compliments", { this.lastComplimentIndex = -1; if (this.config.remoteFile !== null) { - this.complimentFile((response) => { + this.loadComplimentFile().then((response) => { this.config.compliments = JSON.parse(response); this.updateDom(); }); @@ -117,20 +117,13 @@ Module.register("compliments", { /** * Retrieve a file from the local filesystem * - * @param {Function} callback Called when the file is retrieved. + * @returns {Promise} Resolved when the file is loaded */ - complimentFile: function (callback) { - const xobj = new XMLHttpRequest(), - isRemote = this.config.remoteFile.indexOf("http://") === 0 || this.config.remoteFile.indexOf("https://") === 0, - path = isRemote ? this.config.remoteFile : this.file(this.config.remoteFile); - xobj.overrideMimeType("application/json"); - xobj.open("GET", path, true); - xobj.onreadystatechange = function () { - if (xobj.readyState === 4 && xobj.status === 200) { - callback(xobj.responseText); - } - }; - xobj.send(null); + loadComplimentFile: async function () { + const isRemote = this.config.remoteFile.indexOf("http://") === 0 || this.config.remoteFile.indexOf("https://") === 0, + url = isRemote ? this.config.remoteFile : this.file(this.config.remoteFile); + const response = await fetch(url); + return await response.text(); }, /** From 4ea9991d85fd791df373b31698af685745b1556f Mon Sep 17 00:00:00 2001 From: veeck Date: Sun, 6 Nov 2022 11:30:54 +0100 Subject: [PATCH 5/7] Update CHANGELOG --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 516e499a12..ea5a36a935 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,7 +36,8 @@ Special thanks to: @rejas, @sdetweil, @MagMar94 - Reworked how weatherproviders handle units (#2849) - Use unix() method for parsing times, fix suntimes on the way (#2950) - Refactor conversion functions into utils class (#2958) -- The `cors`-method in `server.js` now supports sending and recieving HTTP headers. +- The `cors`-method in `server.js` now supports sending and receiving HTTP headers. +- Cleanup compliments module ### Fixed From f94a1a3e6c273fcb30d4f6c742fb15cfda58eace Mon Sep 17 00:00:00 2001 From: veeck Date: Wed, 9 Nov 2022 07:54:32 +0100 Subject: [PATCH 6/7] Try out fetch fix --- tests/e2e/helpers/global-setup.js | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/e2e/helpers/global-setup.js b/tests/e2e/helpers/global-setup.js index ee8fb83ef2..582ab3ec81 100644 --- a/tests/e2e/helpers/global-setup.js +++ b/tests/e2e/helpers/global-setup.js @@ -35,6 +35,7 @@ exports.getDocument = () => { const url = "http://" + (config.address || "localhost") + ":" + (config.port || "8080"); jsdom.JSDOM.fromURL(url, { resources: "usable", runScripts: "dangerously" }).then((dom) => { dom.window.name = "jsdom"; + dom.window.fetch = corefetch; dom.window.onload = () => { global.document = dom.window.document; resolve(); From 3918ae33a4f0196d98683803b469becab163248b Mon Sep 17 00:00:00 2001 From: veeck Date: Wed, 23 Nov 2022 21:46:00 +0100 Subject: [PATCH 7/7] Create only one span for a compliment --- modules/default/compliments/compliments.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/modules/default/compliments/compliments.js b/modules/default/compliments/compliments.js index a846c3a551..a1fffb8455 100644 --- a/modules/default/compliments/compliments.js +++ b/modules/default/compliments/compliments.js @@ -157,19 +157,23 @@ Module.register("compliments", { const complimentText = this.getRandomCompliment(); // split it into parts on newline text const parts = complimentText.split("\n"); + // create a span to hold the compliment + const compliment = document.createElement("span"); // process all the parts of the compliment text for (const part of parts) { if (part !== "") { - // create a span to hold the part of the compliment - const compliment = document.createElement("span"); // create a text element for each part compliment.appendChild(document.createTextNode(part)); // add a break compliment.appendChild(document.createElement("BR")); - // add compliment part to wrapper - wrapper.appendChild(compliment); } } + // only add compliment to wrapper if there is actual text in there + if (compliment.children.length > 0) { + // remove the last break + compliment.lastElementChild.remove(); + wrapper.appendChild(compliment); + } return wrapper; },