diff --git a/src/jsMain/kotlin/de/kif/frontend/views/calendar/Calendar.kt b/src/jsMain/kotlin/de/kif/frontend/views/calendar/Calendar.kt index 00f7eb3..789b663 100644 --- a/src/jsMain/kotlin/de/kif/frontend/views/calendar/Calendar.kt +++ b/src/jsMain/kotlin/de/kif/frontend/views/calendar/Calendar.kt @@ -10,6 +10,9 @@ import org.w3c.dom.* import kotlin.browser.document import kotlin.browser.window import kotlin.js.Date +import kotlin.math.abs +import kotlin.math.max +import kotlin.math.min class Calendar(calendar: HTMLElement) : View(calendar) { @@ -18,6 +21,8 @@ class Calendar(calendar: HTMLElement) : View(calendar) { val calendarTable = calendar.getElementsByClassName("calendar-table")[0] as HTMLElement private val calendarTableHeader = calendar.getElementsByClassName("calendar-header")[0] as HTMLElement + val htmlBody = document.body ?: createHtmlView() + val day = calendarTable.dataset["day"]?.toIntOrNull() ?: -1 val referenceDate = calendarTable.dataset["reference"]?.toLongOrNull() ?: -1L val nowDate = calendarTable.dataset["now"]?.toLongOrNull() ?: -1L @@ -111,8 +116,34 @@ class Calendar(calendar: HTMLElement) : View(calendar) { else -> 1.0 } + var verticalScroll = it.deltaY * multiplier + + if (verticalScroll > 0) { + val x = html.offsetTop - htmlBody.scrollTop + if (x > 0) { + val bodyScroll = min(x, verticalScroll) + verticalScroll -= bodyScroll + htmlBody.scrollBy(ScrollToOptions(0.0, bodyScroll, ScrollBehavior.INSTANT)) + } else { + if (calendarTable.scrollTop + calendarTable.clientHeight + 5 >= calendarTable.scrollHeight) { + htmlBody.scrollBy(ScrollToOptions(0.0, verticalScroll, ScrollBehavior.INSTANT)) + } + } + } else if (verticalScroll < 0) { + val x = html.offsetTop - htmlBody.scrollTop + if (x < 0) { + val bodyScroll = max(x, verticalScroll) + verticalScroll -= bodyScroll + htmlBody.scrollBy(ScrollToOptions(0.0, bodyScroll, ScrollBehavior.INSTANT)) + } else { + if (calendarTable.scrollTop == 0.0) { + htmlBody.scrollBy(ScrollToOptions(0.0, verticalScroll, ScrollBehavior.INSTANT)) + } + } + } + scrollHorizontalBy(it.deltaX * multiplier, ScrollBehavior.INSTANT) - scrollVerticalBy(it.deltaY * multiplier, ScrollBehavior.INSTANT) + scrollVerticalBy(verticalScroll, ScrollBehavior.INSTANT) it.preventDefault() } diff --git a/src/jsMain/kotlin/de/kif/frontend/views/calendar/CalendarEntry.kt b/src/jsMain/kotlin/de/kif/frontend/views/calendar/CalendarEntry.kt index 75436f8..6d77a2c 100644 --- a/src/jsMain/kotlin/de/kif/frontend/views/calendar/CalendarEntry.kt +++ b/src/jsMain/kotlin/de/kif/frontend/views/calendar/CalendarEntry.kt @@ -174,7 +174,8 @@ class CalendarEntry(private val calendar: CalendarBody, view: HTMLElement) : Vie init { onMouseDown { event -> - if (!editable || event.target != html || "pending" in classList) { + val isValidTarget = event.target == html || event.target == nameView + if (!editable || !isValidTarget || "pending" in classList) { event.stopPropagation() return@onMouseDown }