linked-props: make nested links dynamic (#398)
This commit is contained in:
parent
8a1c0da634
commit
0a9929d0a0
3 changed files with 41 additions and 5 deletions
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue