Improve calendar scolling

This commit is contained in:
Lars Westermann 2019-06-11 18:07:57 +02:00
parent 8913ccc3ce
commit 6d6deaeac1
Signed by: lars.westermann
GPG key ID: 9D417FA5BB9D5E1D
2 changed files with 34 additions and 2 deletions

View file

@ -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()
}

View file

@ -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
}