@@ -89,34 +89,78 @@ export class EOxTimeControlDate extends LitElement {
8989 }
9090
9191 /**
92- * Updates the selected date by stepping forward or backward through available dates .
92+ * Gets the current index and item values for the given key .
9393 *
94- * @param {number } [step=1] - Number of steps to move (positive for forward, negative for backward).
94+ * @param {string } key - The key to get the current index and item values from.
95+ * @returns {Object } An object containing the current index and item values.
9596 */
96- updateStep ( step = 1 ) {
97+ #getCurrIndexAndValues ( key ) {
9798 const EOxTimeControl = this . getEOxTimeControl ( ) ;
9899 const itemValues = Object . keys (
99- groupBy ( EOxTimeControl . items . get ( ) , "date" ) ,
100+ groupBy ( EOxTimeControl . items . get ( ) , key ) ,
100101 ) . sort ( ( a , b ) => new Date ( a ) . getTime ( ) - new Date ( b ) . getTime ( ) ) ;
101102 const index = findIndex ( itemValues , ( date ) => {
102- return (
103- date ===
104- dayjs ( EOxTimeControl . selectedDateRange [ 0 ] ) . format (
105- TIME_CONTROL_DATE_FORMAT ,
106- )
107- ) ;
103+ if ( key === "utc" ) {
104+ return dayjs ( date ) . isSame ( EOxTimeControl . selectedDateRange [ 0 ] ) ;
105+ } else {
106+ return (
107+ date ===
108+ dayjs ( EOxTimeControl . selectedDateRange [ 0 ] ) . format (
109+ TIME_CONTROL_DATE_FORMAT ,
110+ )
111+ ) ;
112+ }
108113 } ) ;
114+ return { index, itemValues } ;
115+ }
116+
117+ /**
118+ * Gets the new index for the given item values, step, and index.
119+ *
120+ * @param {Array<string> } itemValues - The item values to get the new index from.
121+ * @param {number } step - The step to move the index by.
122+ * @param {number } index - The current index.
123+ * @returns {number } The new index.
124+ */
125+ #getNewIndex( itemValues , step , index ) {
109126 let newIndex = index + step ;
110- if ( newIndex > itemValues . length - 1 ) {
111- newIndex = 0 ;
112- }
113- if ( newIndex < 0 ) {
114- newIndex = itemValues . length - 1 ;
127+ if ( newIndex > itemValues . length - 1 ) newIndex = 0 ;
128+ if ( newIndex < 0 ) newIndex = itemValues . length - 1 ;
129+ return newIndex ;
130+ }
131+
132+ /**
133+ * Updates the selected date by stepping forward or backward through available dates.
134+ *
135+ * @param {number } [step=1] - Number of steps to move (positive for forward, negative for backward).
136+ */
137+ updateStep ( step = 1 ) {
138+ const EOxTimeControl = this . getEOxTimeControl ( ) ;
139+ let newIndex , index , itemValues ;
140+ const currIndex = this . #getCurrIndexAndValues( "utc" ) ;
141+ index = currIndex . index ;
142+ itemValues = currIndex . itemValues ;
143+
144+ // When exact value is matched based on date and time, use the index from the utc key
145+ if ( index >= 0 ) {
146+ newIndex = this . #getNewIndex( itemValues , step , index ) ;
147+ } else {
148+ // When exact value is not matched based on date and time, use the index from the date key
149+ const currIndex = this . #getCurrIndexAndValues( "date" ) ;
150+ index = currIndex . index ;
151+ itemValues = currIndex . itemValues ;
152+ newIndex = this . #getNewIndex( itemValues , step , index ) ;
115153 }
116154
117155 const nextDate = itemValues [ newIndex ] ;
156+ const isSameDay = dayjs ( nextDate ) . isSame (
157+ EOxTimeControl . selectedDateRange [ 0 ] ,
158+ "day" ,
159+ ) ;
118160 const nextDateRange = [
119- dayjs ( nextDate ) . startOf ( "day" ) . utc ( ) . format ( ) ,
161+ isSameDay
162+ ? dayjs ( nextDate ) . utc ( ) . format ( )
163+ : dayjs ( nextDate ) . startOf ( "day" ) . utc ( ) . format ( ) ,
120164 dayjs ( nextDate ) . endOf ( "day" ) . utc ( ) . format ( ) ,
121165 ] ;
122166 EOxTimeControl . dateChange ( nextDateRange , EOxTimeControl ) ;
0 commit comments