1+ import { mdiCurrencyMnt } from '@mdi/js' ;
12import dayjs from 'dayjs' ;
23
34import EventClass from '../lib/event.class' ;
45
56// Function to sort events
6- export default function sortEvents ( events , config ) {
7+ export default function sortEvents ( events : EventClass [ ] , config ) {
78 const currentDateTime = dayjs ( ) ; // Current date and time
9+ const sortedEvents : EventClass [ ] = [ ...events ] ; // Array to manipulate events.
10+ const allDayEventsArray : EventClass [ ] = [ ] ; // Array to store the all-day events.
811
9- events . sort ( ( a , b ) => {
10- const isRunningA = currentDateTime . isBetween ( a . startDateTime , a . endDateTime ) ;
11- const isRunningB = currentDateTime . isBetween ( b . startDateTime , b . endDateTime ) ;
12-
13- if ( isRunningA && ! isRunningB ) {
14- return - 1 ;
15- } else if ( ! isRunningA && isRunningB ) {
16- return 1 ;
17- } else {
18- const timeDiffA = Math . min (
19- Math . abs ( a . startDateTime . diff ( currentDateTime ) ) ,
20- Math . abs ( a . endDateTime . diff ( currentDateTime ) ) ,
21- ) ;
22-
23- const timeDiffB = Math . min (
24- Math . abs ( b . startDateTime . diff ( currentDateTime ) ) ,
25- Math . abs ( b . endDateTime . diff ( currentDateTime ) ) ,
26- ) ;
27-
28- return timeDiffA - timeDiffB ;
29- }
30- } ) ;
31-
32- // Combine sorted all-day and non-all-day events
33- const sortedEvents = [ ...events ] ;
34-
35- // Create an array to store the all day events.
36- const allDayEventsArray : EventClass [ ] = [ ] ;
37-
38- // Iterate over the sorted events array and move all of the all day events to the allDayEvents array.
39- for ( let i = sortedEvents . length - 1 ; i >= 0 ; i -- ) {
40- const event = sortedEvents [ i ] ;
12+ // Iterate over the events array and move all all-day events to the allDayEvents array.
13+ for ( let i = events . length - 1 ; i >= 0 ; i -- ) {
14+ const event = events [ i ] ;
4115 if ( event . isAllDayEvent ) {
4216 allDayEventsArray . push ( event ) ;
4317 sortedEvents . splice ( i , 1 ) ;
4418 }
4519 }
4620
47- // Sort the all day events.
21+ // Sort the all- day events.
4822 allDayEventsArray . sort ( ( a , b ) => {
4923 if ( a . entity !== b . entity ) {
5024 return a . entity . entity_id . localeCompare ( b . entity ) ;
@@ -53,24 +27,76 @@ export default function sortEvents(events, config) {
5327 }
5428 } ) ;
5529
56- // If config.allDayBottom is true, add the all day events to the end of the sorted events array.
57- if ( config . allDayBottom ) {
58- sortedEvents . push ( ... allDayEventsArray ) ;
59- }
30+ if ( config . sortBy === 'milestone' ) {
31+ sortedEvents . sort ( ( a , b ) => {
32+ const aStartDiff = a . startDateTime . diff ( currentDateTime ) ;
33+ const bStartDiff = b . startDateTime . diff ( currentDateTime ) ;
6034
61- // Otherwise, add the all day events to the beginning of the sorted events array.
62- else {
63- sortedEvents . unshift ( ...allDayEventsArray ) ;
64- }
35+ if ( aStartDiff <= 0 && bStartDiff <= 0 ) {
36+ // Both events have started, sort by their end times
37+ const aEndDiff = a . endDateTime . diff ( currentDateTime ) ;
38+ const bEndDiff = b . endDateTime . diff ( currentDateTime ) ;
39+
40+ if ( aEndDiff <= 0 && bEndDiff <= 0 ) {
41+ // Both events are ongoing, sort by their end times
42+ return a . endDateTime . diff ( b . endDateTime ) ;
43+ } else {
44+ // One event has finished or is ongoing, prioritize the one finishing sooner
45+ return aEndDiff - bEndDiff ;
46+ }
47+ } else {
48+ // Sort by the difference between the start time and the current time
49+ return aStartDiff - bStartDiff ;
50+ }
51+ } ) ;
6552
66- if ( config . sortBy === 'milestone' ) {
6753 // Move finished events to the bottom when sorting by milestone
6854 sortedEvents . sort ( ( a , b ) => {
6955 if ( a . isFinished !== b . isFinished ) {
7056 return a . isFinished ? 1 : - 1 ;
7157 }
58+ // If both events are finished, sort them by their endDateTime in ascending order.
59+ if ( a . isFinished ) {
60+ return dayjs ( a . endDateTime ) . isBefore ( b . endDateTime ) ? - 1 : 1 ;
61+ }
7262 return 0 ;
7363 } ) ;
7464 }
65+
66+ // Sort events by their start time, finished events are moved to the bottom.
67+ if ( config . sortBy === 'start' ) {
68+ sortedEvents . sort ( ( a , b ) => {
69+ const isRunningA = currentDateTime . isBetween ( a . startDateTime , a . endDateTime ) ;
70+ const isRunningB = currentDateTime . isBetween ( b . startDateTime , b . endDateTime ) ;
71+
72+ if ( isRunningA && ! isRunningB ) {
73+ return - 1 ;
74+ } else if ( ! isRunningA && isRunningB ) {
75+ return 1 ;
76+ } else {
77+ const timeDiffA = Math . min (
78+ Math . abs ( a . startDateTime . diff ( currentDateTime ) ) ,
79+ Math . abs ( a . endDateTime . diff ( currentDateTime ) ) ,
80+ ) ;
81+
82+ const timeDiffB = Math . min (
83+ Math . abs ( b . startDateTime . diff ( currentDateTime ) ) ,
84+ Math . abs ( b . endDateTime . diff ( currentDateTime ) ) ,
85+ ) ;
86+
87+ return timeDiffA - timeDiffB ;
88+ }
89+ } ) ;
90+ }
91+
92+ // If config.allDayBottom is true, add the all-day events to the end of the sorted events array.
93+ if ( config . allDayBottom ) {
94+ sortedEvents . push ( ...allDayEventsArray ) ;
95+ }
96+ // Otherwise, add the all-day events to the beginning of the sorted events array.
97+ else {
98+ sortedEvents . unshift ( ...allDayEventsArray ) ;
99+ }
100+
75101 return sortedEvents ;
76102}
0 commit comments