@@ -3,50 +3,68 @@ import {
33 getVisibleTimetableRect ,
44} from "@/components/timetable" ;
55
6+ /*
7+ Resizes the time indicator to the correct width and positions it at the
8+ y-position corresponding to the current time.
9+ */
610export function resizeAndPositionTimeIndicator ( ) {
711 const time_indicator = document . getElementById ( "time-indicator" ) ;
812 if ( time_indicator == null ) return ;
913
1014 const now_label = document . getElementById ( "time-indicator-label" ) ;
1115 if ( now_label == null ) return ;
1216
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+
1327 const visible = getVisibleTimetableRect ( ) ;
1428 if ( visible == undefined ) return ;
1529
16- time_indicator . style . width = visible . width + "px" ;
17- time_indicator . style . left = visible . left + "px" ;
18-
1930 const now = new Date ( Date . now ( ) ) ;
2031 const hour = now . getHours ( ) ;
2132 const mins = now . getMinutes ( ) ;
2233
23- const top = getTimeTopPosition ( hour , mins ) ;
24- if ( top == undefined ) return ;
25- time_indicator . style . top = top + "px" ;
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" ;
2641
2742 const now_label_rect = now_label . getBoundingClientRect ( ) ;
2843 if ( now_label_rect == undefined ) return ;
2944
30- const now_label_left = visible . left - now_label_rect . width ;
31- const now_label_top = top - now_label_rect . height / 2 ;
32- now_label . style . left = now_label_left + "px" ;
33- now_label . style . top = now_label_top + "px" ;
34-
35- if ( top < visible . top || top > visible . bottom ) {
36- time_indicator . style . display = "none" ;
37- now_label . style . display = "none" ;
38- } else {
39- time_indicator . style . display = "inline" ;
40- now_label . style . display = "inline" ;
41- }
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" ;
4254}
4355
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+ */
4462function TimeIndicator ( ) {
4563 return (
4664 < >
4765 < div
4866 id = "time-indicator-label"
49- className = "absolute z-[1000 ] h-fit w-fit rounded-full bg-slate-400 p-1 pl-2 pr-2"
67+ className = "absolute z-[100 ] h-fit w-fit rounded-full bg-slate-400 p-1 pl-2 pr-2"
5068 >
5169 < p className = "text-slate-100" > Now</ p >
5270 </ div >
0 commit comments