From 636ae9ef2f4e0b25152ceef3f06f7e728e70b93b Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Tue, 25 Jun 2019 12:13:19 +0200 Subject: [PATCH] script: add id to context --- CHANGELOG.md | 1 + src/client/app/managers/widgets.js | 4 ++-- src/client/app/widgets/common/widget.js | 7 ++++--- src/client/app/widgets/inputs/script.js | 5 ++++- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9fa1530e..4ecf2b41 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - modal: `scroll` option - script: `get(id)` function added in `script`execution context - script: `getProp(id, property)` function added in `script`execution context + - script: `id` variable added in `script`execution context (id of the widget that triggered the script) ## 0.46.1 diff --git a/src/client/app/managers/widgets.js b/src/client/app/managers/widgets.js index 55213095..d37b5c6b 100644 --- a/src/client/app/managers/widgets.js +++ b/src/client/app/managers/widgets.js @@ -52,7 +52,7 @@ var WidgetManager = class WidgetManager extends EventEmitter { let v = widget.getValue() for (let i in widgetsById) { if (widgetsById[i] !== widget) { - widgetsById[i].setValue(v,{send:false,sync:false}) + widgetsById[i].setValue(v,{send:false,sync:false, id}) } } } @@ -63,7 +63,7 @@ var WidgetManager = class WidgetManager extends EventEmitter { let v = widget.getValue() for (let i in widgetsByLinkId) { if (widgetsByLinkId[i] !== widget) { - widgetsByLinkId[i].setValue(v,{send: options.send,sync: true}) + widgetsByLinkId[i].setValue(v,{send: options.send,sync: true, id}) } } } diff --git a/src/client/app/widgets/common/widget.js b/src/client/app/widgets/common/widget.js index 0e291108..d00d91a0 100644 --- a/src/client/app/widgets/common/widget.js +++ b/src/client/app/widgets/common/widget.js @@ -672,6 +672,8 @@ class Widget extends EventEmitter { propNames.push('value') } + if (widget && options) options.id = widget.getProp('id') + var reCreate = false, changedProps = [] @@ -713,7 +715,6 @@ class Widget extends EventEmitter { } for (var i in changedProps) { - this.onPropChanged(changedProps[i].propName, options, changedProps[i].oldPropValue) } @@ -728,12 +729,12 @@ class Widget extends EventEmitter { } - onPropChanged(propName, options, oldPropValue) { + onPropChanged(propName, options={}, oldPropValue) { switch(propName) { case 'value': - this.setValue(this.getProp('value'), {sync: true, send: options && options.send}) + this.setValue(this.getProp('value'), {sync: true, send: options.send, id: options.id}) return case 'top': diff --git a/src/client/app/widgets/inputs/script.js b/src/client/app/widgets/inputs/script.js index 9f04aa95..5c01f3d0 100644 --- a/src/client/app/widgets/inputs/script.js +++ b/src/client/app/widgets/inputs/script.js @@ -25,7 +25,8 @@ class Script extends Widget { ]}, script: {type: 'string', value: '', help: [ 'This property is evaluated each time the widget receives a value* if condition is non-falsy. Formulas are given extras variables in this context:', - '- `value`: the value received by the widget', + '- `value`: value received by the widget', + '- `id`: id of the widget that triggered the script', '- `send(target, address, arg1, arg2, ...)`: function for sending osc messages (ignores the script\'s targets and the server\'s defaults unless `target` is `false`; ignores the script\'s `preArgs`)', '- `set(id, value)`: function for setting a widget\'s value', '- `get(id)`: function for getting a widget\'s value', @@ -109,6 +110,7 @@ class Script extends Widget { var context = { value: v, + id: options.id, send: options.send ? this.scriptSend.bind(this) : ()=>{}, set: (id, value)=>{Script.scriptSet(id, value, options)}, get: (id)=>{return Script.scriptGet(id)}, @@ -134,6 +136,7 @@ Script.parsersContexts.condition = { Script.parsersContexts.script = { value: '', + id: '', send: ()=>{}, set: ()=>{}, get: ()=>{},