From b56d05713d4c489458d9c44d89fb5dcd2920d564 Mon Sep 17 00:00:00 2001 From: bordun Date: Sat, 1 May 2021 17:08:32 +0200 Subject: [PATCH] docs: VAR{}, getVar, setVar --- .../docs/docs/widgets/advanced-syntaxes.md | 6 +++++- resources/docs/docs/widgets/scripting.md | 17 +++++++++++++++++ src/client/widgets/scripts/script-vm.js | 2 +- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/resources/docs/docs/widgets/advanced-syntaxes.md b/resources/docs/docs/widgets/advanced-syntaxes.md index 22f5123f..82229fd3 100644 --- a/resources/docs/docs/widgets/advanced-syntaxes.md +++ b/resources/docs/docs/widgets/advanced-syntaxes.md @@ -56,10 +56,14 @@ Options can contain `@{}` blocks. }} ``` +## Custom variables: `VAR{variableName, default}` + +This syntax creates a custom variable in the widget that can be read and modified from any widget's `script` property (see [scripting](./scripting.md#getvarid-name)). Changes made to this variable will be applied automatically. + ## Javascript: `JS{{ }}` -This syntax allows writing formulas in [Javascript](https://developer.mozilla.org/en-US/docs/Web/JavaScript). The code will be compiled as a function and executed in a restricted context. +This syntax allows defining a property using [Javascript](https://developer.mozilla.org/en-US/docs/Web/JavaScript). The code will be compiled as a function and executed in a restricted context. - if no `return` statement is found, the formula will return an empty string - javascript [strict mode](https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Strict_mode) is always enabled diff --git a/resources/docs/docs/widgets/scripting.md b/resources/docs/docs/widgets/scripting.md index de7cdc73..dd2be5eb 100644 --- a/resources/docs/docs/widgets/scripting.md +++ b/resources/docs/docs/widgets/scripting.md @@ -109,6 +109,23 @@ If the event that triggered the script's execution was initiated by a user inter ---- +#### `getVar(id, name)` +- `id`: widget `id` as a string. +- `name`: variable name as a string. + +Returns the value of a widget's custom variable. + +---- + +#### `setVar(id, name, value)` +- `id`: widget `id` as a string. Can be `"this"` to target the host widget, or `"parent"` to target the parent widget. `id` may contains wildcards ('\*'). +- `name`: variable name as a string. +- `value`: new value for the variable. + +Sets the value of a widget's custom variable. If `ìd` contains wildcards, affects all matching widgets. + +---- + #### `send(target, address, ...args)` - `target` (optional): `"ip:port"` or `"midi:device_name"` string. If omitted, the widget's target will be used. - `address`: osc address, must start with a `/` diff --git a/src/client/widgets/scripts/script-vm.js b/src/client/widgets/scripts/script-vm.js index e57ad13c..62430e69 100644 --- a/src/client/widgets/scripts/script-vm.js +++ b/src/client/widgets/scripts/script-vm.js @@ -384,7 +384,7 @@ class ScriptVm extends Vm { for (var imports of ['set', 'get', 'getProp', 'getIndex', 'updateProp', 'send', 'httpGet', 'stateGet', 'stateSet', 'storage', 'setInterval', 'clearInterval', 'setTimeout', 'clearTimeout', 'setFocus', 'unfocus', 'setScroll', 'getScroll', 'toolbar', - 'openUrl']) { + 'openUrl', 'getVar', 'setVar']) { this.sanitize(this.sandbox.contentWindow[imports]) }