From b15416afbf5f88d743eb722265aa3aaf8ad946ab Mon Sep 17 00:00:00 2001 From: bordun Date: Wed, 30 Sep 2020 07:46:56 +0200 Subject: [PATCH] script dry --- src/client/widgets/scripts/script.js | 57 +++++++++++++++------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/src/client/widgets/scripts/script.js b/src/client/widgets/scripts/script.js index 41f9ce5b..23e21c33 100644 --- a/src/client/widgets/scripts/script.js +++ b/src/client/widgets/scripts/script.js @@ -54,17 +54,10 @@ class Script extends Widget { if (this.getProp('event') === 'value') { - try { - this.script = scriptVm.compile(this.getProp('script'), { - id: '', - value: 0 - }) - } 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) + '.script error:\n' + err + line) - this.script = ()=>{} - } + this.compile({ + id: '', + value: 0 + }) } else if (this.getProp('event') === 'keyboard' && this.getProp('keyBinding')) { @@ -76,22 +69,32 @@ class Script extends Widget { }) - try { - this.script = scriptVm.compile(this.getProp('script'), { - type: '', - key: '', - code: 0, - ctrl: false, - shift: false, - alt: false, - meta: false, - }) - } 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) + '.script error:\n' + err + line) - this.script = ()=>{} - } + this.compile({ + type: '', + key: '', + code: 0, + ctrl: false, + shift: false, + alt: false, + meta: false, + ) + + } + + } + + compîle(context) { + + try { + + this.script = scriptVm.compile(this.getProp('script'), context) + + } 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) + '.script error:\n' + err + line) + this.script = ()=>{} }