From 787c6e3b91ac04803fde34b4d71d269b1f3d5830 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Sun, 21 Oct 2018 21:36:19 +0200 Subject: [PATCH] linked-props: make nested links dynamic (#398) --- CHANGELOG.md | 5 ++- .../docs/extras/advanced-property-syntax.md | 3 +- src/browser/js/app/widgets/common/widget.js | 38 ++++++++++++++++++- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a57732f9..85dc532d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/resources/docs/extras/advanced-property-syntax.md b/resources/docs/extras/advanced-property-syntax.md index fba34e40..4f1caa77 100644 --- a/resources/docs/extras/advanced-property-syntax.md +++ b/resources/docs/extras/advanced-property-syntax.md @@ -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{}` diff --git a/src/browser/js/app/widgets/common/widget.js b/src/browser/js/app/widgets/common/widget.js index 03af2d00..52cf750f 100644 --- a/src/browser/js/app/widgets/common/widget.js +++ b/src/browser/js/app/widgets/common/widget.js @@ -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) {