1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Support multiple stores in context sidebar

This commit is contained in:
Nick O'Leary 2018-07-16 16:36:05 +01:00
parent d3598d5854
commit d9d15e41c7
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
3 changed files with 140 additions and 51 deletions

View File

@ -30,10 +30,10 @@ RED.sidebar.context = (function() {
var currentFlow; var currentFlow;
function init() { function init() {
content = $("<div>").css({"position":"relative","height":"100%"}); content = $("<div>").css({"position":"relative","height":"100%"});
content.className = "sidebar-context" content.className = "sidebar-context"
// var toolbar = $('<div class="sidebar-header">'+ // var toolbar = $('<div class="sidebar-header">'+
// '<span class="button-group"><a id="sidebar-context-toggle-live" class="sidebar-header-button-toggle single" href="#"><i class="fa fa-play"></i> <span></span></a></span>'+
// '</div>').appendTo(content); // '</div>').appendTo(content);
var footerToolbar = $('<div>'+ var footerToolbar = $('<div>'+
@ -167,6 +167,7 @@ RED.sidebar.context = (function() {
}) })
updateEntry(globalSection,"context/global","global"); updateEntry(globalSection,"context/global","global");
} }
function updateNode(node,force) { function updateNode(node,force) {
@ -205,25 +206,41 @@ RED.sidebar.context = (function() {
} }
function refreshEntry(section,baseUrl,id) { function refreshEntry(section,baseUrl,id) {
var contextStores = RED.settings.context.stores;
var container = section.table; var container = section.table;
$.getJSON(baseUrl, function(data) { $.getJSON(baseUrl, function(data) {
$(container).empty(); $(container).empty();
var propRow; var sortedData = {};
for (var store in data) {
var keys = Object.keys(data); if (data.hasOwnProperty(store)) {
for (var key in data[store]) {
if (data[store].hasOwnProperty(key)) {
if (!sortedData.hasOwnProperty(key)) {
sortedData[key] = [];
}
data[store][key].store = store;
sortedData[key].push(data[store][key])
}
}
}
}
var keys = Object.keys(sortedData);
keys.sort(); keys.sort();
var l = keys.length; var l = keys.length;
for (var i = 0; i < l; i++) { for (var i = 0; i < l; i++) {
sortedData[keys[i]].forEach(function(v) {
var k = keys[i]; var k = keys[i];
propRow = $('<tr class="node-info-node-row"><td class="sidebar-context-property"></td><td></td></tr>').appendTo(container); var l2 = sortedData[k].length;
var propRow = $('<tr class="node-info-node-row"><td class="sidebar-context-property"></td><td></td></tr>').appendTo(container);
var obj = $(propRow.children()[0]); var obj = $(propRow.children()[0]);
obj.text(k); obj.text(k);
var tools = $('<span class="debug-message-tools button-group"></span>').appendTo(obj); var tools = $('<span class="debug-message-tools button-group"></span>').appendTo(obj);
var refreshItem = $('<button class="editor-button editor-button-small"><i class="fa fa-refresh"></i></button>').appendTo(tools).click(function(e) { var refreshItem = $('<button class="editor-button editor-button-small"><i class="fa fa-refresh"></i></button>').appendTo(tools).click(function(e) {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
$.getJSON(baseUrl+"/"+k, function(data) { $.getJSON(baseUrl+"/"+k+"?store="+v.store, function(data) {
$(propRow.children()[1]).empty(); $(propRow.children()[1]).empty();
var payload = data.msg; var payload = data.msg;
var format = data.format; var format = data.format;
@ -236,13 +253,17 @@ RED.sidebar.context = (function() {
}); });
var payload = data[k].msg; var payload = v.msg;
var format = data[k].format; var format = v.format;
payload = RED.utils.decodeObject(payload,format); payload = RED.utils.decodeObject(payload,format);
RED.utils.createObjectElement(payload, { RED.utils.createObjectElement(payload, {
typeHint: data[k].format, typeHint: v.format,
sourceId: id+"."+k sourceId: id+"."+k
}).appendTo(propRow.children()[1]); }).appendTo(propRow.children()[1]);
if (contextStores.length > 1) {
$("<span>",{class:"sidebar-context-property-storename"}).text(v.store).appendTo($(propRow.children()[0]))
}
});
} }
if (l === 0) { if (l === 0) {
$('<tr class="node-info-node-row red-ui-search-empty blank" colspan="2"><td data-i18n="sidebar.context.empty"></td></tr>').appendTo(container).i18n(); $('<tr class="node-info-node-row red-ui-search-empty blank" colspan="2"><td data-i18n="sidebar.context.empty"></td></tr>').appendTo(container).i18n();

View File

@ -16,7 +16,7 @@
.sidebar-context-stack { .sidebar-context-stack {
position: absolute; position: absolute;
top: 0; // with toolbar: 43px; top: 0;
bottom: 0; bottom: 0;
left: 0; left: 0;
right: 0; right: 0;
@ -32,7 +32,8 @@
.sidebar-context-property { .sidebar-context-property {
position: relative; position: relative;
.debug-message-tools { .debug-message-tools {
right: 3px; right: 0px;
margin-right: 5px;
display: none; display: none;
} }
&:hover .debug-message-tools { &:hover .debug-message-tools {
@ -45,3 +46,9 @@
color: #bbb; color: #bbb;
padding: 1px 3px; padding: 1px 3px;
} }
.sidebar-context-property-storename {
display: block;
font-size: 0.8em;
font-style: italic;
color: #aaa;
}

View File

@ -17,18 +17,58 @@
var log; var log;
var redNodes; var redNodes;
var util; var util;
var settings;
function exportContextStore(scope,ctx, store, result, callback) {
ctx.keys(store,function(err, keys) {
if (err) {
return callback(err);
}
result[store] = {};
var c = keys.length;
if (c === 0) {
callback(null);
} else {
keys.forEach(function(key) {
ctx.get(key,store,function(err, v) {
if (err) {
return callback(err);
}
if (scope !== 'global' ||
store === redNodes.listContextStores().default ||
!settings.hasOwnProperty("functionGlobalContext") ||
!settings.functionGlobalContext.hasOwnProperty(key) ||
settings.functionGlobalContext[key] !== v) {
result[store][key] = util.encodeObject({msg:v});
}
c--;
if (c === 0) {
callback(null);
}
});
});
}
});
}
module.exports = { module.exports = {
init: function(runtime) { init: function(runtime) {
redNodes = runtime.nodes; redNodes = runtime.nodes;
log = runtime.log; log = runtime.log;
util = runtime.util; util = runtime.util;
settings = runtime.settings;
}, },
get: function(req,res) { get: function(req,res) {
var scope = req.params.scope; var scope = req.params.scope;
var id = req.params.id; var id = req.params.id;
var key = req.params[0]; var key = req.params[0];
var availableStores = redNodes.listContextStores();
//{ default: 'default', stores: [ 'default', 'file' ] }
var store = req.query['store'];
if (store && availableStores.stores.indexOf(store) === -1) {
return res.status(404).end();
}
var ctx; var ctx;
if (scope === 'global') { if (scope === 'global') {
ctx = redNodes.getContext('global'); ctx = redNodes.getContext('global');
@ -42,28 +82,49 @@ module.exports = {
} }
if (ctx) { if (ctx) {
if (key) { if (key) {
ctx.get(key,function(err, v) { store = store || availableStores.default;
res.json(util.encodeObject({msg:v})); ctx.get(key,store,function(err, v) {
var encoded = util.encodeObject({msg:v});
if (store !== availableStores.default) {
encoded.store = store;
}
res.json(encoded);
}); });
return; return;
} else { } else {
ctx.keys(function(err, keys) { var stores;
var result = {}; if (!store) {
var c = keys.length; stores = availableStores.stores;
if (c === 0) {
res.json(result);
} else { } else {
keys.forEach(function(key) { stores = [store];
ctx.get(key,function(err, v) { }
result[key] = util.encodeObject({msg:v});
var result = {};
var c = stores.length;
var errorReported = false;
stores.forEach(function(store) {
exportContextStore(scope,ctx,store,result,function(err) {
if (err) {
// TODO: proper error reporting
if (!errorReported) {
errorReported = true;
res.end(400);
}
return;
}
c--; c--;
if (c === 0) { if (c === 0) {
if (!errorReported) {
if (stores.length > 1 && scope === 'global') {
}
res.json(result); res.json(result);
} }
});
});
} }
}); });
})
} }
} else { } else {
res.json({}); res.json({});