Convert some code to es6

This commit is contained in:
rejas 2021-01-04 21:59:41 +01:00 committed by veeck
parent 3d4429d418
commit 5e6cbeb9ba
3 changed files with 56 additions and 62 deletions

View File

@ -95,16 +95,16 @@ Module.register("calendar", {
// indicate no data available yet // indicate no data available yet
this.loaded = false; this.loaded = false;
for (var c in this.config.calendars) { this.config.calendars.forEach((calendar) => {
var calendar = this.config.calendars[c];
calendar.url = calendar.url.replace("webcal://", "http://"); calendar.url = calendar.url.replace("webcal://", "http://");
var calendarConfig = { const calendarConfig = {
maximumEntries: calendar.maximumEntries, maximumEntries: calendar.maximumEntries,
maximumNumberOfDays: calendar.maximumNumberOfDays, maximumNumberOfDays: calendar.maximumNumberOfDays,
broadcastPastEvents: calendar.broadcastPastEvents, broadcastPastEvents: calendar.broadcastPastEvents,
selfSignedCert: calendar.selfSignedCert selfSignedCert: calendar.selfSignedCert
}; };
if (calendar.symbolClass === "undefined" || calendar.symbolClass === null) { if (calendar.symbolClass === "undefined" || calendar.symbolClass === null) {
calendarConfig.symbolClass = ""; calendarConfig.symbolClass = "";
} }
@ -128,7 +128,7 @@ Module.register("calendar", {
// tell helper to start a fetcher for this calendar // tell helper to start a fetcher for this calendar
// fetcher till cycle // fetcher till cycle
this.addCalendar(calendar.url, calendar.auth, calendarConfig); this.addCalendar(calendar.url, calendar.auth, calendarConfig);
} });
}, },
// Override socket notification handler. // Override socket notification handler.
@ -164,8 +164,8 @@ Module.register("calendar", {
const oneHour = oneMinute * 60; const oneHour = oneMinute * 60;
const oneDay = oneHour * 24; const oneDay = oneHour * 24;
var events = this.createEventList(); const events = this.createEventList();
var wrapper = document.createElement("table"); const wrapper = document.createElement("table");
wrapper.className = this.config.tableClass; wrapper.className = this.config.tableClass;
if (events.length === 0) { if (events.length === 0) {
@ -192,10 +192,10 @@ Module.register("calendar", {
var dateAsString = moment(event.startDate, "x").format(this.config.dateFormat); var dateAsString = moment(event.startDate, "x").format(this.config.dateFormat);
if (this.config.timeFormat === "dateheaders") { if (this.config.timeFormat === "dateheaders") {
if (lastSeenDate !== dateAsString) { if (lastSeenDate !== dateAsString) {
var dateRow = document.createElement("tr"); const dateRow = document.createElement("tr");
dateRow.className = "normal"; dateRow.className = "normal";
var dateCell = document.createElement("td");
const dateCell = document.createElement("td");
dateCell.colSpan = "3"; dateCell.colSpan = "3";
dateCell.innerHTML = dateAsString; dateCell.innerHTML = dateAsString;
dateCell.style.paddingTop = "10px"; dateCell.style.paddingTop = "10px";
@ -212,7 +212,7 @@ Module.register("calendar", {
} }
} }
var eventWrapper = document.createElement("tr"); const eventWrapper = document.createElement("tr");
if (this.config.colored && !this.config.coloredSymbolOnly) { if (this.config.colored && !this.config.coloredSymbolOnly) {
eventWrapper.style.cssText = "color:" + this.colorForUrl(event.url); eventWrapper.style.cssText = "color:" + this.colorForUrl(event.url);
@ -220,17 +220,17 @@ Module.register("calendar", {
eventWrapper.className = "normal event"; eventWrapper.className = "normal event";
if (this.config.displaySymbol) { const symbolWrapper = document.createElement("td");
var symbolWrapper = document.createElement("td");
if (this.config.displaySymbol) {
if (this.config.colored && this.config.coloredSymbolOnly) { if (this.config.colored && this.config.coloredSymbolOnly) {
symbolWrapper.style.cssText = "color:" + this.colorForUrl(event.url); symbolWrapper.style.cssText = "color:" + this.colorForUrl(event.url);
} }
var symbolClass = this.symbolClassForUrl(event.url); const symbolClass = this.symbolClassForUrl(event.url);
symbolWrapper.className = "symbol align-right " + symbolClass; symbolWrapper.className = "symbol align-right " + symbolClass;
var symbols = this.symbolsForEvent(event); const symbols = this.symbolsForEvent(event);
// If symbols are displayed and custom symbol is set, replace event symbol // If symbols are displayed and custom symbol is set, replace event symbol
if (this.config.displaySymbol && this.config.customEvents.length > 0) { if (this.config.displaySymbol && this.config.customEvents.length > 0) {
for (ev in this.config.customEvents) { for (ev in this.config.customEvents) {
@ -243,31 +243,29 @@ Module.register("calendar", {
} }
} }
} }
symbols.forEach((s, index) => {
for (var i = 0; i < symbols.length; i++) { const symbol = document.createElement("span");
var symbol = document.createElement("span"); symbol.className = "fa fa-fw fa-" + s;
symbol.className = "fa fa-fw fa-" + symbols[i]; if (index > 0) {
if (i > 0) {
symbol.style.paddingLeft = "5px"; symbol.style.paddingLeft = "5px";
} }
symbolWrapper.appendChild(symbol); symbolWrapper.appendChild(symbol);
} });
eventWrapper.appendChild(symbolWrapper); eventWrapper.appendChild(symbolWrapper);
} else if (this.config.timeFormat === "dateheaders") { } else if (this.config.timeFormat === "dateheaders") {
var blankCell = document.createElement("td"); const blankCell = document.createElement("td");
blankCell.innerHTML = "&nbsp;&nbsp;&nbsp;"; blankCell.innerHTML = "&nbsp;&nbsp;&nbsp;";
eventWrapper.appendChild(blankCell); eventWrapper.appendChild(blankCell);
} }
var titleWrapper = document.createElement("td"), const titleWrapper = document.createElement("td");
repeatingCountTitle = ""; let repeatingCountTitle = "";
if (this.config.displayRepeatingCountTitle && event.firstYear !== undefined) { if (this.config.displayRepeatingCountTitle && event.firstYear !== undefined) {
repeatingCountTitle = this.countTitleForUrl(event.url); repeatingCountTitle = this.countTitleForUrl(event.url);
if (repeatingCountTitle !== "") { if (repeatingCountTitle !== "") {
var thisYear = new Date(parseInt(event.startDate)).getFullYear(), const thisYear = new Date(parseInt(event.startDate)).getFullYear(),
yearDiff = thisYear - event.firstYear; yearDiff = thisYear - event.firstYear;
repeatingCountTitle = ", " + yearDiff + ". " + repeatingCountTitle; repeatingCountTitle = ", " + yearDiff + ". " + repeatingCountTitle;
@ -296,7 +294,7 @@ Module.register("calendar", {
titleWrapper.innerHTML = this.titleTransform(event.title, this.config.titleReplace, this.config.wrapEvents, this.config.maxTitleLength, this.config.maxTitleLines) + repeatingCountTitle; titleWrapper.innerHTML = this.titleTransform(event.title, this.config.titleReplace, this.config.wrapEvents, this.config.maxTitleLength, this.config.maxTitleLines) + repeatingCountTitle;
var titleClass = this.titleClassForUrl(event.url); const titleClass = this.titleClassForUrl(event.url);
if (!this.config.colored) { if (!this.config.colored) {
titleWrapper.className = "title bright " + titleClass; titleWrapper.className = "title bright " + titleClass;
@ -304,14 +302,12 @@ Module.register("calendar", {
titleWrapper.className = "title " + titleClass; titleWrapper.className = "title " + titleClass;
} }
var timeWrapper;
if (this.config.timeFormat === "dateheaders") { if (this.config.timeFormat === "dateheaders") {
if (event.fullDayEvent) { if (event.fullDayEvent) {
titleWrapper.colSpan = "2"; titleWrapper.colSpan = "2";
titleWrapper.align = "left"; titleWrapper.align = "left";
} else { } else {
timeWrapper = document.createElement("td"); const timeWrapper = document.createElement("td");
timeWrapper.className = "time light " + this.timeClassForUrl(event.url); timeWrapper.className = "time light " + this.timeClassForUrl(event.url);
timeWrapper.align = "left"; timeWrapper.align = "left";
timeWrapper.style.paddingLeft = "2px"; timeWrapper.style.paddingLeft = "2px";
@ -322,10 +318,10 @@ Module.register("calendar", {
eventWrapper.appendChild(titleWrapper); eventWrapper.appendChild(titleWrapper);
} else { } else {
timeWrapper = document.createElement("td"); const timeWrapper = document.createElement("td");
eventWrapper.appendChild(titleWrapper); eventWrapper.appendChild(titleWrapper);
var now = new Date(); const now = new Date();
if (this.config.timeFormat === "absolute") { if (this.config.timeFormat === "absolute") {
// Use dateFormat // Use dateFormat
@ -408,15 +404,15 @@ Module.register("calendar", {
if (this.config.showLocation) { if (this.config.showLocation) {
if (event.location !== false) { if (event.location !== false) {
var locationRow = document.createElement("tr"); const locationRow = document.createElement("tr");
locationRow.className = "normal xsmall light"; locationRow.className = "normal xsmall light";
if (this.config.displaySymbol) { if (this.config.displaySymbol) {
var symbolCell = document.createElement("td"); const symbolCell = document.createElement("td");
locationRow.appendChild(symbolCell); locationRow.appendChild(symbolCell);
} }
var descCell = document.createElement("td"); const descCell = document.createElement("td");
descCell.className = "location"; descCell.className = "location";
descCell.colSpan = "2"; descCell.colSpan = "2";
descCell.innerHTML = this.titleTransform(event.location, this.config.locationTitleReplace, this.config.wrapLocationEvents, this.config.maxLocationTitleLength, this.config.maxEventTitleLines); descCell.innerHTML = this.titleTransform(event.location, this.config.locationTitleReplace, this.config.wrapLocationEvents, this.config.maxLocationTitleLength, this.config.maxEventTitleLines);
@ -464,8 +460,7 @@ Module.register("calendar", {
* @returns {boolean} True if the calendar config contains the url, False otherwise * @returns {boolean} True if the calendar config contains the url, False otherwise
*/ */
hasCalendarURL: function (url) { hasCalendarURL: function (url) {
for (var c in this.config.calendars) { for (const calendar of this.config.calendars) {
var calendar = this.config.calendars[c];
if (calendar.url === url) { if (calendar.url === url) {
return true; return true;
} }
@ -480,10 +475,11 @@ Module.register("calendar", {
* @returns {object[]} Array with events. * @returns {object[]} Array with events.
*/ */
createEventList: function () { createEventList: function () {
var events = []; const now = new Date();
var today = moment().startOf("day"); const today = moment().startOf("day");
var now = new Date(); const future = moment().startOf("day").add(this.config.maximumNumberOfDays, "days").toDate();
var future = moment().startOf("day").add(this.config.maximumNumberOfDays, "days").toDate(); let events = [];
for (var c in this.calendarData) { for (var c in this.calendarData) {
var calendar = this.calendarData[c]; var calendar = this.calendarData[c];
for (var e in calendar) { for (var e in calendar) {
@ -512,11 +508,11 @@ Module.register("calendar", {
/* if sliceMultiDayEvents is set to true, multiday events (events exceeding at least one midnight) are sliced into days, /* if sliceMultiDayEvents is set to true, multiday events (events exceeding at least one midnight) are sliced into days,
* otherwise, esp. in dateheaders mode it is not clear how long these events are. * otherwise, esp. in dateheaders mode it is not clear how long these events are.
*/ */
var maxCount = Math.ceil((event.endDate - 1 - moment(event.startDate, "x").endOf("day").format("x")) / (1000 * 60 * 60 * 24)) + 1; const maxCount = Math.ceil((event.endDate - 1 - moment(event.startDate, "x").endOf("day").format("x")) / (1000 * 60 * 60 * 24)) + 1;
if (this.config.sliceMultiDayEvents && maxCount > 1) { if (this.config.sliceMultiDayEvents && maxCount > 1) {
var splitEvents = []; const splitEvents = [];
var midnight = moment(event.startDate, "x").clone().startOf("day").add(1, "day").format("x"); let midnight = moment(event.startDate, "x").clone().startOf("day").add(1, "day").format("x");
var count = 1; let count = 1;
while (event.endDate > midnight) { while (event.endDate > midnight) {
var thisEvent = JSON.parse(JSON.stringify(event)); // clone object var thisEvent = JSON.parse(JSON.stringify(event)); // clone object
thisEvent.today = thisEvent.startDate >= today && thisEvent.startDate < today + 24 * 60 * 60 * 1000; thisEvent.today = thisEvent.startDate >= today && thisEvent.startDate < today + 24 * 60 * 60 * 1000;
@ -532,9 +528,9 @@ Module.register("calendar", {
event.title += " (" + count + "/" + maxCount + ")"; event.title += " (" + count + "/" + maxCount + ")";
splitEvents.push(event); splitEvents.push(event);
for (event of splitEvents) { for (let splitEvent of splitEvents) {
if (event.endDate > now && event.endDate <= future) { if (splitEvent.endDate > now && splitEvent.endDate <= future) {
events.push(event); events.push(splitEvent);
} }
} }
} else { } else {
@ -550,12 +546,11 @@ Module.register("calendar", {
// Limit the number of days displayed // Limit the number of days displayed
// If limitDays is set > 0, limit display to that number of days // If limitDays is set > 0, limit display to that number of days
if (this.config.limitDays > 0) { if (this.config.limitDays > 0) {
var newEvents = []; let newEvents = [];
var lastDate = today.clone().subtract(1, "days").format("YYYYMMDD"); let lastDate = today.clone().subtract(1, "days").format("YYYYMMDD");
var days = 0; let days = 0;
var eventDate; for (const ev of events) {
for (var ev of events) { let eventDate = moment(ev.startDate, "x").format("YYYYMMDD");
eventDate = moment(ev.startDate, "x").format("YYYYMMDD");
// if date of event is later than lastdate // if date of event is later than lastdate
// check if we already are showing max unique days // check if we already are showing max unique days
if (eventDate > lastDate) { if (eventDate > lastDate) {
@ -579,7 +574,7 @@ Module.register("calendar", {
}, },
listContainsEvent: function (eventList, event) { listContainsEvent: function (eventList, event) {
for (var evt of eventList) { for (const evt of eventList) {
if (evt.title === event.title && parseInt(evt.startDate) === parseInt(event.startDate)) { if (evt.title === event.title && parseInt(evt.startDate) === parseInt(event.startDate)) {
return true; return true;
} }
@ -708,8 +703,7 @@ Module.register("calendar", {
* @returns {*} The property * @returns {*} The property
*/ */
getCalendarProperty: function (url, property, defaultValue) { getCalendarProperty: function (url, property, defaultValue) {
for (var c in this.config.calendars) { for (const calendar of this.config.calendars) {
var calendar = this.config.calendars[c];
if (calendar.url === url && calendar.hasOwnProperty(property)) { if (calendar.url === url && calendar.hasOwnProperty(property)) {
return calendar[property]; return calendar[property];
} }

View File

@ -10,7 +10,7 @@ const afterEach = global.afterEach;
describe("Calendar module", function () { describe("Calendar module", function () {
helpers.setupTimeout(this); helpers.setupTimeout(this);
var app = null; let app = null;
beforeEach(function () { beforeEach(function () {
return helpers return helpers

View File

@ -45,42 +45,42 @@ describe("Functions into modules/default/calendar/calendar.js", function () {
}); });
it("should return a 12-hour longDateFormat when using the 'en' locale", function () { it("should return a 12-hour longDateFormat when using the 'en' locale", function () {
var localeBackup = moment.locale(); const localeBackup = moment.locale();
moment.locale("en"); moment.locale("en");
expect(Module.definitions.calendar.getLocaleSpecification()).to.deep.equal({ longDateFormat: { LT: "h:mm A" } }); expect(Module.definitions.calendar.getLocaleSpecification()).to.deep.equal({ longDateFormat: { LT: "h:mm A" } });
moment.locale(localeBackup); moment.locale(localeBackup);
}); });
it("should return a 12-hour longDateFormat when using the 'au' locale", function () { it("should return a 12-hour longDateFormat when using the 'au' locale", function () {
var localeBackup = moment.locale(); const localeBackup = moment.locale();
moment.locale("au"); moment.locale("au");
expect(Module.definitions.calendar.getLocaleSpecification()).to.deep.equal({ longDateFormat: { LT: "h:mm A" } }); expect(Module.definitions.calendar.getLocaleSpecification()).to.deep.equal({ longDateFormat: { LT: "h:mm A" } });
moment.locale(localeBackup); moment.locale(localeBackup);
}); });
it("should return a 12-hour longDateFormat when using the 'eg' locale", function () { it("should return a 12-hour longDateFormat when using the 'eg' locale", function () {
var localeBackup = moment.locale(); const localeBackup = moment.locale();
moment.locale("eg"); moment.locale("eg");
expect(Module.definitions.calendar.getLocaleSpecification()).to.deep.equal({ longDateFormat: { LT: "h:mm A" } }); expect(Module.definitions.calendar.getLocaleSpecification()).to.deep.equal({ longDateFormat: { LT: "h:mm A" } });
moment.locale(localeBackup); moment.locale(localeBackup);
}); });
it("should return a 24-hour longDateFormat when using the 'nl' locale", function () { it("should return a 24-hour longDateFormat when using the 'nl' locale", function () {
var localeBackup = moment.locale(); const localeBackup = moment.locale();
moment.locale("nl"); moment.locale("nl");
expect(Module.definitions.calendar.getLocaleSpecification()).to.deep.equal({ longDateFormat: { LT: "HH:mm" } }); expect(Module.definitions.calendar.getLocaleSpecification()).to.deep.equal({ longDateFormat: { LT: "HH:mm" } });
moment.locale(localeBackup); moment.locale(localeBackup);
}); });
it("should return a 24-hour longDateFormat when using the 'fr' locale", function () { it("should return a 24-hour longDateFormat when using the 'fr' locale", function () {
var localeBackup = moment.locale(); const localeBackup = moment.locale();
moment.locale("fr"); moment.locale("fr");
expect(Module.definitions.calendar.getLocaleSpecification()).to.deep.equal({ longDateFormat: { LT: "HH:mm" } }); expect(Module.definitions.calendar.getLocaleSpecification()).to.deep.equal({ longDateFormat: { LT: "HH:mm" } });
moment.locale(localeBackup); moment.locale(localeBackup);
}); });
it("should return a 24-hour longDateFormat when using the 'uk' locale", function () { it("should return a 24-hour longDateFormat when using the 'uk' locale", function () {
var localeBackup = moment.locale(); const localeBackup = moment.locale();
moment.locale("uk"); moment.locale("uk");
expect(Module.definitions.calendar.getLocaleSpecification()).to.deep.equal({ longDateFormat: { LT: "HH:mm" } }); expect(Module.definitions.calendar.getLocaleSpecification()).to.deep.equal({ longDateFormat: { LT: "HH:mm" } });
moment.locale(localeBackup); moment.locale(localeBackup);