Skip to content

Commit 4e92c1a

Browse files
authored
Fix: Exact time based selection for slider and navigation based timecontrol element (#2122)
* fix: timebased timecontrol element return data with exact time if avaiable * fix: repeated update in slider * fix: reomve startOf from initDate function
1 parent 6bada56 commit 4e92c1a

File tree

4 files changed

+73
-21
lines changed

4 files changed

+73
-21
lines changed

elements/timecontrol/src/components/timecontrol-date.js

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

elements/timecontrol/src/components/timecontrol-slider.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ export class EOxTimeControlSlider extends LitElement {
210210
*/
211211
setDateRange(dateRange, data) {
212212
this.#selectedDateRange = dateRange;
213+
let exactMatch = null;
213214
const slider = this.getSliderInstance();
214215
if (data && data.length) {
215216
this.#items = Object.keys(groupBy(data, "utc")).sort((a, b) =>
@@ -221,6 +222,7 @@ export class EOxTimeControlSlider extends LitElement {
221222
const end = dayjs(this.#selectedDateRange[1]);
222223
const filteredItems = this.#items.filter((utc) => {
223224
const date = dayjs(utc);
225+
if (date.isSame(start)) exactMatch = utc;
224226
return (
225227
(date.isAfter(start) || date.isSame(start, "day")) &&
226228
(date.isBefore(end) || date.isSame(end, "day"))
@@ -230,7 +232,7 @@ export class EOxTimeControlSlider extends LitElement {
230232
}
231233
if (slider) {
232234
slider.setAttribute("data", this.#items.join(","));
233-
slider.setAttribute("value1", this.#filteredItems[0]);
235+
slider.setAttribute("value1", exactMatch || this.#filteredItems[0]);
234236
}
235237

236238
// Generate ticks after items are set
@@ -269,8 +271,14 @@ export class EOxTimeControlSlider extends LitElement {
269271

270272
handleChange(evt) {
271273
const EOxTimeControl = this.getEOxTimeControl();
272-
const start = dayjs(evt.detail.value1).format();
273-
const end = dayjs(evt.detail.value1).endOf("day").format();
274+
const start = dayjs(evt.detail.value1).utc().format();
275+
const end = dayjs(evt.detail.value1).endOf("day").utc().format();
276+
277+
if (
278+
start === EOxTimeControl.selectedDateRange[0] &&
279+
end === EOxTimeControl.selectedDateRange[1]
280+
)
281+
return;
274282
EOxTimeControl.dateChange([start, end], EOxTimeControl);
275283
}
276284

elements/timecontrol/src/helpers/get-init-date.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default function getInitDate(initDate) {
2626
[start, end] = initDate;
2727
}
2828

29-
start = dayjs(start).startOf("day").utc().format();
29+
start = dayjs(start).utc().format();
3030
end = dayjs(end).endOf("day").utc().format();
3131

3232
return [start, end];

elements/timecontrol/src/methods/timecontrol/first-updated.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export default function firstUpdatedMethod(EOxTimeControl, emitUpdateEvent) {
154154
const dateRange =
155155
initDateRange ||
156156
/** @type {DateRange} */ ([
157-
utc.startOf("day").utc().format(),
157+
utc.utc().format(),
158158
utc.endOf("day").utc().format(),
159159
]);
160160

0 commit comments

Comments
 (0)