Merge 843afd075f31a2627f298492baee6485afe6526f into 010c8eccc85a39b0c59a32ba6b0dfa7a4d91ea5b

This commit is contained in:
Yuewen Liu 2023-03-02 22:13:33 +01:00 committed by GitHub
commit 80a9ad501a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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;