@@ -705,30 +705,24 @@ Module.register("calendar", {
705705 * Limit the number of days displayed
706706 * If limitDays is set > 0, limit display to that number of days
707707 */
708- if ( this . config . limitDays > 0 ) {
709- let newEvents = [ ] ;
710- let lastDate = today . clone ( ) . subtract ( 1 , "days" ) ;
711- let days = 0 ;
712- for ( const ev of events ) {
713- let eventDate = this . timestampToMoment ( ev . startDate ) ;
714-
715- /*
716- * if date of event is later than lastdate
717- * check if we already are showing max unique days
718- */
719- if ( eventDate . isAfter ( lastDate ) ) {
720- // if the only entry in the first day is a full day event that day is not counted as unique
721- if ( ! this . config . limitDaysNeverSkip && newEvents . length === 1 && days === 1 && newEvents [ 0 ] . fullDayEvent ) {
722- days -- ;
723- }
724- days ++ ;
725- if ( days > this . config . limitDays ) {
726- continue ;
727- } else {
728- lastDate = eventDate ;
729- }
708+ if ( this . config . limitDays > 0 && events . length > 0 ) {
709+ // Group all events by date, events on the same date will be in a list with the key being the date.
710+ const eventsByDate = Object . groupBy ( events , ( ev ) => this . timestampToMoment ( ev . startDate ) . format ( "YYYY-MM-DD" ) ) ;
711+ const newEvents = [ ] ;
712+ let currentDate = moment ( ) ;
713+ let daysCollected = 0 ;
714+
715+ while ( daysCollected < this . config . limitDays ) {
716+ const dateStr = currentDate . format ( "YYYY-MM-DD" ) ;
717+ // Check if there are events on the currentDate
718+ if ( eventsByDate [ dateStr ] && eventsByDate [ dateStr ] . length > 0 ) {
719+ // 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.
720+ newEvents . push ( ...eventsByDate [ dateStr ] . filter ( ( ev ) => this . timestampToMoment ( ev . endDate ) . isAfter ( moment ( ) ) ) ) ;
721+ // Since we found a day with events, increase the daysCollected by 1
722+ daysCollected ++ ;
730723 }
731- newEvents . push ( ev ) ;
724+ // Search for the next day
725+ currentDate . add ( 1 , "day" ) ;
732726 }
733727 events = newEvents ;
734728 }
0 commit comments