mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-09-14 16:13:57 +00:00
Fix limitdays regression (#3863)
fixes #3840 this is a rewrite --------- Co-authored-by: veeck <gitkraken@veeck.de>
This commit is contained in:
@@ -705,30 +705,24 @@ Module.register("calendar", {
|
||||
* Limit the number of days displayed
|
||||
* If limitDays is set > 0, limit display to that number of days
|
||||
*/
|
||||
if (this.config.limitDays > 0) {
|
||||
let newEvents = [];
|
||||
let lastDate = today.clone().subtract(1, "days");
|
||||
let days = 0;
|
||||
for (const ev of events) {
|
||||
let eventDate = this.timestampToMoment(ev.startDate);
|
||||
if (this.config.limitDays > 0 && events.length > 0) { // watch out for initial display before events arrive from helper
|
||||
// Group all events by date, events on the same date will be in a list with the key being the date.
|
||||
const eventsByDate = Object.groupBy(events, (ev) => this.timestampToMoment(ev.startDate).format("YYYY-MM-DD"));
|
||||
const newEvents = [];
|
||||
let currentDate = moment();
|
||||
let daysCollected = 0;
|
||||
|
||||
/*
|
||||
* if date of event is later than lastdate
|
||||
* check if we already are showing max unique days
|
||||
*/
|
||||
if (eventDate.isAfter(lastDate)) {
|
||||
// if the only entry in the first day is a full day event that day is not counted as unique
|
||||
if (!this.config.limitDaysNeverSkip && newEvents.length === 1 && days === 1 && newEvents[0].fullDayEvent) {
|
||||
days--;
|
||||
}
|
||||
days++;
|
||||
if (days > this.config.limitDays) {
|
||||
continue;
|
||||
} else {
|
||||
lastDate = eventDate;
|
||||
}
|
||||
while (daysCollected < this.config.limitDays) {
|
||||
const dateStr = currentDate.format("YYYY-MM-DD");
|
||||
// Check if there are events on the currentDate
|
||||
if (eventsByDate[dateStr] && eventsByDate[dateStr].length > 0) {
|
||||
// If there are any events today then get all those events and select the currently active events and the events that are starting later in the day.
|
||||
newEvents.push(...eventsByDate[dateStr].filter((ev) => this.timestampToMoment(ev.endDate).isAfter(moment())));
|
||||
// Since we found a day with events, increase the daysCollected by 1
|
||||
daysCollected++;
|
||||
}
|
||||
newEvents.push(ev);
|
||||
// Search for the next day
|
||||
currentDate.add(1, "day");
|
||||
}
|
||||
events = newEvents;
|
||||
}
|
||||
|
Reference in New Issue
Block a user