From 467b2cd4c23fbc6f769ded6ca956e93d1de49c5d Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Sun, 9 Jan 2022 12:07:55 +0100 Subject: [PATCH] - renamed `script` to `onValue`, `draw` to `onDraw`, `touch` to `onTouch` - added `onCreate` script property to all widgets --- CHANGELOG.md | 6 +- package.json | 2 +- .../docs/docs/widgets/advanced-syntaxes.md | 2 +- resources/docs/docs/widgets/canvas.md | 18 ++-- resources/docs/docs/widgets/scripting.md | 87 ++++++++++--------- src/client/managers/session/session.js | 31 ++++++- src/client/ui/ui-inspector-field.js | 10 ++- src/client/widgets/basics/button.js | 2 +- src/client/widgets/common/canvas.js | 4 +- src/client/widgets/common/widget.js | 47 +++++----- src/client/widgets/containers/clone.js | 7 +- src/client/widgets/containers/fragment.js | 8 +- src/client/widgets/containers/matrix.js | 8 +- src/client/widgets/pads/canvas.js | 64 +++++++------- src/client/widgets/scripts/script.js | 29 ++++--- src/client/widgets/sliders/encoder.js | 2 +- 16 files changed, 194 insertions(+), 133 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f6c999e6..97a32688 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,14 @@ # Changelog -## 1.13.3 +## 1.14.0 - bug fixes - misc: sending typed arguments (`{type, value}` objects) should override the widget's `typeTags` definition +- widgets + - renamed `script` to `onValue`, `draw` to `onDraw`, `touch` to `onTouch` + - added `onCreate` script property to all widgets + ## 1.13.2 - bug fixes diff --git a/package.json b/package.json index 93f8a85e..46e424c9 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "open-stage-control", "productName": "Open Stage Control", "description": "Libre and modular OSC / MIDI controller", - "version": "1.13.2", + "version": "1.14.0", "author": { "name": "Jean-Emmanuel Doucet", "email": "jean-emmanuel@ammd.net" diff --git a/resources/docs/docs/widgets/advanced-syntaxes.md b/resources/docs/docs/widgets/advanced-syntaxes.md index d49149a2..2a9f80d1 100644 --- a/resources/docs/docs/widgets/advanced-syntaxes.md +++ b/resources/docs/docs/widgets/advanced-syntaxes.md @@ -58,7 +58,7 @@ 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. +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{ }` diff --git a/resources/docs/docs/widgets/canvas.md b/resources/docs/docs/widgets/canvas.md index 59da5259..35b877fa 100644 --- a/resources/docs/docs/widgets/canvas.md +++ b/resources/docs/docs/widgets/canvas.md @@ -26,7 +26,7 @@ This is a convenience option that clears the canvas context and calls [`beginPat If this property is enabled, the widget will be redrawn at a fixed rate even if not interacted with (see [`draw`](#draw)). -### `touch` +### `onTouch` This script is executed when the widget is touched, when it is released, and during the movement of the pointer while the widget is touched. @@ -48,10 +48,10 @@ This script has access to the same variables and functions as the `script` prope !!! note "Extra html elements" - Elements added using the `html` property can be touched as well, `event.targetName` can be used to determine which element is touched. + Elements added using the `html` property can be touched as well, `event.targetName` can be used to determine which element is touched. -### `draw` +### `onDraw` This script is executed when the widget should be redrawn, which is when it's touched and when it receives a value. @@ -70,7 +70,7 @@ Let's create a simple xy pad, with a value made of two numbers between 0 and 1. First, we use the `touch` property to store the touch coordinates in the `locals` object. We also call `set()` to store these in the widget's value (this way, the widget can send messages and sync with other widgets). ```js -// touch property +// onTouch // store normalized coordinates if (event.type == "start") { @@ -91,7 +91,7 @@ set("this", [locals.x, locals.y]) Then, we use the `draw` property to draw a circle at the touch coordinates. ```js -// draw property +// onDraw // draw circle at touch coordinates ctx.arc(value[0] * width, value[1] * height, 6, 0, Math.PI * 2) @@ -104,7 +104,7 @@ ctx.stroke() Finally, we use the `script` property to apply limits to the values. ```js -// script property +// onValue // apply limits var x = Math.max(0, Math.min(1, value[0])), @@ -124,7 +124,7 @@ set("this", [x, y], {sync: false, send: false}) Let's build a row of 20-sliders with a single widget. We first set `valueLength` to... 20 ! ```js -// touch property +// onTouch // store normalized coordinates if (event.type == "start") { @@ -152,7 +152,7 @@ set("this", value) ``` ```js -// draw property +// onDraw ctx.fillStyle = cssVars.colorFill ctx.globalAlpha = cssVars.alphaFillOn @@ -166,7 +166,7 @@ for (var i in value){ ``` ```js -// script property +// onValue // apply limits for (var i in value) { diff --git a/resources/docs/docs/widgets/scripting.md b/resources/docs/docs/widgets/scripting.md index 241848cb..442b8ea5 100644 --- a/resources/docs/docs/widgets/scripting.md +++ b/resources/docs/docs/widgets/scripting.md @@ -1,33 +1,58 @@ # Scripting -The `script` property allows widgets to run [Javascript](https://developer.mozilla.org/en-US/docs/Web/JavaScript) upon specific events. +Widgets can run [Javascript](https://developer.mozilla.org/en-US/docs/Web/JavaScript) scripts upon specific events. These scripts are written in special properties under the scripting category. -!!! info "It's slightly different from the [javascript property syntax ](../advanced-syntaxes/#javascript-js-code)" +Scripts should not contain any [advanced syntax](../advanced-syntaxes/) code (`JS{}`, `#{}, @{}` and `OSC{}`). - - no `JS{{}}` wrapper, just the code - - `@{}`, `OSC{}`, `JS{{}}` and `#{}` blocks are replaced with their literal value before the script's compilation, therefore **it is recommended to avoid these syntaxes** and use `getProp()` and `get()` instead. +## Events -## Event types +### `onCreate` -!!! note "" - The `value` event is the only one available for all widgets except the `script` widget. +This script is called when the widget is created. If the widget has children, it will be executed after the children are created. -=== "Event: value" +### `onValue` - When set on this mode, the script is called when the widget receives a value. +This script is called when the widget receives a value. - The following variables are available in this context: +The following variables are available in this context: - - `id` (`string`): id of the widget that's reponsible for the value update - - `value`: widget's value - - `touch`: see [Touch state](#touch-state) +- `id` (`string`): id of the widget that's reponsible for the value update +- `value`: widget's value +- `touch`: see [Touch state](#touch-state) - ??? infos "Keyboard & Matrix" - In keyboards and matrices, `id` is the id of the child widget that triggered the event, and `value` is an array containing the children's values. - The touched widget's value can be retrieved with: - ```javascript - value[getIndex(id)] - ``` +??? infos "Keyboard & Matrix" + In keyboards and matrices, `id` is the id of the child widget that triggered the event, and `value` is an array containing the children's values. + The touched widget's value can be retrieved with: + ```javascript + value[getIndex(id)] + ``` + +??? infos "Touch state" + + When some widgets are touched or released, a special value event can be caught to trigger custom actions. + + If the variable `touch` is not `undefined`, this means it holds the widget's current touch state: + + - `0/1` for the widget's touch state (`fader`, `knob` and `xy`, `range` and `multixy`) + - `[i, 0/1]` if the widget is multi touch (`range` and `multixy`). In this case `i` is the touched handle's index, starting with `0`. + + ```js + if (touch !== undefined) { + // send multi touch state + if (touch.length) send('/touch_address', touch[0], touch[1]) + // send global touch state + else send('/touch_address', touch) + } else { + // do something with the value ? + } + ``` + + To avoid unwanted script executions, touch state events will only be caught if the script contains the word `touch`. + + +### `onEvent` + +This script is executed when the event specified in the `event` property occurs. === "Event: keyboard" @@ -45,31 +70,15 @@ The `script` property allows widgets to run [Javascript](https://developer.mozil - `alt` (`boolean`): `true` if alt key is pressed - `meta` (`boolean`): `true` if meta key is pressed -=== "Event: once" +=== "Event: value" - When set on this mode, the script is called only once, when it is created. + See `onValue`. -## Touch state -When some widgets are touched or released, a special event can be caught to trigger custom actions. +### `onTouch` / `onDraw` -If the variable `touch` is not `undefined`, this means it holds the widget's current touch state: +See [canvas](../canvas/). -- `0/1` for the widget's touch state (`fader`, `knob` and `xy`, `range` and `multixy`) -- `[i, 0/1]` if the widget is multi touch (`range` and `multixy`). In this case `i` is the touched handle's index, starting with `0`. - -```js -if (touch !== undefined) { - // send multi touch state - if (touch.length) send('/touch_address', touch[0], touch[1]) - // send global touch state - else send('/touch_address', touch) -} else { - // do something with the value ? -} -``` - -To avoid unwanted script executions, touch state events will only be caught if the script contains the word `touch`. ## Available variables diff --git a/src/client/managers/session/session.js b/src/client/managers/session/session.js index db0a29ff..08212767 100644 --- a/src/client/managers/session/session.js +++ b/src/client/managers/session/session.js @@ -2,7 +2,7 @@ var semver = require('semver'), UiModal = require('../../ui/ui-modal'), locales = require('../../locales') -module.exports = class Session { +class Session { constructor(data, type) { @@ -18,7 +18,7 @@ module.exports = class Session { var version = data.version || '0.0.0', warning = false - for (var converter of converters) { + for (var converter of Session.converters) { if (semver.lte(version, converter.version)) { @@ -102,7 +102,7 @@ module.exports = class Session { } -var converters = [ +Session.converters = [ { version: '0.49.12', warning: true, @@ -285,6 +285,31 @@ var converters = [ delete data.session return data + } + }, + { + version: '1.13.2', + widget: (data)=>{ + + if (data.script !== undefined) { + if (data.type === 'script') { + if (data.event === 'once') { + data.onCreate = data.script + data.event = 'value' + } + else data.onEvent = data.script + } else { + data.onValue = data.script + } + } + if (data.type === 'canvas') { + data.onDraw = data.draw + data.onTouch = data.touch + } + } } ] + + +module.exports = Session diff --git a/src/client/ui/ui-inspector-field.js b/src/client/ui/ui-inspector-field.js index c39bc96a..b6c5aea9 100644 --- a/src/client/ui/ui-inspector-field.js +++ b/src/client/ui/ui-inspector-field.js @@ -213,15 +213,17 @@ class UiInspectorField extends UiWidget { }) if (this.default.editor === 'javascript') { let globals = {} - if (this.name === 'script') { - if (this.widget.type !== 'script' || this.widget.getProp('event') === 'value') { + if (this.name === 'onEvent') { + if (this.widget.getProp('event') === 'value') { globals = {id: true, value: true, touch: true} } else if (this.widget.getProp('event') === 'keyboard') { globals = {type: true, key: true, code: true, ctrl: true, shift: true, alt: true, meta: true} } - } else if (this.name === 'draw') { + } else if (this.name === 'onValue') { + globals = {id: true, value: true, touch: true} + } else if (this.name === 'onDraw') { globals = {value: true, height: true, width: true, ctx: true, cssVars: true} - } else if (this.name === 'touch') { + } else if (this.name === 'onTouch') { globals = {value: true, height: true, width: true, event: true} } globals.locals = true diff --git a/src/client/widgets/basics/button.js b/src/client/widgets/basics/button.js index 1ea3beb7..8f543f4f 100644 --- a/src/client/widgets/basics/button.js +++ b/src/client/widgets/basics/button.js @@ -46,7 +46,7 @@ class Button extends Widget { } }) - defaults.scripting.script.help.push( + defaults.scripting.onValue.help.push( 'Additionnal variables:', '- `locals.touchCoords`: `[x, y]` array representing the touch coordinates, normalized between 0 and 1.' ) diff --git a/src/client/widgets/common/canvas.js b/src/client/widgets/common/canvas.js index cd6f0052..ab616669 100644 --- a/src/client/widgets/common/canvas.js +++ b/src/client/widgets/common/canvas.js @@ -14,7 +14,7 @@ class Canvas extends Widget { '- `--widget-width`', '- `--widget-height`', ) - defaults.scripting.script.help.push( + defaults.scripting.onValue.help.push( 'Canvas-based widget have their computed width and height available as local variables:', '- `locals.width`', '- `locals.height`', @@ -96,7 +96,7 @@ class Canvas extends Widget { } setVisibility(){ - + var visible = this.visible super.setVisibility() if (!visible && this.visible) this.batchDraw() diff --git a/src/client/widgets/common/widget.js b/src/client/widgets/common/widget.js index f633bcaf..07e64339 100644 --- a/src/client/widgets/common/widget.js +++ b/src/client/widgets/common/widget.js @@ -138,7 +138,8 @@ class Widget extends EventEmitter { bypass: {type: 'boolean', value: false, help: 'Set to `true` to prevent the widget from sending any osc message'} }, scripting: { - script: {type: 'script', value: '', editor: 'javascript', help: ['Script executed whenever the widget\'s value updates. See documentation.']}, + onCreate: {type: 'script', value: '', editor: 'javascript', help: ['Script executed when the widget is created. See documentation.']}, + onValue: {type: 'script', value: '', editor: 'javascript', help: ['Script executed when the widget\'s value updates. See documentation.']}, } } @@ -216,25 +217,25 @@ class Widget extends EventEmitter { this.decimals = Math.min(20, Math.max(this.getProp('decimals'), 0)) } + this.scripts = {} Script = Script || require('../scripts/script') - if (this.getProp('script') && !(this instanceof Script)) { - if (typeof this.props.script == 'string' && this.props.script.match(/OSC\{|@\{|VAR\{/)) { - this.errorProp('script', 'syntax', 'using advanced syntaxes in the script property is strongly discouraged and may result in unexpected behaviors.') + if (!(this instanceof Script)) { + if (this.getProp('onCreate')) { + this.scripts.onCreate = new Script({props: { + id: this.getProp('id') + '.onCreate', + onEvent: this.getProp('onCreate') + }, builtIn: true, parent: this, context: {}}) } - this.script = new Script({props:{ - id: this.getProp('id') + '/script', - script: this.getProp('script'), - event: 'value' - }, builtIn: true, parent: this}) - this.script._not_editable = true - this.on('change', (e)=>{ - if (e.widget === this && this.mounted && !e.options.fromEdit && e.options.script !== false) { - this.script.setValue(e.options.widget ? e.options.widget.value : this.value, {...e.options, id: e.options.id || e.id}) - } - }) - if (String(this.getProp('script')).includes('touch')) { - this.on('touch', (e)=>{ - this.script.setValue(this.value, {id: this.getProp('id'), touch: e.touch, sync: true, send: true}) + if (this.getProp('onValue')) { + this.scripts.onValue = new Script({props:{ + id: this.getProp('id') + '.onValue', + onEvent: this.getProp('onValue'), + event: 'value' + }, builtIn: true, parent: this}) + this.on('change', (e)=>{ + if (e.widget === this && this.mounted && !e.options.fromEdit && e.options.script !== false) { + this.scripts.onValue.setValue(e.options.widget ? e.options.widget.value : this.value, {...e.options, id: e.options.id || e.id}) + } }) } } @@ -294,6 +295,8 @@ class Widget extends EventEmitter { index: index }) + if (this.scripts.onCreate) this.scripts.onCreate.run({}) + } changed(options) { @@ -979,9 +982,9 @@ class Widget extends EventEmitter { // ignore clone wrapper if (widget.parent !== widgetManager && widget.parent.getProp('type') === 'clone') widget = widget.parent - console._error(`${id}.${name} ${type} error${line}: ${error}`) + console._error(`${id}${name ? '.' + name : ''} ${type} error${line}: ${error}`) console.debug(error) - uiConsole.log('error', `${id}.${name} ${type} error${line}: ${error}`, true) + uiConsole.log('error', `${id}${name ? '.' + name : ''} ${type} error${line}: ${error}`, true) } setContainerStyles(styles = ['geometry', 'css', 'visibility']) { @@ -1177,8 +1180,8 @@ class Widget extends EventEmitter { onRemove(){ - if (this.getProp('script') && !(this instanceof Script)) { - this.script.onRemove() + for (var k in this.scripts) { + this.scripts[k].onRemove() } this.removed = true diff --git a/src/client/widgets/containers/clone.js b/src/client/widgets/containers/clone.js index d6fe17a0..95bb19b7 100644 --- a/src/client/widgets/containers/clone.js +++ b/src/client/widgets/containers/clone.js @@ -4,7 +4,8 @@ var Container = require('../common/container'), parser = require('../../parser'), {deepCopy, deepEqual} = require('../../utils'), {diff, diffToWidget} = require('../../editor/diff'), - html = require('nanohtml') + html = require('nanohtml'), + Session = require('../../managers/session/session') var excludedCloneClasses = ['widget', 'absolute-position', 'not-editable', 'editing', 'flex-expand', 'no-interaction'] @@ -155,6 +156,8 @@ class Clone extends Container() { var data = {...deepCopy(this.cloneTarget.props), ...this.getProp('props')} + Session.converters['1.13.2'].widget(data) + parser.parse({ data: data, parentNode: this.widget, @@ -219,6 +222,8 @@ class Clone extends Container() { var data = {...deepCopy(this.cloneTarget.props), ...this.getProp('props')}, clone = this.children[0] + Session.converters['1.13.2'].widget(data) + var delta = diff.diff(clone.props, data) || {}, [widget, patch] = diffToWidget(clone, delta), changedProps = Object.keys(patch) diff --git a/src/client/widgets/containers/fragment.js b/src/client/widgets/containers/fragment.js index baf879e3..d31125a8 100644 --- a/src/client/widgets/containers/fragment.js +++ b/src/client/widgets/containers/fragment.js @@ -5,7 +5,9 @@ var Container = require('../common/container'), {deepCopy, deepEqual} = require('../../utils'), {diff, diffToWidget} = require('../../editor/diff'), html = require('nanohtml'), - sessionManager + sessionManager, + Session = require('../../managers/session/session') + var excludedfragmentClasses = ['widget', 'absolute-position', 'not-editable', 'editing', 'flex-expand', 'no-interaction'] @@ -87,6 +89,8 @@ class Fragment extends Container() { var data = {...deepCopy(fragment.getRoot()), ...this.getProp('props')} + Session.converters['1.13.2'].widget(data) + parser.parse({ data: data, parentNode: this.widget, @@ -141,6 +145,8 @@ class Fragment extends Container() { var data = {...deepCopy(fragment.getRoot()), ...this.getProp('props')}, fragmentWidget = this.children[0] + Session.converters['1.13.2'].widget(data) + var delta = diff.diff(fragmentWidget.props, data) || {}, [widget, patch] = diffToWidget(fragmentWidget, delta), changedProps = Object.keys(patch) diff --git a/src/client/widgets/containers/matrix.js b/src/client/widgets/containers/matrix.js index 055f9c09..bb18e211 100644 --- a/src/client/widgets/containers/matrix.js +++ b/src/client/widgets/containers/matrix.js @@ -1,7 +1,7 @@ var Panel = require('./panel'), parser = require('../../parser'), - {deepCopy} = require('../../utils') - + {deepCopy} = require('../../utils'), + Session = require('../../managers/session/session') class Matrix extends Panel { @@ -73,6 +73,8 @@ class Matrix extends Panel { data = JSON.parse(JSON.stringify(data).replace(/(JS|#|OSC|@|VAR)_\{/g, '$1{')) + Session.converters['1.13.2'].widget(data) + var widget = parser.parse({ data: data, parentNode: this.widget, @@ -134,6 +136,8 @@ class Matrix extends Panel { data = JSON.parse(JSON.stringify(data).replace(/(JS|#|OSC|@|VAR)_\{/g, '$1{')) + Session.converters['1.13.2'].widget(data) + if (typeof oldPropValue[i] === 'object' && oldPropValue[i] !== null) { // if an overridden default props is removed, set it back to default let overriddenDefaults, diff --git a/src/client/widgets/pads/canvas.js b/src/client/widgets/pads/canvas.js index d05f2c72..e7a9aae0 100644 --- a/src/client/widgets/pads/canvas.js +++ b/src/client/widgets/pads/canvas.js @@ -13,7 +13,7 @@ class CanvasWidget extends Canvas { static defaults() { - return super.defaults().extend({ + var defaults = super.defaults().extend({ class_specific: { valueLength: {type: 'number', value: 1, help:[ 'Defines the number of values accepted by the widget (minimum 1). Incoming messages that don\'t comply will be ignored', @@ -26,14 +26,15 @@ class CanvasWidget extends Canvas { 'If set to `true`, `draw` will be called at each frame, otherwise it will be called only when the widget is touched and when it receives a value.', 'Can be a number between 1 and 60 to specify the framerate (default: 30 fps).' ]}, - touch: {type: 'string', value: '', editor:'javascript', help: [ - 'Script executed when the widget is touched. See documentation.', - ]}, - draw: {type: 'string', value: '', editor:'javascript', help: [ - 'Script executed when the widget is redrawn. See documentation.' - ]}, } }) + defaults.scripting = { + onCreate: {type: 'script', value: '', editor: 'javascript', help: ['Script executed when the widget is created. See documentation.']}, + onTouch: {type: 'script', value: '', editor:'javascript', help: ['Script executed when the widget is touched. See documentation.',]}, + onDraw: {type: 'script', value: '', editor:'javascript', help: ['Script executed when the widget is redrawn. See documentation.']}, + onValue: {type: 'script', value: '', editor: 'javascript', help: ['Script executed whenever the widget\'s value updates. See documentation.']}, + } + return defaults } @@ -48,28 +49,29 @@ class CanvasWidget extends Canvas { this.valueLength = Math.max(1, parseInt(this.getProp('valueLength')) || 1) this.value = this.valueLength > 1 ? Array(this.valueLength).fill(0) : 0 - this.drawScript = new Script({props:{ - id: this.getProp('id') + '/drawScript', - script: this.getProp('draw'), - event: { - value: 0, - width: 100, - height: 100, - ctx: {}, - cssVars: {} - } - }, builtIn: true, parent: this}) + this.scripts.onDraw = new Script({props:{ + id: this.getProp('id') + '.onDraw', + onEvent: this.getProp('onDraw'), + }, builtIn: true, parent: this, context: { + value: 0, + width: 100, + height: 100, + ctx: {}, + cssVars: {} + }}) + this.scripts.onDraw._not_editable = true - this.touchScript = new Script({props:{ - id: this.getProp('id') + '/touchScript', - script: this.getProp('touch'), - event: { - value: 0, - width: 100, - height: 100, - event: {} - } - }, builtIn: true, parent: this}) + + this.scripts.onTouch = new Script({props:{ + id: this.getProp('id') + '.onTouch', + onEvent: this.getProp('onTouch'), + }, builtIn: true, parent: this, context: { + value: 0, + width: 100, + height: 100, + event: {} + }}) + this.scripts.onTouch._not_editable = true var touchCb = (e, type)=>{ @@ -82,7 +84,7 @@ class CanvasWidget extends Canvas { else event.target = null event.type = type - this.touchScript.run({ + this.scripts.onTouch.run({ value: this.value, width: this.width, height: this.height, @@ -133,14 +135,14 @@ class CanvasWidget extends Canvas { draw() { - if (!this.getProp('draw')) return + if (!this.getProp('onDraw')) return if (this.getProp('autoClear')) { this.clear() this.ctx.beginPath() } - this.drawScript.run({ + this.scripts.onDraw.run({ value: this.value, ctx: this.ctx, width: this.width, diff --git a/src/client/widgets/scripts/script.js b/src/client/widgets/scripts/script.js index 3eee8bc5..69496a59 100644 --- a/src/client/widgets/scripts/script.js +++ b/src/client/widgets/scripts/script.js @@ -20,8 +20,9 @@ class Script extends Widget { delete defaults.geometry delete defaults.style defaults.scripting = { - event: {type: 'string', value: 'value', choices: ['value', 'keyboard', 'once'], help: 'Define which events trigger the script\'s execution.'}, - script: {type: 'script', value: '', editor:'javascript', help: 'Script executed whenever the widget receives the defined event. See documentation.'}, + event: {type: 'string', value: 'value', choices: ['value', 'keyboard'], help: 'Define which events trigger the script\'s execution.'}, + onCreate: {type: 'script', value: '', editor: 'javascript', help: ['Script executed when the widget is created. See documentation.']}, + onEvent: {type: 'script', value: '', editor:'javascript', help: 'Script executed whenever the widget receives the defined event. See documentation.'}, _separator: 'event: keyboard', @@ -44,13 +45,18 @@ class Script extends Widget { this.noValueState = true this.builtIn = options.builtIn + if (this.builtIn) this._not_editable = true this.scriptLock = false this.timeouts = {} this.intervals = {} - if (this.getProp('event') === 'value') { + if (!this.getProp('onEvent')) { + + this.script = ()=>{} + + } else if (this.getProp('event') === 'value') { this.compile({ id: '', @@ -78,16 +84,11 @@ class Script extends Widget { meta: false, }) - } else if (this.getProp('event') === 'once') { + } else if (options.context) { - this.compile({}) - setTimeout(()=>{ - this.run({}, {send: true, sync: true}) - }) + // custom (internal/builtin) event + this.compile(options.context) - - } else if (typeof this.getProp('event') === 'object'){ - this.compile(this.getProp('event')) } } @@ -96,11 +97,11 @@ class Script extends Widget { try { - this.script = scriptVm.compile(this.getProp('script'), context) + this.script = scriptVm.compile(this.getProp('onEvent'), context) } catch(err) { - this.errorProp('script', 'javascript', err) + this.errorProp(this.builtIn ? null : 'onEvent', 'javascript', err) this.script = ()=>{} } @@ -119,7 +120,7 @@ class Script extends Widget { try { returnValue = this.script(context, this.builtIn ? this.parent.parsersLocalScope : this.parsersLocalScope) } catch(err) { - this.errorProp('script', 'javascript', err) + this.errorProp(this.builtIn ? null : 'onEvent', 'javascript', err) } this.scriptLock = false diff --git a/src/client/widgets/sliders/encoder.js b/src/client/widgets/sliders/encoder.js index 5ce90df2..49b0d894 100644 --- a/src/client/widgets/sliders/encoder.js +++ b/src/client/widgets/sliders/encoder.js @@ -44,7 +44,7 @@ module.exports = class Encoder extends StaticProperties(Knob, {angle: 360, range } }) - defaults.scripting.script.help.push( + defaults.scripting.onValue.help.push( 'Additionnal variables:', '- `locals.speed`: encoder\'s speed (reduce `sensitivity` to increase averaging)', '- `locals.angle`: encoder\'s angle in degrees'