mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-06-27 19:53:36 +00:00
Use object.entries to iterate over data
This commit is contained in:
parent
1e5bd98f02
commit
daa6f5051c
@ -70,260 +70,257 @@ const CalendarFetcher = function (url, reloadInterval, excludedEvents, maximumEn
|
|||||||
return event[time].length === 8 ? moment(event[time], "YYYYMMDD") : moment(new Date(event[time]));
|
return event[time].length === 8 ? moment(event[time], "YYYYMMDD") : moment(new Date(event[time]));
|
||||||
};
|
};
|
||||||
|
|
||||||
for (let k in data) {
|
Object.entries(data).forEach(([key, event]) => {
|
||||||
if (data.hasOwnProperty(k)) {
|
const now = new Date();
|
||||||
const event = data[k];
|
const today = moment().startOf("day").toDate();
|
||||||
const now = new Date();
|
const future = moment().startOf("day").add(maximumNumberOfDays, "days").subtract(1, "seconds").toDate(); // Subtract 1 second so that events that start on the middle of the night will not repeat.
|
||||||
const today = moment().startOf("day").toDate();
|
let past = today;
|
||||||
const future = moment().startOf("day").add(maximumNumberOfDays, "days").subtract(1, "seconds").toDate(); // Subtract 1 second so that events that start on the middle of the night will not repeat.
|
|
||||||
let past = today;
|
|
||||||
|
|
||||||
if (includePastEvents) {
|
if (includePastEvents) {
|
||||||
past = moment().startOf("day").subtract(maximumNumberOfDays, "days").toDate();
|
past = moment().startOf("day").subtract(maximumNumberOfDays, "days").toDate();
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: Ugly fix to solve the facebook birthday issue.
|
||||||
|
// Otherwise, the recurring events only show the birthday for next year.
|
||||||
|
let isFacebookBirthday = false;
|
||||||
|
if (typeof event.uid !== "undefined") {
|
||||||
|
if (event.uid.indexOf("@facebook.com") !== -1) {
|
||||||
|
isFacebookBirthday = true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// FIXME: Ugly fix to solve the facebook birthday issue.
|
if (event.type === "VEVENT") {
|
||||||
// Otherwise, the recurring events only show the birthday for next year.
|
let startDate = eventDate(event, "start");
|
||||||
let isFacebookBirthday = false;
|
let endDate;
|
||||||
if (typeof event.uid !== "undefined") {
|
|
||||||
if (event.uid.indexOf("@facebook.com") !== -1) {
|
|
||||||
isFacebookBirthday = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (event.type === "VEVENT") {
|
if (typeof event.end !== "undefined") {
|
||||||
let startDate = eventDate(event, "start");
|
endDate = eventDate(event, "end");
|
||||||
let endDate;
|
} else if (typeof event.duration !== "undefined") {
|
||||||
|
endDate = startDate.clone().add(moment.duration(event.duration));
|
||||||
if (typeof event.end !== "undefined") {
|
} else {
|
||||||
endDate = eventDate(event, "end");
|
if (!isFacebookBirthday) {
|
||||||
} else if (typeof event.duration !== "undefined") {
|
endDate = startDate;
|
||||||
endDate = startDate.clone().add(moment.duration(event.duration));
|
|
||||||
} else {
|
} else {
|
||||||
if (!isFacebookBirthday) {
|
endDate = moment(startDate).add(1, "days");
|
||||||
endDate = startDate;
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// calculate the duration of the event for use with recurring events.
|
||||||
|
let duration = parseInt(endDate.format("x")) - parseInt(startDate.format("x"));
|
||||||
|
|
||||||
|
if (event.start.length === 8) {
|
||||||
|
startDate = startDate.startOf("day");
|
||||||
|
}
|
||||||
|
|
||||||
|
const title = getTitleFromEvent(event);
|
||||||
|
|
||||||
|
let excluded = false,
|
||||||
|
dateFilter = null;
|
||||||
|
|
||||||
|
for (let f in excludedEvents) {
|
||||||
|
let filter = excludedEvents[f],
|
||||||
|
testTitle = title.toLowerCase(),
|
||||||
|
until = null,
|
||||||
|
useRegex = false,
|
||||||
|
regexFlags = "g";
|
||||||
|
|
||||||
|
if (filter instanceof Object) {
|
||||||
|
if (typeof filter.until !== "undefined") {
|
||||||
|
until = filter.until;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof filter.regex !== "undefined") {
|
||||||
|
useRegex = filter.regex;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If additional advanced filtering is added in, this section
|
||||||
|
// must remain last as we overwrite the filter object with the
|
||||||
|
// filterBy string
|
||||||
|
if (filter.caseSensitive) {
|
||||||
|
filter = filter.filterBy;
|
||||||
|
testTitle = title;
|
||||||
|
} else if (useRegex) {
|
||||||
|
filter = filter.filterBy;
|
||||||
|
testTitle = title;
|
||||||
|
regexFlags += "i";
|
||||||
} else {
|
} else {
|
||||||
endDate = moment(startDate).add(1, "days");
|
filter = filter.filterBy.toLowerCase();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
filter = filter.toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (testTitleByFilter(testTitle, filter, useRegex, regexFlags)) {
|
||||||
|
if (until) {
|
||||||
|
dateFilter = until;
|
||||||
|
} else {
|
||||||
|
excluded = true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (excluded) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const location = event.location || false;
|
||||||
|
const geo = event.geo || false;
|
||||||
|
const description = event.description || false;
|
||||||
|
|
||||||
|
if (typeof event.rrule !== "undefined" && event.rrule !== null && !isFacebookBirthday) {
|
||||||
|
const rule = event.rrule;
|
||||||
|
let addedEvents = 0;
|
||||||
|
|
||||||
|
const pastMoment = moment(past);
|
||||||
|
const futureMoment = moment(future);
|
||||||
|
|
||||||
|
// can cause problems with e.g. birthdays before 1900
|
||||||
|
if ((rule.options && rule.origOptions && rule.origOptions.dtstart && rule.origOptions.dtstart.getFullYear() < 1900) || (rule.options && rule.options.dtstart && rule.options.dtstart.getFullYear() < 1900)) {
|
||||||
|
rule.origOptions.dtstart.setYear(1900);
|
||||||
|
rule.options.dtstart.setYear(1900);
|
||||||
|
}
|
||||||
|
|
||||||
|
// For recurring events, get the set of start dates that fall within the range
|
||||||
|
// of dates we're looking for.
|
||||||
|
// kblankenship1989 - to fix issue #1798, converting all dates to locale time first, then converting back to UTC time
|
||||||
|
const pastLocal = pastMoment.subtract(past.getTimezoneOffset(), "minutes").toDate();
|
||||||
|
const futureLocal = futureMoment.subtract(future.getTimezoneOffset(), "minutes").toDate();
|
||||||
|
const datesLocal = rule.between(pastLocal, futureLocal, true, limitFunction);
|
||||||
|
const dates = datesLocal.map(function (dateLocal) {
|
||||||
|
return moment(dateLocal).add(dateLocal.getTimezoneOffset(), "minutes").toDate();
|
||||||
|
});
|
||||||
|
|
||||||
|
// The "dates" array contains the set of dates within our desired date range range that are valid
|
||||||
|
// for the recurrence rule. *However*, it's possible for us to have a specific recurrence that
|
||||||
|
// had its date changed from outside the range to inside the range. For the time being,
|
||||||
|
// we'll handle this by adding *all* recurrence entries into the set of dates that we check,
|
||||||
|
// because the logic below will filter out any recurrences that don't actually belong within
|
||||||
|
// our display range.
|
||||||
|
// Would be great if there was a better way to handle this.
|
||||||
|
if (event.recurrences !== undefined) {
|
||||||
|
for (let r in event.recurrences) {
|
||||||
|
// Only add dates that weren't already in the range we added from the rrule so that
|
||||||
|
// we don"t double-add those events.
|
||||||
|
if (moment(new Date(r)).isBetween(pastMoment, futureMoment) !== true) {
|
||||||
|
dates.push(new Date(r));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// calculate the duration of the event for use with recurring events.
|
// Loop through the set of date entries to see which recurrences should be added to our event list.
|
||||||
let duration = parseInt(endDate.format("x")) - parseInt(startDate.format("x"));
|
for (let d in dates) {
|
||||||
|
const date = dates[d];
|
||||||
|
// ical.js started returning recurrences and exdates as ISOStrings without time information.
|
||||||
|
// .toISOString().substring(0,10) is the method they use to calculate keys, so we'll do the same
|
||||||
|
// (see https://github.com/peterbraden/ical.js/pull/84 )
|
||||||
|
const dateKey = date.toISOString().substring(0, 10);
|
||||||
|
let curEvent = event;
|
||||||
|
let showRecurrence = true;
|
||||||
|
|
||||||
if (event.start.length === 8) {
|
// Stop parsing this event's recurrences if we've already found maximumEntries worth of recurrences.
|
||||||
startDate = startDate.startOf("day");
|
// (The logic below would still filter the extras, but the check is simple since we're already tracking the count)
|
||||||
}
|
if (addedEvents >= maximumEntries) {
|
||||||
|
|
||||||
const title = getTitleFromEvent(event);
|
|
||||||
|
|
||||||
let excluded = false,
|
|
||||||
dateFilter = null;
|
|
||||||
|
|
||||||
for (let f in excludedEvents) {
|
|
||||||
let filter = excludedEvents[f],
|
|
||||||
testTitle = title.toLowerCase(),
|
|
||||||
until = null,
|
|
||||||
useRegex = false,
|
|
||||||
regexFlags = "g";
|
|
||||||
|
|
||||||
if (filter instanceof Object) {
|
|
||||||
if (typeof filter.until !== "undefined") {
|
|
||||||
until = filter.until;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof filter.regex !== "undefined") {
|
|
||||||
useRegex = filter.regex;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If additional advanced filtering is added in, this section
|
|
||||||
// must remain last as we overwrite the filter object with the
|
|
||||||
// filterBy string
|
|
||||||
if (filter.caseSensitive) {
|
|
||||||
filter = filter.filterBy;
|
|
||||||
testTitle = title;
|
|
||||||
} else if (useRegex) {
|
|
||||||
filter = filter.filterBy;
|
|
||||||
testTitle = title;
|
|
||||||
regexFlags += "i";
|
|
||||||
} else {
|
|
||||||
filter = filter.filterBy.toLowerCase();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
filter = filter.toLowerCase();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (testTitleByFilter(testTitle, filter, useRegex, regexFlags)) {
|
|
||||||
if (until) {
|
|
||||||
dateFilter = until;
|
|
||||||
} else {
|
|
||||||
excluded = true;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (excluded) {
|
startDate = moment(date);
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const location = event.location || false;
|
// For each date that we're checking, it's possible that there is a recurrence override for that one day.
|
||||||
const geo = event.geo || false;
|
if (curEvent.recurrences !== undefined && curEvent.recurrences[dateKey] !== undefined) {
|
||||||
const description = event.description || false;
|
// We found an override, so for this recurrence, use a potentially different title, start date, and duration.
|
||||||
|
curEvent = curEvent.recurrences[dateKey];
|
||||||
if (typeof event.rrule !== "undefined" && event.rrule !== null && !isFacebookBirthday) {
|
startDate = moment(curEvent.start);
|
||||||
const rule = event.rrule;
|
duration = parseInt(moment(curEvent.end).format("x")) - parseInt(startDate.format("x"));
|
||||||
let addedEvents = 0;
|
}
|
||||||
|
// If there's no recurrence override, check for an exception date. Exception dates represent exceptions to the rule.
|
||||||
const pastMoment = moment(past);
|
else if (curEvent.exdate !== undefined && curEvent.exdate[dateKey] !== undefined) {
|
||||||
const futureMoment = moment(future);
|
// This date is an exception date, which means we should skip it in the recurrence pattern.
|
||||||
|
showRecurrence = false;
|
||||||
// can cause problems with e.g. birthdays before 1900
|
|
||||||
if ((rule.options && rule.origOptions && rule.origOptions.dtstart && rule.origOptions.dtstart.getFullYear() < 1900) || (rule.options && rule.options.dtstart && rule.options.dtstart.getFullYear() < 1900)) {
|
|
||||||
rule.origOptions.dtstart.setYear(1900);
|
|
||||||
rule.options.dtstart.setYear(1900);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// For recurring events, get the set of start dates that fall within the range
|
endDate = moment(parseInt(startDate.format("x")) + duration, "x");
|
||||||
// of dates we're looking for.
|
if (startDate.format("x") === endDate.format("x")) {
|
||||||
// kblankenship1989 - to fix issue #1798, converting all dates to locale time first, then converting back to UTC time
|
endDate = endDate.endOf("day");
|
||||||
const pastLocal = pastMoment.subtract(past.getTimezoneOffset(), "minutes").toDate();
|
|
||||||
const futureLocal = futureMoment.subtract(future.getTimezoneOffset(), "minutes").toDate();
|
|
||||||
const datesLocal = rule.between(pastLocal, futureLocal, true, limitFunction);
|
|
||||||
const dates = datesLocal.map(function (dateLocal) {
|
|
||||||
return moment(dateLocal).add(dateLocal.getTimezoneOffset(), "minutes").toDate();
|
|
||||||
});
|
|
||||||
|
|
||||||
// The "dates" array contains the set of dates within our desired date range range that are valid
|
|
||||||
// for the recurrence rule. *However*, it's possible for us to have a specific recurrence that
|
|
||||||
// had its date changed from outside the range to inside the range. For the time being,
|
|
||||||
// we'll handle this by adding *all* recurrence entries into the set of dates that we check,
|
|
||||||
// because the logic below will filter out any recurrences that don't actually belong within
|
|
||||||
// our display range.
|
|
||||||
// Would be great if there was a better way to handle this.
|
|
||||||
if (event.recurrences !== undefined) {
|
|
||||||
for (let r in event.recurrences) {
|
|
||||||
// Only add dates that weren't already in the range we added from the rrule so that
|
|
||||||
// we don"t double-add those events.
|
|
||||||
if (moment(new Date(r)).isBetween(pastMoment, futureMoment) !== true) {
|
|
||||||
dates.push(new Date(r));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Loop through the set of date entries to see which recurrences should be added to our event list.
|
const recurrenceTitle = getTitleFromEvent(curEvent);
|
||||||
for (let d in dates) {
|
|
||||||
const date = dates[d];
|
|
||||||
// ical.js started returning recurrences and exdates as ISOStrings without time information.
|
|
||||||
// .toISOString().substring(0,10) is the method they use to calculate keys, so we'll do the same
|
|
||||||
// (see https://github.com/peterbraden/ical.js/pull/84 )
|
|
||||||
const dateKey = date.toISOString().substring(0, 10);
|
|
||||||
let curEvent = event;
|
|
||||||
let showRecurrence = true;
|
|
||||||
|
|
||||||
// Stop parsing this event's recurrences if we've already found maximumEntries worth of recurrences.
|
// If this recurrence ends before the start of the date range, or starts after the end of the date range, don"t add
|
||||||
// (The logic below would still filter the extras, but the check is simple since we're already tracking the count)
|
// it to the event list.
|
||||||
if (addedEvents >= maximumEntries) {
|
if (endDate.isBefore(past) || startDate.isAfter(future)) {
|
||||||
break;
|
showRecurrence = false;
|
||||||
}
|
|
||||||
|
|
||||||
startDate = moment(date);
|
|
||||||
|
|
||||||
// For each date that we're checking, it's possible that there is a recurrence override for that one day.
|
|
||||||
if (curEvent.recurrences !== undefined && curEvent.recurrences[dateKey] !== undefined) {
|
|
||||||
// We found an override, so for this recurrence, use a potentially different title, start date, and duration.
|
|
||||||
curEvent = curEvent.recurrences[dateKey];
|
|
||||||
startDate = moment(curEvent.start);
|
|
||||||
duration = parseInt(moment(curEvent.end).format("x")) - parseInt(startDate.format("x"));
|
|
||||||
}
|
|
||||||
// If there's no recurrence override, check for an exception date. Exception dates represent exceptions to the rule.
|
|
||||||
else if (curEvent.exdate !== undefined && curEvent.exdate[dateKey] !== undefined) {
|
|
||||||
// This date is an exception date, which means we should skip it in the recurrence pattern.
|
|
||||||
showRecurrence = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
endDate = moment(parseInt(startDate.format("x")) + duration, "x");
|
|
||||||
if (startDate.format("x") === endDate.format("x")) {
|
|
||||||
endDate = endDate.endOf("day");
|
|
||||||
}
|
|
||||||
|
|
||||||
const recurrenceTitle = getTitleFromEvent(curEvent);
|
|
||||||
|
|
||||||
// If this recurrence ends before the start of the date range, or starts after the end of the date range, don"t add
|
|
||||||
// it to the event list.
|
|
||||||
if (endDate.isBefore(past) || startDate.isAfter(future)) {
|
|
||||||
showRecurrence = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (timeFilterApplies(now, endDate, dateFilter)) {
|
|
||||||
showRecurrence = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (showRecurrence === true && addedEvents < maximumEntries) {
|
|
||||||
addedEvents++;
|
|
||||||
newEvents.push({
|
|
||||||
title: recurrenceTitle,
|
|
||||||
startDate: startDate.format("x"),
|
|
||||||
endDate: endDate.format("x"),
|
|
||||||
fullDayEvent: isFullDayEvent(event),
|
|
||||||
class: event.class,
|
|
||||||
firstYear: event.start.getFullYear(),
|
|
||||||
location: location,
|
|
||||||
geo: geo,
|
|
||||||
description: description
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// end recurring event parsing
|
|
||||||
} else {
|
|
||||||
// Single event.
|
|
||||||
const fullDayEvent = isFacebookBirthday ? true : isFullDayEvent(event);
|
|
||||||
|
|
||||||
if (includePastEvents) {
|
|
||||||
// Past event is too far in the past, so skip.
|
|
||||||
if (endDate < past) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// It's not a fullday event, and it is in the past, so skip.
|
|
||||||
if (!fullDayEvent && endDate < new Date()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// It's a fullday event, and it is before today, So skip.
|
|
||||||
if (fullDayEvent && endDate <= today) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// It exceeds the maximumNumberOfDays limit, so skip.
|
|
||||||
if (startDate > future) {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (timeFilterApplies(now, endDate, dateFilter)) {
|
if (timeFilterApplies(now, endDate, dateFilter)) {
|
||||||
continue;
|
showRecurrence = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adjust start date so multiple day events will be displayed as happening today even though they started some days ago already
|
if (showRecurrence === true && addedEvents < maximumEntries) {
|
||||||
if (fullDayEvent && startDate <= today) {
|
addedEvents++;
|
||||||
startDate = moment(today);
|
newEvents.push({
|
||||||
|
title: recurrenceTitle,
|
||||||
|
startDate: startDate.format("x"),
|
||||||
|
endDate: endDate.format("x"),
|
||||||
|
fullDayEvent: isFullDayEvent(event),
|
||||||
|
class: event.class,
|
||||||
|
firstYear: event.start.getFullYear(),
|
||||||
|
location: location,
|
||||||
|
geo: geo,
|
||||||
|
description: description
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Every thing is good. Add it to the list.
|
|
||||||
newEvents.push({
|
|
||||||
title: title,
|
|
||||||
startDate: startDate.format("x"),
|
|
||||||
endDate: endDate.format("x"),
|
|
||||||
fullDayEvent: fullDayEvent,
|
|
||||||
class: event.class,
|
|
||||||
location: location,
|
|
||||||
geo: geo,
|
|
||||||
description: description
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
// end recurring event parsing
|
||||||
|
} else {
|
||||||
|
// Single event.
|
||||||
|
const fullDayEvent = isFacebookBirthday ? true : isFullDayEvent(event);
|
||||||
|
|
||||||
|
if (includePastEvents) {
|
||||||
|
// Past event is too far in the past, so skip.
|
||||||
|
if (endDate < past) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// It's not a fullday event, and it is in the past, so skip.
|
||||||
|
if (!fullDayEvent && endDate < new Date()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// It's a fullday event, and it is before today, So skip.
|
||||||
|
if (fullDayEvent && endDate <= today) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// It exceeds the maximumNumberOfDays limit, so skip.
|
||||||
|
if (startDate > future) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (timeFilterApplies(now, endDate, dateFilter)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Adjust start date so multiple day events will be displayed as happening today even though they started some days ago already
|
||||||
|
if (fullDayEvent && startDate <= today) {
|
||||||
|
startDate = moment(today);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Every thing is good. Add it to the list.
|
||||||
|
newEvents.push({
|
||||||
|
title: title,
|
||||||
|
startDate: startDate.format("x"),
|
||||||
|
endDate: endDate.format("x"),
|
||||||
|
fullDayEvent: fullDayEvent,
|
||||||
|
class: event.class,
|
||||||
|
location: location,
|
||||||
|
geo: geo,
|
||||||
|
description: description
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
newEvents.sort(function (a, b) {
|
newEvents.sort(function (a, b) {
|
||||||
return a.startDate - b.startDate;
|
return a.startDate - b.startDate;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user