|
| 1 | +import { |
| 2 | + getTimeTopPosition, |
| 3 | + getVisibleTimetableRect, |
| 4 | +} from "@/components/timetable"; |
| 5 | + |
| 6 | +/* |
| 7 | +Resizes the time indicator to the correct width and positions it at the |
| 8 | +y-position corresponding to the current time. |
| 9 | +*/ |
| 10 | +export function resizeAndPositionTimeIndicator() { |
| 11 | + const time_indicator = document.getElementById("time-indicator"); |
| 12 | + if (time_indicator == null) return; |
| 13 | + |
| 14 | + const now_label = document.getElementById("time-indicator-label"); |
| 15 | + if (now_label == null) return; |
| 16 | + |
| 17 | + const separator_rect = document |
| 18 | + .getElementById("timetable-separator") |
| 19 | + ?.getBoundingClientRect(); |
| 20 | + if (separator_rect == undefined) return; |
| 21 | + |
| 22 | + const content_rect = document |
| 23 | + .getElementById("timetable-content") |
| 24 | + ?.getBoundingClientRect(); |
| 25 | + if (content_rect == undefined) return; |
| 26 | + |
| 27 | + const visible = getVisibleTimetableRect(); |
| 28 | + if (visible == undefined) return; |
| 29 | + |
| 30 | + const now = new Date(Date.now()); |
| 31 | + const hour = now.getHours(); |
| 32 | + const mins = now.getMinutes(); |
| 33 | + |
| 34 | + const time_top = getTimeTopPosition(hour, mins); |
| 35 | + if (time_top == undefined) return; |
| 36 | + |
| 37 | + time_indicator.style.left = |
| 38 | + visible.left - separator_rect.width - content_rect.left + "px"; |
| 39 | + time_indicator.style.top = time_top - content_rect.top + "px"; |
| 40 | + time_indicator.style.width = visible.width + separator_rect.width + "px"; |
| 41 | + |
| 42 | + const now_label_rect = now_label.getBoundingClientRect(); |
| 43 | + if (now_label_rect == undefined) return; |
| 44 | + |
| 45 | + const now_label_left = |
| 46 | + visible.left - now_label_rect.width - separator_rect.width; |
| 47 | + const now_label_top = time_top - now_label_rect.height / 2; |
| 48 | + |
| 49 | + const parent_rect = time_indicator.parentElement?.getBoundingClientRect(); |
| 50 | + if (parent_rect == undefined) return; |
| 51 | + |
| 52 | + now_label.style.left = now_label_left - parent_rect.left + "px"; |
| 53 | + now_label.style.top = now_label_top - parent_rect.top + "px"; |
| 54 | +} |
| 55 | + |
| 56 | +/* |
| 57 | +Component consisting of a rounded label containing the text "Now" and a 2px |
| 58 | +thick line stretching across the visible area of the timetable. |
| 59 | +
|
| 60 | +There should only be one TimeIndicator per page. |
| 61 | +*/ |
| 62 | +function TimeIndicator() { |
| 63 | + return ( |
| 64 | + <> |
| 65 | + <div |
| 66 | + id="time-indicator-label" |
| 67 | + className="absolute z-[100] h-fit w-fit rounded-full bg-slate-400 p-1 pl-2 pr-2" |
| 68 | + > |
| 69 | + <p className="text-slate-100">Now</p> |
| 70 | + </div> |
| 71 | + <div |
| 72 | + id="time-indicator" |
| 73 | + className="absolute z-[100] min-h-[2px] bg-slate-200 opacity-75" |
| 74 | + ></div> |
| 75 | + </> |
| 76 | + ); |
| 77 | +} |
| 78 | + |
| 79 | +export default TimeIndicator; |
0 commit comments