/!\ events: groundwork for containers onTouch scripts:

Drag events are now reverse-bubbling (from parents to child) to allow containers to monitor/control interaction inside them
This commit is contained in:
jean-emmanuel 2025-03-21 09:23:44 +01:00
parent e001d3be10
commit 4d30880d0f
6 changed files with 79 additions and 9 deletions

View file

@ -42,6 +42,7 @@ This script has access to the same variables and functions as the `script` prope
- `targetTagName`: tag name of the html element under the pointer
- `offsetX`, `offsetY`: touch coordinates in pixels, relative to the html element under the pointer
- `movementX`, `movementY`: movement of the pointer in pixels since the last event
- `pageX`, `pageY`: touch coordinates in pixels, relative to the page
- `pointerId`: unique identifier used to differentiate fingers in multitouch situation
- `altKey`, `ctrlKey`, `shiftKey`: keyboard modifier states
- `force`: amount of pressure applied to the touch surface between `0` and `1` (see [Touch.force](https://developer.mozilla.org/en-US/docs/Web/API/Touch/force)). Equals `0` if the API is not supported or if no pressure information is found.

View file

@ -78,7 +78,15 @@ This script has access to the same variables and functions as the `script` prope
- `type`: `"start"` or `"stop"`
- `handle`: `undefined` if the event concerns the widget, `integer` if it concerns one of it's handles (`multixy` and `range` widgets only)
Note: the [canvas](../canvas/) widgets exposes more informations in its `onTouch` script.
[Canvas](../canvas/) and containers expose more informations in their `onTouch` script.
??? infos "Containers onTouch"
Containers (root, panel, etc) can monitor touch events happening inside them, it works like with [Canvas](../canvas/#ontouch), with a few differences:
- setting `event.preventDefault` to `true` will prevent the event from reaching widgets in the container ('start' and 'move' events only)
- setting `event.allowScroll` to `true` will allow touch scrolling where it would normally be prevented ('start' event only)
- `width` and `height` are undefined
### `onDraw`

View file

@ -15,6 +15,7 @@ function pointerDownHandler(event) {
}
event = normalizeDragEvent(event)
event.stopPropagation = true
var target = closestDragContainer(event.target)
@ -113,6 +114,7 @@ function pointerMoveHandler(event) {
function pointerUpHandler(event) {
event = normalizeDragEvent(event, previousPointers[event.pointerId])
event.stopPropagation = true
triggerWidgetEvent(targets[event.pointerId], 'dragend', event)
@ -157,6 +159,7 @@ function pointerUpFilter(event) {
function mouseDownCapture(event) {
if (event.pointerType === 'touch') return
event.stopPropagation()
// event.pointerId = 'mouse'
pointerDownHandler(event)
}
@ -213,6 +216,9 @@ domObserver.deactivate = ()=>{
function touchDownCapture(event) {
var preventDefault = true
event.stopPropagation()
for (var i in event.changedTouches) {
if (isNaN(i) || !event.changedTouches[i]) continue
var touchEvent = event.changedTouches[i]
@ -225,7 +231,8 @@ function touchDownCapture(event) {
touchEvent = pointerDownHandler(touchEvent)
if (touchEvent.cancelDragEvent) preventDefault = false
// allow scroll if drag is cancelled or widget is a container
if (touchEvent.cancelDragEvent || touchEvent.allowScroll) preventDefault = false
}
if (preventDefault) event.preventDefault()
}
@ -261,6 +268,20 @@ function touchUpCapture(event) {
}
function ancestorDragContainers(target) {
var containers = [],
container = target
while (container !== null) {
if (container._drag_widget) {
containers.splice(0, 0, container._drag_widget)
if (container._drag_widget.root) break
}
container = container.parentNode
}
return containers
}
function closestDragContainer(target) {
var container = target
while (container !== null) {
@ -276,8 +297,18 @@ function closestDragContainer(target) {
// Callback trigger
function triggerWidgetEvent(target, name, event) {
var container = closestDragContainer(target)
if (container) container._drag_widget.trigger(name, event)
// trigger drag events from parent to child
var widgets = ancestorDragContainers(target),
widget
for (widget of widgets) {
widget.trigger(name, event)
if (event.preventDefault) break
}
// if child is a container, let it scroll
if (widget && widget.scroll) event.allowScroll = true
}
// init

View file

@ -45,6 +45,9 @@ class Panel extends Container() {
children: {
widgets: {type: 'array', value: [], help: 'Each element of the array must be a widget object. A panel cannot contain widgets and tabs simultaneously.'},
tabs: {type: 'array', value: [], help: 'Each element of the array must be a tab object. A panel cannot contain widgets and tabs simultaneously'},
},
scripting: {
onTouch: {type: 'script', value: '', editor:'javascript', help: ['Script executed when the session is touched and released, and when the pointer moves when the widget is touched. See <a href="https://openstagecontrol.ammd.net/docs/widgets/canvas/">documentation</a>.',]},
}
})
@ -267,6 +270,12 @@ class Panel extends Container() {
if (this.getProp('traversing')) this.setTraversing()
if (this.getProp('onTouch')) {
this.on('draginit',(e)=>this.onTouch(e, 'start'), {element: this.container, multitouch: true})
this.on('drag',(e)=>this.onTouch(e, 'move'), {element: this.container, multitouch: true})
this.on('dragend',(e)=>this.onTouch(e, 'stop'), {element: this.container, multitouch: true})
}
}
createNavigation() {
@ -348,6 +357,30 @@ class Panel extends Container() {
}
onTouch(e, name) {
var event = {...e}
delete event.firstTarget
event.targetName = event.target.getAttribute('name')
event.targetTagName = event.target.tagName
var w = widgetManager.getWidgetByElement(event.target)
if (w) event.target = w === this ? 'this' : w.getProp('id')
else event.target = null
event.type = name
this.scripts.onTouch.run({
value: this.value,
event: event
}, {sync: true, send: true})
if (name != 'stop' && event.preventDefault) e.preventDefault = true
if (event.allowScroll) e.allowScroll = true
}
onPropChanged(propName, options, oldPropValue) {
if (super.onPropChanged(...arguments)) return true

View file

@ -47,6 +47,7 @@ class Root extends StaticProperties(Panel, {visible: true, label: false, id: 'ro
super(options)
this.widget.classList.add('root')
this.root = true
this.checkMenuVisibility()
@ -61,7 +62,7 @@ class Root extends StaticProperties(Panel, {visible: true, label: false, id: 'ro
if (READ_ONLY) return
mainMenu = mainMenu || require('../../ui/main-menu')
if (this.getProp('hideMenu')) {
mainMenu.container.style.display = 'none'
} else {

View file

@ -119,8 +119,6 @@ class MultiXy extends Pad {
if (!i) return
e.stopPropagation = true
this.pads[i].touched = 0
this.pads[i].trigger('dragend', e)
@ -146,8 +144,6 @@ class MultiXy extends Pad {
if (!id) return
e.stopPropagation = true
this.trigger('touch', {stopPropagation: true, touch: [parseInt(id), 1]})
this.pads[id].trigger('draginit', e)