diff --git a/packages/node_modules/@node-red/editor-client/src/js/ui/editors/expression.js b/packages/node_modules/@node-red/editor-client/src/js/ui/editors/expression.js index d470e14f2..d65a72bb8 100644 --- a/packages/node_modules/@node-red/editor-client/src/js/ui/editors/expression.js +++ b/packages/node_modules/@node-red/editor-client/src/js/ui/editors/expression.js @@ -249,12 +249,50 @@ e.preventDefault(); RED.sidebar.help.set(RED._("expressionEditor.compatModeDesc")); }) + + // convert context data base on format + function convertContextData(data) { + let context = data; + switch (context.format) { + case "Object": + context = JSON.parse(context.msg); + break; + case "number": + context = Number(context.msg); + break; + case "boolean": + context = context.msg === 'true'; + break; + case "undefined": + context = undefined; + break; + default: + // format string[*] and unknown format will go here + context = context.msg; + break; + } + return context; + } + + let flowContext={}; + var fetchFlowContext = function(key){ + $.getJSON("context/flow/" + RED.workspaces.active()+"/"+key,function (data) { + flowContext[key] = convertContextData(data); + testExpression(); + }) + }; + let globalContext={}; + var fetchGlobalContext = function(key){ + $.getJSON("context/global/"+key,function (data) { + globalContext[key] = convertContextData(data); + testExpression(); + }) + }; var testExpression = function() { var value = testDataEditor.getValue(); var parsedData; var currentExpression = expressionEditor.getValue(); var expr; - var usesContext = false; var usesEnv = false; var usesMoment = false; var usesClone = false; @@ -262,13 +300,30 @@ $(".red-ui-editor-type-expression-legacy").toggle(legacyMode); try { expr = jsonata(currentExpression); - expr.assign('flowContext',function(val) { - usesContext = true; - return null; - }); - expr.assign('globalContext',function(val) { - usesContext = true; - return null; + var getValueFromContext = function(key,context) { + try { + return key.split('.').reduce((previous,current) => previous[current],context); + } catch (e) { + return undefined; + } + } + expr.assign('flowContext',function(val){ + var key = val.split('.')[0]; + if (flowContext.hasOwnProperty(key)) { + return getValueFromContext(val,flowContext); + }else if (key){ + fetchFlowContext(key); + } + return undefined; + }) + expr.assign('globalContext',function(val){ + var key = val.split('.')[0]; + if (globalContext.hasOwnProperty(key)) { + return getValueFromContext(val,globalContext); + }else if (key){ + fetchGlobalContext(key); + } + return undefined; }); expr.assign("env", function(name) { usesEnv = true; @@ -295,10 +350,6 @@ try { var result = expr.evaluate(legacyMode?{msg:parsedData}:parsedData); - if (usesContext) { - testResultEditor.setValue(RED._("expressionEditor.errors.context-unsupported"),-1); - return; - } if (usesEnv) { testResultEditor.setValue(RED._("expressionEditor.errors.env-unsupported"),-1); return;