events: use delegated events as much as possible to avoid adding dom listeners when creating widgets
This commit is contained in:
parent
a7683787d2
commit
459a37960c
23 changed files with 172 additions and 113 deletions
|
|
@ -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 {
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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})
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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'
|
||||
|
|
|
|||
|
|
@ -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)=>{
|
||||
|
||||
|
|
|
|||
|
|
@ -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})
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -60,7 +60,7 @@ class Keyboard extends Panel {
|
|||
}, {element: this.widget})
|
||||
|
||||
|
||||
this.on('change',(e)=>{
|
||||
this.on('value-changed',(e)=>{
|
||||
|
||||
var widget = e.widget
|
||||
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ class Matrix extends Panel {
|
|||
|
||||
this.value = []
|
||||
|
||||
this.on('change',(e)=>{
|
||||
this.on('value-changed',(e)=>{
|
||||
|
||||
if (e.widget === this) return
|
||||
|
||||
|
|
|
|||
|
|
@ -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})
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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})
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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})
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
})
|
||||
|
|
|
|||
|
|
@ -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})
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -69,7 +69,7 @@ class Range extends Fader {
|
|||
]
|
||||
|
||||
|
||||
this.on('change',(e)=>{
|
||||
this.on('value-changed',(e)=>{
|
||||
|
||||
if (e.widget == this) return
|
||||
|
||||
|
|
|
|||
|
|
@ -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])
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue