From 4d30880d0f74b544a2b25090de0780c0524d01e3 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Fri, 21 Mar 2025 09:23:44 +0100 Subject: [PATCH] /!\ 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 --- resources/docs/docs/widgets/canvas.md | 1 + resources/docs/docs/widgets/scripting.md | 10 ++++++- src/client/events/drag.js | 37 ++++++++++++++++++++++-- src/client/widgets/containers/panel.js | 33 +++++++++++++++++++++ src/client/widgets/containers/root.js | 3 +- src/client/widgets/pads/multixy.js | 4 --- 6 files changed, 79 insertions(+), 9 deletions(-) diff --git a/resources/docs/docs/widgets/canvas.md b/resources/docs/docs/widgets/canvas.md index 80695f42..5ae9bb0d 100644 --- a/resources/docs/docs/widgets/canvas.md +++ b/resources/docs/docs/widgets/canvas.md @@ -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. diff --git a/resources/docs/docs/widgets/scripting.md b/resources/docs/docs/widgets/scripting.md index f806cc2c..1e7aecb3 100644 --- a/resources/docs/docs/widgets/scripting.md +++ b/resources/docs/docs/widgets/scripting.md @@ -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` diff --git a/src/client/events/drag.js b/src/client/events/drag.js index e0bcced1..f21ce235 100644 --- a/src/client/events/drag.js +++ b/src/client/events/drag.js @@ -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 diff --git a/src/client/widgets/containers/panel.js b/src/client/widgets/containers/panel.js index 703a6238..f0a00b9b 100644 --- a/src/client/widgets/containers/panel.js +++ b/src/client/widgets/containers/panel.js @@ -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 documentation.',]}, } }) @@ -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 diff --git a/src/client/widgets/containers/root.js b/src/client/widgets/containers/root.js index 16aa480b..3e131a28 100644 --- a/src/client/widgets/containers/root.js +++ b/src/client/widgets/containers/root.js @@ -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 { diff --git a/src/client/widgets/pads/multixy.js b/src/client/widgets/pads/multixy.js index b2c19be5..eaad5713 100644 --- a/src/client/widgets/pads/multixy.js +++ b/src/client/widgets/pads/multixy.js @@ -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)