linked-props: make nested links dynamic (#398)

This commit is contained in:
jean-emmanuel 2018-10-21 21:36:19 +02:00
parent cb2097a873
commit 787c6e3b91
3 changed files with 41 additions and 5 deletions

View file

@ -1,12 +1,15 @@
# Changelog
## 0.38.1
## 0.38.2
- bug fixes
- `@{parent}` calls not working consistently in clone widgets
- `slider/pads`: inteverted ranges not working
- fix `doubleTap` for sliders with `snap` set to `true`
- undo/redo errors
- widgets
- 1-level property inheritance nesting (`@{id_@{id_2}}`) are now updated dynamically
- editor
- major performance improvements (containers' children are reused whenever possible instead of being recreated)
- removed widget deletion confirmation popup

View file

@ -45,8 +45,7 @@ Some properties have much cheaper update routines and can be considered as `dyna
### Nesting
The inheritance syntax supports nesting with a limitation: nested calls are not updated when the target's property/value updates, they are only resolved once at the host widget's initialization.
The inheritance syntax supports 1-level nesting : `@{fader_@{toggle_1}}`
## OSC listeners: `OSC{}`

View file

@ -273,6 +273,7 @@ class Widget extends EventEmitter {
// @{props} links lists
this.linkedProps = {}
this.nestedLinkedProps = {}
this.linkedPropsValue = {}
// OSC{/path} receivers
@ -347,9 +348,13 @@ class Widget extends EventEmitter {
if (type === 'widget-created') return
id = 'parent'
}
if (widget === this) id = 'this'
if (this.nestedLinkedProps[id]) {
this.updateLinkedPropsWithNesting(id)
}
if (linkedProps[id]) {
this.updateProps(linkedProps[id], widget, options, changedProps)
}
@ -366,6 +371,30 @@ class Widget extends EventEmitter {
}
updateLinkedPropsWithNesting(i) {
// 1. remove all linked props found in properties bound to the nested link
// 2. resolve these props again and recreate links (3rd arg to true)
for (var prop of this.nestedLinkedProps[i]) {
for (var linksStores of [this.linkedPropsValue, this.linkedProps]) {
for (let id in linksStores) {
if (linksStores[id].includes(prop)) {
linksStores[id].splice(linksStores[id].indexOf(prop), 1)
if (!linksStores[id].length) delete linksStores[id]
}
}
}
this.resolveProp(prop, undefined, true, this)
}
}
resolveProp(propName, propValue, storeLinks=true, originalWidget, originalPropName, context) {
propValue = propValue !== undefined ? propValue : deepCopy(this.props[propName])
@ -381,7 +410,7 @@ class Widget extends EventEmitter {
propValue = propValue.replace(/@\{(?:[^{}]|(@\{[^{}]*\}))*\}/g, (m, nested)=>{
if (nested) {
m = m.replace(nested, this.resolveProp(propName, nested, storeLinks, this))
m = m.replace(nested, this.resolveProp(propName, nested, storeLinks ? 'nested' : false, this))
}
let id = m.substr(2, m.length - 3).split('.'),
@ -442,6 +471,11 @@ class Widget extends EventEmitter {
}
if (storeLinks === 'nested') {
if (!this.nestedLinkedProps[id]) this.nestedLinkedProps[id] = []
if (this.nestedLinkedProps[id].indexOf(propName) == -1) this.nestedLinkedProps[id].push(propName)
}
}
for (var i in widgets) {