From 9c9b132c625b9379f96850faa9837a2f69b98cbf Mon Sep 17 00:00:00 2001 From: bordun Date: Mon, 13 Jul 2020 18:26:32 +0200 Subject: [PATCH] a-syntax: compile JS{{}} before #{} --- .../docs/docs/widgets/advanced-syntaxes.md | 7 +++- src/client/widgets/common/widget.js | 38 +++++++++---------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/resources/docs/docs/widgets/advanced-syntaxes.md b/resources/docs/docs/widgets/advanced-syntaxes.md index 32ffaa8e..6352d26b 100644 --- a/resources/docs/docs/widgets/advanced-syntaxes.md +++ b/resources/docs/docs/widgets/advanced-syntaxes.md @@ -67,6 +67,8 @@ This syntax allows writing formulas in pure javascript. The code will be compile - javascript [strict mode](https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Strict_mode) is always enabled - `setTimeout` and `setInterval` functions are not available +A property cannot contain multiple `JS{{}}` blocks + #### Available variables - `console`: javascript console @@ -87,4 +89,7 @@ Parsing errors can be read in the DevTool console (F12). ## Javascript: `#{ }` -This is a shorthand for the `JS{{}}` syntax, the only difference being that `` is automatically prepended with a `return` statement. +This is a shorthand for the `JS{{}}` syntax, the only difference being that `` is automatically prepended with a `return` statement. A property can contain multiple `#{}` blocks. + +!!! note "Compilation order" + `#{}` blocks are compiled after `JS{{}}` blocks diff --git a/src/client/widgets/common/widget.js b/src/client/widgets/common/widget.js index ab7fad19..ea96a0ef 100644 --- a/src/client/widgets/common/widget.js +++ b/src/client/widgets/common/widget.js @@ -629,6 +629,24 @@ class Widget extends EventEmitter { return varname }) + try { + propValue = propValue.replace(/JS\{\{([\s\S]*)\}\}/g, (m, code)=>{ + + if (!this.parsers[code]) this.parsers[code] = vm.compile(code, defaultScope) + + let r = this.parsers[code](jsScope, this.parsersLocalScope) + + if (r === undefined) r = '' + + return typeof r !== 'string' ? JSON.stringify(r) : r + + }) + } catch (err) { + let stackline = err.stack ? (err.stack.match(/>:([0-9]+):[0-9]+/) || '') : '', + line = stackline.length > 1 ? ' at line ' + (parseInt(stackline[1]) - 2) : '' + console.log((this.getProp('id') || this.props.id) + '.' + propName + ': JS{{}} error:\n' + err + line) + } + try { propValue = propValue.replace(/#\{(?:[^{}]|\{[^{}]*\})*\}/g, (m)=>{ // one bracket nesting allowed, if we need two: #\{(?:[^{}]|\{(?:[^{}]|\{[^{}]*\})*\})*\} @@ -650,24 +668,6 @@ class Widget extends EventEmitter { console.log((this.getProp('id') || this.props.id) + '.' + propName + ': #{} error:\n' + err + line) } - try { - propValue = propValue.replace(/JS\{\{([\s\S]*)\}\}/g, (m, code)=>{ - - if (!this.parsers[code]) this.parsers[code] = vm.compile(code, defaultScope) - - let r = this.parsers[code](jsScope, this.parsersLocalScope) - - if (r === undefined) r = '' - - return typeof r !== 'string' ? JSON.stringify(r) : r - - }) - } catch (err) { - let stackline = err.stack ? (err.stack.match(/>:([0-9]+):[0-9]+/) || '') : '', - line = stackline.length > 1 ? ' at line ' + (parseInt(stackline[1]) - 2) : '' - console.log((this.getProp('id') || this.props.id) + '.' + propName + ': JS{{}} error:\n' + err + line) - } - for (let k in variables) { var v = typeof variables[k] === 'string' ? variables[k] : JSON.stringify(variables[k]) propValue = propValue.replace(new RegExp(k, 'g'), v) @@ -846,7 +846,7 @@ class Widget extends EventEmitter { if (data.address === 'auto') data.address = '/' + this.getProp('id') if (oldData.address === 'auto') oldData.address = '/' + this.getProp('id') - + widgetManager.registerWidget(this, data, oldData) return