From 42f316cf8176f3067ea6a21450bca974764f24ef Mon Sep 17 00:00:00 2001 From: bordun Date: Tue, 3 Nov 2020 09:11:53 +0100 Subject: [PATCH] script: add `getIndex` function --- CHANGELOG.md | 4 ++++ resources/docs/docs/widgets/scripting.md | 7 +++++++ src/client/widgets/scripts/script-vm.js | 9 ++++++++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 947b3c15..4445b4aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,12 @@ ## 1.6.1 +- bug fixes + - keyboard: prevent `script` property from being copied to each key + - widgets - keyboard: make `on` and `off` properties dynamic + - script: add `getIndex` function ## 1.6.0 diff --git a/resources/docs/docs/widgets/scripting.md b/resources/docs/docs/widgets/scripting.md index 8b9a80db..a551d3a1 100644 --- a/resources/docs/docs/widgets/scripting.md +++ b/resources/docs/docs/widgets/scripting.md @@ -96,6 +96,13 @@ Returns the property called `"name"` of the first matching widget. ---- +#### `getIndex(id)` +- `id` (optional): widget `id` as a string. Defaults to `"this"`. + +Returns the widget's index in its container. + +---- + #### `updateProp(id, name)` - `id`: widget `id` as a string. Can be `"this"` to target the host widget, or `"parent"` to target the parent widget. - `name`: property name. diff --git a/src/client/widgets/scripts/script-vm.js b/src/client/widgets/scripts/script-vm.js index a18e10ef..a661110f 100644 --- a/src/client/widgets/scripts/script-vm.js +++ b/src/client/widgets/scripts/script-vm.js @@ -162,6 +162,13 @@ class ScriptVm extends Vm { } + this.sandbox.contentWindow.getIndex = (id = 'this')=>{ + + var widget = this.resolveId(id).pop() + if (widget) return widget.parent.children.indexOf(widget) + + } + this.sandbox.contentWindow.getScroll = (id)=>{ var widgets = this.resolveId(id) @@ -346,7 +353,7 @@ class ScriptVm extends Vm { } - for (var imports of ['set', 'get', 'getProp', 'updateProp', 'send', 'httpGet', 'stateGet', 'stateSet', 'storage', + for (var imports of ['set', 'get', 'getProp', 'getIndex', 'updateProp', 'send', 'httpGet', 'stateGet', 'stateSet', 'storage', 'setInterval', 'clearInterval', 'setTimeout', 'clearTimeout', 'unfocus', 'setScroll', 'getScroll', 'toolbar']) { this.sanitize(this.sandbox.contentWindow[imports]) }