events: use delegated events as much as possible to avoid adding dom listeners when creating widgets

This commit is contained in:
jean-emmanuel 2022-01-28 16:06:26 +01:00
parent a7683787d2
commit 459a37960c
23 changed files with 172 additions and 113 deletions

View file

@ -0,0 +1,47 @@
module.exports = function(eventName, options={}){
var widget_storage_key = '_' + eventName + '_widget'
function closestWidgetContainer(target) {
var container = target
while (container !== null) {
if (container[widget_storage_key]) {
return container
} else {
container = container.parentNode
}
}
return null
}
function triggerWidgetEvent(target, event) {
var container = closestWidgetContainer(target)
if (container) container[widget_storage_key].trigger(eventName, event)
}
DOM.ready(()=>{
var element = options.element || document
document.addEventListener(eventName, (event)=>{
triggerWidgetEvent(event.target, event)
}, {passive: !!options.passive, capture: !!options.capture})
})
return {
setup: function(options) {
var {element} = options || {element: this.container}
element[widget_storage_key] = this
},
teardown: function(options) {
var {element} = options || {element: this.container}
delete element[widget_storage_key]
}
}
}

View file

@ -8,7 +8,7 @@ function pointerDownHandler(event) {
if (event.capturedByEditor || event.touchPunch) return
if (!event.multitouch) {
if (!event.target._drag_multitouch) {
for (var i in targets) {
if (targets[i] == event.target) return
}
@ -18,9 +18,11 @@ function pointerDownHandler(event) {
if (event.traversingStack) {
event.target = event.target.closest('.drag-event')
event.target = closestDragContainer(event.target)
var widget = event.target.closest('.drag-event')._drag_widget,
if (!event.target) return
var widget = closestDragContainer(event.target)._drag_widget,
local = event.traversingStack.stack[event.traversingStack.stack.length - 1]
if (widget.getProp) event.traversingStack.firstType = widget.getProp('type')
@ -53,7 +55,7 @@ function pointerMoveHandler(event) {
document.elementFromPoint(event.clientX, event.clientY)
: event.target
if (target) target = target.closest('.drag-event')
if (target) target = closestDragContainer(target)
var local = null
if (target && event.traversingStack) {
@ -66,7 +68,7 @@ function pointerMoveHandler(event) {
if (!local) {
target = null
} else if (local.mode === TRAVERSING_SAMEWIDGET && local.type) {
var widget = target.closest('.drag-event')._drag_widget
var widget = closestDragContainer(target)._drag_widget
if (widget.getProp && local.type !== widget.getProp('type')) target = null
}
}
@ -131,14 +133,14 @@ function pointerUpFilter(event) {
// Mouse events wrappers
function mouseMultiWrapper(event) {
mouseDownCapture(event, true)
}
// function mouseMultiWrapper(event) {
// mouseDownCapture(event, true)
// }
function mouseDownCapture(event, multitouch) {
if (event.pointerType === 'touch') return
// event.pointerId = 'mouse'
event.multitouch = multitouch
// event.multitouch = multitouch
pointerDownHandler(event)
}
@ -158,9 +160,9 @@ function mouseUpCapture(event){
// Touch events wrappers
function touchMultiWrapper(event) {
touchDownCapture(event, true)
}
// function touchMultiWrapper(event) {
// touchDownCapture(event, true)
// }
function touchDownCapture(event, multitouch) {
@ -174,7 +176,7 @@ function touchDownCapture(event, multitouch) {
}
touchEvent.pointerId = touchEvent.identifier
touchEvent.multitouch = multitouch
// touchEvent.multitouch = multitouch
pointerDownHandler(touchEvent)
}
@ -210,14 +212,24 @@ function touchUpCapture(event) {
}
}
function closestDragContainer(target) {
var container = target
while (container !== null) {
if (container._drag_widget) {
return container
} else {
container = container.parentNode
}
}
return null
}
// Callback trigger
function triggerWidgetEvent(target, name, event) {
if (target !== null && target._drag_widget) {
target._drag_widget.trigger(name, event)
} else if (target !== null) {
triggerWidgetEvent(target.closest('.drag-event'), name, event)
}
var container = closestDragContainer(target)
if (container) container._drag_widget.trigger(name, event)
}
// init
@ -228,6 +240,9 @@ DOM.ready(()=>{
document.addEventListener('touchmove', touchMoveCapture, true)
DOM.addEventListener(document, 'touchend touchcancel', touchUpCapture, true)
document.addEventListener('touchstart', touchDownCapture, false)
if (!iOS) document.addEventListener('pointerdown', mouseDownCapture)
})
module.exports = {
@ -246,16 +261,8 @@ module.exports = {
var {element, multitouch} = options
element._drag_widget = this
element._drag_multitouch = multitouch
element.style.touchAction = 'none'
element.classList.add('drag-event')
if (multitouch) {
element.addEventListener('touchstart', touchMultiWrapper, false)
if (!iOS) element.addEventListener('pointerdown', mouseMultiWrapper)
} else {
element.addEventListener('touchstart', touchDownCapture, false)
if (!iOS) element.addEventListener('pointerdown', mouseDownCapture)
}
},
@ -273,16 +280,8 @@ module.exports = {
var {element, multitouch} = options
delete element._drag_widget
delete element._drag_multitouch
element.style.touchAction = ''
element.classList.remove('drag-event')
if (multitouch) {
element.removeEventListener('touchstart', touchMultiWrapper, false)
if (!iOS) element.removeEventListener('pointerdown', mouseMultiWrapper)
} else {
element.removeEventListener('touchstart', touchDownCapture, false)
if (!iOS) element.removeEventListener('pointerdown', mouseDownCapture)
}
},

View file

@ -2,6 +2,13 @@ var customEvents = {}
setTimeout(()=>{
customEvents['draginit'] = customEvents['drag'] = customEvents['dragend'] = require('./drag')
customEvents['resize'] = require('./resize')
customEvents['wheel'] = require('./dom-event')('wheel')
customEvents['scroll'] = require('./dom-event')('scroll', {capture: true})
customEvents['click'] = require('./dom-event')('click')
customEvents['fast-click'] = require('./dom-event')('fast-click')
customEvents['focus'] = require('./dom-event')('focus', {capture: true})
customEvents['blur'] = require('./dom-event')('blur', {capture: true})
customEvents['change'] = require('./dom-event')('change')
})
// micro optimisation from eventemitter3

View file

@ -156,7 +156,10 @@ class WidgetManager extends EventEmitter {
id = widget.getProp('id')
if (this.widgets[hash]) {
if (this.widgets[hash].onRemove) this.widgets[hash].onRemove()
if (this.widgets[hash].onRemove) {
this.widgets[hash].onRemove()
this.widgets[hash].off()
}
delete this.widgets[hash]

View file

@ -1,5 +1,5 @@
var Widget = require('../common/widget'),
doubletab = require('../mixins/double_tap'),
doubleTap = require('../mixins/double_tap'),
html = require('nanohtml'),
{deepEqual, isJSON} = require('../../utils'),
{iconify} = require('../../ui/utils')
@ -80,14 +80,14 @@ class Button extends Widget {
if (this.getProp('doubleTap')) {
doubletab(this.container, (e)=>{
doubleTap(this, (e)=>{
this.active = true
this.setValue(this.getProp('on'), {sync: true, send: true, y: e.offsetY, x: e.offsetX})
if (tap) this.container.classList.add('active')
})
}, {element: this.container})
} else {
@ -120,13 +120,13 @@ class Button extends Widget {
if (this.getProp('doubleTap')) {
doubletab(this.container, (e)=>{
doubleTap(this, (e)=>{
this.active = true
this.setValue(this.state ? this.getProp('off') : this.getProp('on'), {sync: true, send: true, y: e.offsetY, x: e.offsetX})
})
}, {element: this.container})
} else {

View file

@ -53,9 +53,9 @@ class Dropdown extends MenuBase {
this.parseValues()
this.select.addEventListener('change', ()=>{
this.on('change', (e)=>{
this.setValue(this.values[this.select.selectedIndex], {sync:true, send:true, fromLocal:true})
})
}, {element: this.select})
this.select.selectedIndex = -1
this.selected = -1

View file

@ -43,7 +43,7 @@ module.exports = class File extends Widget {
if (this.getProp('hidePath')) this.widget.classList.add('hide-path')
this.text = DOM.get(this.widget, '.text')[0]
this.widget.addEventListener('fast-click', (e)=>{
this.on('fast-click', (e)=>{
if (e.capturedByEditor === true) return
@ -60,7 +60,7 @@ module.exports = class File extends Widget {
})
})
})
}, {element: this.widget})
}

View file

@ -54,11 +54,11 @@ class Input extends Canvas {
if (this.getProp('interaction')) {
this.widget.setAttribute('tabindex', 0)
this.widget.addEventListener('focus', this.focus.bind(this))
this.on('focus', this.focus.bind(this), {element: this.widget})
this.input = html`<input class="no-keybinding"></input>`
this.input.addEventListener('blur', (e)=>{
this.on('blur', (e)=>{
this.blur(true)
})
}, {element: this.input})
if (this.getProp('numeric')) {
this.input.type = 'number'

View file

@ -1,6 +1,6 @@
var MenuBase = require('./menu-base'),
{iconify} = require('../../ui/utils'),
doubletap = require('../mixins/double_tap'),
doubleTap = require('../mixins/double_tap'),
html = require('nanohtml'),
raw = require('nanohtml/raw'),
iOS = require('../../ui/ios')
@ -83,10 +83,10 @@ class Menu extends MenuBase {
if (this.getProp('doubleTap')) {
doubletap(this.widget, (e)=>{
doubleTap(this, (e)=>{
if (this.opened) return
this.open(e)
})
}, {element: this.widget})
this.on('draginit',(e)=>{

View file

@ -90,12 +90,12 @@ class Switch extends MenuBase {
if (this.getProp('mode') === 'slide' || e.traversing) dragCallback(e, true)
} , {element: this.widget})
} else if (this.getProp('mode') === 'click') {
this.widget.addEventListener('click', dragCallback)
this.on('click', dragCallback, {element: this.widget})
} else if (this.getProp('mode') === 'flip') {
this.widget.addEventListener('click', ()=>{
this.on('click', ()=>{
var i = (this.getIndex(this.value) + 1) % this.values.length
this.setValue(this.values[i], {sync: true, send: true})
})
}, {element: this.widget})
}
}

View file

@ -43,10 +43,10 @@ class TextArea extends Widget {
if (this.getProp('interaction')) {
this.input.setAttribute('tabindex', 0)
this.input.addEventListener('focus', this.focus.bind(this))
this.input.addEventListener('blur', (e)=>{
this.on('focus', this.focus.bind(this), {element: this.input})
this.on('blur', (e)=>{
this.blur(true)
})
}, {element: this.input})
this.input.addEventListener('keydown', (e)=>{
if (e.keyCode === 13 && !e.shiftKey) {

View file

@ -241,7 +241,7 @@ class Widget extends EventEmitter {
code: this.getProp('onValue'),
context: {value: 0, id: '', touch: undefined}
})
this.on('change', (e)=>{
this.on('value-changed', (e)=>{
if (e.widget === this && this.mounted && !e.options.fromEdit && e.options.script !== false) {
this.scripts.onValue.run({
value: e.options.widget ? e.options.widget.value : this.value,
@ -341,7 +341,7 @@ class Widget extends EventEmitter {
changed(options) {
this.trigger('change', {
this.trigger('value-changed', {
widget: this,
options: options,
id: this.getProp('id'),
@ -478,14 +478,14 @@ class Widget extends EventEmitter {
if (!this.linkedValueChangedCallback && Object.keys(this.linkedPropsValue).length) {
this.linkedValueChangedCallback = (e)=>{
this.onLinkedPropsChanged(e, 'change')
this.onLinkedPropsChanged(e, 'value-changed')
}
widgetManager.on('change', this.linkedValueChangedCallback, {context: this})
widgetManager.on('value-changed', this.linkedValueChangedCallback, {context: this})
} else if (this.linkedPropsValueCallback && !Object.keys(this.linkedPropsValue).length) {
widgetManager.off('change', this.linkedValueChangedCallback)
widgetManager.off('value-changed', this.linkedValueChangedCallback)
delete this.linkedValueChangedCallback
}
@ -514,8 +514,8 @@ class Widget extends EventEmitter {
onLinkedPropsChanged(e, type) {
var {id, widget, options} = e,
changedProps = type === 'prop-changed' ? e.props : type === 'change' ? ['value'] : undefined,
linkedProps = type === 'change' ? this.linkedPropsValue : this.linkedProps
changedProps = type === 'prop-changed' ? e.props : type === 'value-changed' ? ['value'] : undefined,
linkedProps = type === 'value-changed' ? this.linkedPropsValue : this.linkedProps
if (widget === this.parent) {
if (type === 'widget-created') return
@ -1268,11 +1268,11 @@ class Widget extends EventEmitter {
}
this.removed = true
this.off('change')
this.off('touch')
this.off('draginit')
this.off('drag')
this.off('dragend')
// this.off('value-changed')
// this.off('touch')
// this.off('draginit')
// this.off('drag')
// this.off('dragend')
widgetManager.off(undefined, undefined, this)
sessionManager.off(undefined, undefined, this)
this.removeOscReceivers()

View file

@ -60,7 +60,7 @@ class Keyboard extends Panel {
}, {element: this.widget})
this.on('change',(e)=>{
this.on('value-changed',(e)=>{
var widget = e.widget

View file

@ -41,7 +41,7 @@ class Matrix extends Panel {
this.value = []
this.on('change',(e)=>{
this.on('value-changed',(e)=>{
if (e.widget === this) return

View file

@ -1,7 +1,7 @@
var Panel = require('./panel'),
{icon, iconify} = require('../../ui/utils'),
resize = require('../../events/resize'),
doubletab = require('../mixins/double_tap'),
doubleTap = require('../mixins/double_tap'),
html = require('nanohtml'),
raw = require('nanohtml/raw'),
iOS13 = require('../../ui/ios') === 13
@ -99,24 +99,28 @@ class Modal extends Panel {
this.toggle = this.container.appendChild(html`<div class="toggle"></div>`)
if (this.getProp('doubleTap')) {
doubletab(this.toggle, ()=>{
doubleTap(this, ()=>{
this.setValue(1, {sync:true, send:true})
})
}, {element: this.toggle})
} else {
this.toggle.addEventListener('fast-click',(e)=>{
this.on('fast-click',(e)=>{
if (e.capturedByEditor === true) return
e.detail.preventOriginalEvent = true
this.setValue(1, {sync:true, send:true})
})
if (e.target === this.toggle) {
this.setValue(1, {sync:true, send:true})
}
}, {element: this.container})
}
var closer = DOM.get(this.popup, '.closer')[0]
this.popup.addEventListener('fast-click',(e)=>{
this.on('fast-click',(e)=>{
if ((e.target === this.popup || e.target === closer) && this.value == 1) {
e.detail.preventOriginalEvent = true
this.setValue(0, {sync:true, send:true})
}
})
}, {element: this.container})
this.escapeKeyHandler = ((e)=>{
if (e.keyCode==27) this.setValue(0, {sync:true, send:true})

View file

@ -106,11 +106,11 @@ class Panel extends Container() {
this.iosScrollbars[dir]._scrollable = false
this.container.appendChild(this.iosScrollbars[dir].container)
}
this.iosScrollbars.horizontal.on('change', (e)=>{
this.iosScrollbars.horizontal.on('value-changed', (e)=>{
e.stopPropagation = true
this.widget.scrollLeft = parseInt(e.widget.getValue() * (this.widget.scrollWidth - this.widget.clientWidth))
})
this.iosScrollbars.vertical.on('change', (e)=>{
this.iosScrollbars.vertical.on('value-changed', (e)=>{
e.stopPropagation = true
this.widget.scrollTop = parseInt(e.widget.getValue() * (this.widget.scrollHeight - this.widget.clientHeight))
})
@ -145,7 +145,7 @@ class Panel extends Container() {
this.scrollWidth = this.widget.scrollWidth - this.widget.clientWidth
this.scrollHeight = this.widget.scrollHeight - this.widget.clientHeight
})
this.widget.addEventListener('scroll', ()=>{
this.on('scroll', ()=>{
this.scrollWidth = this.widget.scrollWidth - this.widget.clientWidth
this.scrollHeight = this.widget.scrollHeight - this.widget.clientHeight
this.scroll = [this.widget.scrollLeft, this.widget.scrollTop]
@ -158,7 +158,7 @@ class Panel extends Container() {
if (this.props.type !== 'modal') {
this.setValue([x, y], {sync: true, send:true})
}
})
}, {element: this.widget})
}
@ -199,11 +199,11 @@ class Panel extends Container() {
this.createNavigation()
this.navigation.addEventListener('fast-click', (e)=>{
this.on('fast-click', (e)=>{
if (!e.target.hasAttribute('data-widget') || e.capturedByEditor) return
var index = DOM.index(e.target)
this.setValue(index, {sync: true, send:this.value != index})
})
}, {element: this.navigation})
this.on('tab-created', (e)=>{
this.createNavigation()

View file

@ -169,10 +169,10 @@ class PatchBay extends Container(Canvas) {
this.connecting = []
this.widget.addEventListener('fast-click', (e)=>{
this.on('fast-click', (e)=>{
this.toggleConnection(e.target)
e.stopPropagation()
})
}, {element: this.widget})
this.mousePosition = []
@ -221,9 +221,9 @@ class PatchBay extends Container(Canvas) {
if (this.connecting.length) {
var cb = (e)=>{
this.toggleConnection()
document.removeEventListener('fast-click', cb)
this.off('fast-click', cb)
}
document.addEventListener('fast-click', cb)
this.on('fast-click',cb, {element: this.widget})
}
}

View file

@ -1,10 +1,10 @@
module.exports = (element, callback, options={})=>{
module.exports = (self, callback, options={})=>{
var lastTapTime = 0,
lastTapX,
lastTapY
element.addEventListener(options.click ? 'click' : 'fast-click', (event)=>{
self.on(options.click ? 'click' : 'fast-click', (event)=>{
if (event.capturedByEditor) return
@ -38,6 +38,6 @@ module.exports = (element, callback, options={})=>{
}
})
}, {element: options.element})
}

View file

@ -157,7 +157,7 @@ module.exports = class MultiXy extends Pad {
}, {element: this.widget, multitouch: true})
this.on('change',(e)=>{
this.on('value-changed',(e)=>{
if (e.widget == this) return
e.stopPropagation = true
if (e.options.spring) e.options.dragged = true

View file

@ -60,7 +60,7 @@ module.exports = class Rgb extends Widget {
this.pad.sendValue = ()=>{}
this.pad.container.classList.add('not-editable', 'pad')
this.widget.appendChild(this.pad.container)
this.pad.on('change',(e)=>{
this.pad.on('value-changed',(e)=>{
e.stopPropagation = true
this.dragHandle()
})
@ -78,7 +78,7 @@ module.exports = class Rgb extends Widget {
}, parent: this})
this.hue.container.classList.add('not-editable', 'hue')
this.widget.appendChild(this.hue.container)
this.hue.on('change',(e)=>{
this.hue.on('value-changed',(e)=>{
e.stopPropagation = true
this.dragHandle(true)
})
@ -99,7 +99,7 @@ module.exports = class Rgb extends Widget {
}, parent: this})
this.alpha.container.classList.add('not-editable', 'alpha')
this.widget.appendChild(this.alpha.container)
this.alpha.on('change',(e)=>{
this.alpha.on('value-changed',(e)=>{
e.stopPropagation = true
this.dragAlphaHandle()
})

View file

@ -1,6 +1,6 @@
var Pad = require('./pad'),
Fader = require('../sliders/fader'),
doubletab = require('../mixins/double_tap'),
doubleTap = require('../mixins/double_tap'),
touchstate = require('../mixins/touch_state'),
fastdom = require('fastdom')
@ -80,7 +80,7 @@ module.exports = class Xy extends Pad {
}, parent: this}),
}
this.on('change',(e)=>{
this.on('value-changed',(e)=>{
if (e.widget == this) return
e.stopPropagation = true
})
@ -119,17 +119,17 @@ module.exports = class Xy extends Pad {
if (typeof this.getProp('doubleTap') === 'string' && this.getProp('doubleTap')[0] === '/') {
doubletab(this.widget, ()=>{
doubleTap(this, ()=>{
this.sendValue({v:null, address: this.getProp('doubleTap')})
})
}, {element: this.widget})
} else {
doubletab(this.widget, ()=>{
doubleTap(this, ()=>{
this.faders.x.setValue(this.faders.x.getSpringValue(), {sync: false, send:false, dragged:true})
this.faders.y.setValue(this.faders.y.getSpringValue(), {sync: false, send:false, dragged:true})
this.setValue([this.faders.x.getSpringValue(),this.faders.y.getSpringValue()],{sync:true, send:true, spring:true})
})
}, {element: this.widget})
}

View file

@ -69,7 +69,7 @@ class Range extends Fader {
]
this.on('change',(e)=>{
this.on('value-changed',(e)=>{
if (e.widget == this) return

View file

@ -1,7 +1,7 @@
var {clip, mapToScale} = require('../utils'),
Canvas = require('../common/canvas'),
touchstate = require('../mixins/touch_state'),
doubletab = require('../mixins/double_tap'),
doubleTap = require('../mixins/double_tap'),
html = require('nanohtml')
class Slider extends Canvas {
@ -58,28 +58,27 @@ class Slider extends Canvas {
if (typeof this.getProp('doubleTap') === 'string' && this.getProp('doubleTap')[0] === '/') {
doubletab(this.widget, ()=>{
doubleTap(this, ()=>{
this.sendValue({v:null, address: this.getProp('doubleTap')})
})
}, {element: this.widget})
} else {
doubletab(this.widget, ()=>{
this.setValue(this.getSpringValue(),{sync:true, send:true, fromLocal:true, doubleTab:true})
})
doubleTap(this, ()=>{
this.setValue(this.getSpringValue(),{sync:true, send:true, fromLocal:true, doubleTap:true})
}, {element: this.widget})
}
}
this.widget.addEventListener('wheel',this.mousewheelHandleProxy.bind(this))
touchstate(this, {element: this.widget, multitouch: options.multitouch})
this.on('draginit', this.draginitHandleProxy.bind(this), {element:this.widget, multitouch: options.multitouch})
this.on('drag', this.dragHandleProxy.bind(this), {element:this.widget, multitouch: options.multitouch})
this.on('dragend', this.dragendHandleProxy.bind(this), {element:this.widget, multitouch: options.multitouch})
this.on('wheel',this.mousewheelHandleProxy.bind(this), {element: this.widget})
this.setSteps()
@ -207,7 +206,7 @@ class Slider extends Canvas {
setValue(v,options={}) {
if (typeof v != 'number') return
if (this.touched && !options.dragged && !options.doubleTab) return this.setValueTouchedQueue = [v, options]
if (this.touched && !options.dragged && !options.doubleTap) return this.setValueTouchedQueue = [v, options]
var value = clip(v,[this.rangeValsMin,this.rangeValsMax])