feat: drag the timeline with the mouse

This commit is contained in:
fromme committed 2026-11-12 11:14:25 +01:00
1 parent e976342c09
commit 9ba6529ee0
3 files changed
+95 -1

No files matched your search

@@ -1,12 +1,26 @@
import { Controller } from '@hotwired/stimulus'
// pixels the mouse has to move before a press becomes a drag, so clicks on bars still work
const DRAG_THRESHOLD = 4
export default class extends Controller {
static targets = [ 'scroller', 'today' ]
connect() {
this.drag = null
this.suppressClick = this.suppressClick.bind(this)
requestAnimationFrame(() => this.scrollToToday({ behavior: 'instant' }))
}
scrollerTargetConnected(scroller) {
// capture phase, so a drag ending on a link or button does not also open it
scroller.addEventListener('click', this.suppressClick, true)
}
scrollerTargetDisconnected(scroller) {
scroller.removeEventListener('click', this.suppressClick, true)
}
// centres today's column; the house column stays in front of the left part
scrollToToday({ behavior = 'smooth' } = {}) {
if (false === this.hasScrollerTarget || false === this.hasTodayTarget) {
@@ -23,4 +37,72 @@ export default class extends Controller {
scroller.scrollTo({ left: Math.min(Math.max(0, centered), maxScrollLeft), behavior })
}
// pressing the mouse button on the timeline and moving drags it along; touch scrolls natively
startDrag(event) {
if (0 !== event.button || 'mouse' !== event.pointerType) {
return
}
// a drag without a following click must not swallow the next one
this.dragged = false
this.drag = {
pointerId: event.pointerId,
x: event.clientX,
y: event.clientY,
scrollLeft: this.scrollerTarget.scrollLeft,
scrollTop: this.scrollerTarget.scrollTop,
moved: false,
}
}
moveDrag(event) {
if (null === this.drag || event.pointerId !== this.drag.pointerId) {
return
}
const dx = event.clientX - this.drag.x
const dy = event.clientY - this.drag.y
if (false === this.drag.moved) {
if (Math.hypot(dx, dy) < DRAG_THRESHOLD) {
return
}
this.drag.moved = true
this.scrollerTarget.setPointerCapture(event.pointerId)
this.scrollerTarget.classList.add('timeline--dragging')
window.getSelection()?.removeAllRanges()
}
this.scrollerTarget.scrollLeft = this.drag.scrollLeft - dx
this.scrollerTarget.scrollTop = this.drag.scrollTop - dy
}
endDrag(event) {
if (null === this.drag || event.pointerId !== this.drag.pointerId) {
return
}
this.dragged = this.drag.moved
this.drag = null
this.scrollerTarget.classList.remove('timeline--dragging')
if (this.scrollerTarget.hasPointerCapture(event.pointerId)) {
this.scrollerTarget.releasePointerCapture(event.pointerId)
}
}
// links are draggable by default, which would take over the press
preventNativeDrag(event) {
event.preventDefault()
}
suppressClick(event) {
if (true !== this.dragged) {
return
}
this.dragged = false
event.preventDefault()
event.stopPropagation()
}
}
+10
View File
@@ -54,3 +54,13 @@
.timeline-bar--staffed {
@apply border border-emerald-800 bg-emerald-700 text-white;
}
/* timeline_scroll_controller.js: an open hand over the timeline, a closed one while dragging */
.timeline {
@apply cursor-grab;
}
.timeline--dragging,
.timeline--dragging * {
@apply cursor-grabbing select-none;
}
@@ -70,7 +70,8 @@
{% else %}
<div class="timeline card mb-4"
style="--timeline-days: {{ calendar.days|length }}"
{{ stimulus_target('timeline-scroll', 'scroller') }}>
{{ stimulus_target('timeline-scroll', 'scroller') }}
{{ stimulus_action('timeline-scroll', 'startDrag', 'pointerdown')|stimulus_action('timeline-scroll', 'moveDrag', 'pointermove')|stimulus_action('timeline-scroll', 'endDrag', 'pointerup')|stimulus_action('timeline-scroll', 'endDrag', 'pointercancel')|stimulus_action('timeline-scroll', 'preventNativeDrag', 'dragstart') }}>
<div class="relative w-max min-w-full">
{# weekends, month boundaries and today, behind the bars #}
<div class="timeline-grid timeline-background" aria-hidden="true">
@@ -134,6 +135,7 @@
<li class="flex items-center gap-x-2"><span class="timeline-bar timeline-bar--pending w-10 h-5"></span> in Bewerbung oder Vertrag in Prüfung</li>
<li class="flex items-center gap-x-2"><span class="timeline-bar timeline-bar--open w-10 h-5"></span> offen, noch keine Bewerbung</li>
<li class="flex items-center gap-x-2"><span class="inline-block h-5 w-0.5 bg-brand-bright"></span> heute</li>
<li class="text-muted sm:ml-auto">Mit gedrückter Maustaste lässt sich die Zeitleiste verschieben</li>
</ul>
</div>
{% endblock %}