Merge pull request #394 from jean-emmanuel/dynamic-props-edit
Dynamic props edition
This commit is contained in:
commit
bce85a392b
10 changed files with 208 additions and 92 deletions
|
|
@ -14,6 +14,43 @@ DOM.ready(()=>{
|
|||
|
||||
function updateWidget(widget, options={}) {
|
||||
|
||||
if (options.changedProps) {
|
||||
// alternate routine for small edits
|
||||
var propNames = options.changedProps,
|
||||
propValues = propNames.map(x => widget.props[x]),
|
||||
linkedProps = []
|
||||
|
||||
Object.values(widget.linkedProps).forEach(l => linkedProps = linkedProps.concat(l))
|
||||
Object.values(widget.linkedPropsValue).forEach(l => linkedProps = linkedProps.concat(l))
|
||||
Object.values(widget.oscReceivers).forEach(r => linkedProps = linkedProps.concat(r.propNames))
|
||||
|
||||
if (
|
||||
// if non dynamic props have changed, skip this and use rebuild routine
|
||||
!propNames.some(n => !widget.constructor.dynamicProps.includes(n))
|
||||
) {
|
||||
|
||||
var edited = widget.updateProps(options.changedProps, null) || widget
|
||||
|
||||
if (
|
||||
// if prop/osc listeners have changed (@{} / OSC{})
|
||||
// refresh the widget's props cache and update linked props bindings
|
||||
!propValues.some(x => typeof x === 'string' && x.match(/OSC\{|@\{/))
|
||||
&& !propNames.some(n => linkedProps.includes(n))
|
||||
) {
|
||||
widget.createPropsCache()
|
||||
}
|
||||
|
||||
if (editor.selectedWidgets.includes(widget) && !options.preventSelect) {
|
||||
editor.select(edited)
|
||||
}
|
||||
|
||||
return edited
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
var reuseChildren = options.reuseChildren !== false && widget instanceof Panel
|
||||
|
||||
// save state
|
||||
|
|
|
|||
|
|
@ -67,8 +67,8 @@ module.exports = function editField(editor, widget, propName, defaultValue){
|
|||
|
||||
if (defaultValue.type.includes('boolean')) {
|
||||
|
||||
var toggle = DOM.create(`
|
||||
<span class="checkbox ${widget.props[propName] ? 'on' : ''}">
|
||||
let toggle = DOM.create(`
|
||||
<span class="checkbox ${widget.getProp(propName) ? 'on' : ''}">
|
||||
${icon('check')}
|
||||
</span>
|
||||
`)
|
||||
|
|
@ -85,10 +85,13 @@ module.exports = function editField(editor, widget, propName, defaultValue){
|
|||
field.appendChild(input)
|
||||
|
||||
}
|
||||
|
||||
var lock
|
||||
var onChange = ()=>{
|
||||
|
||||
input.removeEventListener('change', onChange)
|
||||
if (lock) return
|
||||
lock = true
|
||||
|
||||
input.blur()
|
||||
|
||||
var v
|
||||
|
||||
|
|
@ -101,11 +104,13 @@ module.exports = function editField(editor, widget, propName, defaultValue){
|
|||
var newWidgets = []
|
||||
for (var w of editor.selectedWidgets) {
|
||||
w.props[propName] = v !== '' ? v : deepCopy(defaultValue.value)
|
||||
newWidgets.push(updateWidget(w, {preventSelect: editor.selectedWidgets.length > 1}))
|
||||
newWidgets.push(updateWidget(w, {changedProps: [propName], preventSelect: editor.selectedWidgets.length > 1}))
|
||||
}
|
||||
editor.pushHistory()
|
||||
if (newWidgets.length > 1) editor.select(newWidgets)
|
||||
|
||||
lock = false
|
||||
|
||||
}
|
||||
|
||||
input.addEventListener('change', onChange)
|
||||
|
|
|
|||
|
|
@ -96,6 +96,10 @@ var Editor = class Editor {
|
|||
})
|
||||
keyboardJS.bind(['alt + up', 'alt + down', 'alt + right', 'alt + left'], (e)=>{
|
||||
if (e.target.classList.contains('no-keybinding')) return
|
||||
if (!this.selectedWidgets.length) return
|
||||
|
||||
if (this.selectedWidgets[0].props.height === undefined && e.key.match(/Arrow(Up|Down)/)) return
|
||||
if (this.selectedWidgets[0].props.width === undefined && e.key.match(/Arrow(Left|Right)/)) return
|
||||
|
||||
var deltaW = e.key === 'ArrowLeft' ? -GRIDWIDTH : e.key === 'ArrowRight' ? GRIDWIDTH : 0,
|
||||
deltaH = e.key === 'ArrowUp' ? -GRIDWIDTH : e.key === 'ArrowDown' ? GRIDWIDTH : 0
|
||||
|
|
@ -109,6 +113,10 @@ var Editor = class Editor {
|
|||
})
|
||||
keyboardJS.bind(['up', 'down', 'right', 'left'], (e)=>{
|
||||
if (e.target.classList.contains('no-keybinding')) return
|
||||
if (!this.selectedWidgets.length) return
|
||||
|
||||
if (this.selectedWidgets[0].props.top === undefined && e.key.match(/Arrow(Up|Down)/)) return
|
||||
if (this.selectedWidgets[0].props.left === undefined && e.key.match(/Arrow(Left|Right)/)) return
|
||||
|
||||
var deltaX = e.key === 'ArrowLeft' ? -GRIDWIDTH : e.key === 'ArrowRight' ? GRIDWIDTH : 0,
|
||||
deltaY = e.key === 'ArrowUp' ? -GRIDWIDTH : e.key === 'ArrowDown' ? GRIDWIDTH : 0
|
||||
|
|
@ -689,7 +697,7 @@ var Editor = class Editor {
|
|||
}
|
||||
}
|
||||
|
||||
if (w.props.width !== undefined || w.props.height !== undefined) newWidgets.push(updateWidget(w, {preventSelect: this.selectedWidgets.length > 1}))
|
||||
if (w.props.width !== undefined || w.props.height !== undefined) newWidgets.push(updateWidget(w, {changedProps: ['width', 'height'], preventSelect: this.selectedWidgets.length > 1}))
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -720,7 +728,7 @@ var Editor = class Editor {
|
|||
w.props.left = newLeft
|
||||
}
|
||||
|
||||
newWidgets.push(updateWidget(w, {preventSelect: this.selectedWidgets.length > 1}))
|
||||
newWidgets.push(updateWidget(w, {changedProps: ['top', 'left'], preventSelect: this.selectedWidgets.length > 1}))
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -94,15 +94,22 @@ module.exports = class EventEmitter extends WolfyEventEmitter {
|
|||
|
||||
}
|
||||
|
||||
removeEventContext(context) {
|
||||
removeEventContext(context, evt, listener) {
|
||||
|
||||
var events = this._contextEvents[context.hash]
|
||||
|
||||
if (events) {
|
||||
|
||||
for (var i = 0; i < events.length; i++) {
|
||||
|
||||
if (evt && evt !== events[i][0]) continue
|
||||
if (listener && listener !== events[i][1]) continue
|
||||
|
||||
this.removeListener(events[i][0], events[i][1])
|
||||
}
|
||||
delete this._contextEvents[context.hash]
|
||||
|
||||
if (!this._contextEvents[context.hash].length) delete this._contextEvents[context.hash]
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,13 +47,15 @@ class Container extends Widget {
|
|||
// ...right ? :)
|
||||
var tabs = this.getProp('tabs'),
|
||||
widgets= this.getProp('widgets'),
|
||||
type = tabs && tabs.length ? 'tabs' : widgets && widgets.length ? 'widgets' : null
|
||||
type = tabs && tabs.length ? 'tabs' : widgets && widgets.length ? 'widgets' : null,
|
||||
data = type ? this.getProp(type) : null
|
||||
|
||||
if (!type) return
|
||||
|
||||
|
||||
for (let i in this.children) {
|
||||
if (this.children[i]) {
|
||||
this.children[i].props = this.getProp(type)[i]
|
||||
if (this.children[i] && data[i]) {
|
||||
this.children[i].props = data[i]
|
||||
this.children[i].cachedProps[type] = this.children[i].props[type]
|
||||
if (this.children[i].alignChildrenProps) {
|
||||
this.children[i].alignChildrenProps()
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ module.exports = class OscReceiver {
|
|||
|
||||
if (this.address !== address) {
|
||||
|
||||
if (this.address) osc.off(this.address, this.bindedCallback)
|
||||
if (this.address) osc.removeEventContext(this.parent, this.address, this.bindedCallback)
|
||||
|
||||
this.address = address
|
||||
osc.on(this.address, this.bindedCallback, {context: this.parent})
|
||||
|
|
|
|||
|
|
@ -148,67 +148,14 @@ class Widget extends EventEmitter {
|
|||
delete this.props[this.parent.getProp('horizontal') ? 'height' : 'width']
|
||||
}
|
||||
|
||||
// @{props} links lists
|
||||
this.linkedProps = {}
|
||||
this.linkedPropsValue = {}
|
||||
|
||||
// OSC{/path} receivers
|
||||
this.oscReceivers = {}
|
||||
|
||||
// cache props (resolve @{props})
|
||||
this.cachedProps = {}
|
||||
|
||||
for (var k in this.props) {
|
||||
if (k != 'widgets' && k != 'tabs') {
|
||||
this.cachedProps[k] = this.resolveProp(k, undefined, true)
|
||||
} else {
|
||||
this.cachedProps[k] = this.props[k]
|
||||
}
|
||||
}
|
||||
this.createPropsCache()
|
||||
|
||||
if (this.getProp('id') == 'root' && !options.root) {
|
||||
this.cachedProps.id = '_root'
|
||||
this.errors.id = 'There can only be one root'
|
||||
}
|
||||
|
||||
if (Object.keys(this.linkedProps).length) {
|
||||
|
||||
widgetManager.on('widget-created', (e)=>{
|
||||
var {id, widget, options} = e
|
||||
if (widget == this.parent) return
|
||||
if (widget == this) id = 'this'
|
||||
if (this.linkedProps[id]) {
|
||||
this.updateProps(this.linkedProps[id], widget, options)
|
||||
}
|
||||
}, {context: this})
|
||||
|
||||
widgetManager.on('prop-changed', (e)=>{
|
||||
let {id, widget, options} = e
|
||||
if (widget == this) id = 'this'
|
||||
if (widget == this.parent) id = 'parent'
|
||||
if (this.linkedProps[id]) {
|
||||
this.updateProps(this.linkedProps[id], widget, options, e.props)
|
||||
}
|
||||
}, {context: this})
|
||||
|
||||
}
|
||||
|
||||
if (Object.keys(this.linkedPropsValue).length) {
|
||||
|
||||
widgetManager.on('change', (e)=>{
|
||||
var {id, widget, options} = e
|
||||
if (widget == this) id = 'this'
|
||||
if (widget == this.parent) id = 'parent'
|
||||
if (this.linkedPropsValue[id]) {
|
||||
this.updateProps(this.linkedPropsValue[id], widget, options, ['value'])
|
||||
}
|
||||
}, {context: this})
|
||||
|
||||
}
|
||||
|
||||
var selfLinkedOSCProps = (this.linkedPropsValue['this'] || []).filter(i=>OSCProps.indexOf(i) > -1)
|
||||
this.selfLinkedOSCProps = selfLinkedOSCProps.length ? selfLinkedOSCProps : false
|
||||
|
||||
this.createLinkedPropsBindings()
|
||||
|
||||
this.disabledProps = []
|
||||
|
||||
|
|
@ -322,6 +269,99 @@ class Widget extends EventEmitter {
|
|||
|
||||
getSplit() {}
|
||||
|
||||
createPropsCache() {
|
||||
|
||||
// @{props} links lists
|
||||
this.linkedProps = {}
|
||||
this.linkedPropsValue = {}
|
||||
|
||||
// OSC{/path} receivers
|
||||
if (this.oscReceivers) {
|
||||
this.removeOscReceivers()
|
||||
}
|
||||
this.oscReceivers = {}
|
||||
|
||||
// Cache resolved props
|
||||
this.cachedProps = {}
|
||||
|
||||
for (var k in this.props) {
|
||||
if (k != 'widgets' && k != 'tabs') {
|
||||
this.cachedProps[k] = this.resolveProp(k, undefined, true)
|
||||
} else {
|
||||
this.cachedProps[k] = this.props[k]
|
||||
}
|
||||
}
|
||||
|
||||
this.createLinkedPropsBindings()
|
||||
|
||||
}
|
||||
|
||||
createLinkedPropsBindings() {
|
||||
|
||||
if (!this.linkedCreatedCallback && Object.keys(this.linkedProps).length) {
|
||||
|
||||
this.linkedCreatedCallback = (e)=>{
|
||||
this.onLinkedPropsChanged(e, 'widget-created')
|
||||
}
|
||||
|
||||
this.linkedPropChangedCallback = (e)=>{
|
||||
this.onLinkedPropsChanged(e, 'prop-changed')
|
||||
}
|
||||
|
||||
widgetManager.on('widget-created', this.linkedCreatedCallback, {context: this})
|
||||
widgetManager.on('prop-changed', this.linkedPropChangedCallback, {context: this})
|
||||
|
||||
} else if (this.linkedCreatedCallback && !Object.keys(this.linkedPropsValue).length) {
|
||||
|
||||
widgetManager.removeEventContext('widget-created', this.linkedCreatedCallback, {context: this})
|
||||
widgetManager.removeEventContext('prop-changed', this.linkedPropChangedCallback, {context: this})
|
||||
|
||||
}
|
||||
|
||||
if (!this.linkedValueChangedCallback && Object.keys(this.linkedPropsValue).length) {
|
||||
|
||||
this.linkedValueChangedCallback = (e)=>{
|
||||
this.onLinkedPropsChanged(e, 'change')
|
||||
}
|
||||
|
||||
widgetManager.on('change', this.linkedValueChangedCallback, {context: this})
|
||||
|
||||
} else if (this.linkedPropsValueCallback && !Object.keys(this.linkedPropsValue).length) {
|
||||
|
||||
widgetManager.removeEventContext('change', this.linkedValueChangedCallback, {context: this})
|
||||
|
||||
}
|
||||
|
||||
var selfLinkedOSCProps = (this.linkedPropsValue['this'] || []).filter(i=>OSCProps.indexOf(i) > -1)
|
||||
this.selfLinkedOSCProps = selfLinkedOSCProps.length ? selfLinkedOSCProps : false
|
||||
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
if (widget === this.parent) return
|
||||
if (widget === this) id = 'this'
|
||||
|
||||
if (linkedProps[id]) {
|
||||
this.updateProps(linkedProps[id], widget, options, changedProps)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
removeOscReceivers() {
|
||||
|
||||
osc.removeEventContext(this)
|
||||
for (var i in this.oscReceivers) {
|
||||
oscReceiverState[i] = this.oscReceivers[i].value
|
||||
}
|
||||
this.oscReceivers = {}
|
||||
|
||||
}
|
||||
|
||||
resolveProp(propName, propValue, storeLinks=true, originalWidget, originalPropName, context) {
|
||||
|
||||
propValue = propValue !== undefined ? propValue : deepCopy(this.props[propName])
|
||||
|
|
@ -549,14 +589,15 @@ class Widget extends EventEmitter {
|
|||
|
||||
}
|
||||
}
|
||||
if (reCreate && !this.contains(widget) && widget !== this && !(widget === this && updatedProps.length === 1 && updatedProps[0] === 'value')) {
|
||||
|
||||
this.reCreateWidget({reCreateOptions: options})
|
||||
return true
|
||||
if (reCreate && (!widget || !this.contains(widget)) && widget !== this && !(widget === this && updatedProps.length === 1 && updatedProps[0] === 'value')) {
|
||||
|
||||
return this.reCreateWidget({reCreateOptions: options})
|
||||
|
||||
} else if (changedProps.length) {
|
||||
|
||||
for (var i in changedProps) {
|
||||
|
||||
this.onPropChanged(changedProps[i].propName, options, changedProps[i].oldPropValue)
|
||||
}
|
||||
|
||||
|
|
@ -581,6 +622,9 @@ class Widget extends EventEmitter {
|
|||
|
||||
case 'top':
|
||||
case 'left':
|
||||
this.setContainerStyles(['geometry'])
|
||||
return
|
||||
|
||||
case 'height':
|
||||
case 'width':
|
||||
this.setContainerStyles(['geometry'])
|
||||
|
|
@ -589,6 +633,9 @@ class Widget extends EventEmitter {
|
|||
|
||||
case 'label':
|
||||
this.setContainerStyles(['label'])
|
||||
if (oldPropValue === false || this.getProp('label') === false) {
|
||||
resize.check(this.container)
|
||||
}
|
||||
return
|
||||
|
||||
case 'css':
|
||||
|
|
@ -656,6 +703,7 @@ class Widget extends EventEmitter {
|
|||
if (this.getProp('label') === false) {
|
||||
this.container.classList.add('nolabel')
|
||||
} else {
|
||||
this.container.classList.remove('nolabel')
|
||||
var label = this.getProp('label') == 'auto'?
|
||||
this.getProp('id'):
|
||||
iconify(this.getProp('label'))
|
||||
|
|
@ -717,17 +765,14 @@ class Widget extends EventEmitter {
|
|||
|
||||
reCreateWidget(options={}){
|
||||
|
||||
updateWidget(this, options)
|
||||
return updateWidget(this, options)
|
||||
|
||||
}
|
||||
|
||||
onRemove(){
|
||||
|
||||
widgetManager.removeEventContext(this)
|
||||
osc.removeEventContext(this)
|
||||
for (var i in this.oscReceivers) {
|
||||
oscReceiverState[i] = this.oscReceivers[i].value
|
||||
}
|
||||
this.removeOscReceivers()
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -69,6 +69,18 @@ class Clone extends Container {
|
|||
|
||||
}, {context: this})
|
||||
|
||||
widgetManager.on('prop-changed', (e)=>{
|
||||
|
||||
if (!this.cloneTarget) return
|
||||
|
||||
if (e.widget === this.cloneTarget || this.cloneTarget.contains(e.widget)) {
|
||||
|
||||
this.updateClone()
|
||||
|
||||
}
|
||||
|
||||
}, {context: this})
|
||||
|
||||
}
|
||||
|
||||
getCloneTarget() {
|
||||
|
|
|
|||
|
|
@ -25,4 +25,20 @@ module.exports = class _matrix_base extends Container {
|
|||
|
||||
}
|
||||
|
||||
onPropChanged(propName, options, oldPropValue) {
|
||||
|
||||
if (super.onPropChanged(...arguments)) return true
|
||||
|
||||
switch (propName) {
|
||||
|
||||
case 'color':
|
||||
for (var w of this.children) {
|
||||
w.onPropChanged('color')
|
||||
}
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -97,20 +97,4 @@ module.exports = class Multifader extends _matrix_base {
|
|||
|
||||
}
|
||||
|
||||
onPropChanged(propName, options, oldPropValue) {
|
||||
|
||||
if (super.onPropChanged(...arguments)) return true
|
||||
|
||||
switch (propName) {
|
||||
|
||||
case 'color':
|
||||
for (var w of this.children) {
|
||||
w.onPropChanged('color')
|
||||
}
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue