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

Add ability to delete context values from sidebar

This commit is contained in:
Nick O'Leary 2018-11-12 17:04:22 +00:00
parent 1c66c88f95
commit bfd98f3767
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
9 changed files with 225 additions and 16 deletions

View File

@ -37,5 +37,20 @@ module.exports = {
}).catch(function(err) { }).catch(function(err) {
apiUtils.rejectHandler(req,res,err); apiUtils.rejectHandler(req,res,err);
}) })
},
delete: function(req,res) {
var opts = {
user: req.user,
scope: req.params.scope,
id: req.params.id,
key: req.params[0],
store: req.query['store']
}
runtimeAPI.context.delete(opts).then(function(result) {
res.status(204).end();
}).catch(function(err) {
apiUtils.rejectHandler(req,res,err);
})
} }
} }

View File

@ -62,6 +62,11 @@ module.exports = {
adminApp.get("/context/:scope(node|flow)/:id",needsPermission("context.read"),context.get,apiUtil.errorHandler); adminApp.get("/context/:scope(node|flow)/:id",needsPermission("context.read"),context.get,apiUtil.errorHandler);
adminApp.get("/context/:scope(node|flow)/:id/*",needsPermission("context.read"),context.get,apiUtil.errorHandler); adminApp.get("/context/:scope(node|flow)/:id/*",needsPermission("context.read"),context.get,apiUtil.errorHandler);
// adminApp.delete("/context/:scope(global)",needsPermission("context.write"),context.delete,apiUtil.errorHandler);
adminApp.delete("/context/:scope(global)/*",needsPermission("context.write"),context.delete,apiUtil.errorHandler);
// adminApp.delete("/context/:scope(node|flow)/:id",needsPermission("context.write"),context.delete,apiUtil.errorHandler);
adminApp.delete("/context/:scope(node|flow)/:id/*",needsPermission("context.write"),context.delete,apiUtil.errorHandler);
return adminApp; return adminApp;
} }
} }

View File

@ -515,7 +515,8 @@
"empty": "empty", "empty": "empty",
"node": "Node", "node": "Node",
"flow": "Flow", "flow": "Flow",
"global": "Global" "global": "Global",
"deleteConfirm": "Are you sure you want to delete this item?"
}, },
"palette": { "palette": {
"name": "Palette management", "name": "Palette management",

View File

@ -131,6 +131,7 @@ RED.popover = (function() {
} }
} }
var closePopup = function(instant) { var closePopup = function(instant) {
$(document).off('mousedown.modal-popover-close');
if (!active) { if (!active) {
if (div) { if (div) {
if (instant) { if (instant) {
@ -171,6 +172,17 @@ RED.popover = (function() {
openPopup(); openPopup();
} }
}); });
} else if (trigger === 'modal') {
$(document).on('mousedown.modal-popover-close', function (event) {
var target = event.target;
while (target.nodeName !== 'BODY' && target !== div[0]) {
target = target.parentElement;
}
if (target.nodeName === 'BODY') {
active = false;
closePopup();
}
});
} else if (autoClose) { } else if (autoClose) {
setTimeout(function() { setTimeout(function() {
active = false; active = false;

View File

@ -237,29 +237,83 @@ RED.sidebar.context = (function() {
var propRow = $('<tr class="node-info-node-row"><td class="sidebar-context-property"></td><td></td></tr>').appendTo(container); 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="button-group"></span>');
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+"?store="+v.store, function(data) { $.getJSON(baseUrl+"/"+k+"?store="+v.store, function(data) {
$(propRow.children()[1]).empty(); if (data.msg !== payload || data.format !== format) {
var payload = data.msg; payload = data.msg;
var format = data.format; format = data.format;
payload = RED.utils.decodeObject(payload,format); tools.detach();
RED.utils.createObjectElement(payload, { $(propRow.children()[1]).empty();
typeHint: data.format, RED.utils.createObjectElement(RED.utils.decodeObject(payload,format), {
sourceId: id+"."+k typeHint: data.format,
}).appendTo(propRow.children()[1]); sourceId: id+"."+k,
tools: tools
}).appendTo(propRow.children()[1]);
}
}) })
}); });
var deleteItem = $('<button class="editor-button editor-button-small"><i class="fa fa-trash"></i></button>').appendTo(tools).click(function(e) {
e.preventDefault();
e.stopPropagation();
var popover = RED.popover.create({
trigger: 'modal',
target: propRow,
direction: "left",
content: function() {
var content = $('<div>');
$('<p data-i18n="sidebar.context.deleteConfirm"></p>').appendTo(content);
var row = $('<p>').appendTo(content);
var bg = $('<span class="button-group"></span>').appendTo(row);
$('<button class="editor-button" data-i18n="common.label.cancel"></button>').appendTo(bg).click(function(e) {
e.preventDefault();
popover.close();
});
bg = $('<span class="button-group"></span>').appendTo(row);
$('<button class="editor-button primary" data-i18n="common.label.delete"></button>').appendTo(bg).click(function(e) {
e.preventDefault();
popover.close();
$.ajax({
url: baseUrl+"/"+k+"?store="+v.store,
type: "DELETE"
}).done(function(data,textStatus,xhr) {
$.getJSON(baseUrl+"/"+k+"?store="+v.store, function(data) {
if (data.format === 'undefined') {
propRow.remove();
if (container.children().length === 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();
}
} else {
payload = data.msg;
format = data.format;
tools.detach();
$(propRow.children()[1]).empty();
RED.utils.createObjectElement(RED.utils.decodeObject(payload,format), {
typeHint: data.format,
sourceId: id+"."+k,
tools: tools
}).appendTo(propRow.children()[1]);
}
});
}).fail(function(xhr,textStatus,err) {
})
});
return content.i18n();
}
});
popover.open();
});
var payload = v.msg; var payload = v.msg;
var format = v.format; var format = v.format;
payload = RED.utils.decodeObject(payload,format); RED.utils.createObjectElement(RED.utils.decodeObject(payload,format), {
RED.utils.createObjectElement(payload, {
typeHint: v.format, typeHint: v.format,
sourceId: id+"."+k sourceId: id+"."+k,
tools: tools
}).appendTo(propRow.children()[1]); }).appendTo(propRow.children()[1]);
if (contextStores.length > 1) { if (contextStores.length > 1) {
$("<span>",{class:"sidebar-context-property-storename"}).text(v.store).appendTo($(propRow.children()[0])) $("<span>",{class:"sidebar-context-property-storename"}).text(v.store).appendTo($(propRow.children()[0]))

View File

@ -113,7 +113,7 @@ RED.utils = (function() {
var pinnedPaths = {}; var pinnedPaths = {};
var formattedPaths = {}; var formattedPaths = {};
function addMessageControls(obj,sourceId,key,msg,rootPath,strippedKey) { function addMessageControls(obj,sourceId,key,msg,rootPath,strippedKey,extraTools) {
if (!pinnedPaths.hasOwnProperty(sourceId)) { if (!pinnedPaths.hasOwnProperty(sourceId)) {
pinnedPaths[sourceId] = {} pinnedPaths[sourceId] = {}
} }
@ -150,6 +150,10 @@ RED.utils = (function() {
}).toggleClass("selected",isPinned); }).toggleClass("selected",isPinned);
obj.toggleClass("debug-message-row-pinned",isPinned); obj.toggleClass("debug-message-row-pinned",isPinned);
} }
if (extraTools) {
extraTools.addClass("debug-message-tools-other");
extraTools.appendTo(tools);
}
} }
function checkExpanded(strippedKey,expandPaths,minRange,maxRange) { function checkExpanded(strippedKey,expandPaths,minRange,maxRange) {
if (expandPaths && expandPaths.length > 0) { if (expandPaths && expandPaths.length > 0) {
@ -243,6 +247,7 @@ RED.utils = (function() {
var expandPaths = options.expandPaths; var expandPaths = options.expandPaths;
var ontoggle = options.ontoggle; var ontoggle = options.ontoggle;
var exposeApi = options.exposeApi; var exposeApi = options.exposeApi;
var tools = options.tools;
var subElements = {}; var subElements = {};
var i; var i;
@ -262,7 +267,7 @@ RED.utils = (function() {
} }
header = $('<span class="debug-message-row"></span>').appendTo(element); header = $('<span class="debug-message-row"></span>').appendTo(element);
if (sourceId) { if (sourceId) {
addMessageControls(header,sourceId,path,obj,rootPath,strippedKey); addMessageControls(header,sourceId,path,obj,rootPath,strippedKey,tools);
} }
if (!key) { if (!key) {
element.addClass("debug-message-top-level"); element.addClass("debug-message-top-level");

View File

@ -81,6 +81,9 @@
.debug-message-tools-pin { .debug-message-tools-pin {
display: inline-block; display: inline-block;
} }
.debug-message-tools-other {
display: inline-block;
}
} }
} }
} }
@ -134,6 +137,9 @@
.debug-message-tools-copy { .debug-message-tools-copy {
display: none; display: none;
} }
.debug-message-tools-other {
display: none;
}
} }
.debug-message-payload { .debug-message-payload {
display: block; display: block;

View File

@ -146,3 +146,16 @@
border-radius:3px; border-radius:3px;
padding: 1px 2px; padding: 1px 2px;
} }
.red-ui-popover .editor-button {
&:not(.primary) {
color: #444 !important;
border-color: rgba(0,0,0,0);
}
&.primary {
border-color: #bbb;
}
&.primary:hover {
border-color: #666 !important;
}
}

View File

@ -20,7 +20,6 @@
var runtime; var runtime;
// TODO: move runtime/util to util/index
var util = require("@node-red/util").util; var util = require("@node-red/util").util;
function exportContextStore(scope,ctx, store, result, callback) { function exportContextStore(scope,ctx, store, result, callback) {
@ -152,5 +151,104 @@ var api = module.exports = {
resolve({}); resolve({});
} }
}) })
},
/**
* Gets the info of an individual node set
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @param {String} opts.scope - the scope of the context
* @param {String} opts.id - the id of the context
* @param {String} opts.store - the context store
* @param {String} opts.key - the context key
* @return {Promise} - the node information
* @memberof RED.nodes
*/
delete: function(opts) {
return new Promise(function(resolve,reject) {
var scope = opts.scope;
var id = opts.id;
var store = opts.store;
var key = opts.key;
var availableStores = runtime.nodes.listContextStores();
//{ default: 'default', stores: [ 'default', 'file' ] }
if (store && availableStores.stores.indexOf(store) === -1) {
runtime.log.audit({event: "context.get",scope:scope,id:id,store:store,key:key,error:"not_found"});
var err = new Error();
err.code = "not_found";
err.status = 404;
return reject(err);
}
var ctx;
if (scope === 'global') {
ctx = runtime.nodes.getContext('global');
} else if (scope === 'flow') {
ctx = runtime.nodes.getContext(id);
} else if (scope === 'node') {
var node = runtime.nodes.getNode(id);
if (node) {
ctx = node.context();
}
}
if (ctx) {
if (key) {
store = store || availableStores.default;
ctx.set(key,undefined,store,function(err) {
runtime.log.audit({event: "context.delete",scope:scope,id:id,store:store,key:key});
resolve();
});
return;
} else {
// TODO: support deleting whole context
runtime.log.audit({event: "context.get",scope:scope,id:id,store:store,key:key,error:"not_found"});
var err = new Error();
err.code = "not_found";
err.status = 404;
return reject(err);
// var stores;
// if (!store) {
// stores = availableStores.stores;
// } else {
// stores = [store];
// }
//
// 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;
// runtime.log.audit({event: "context.delete",scope:scope,id:id,store:store,key:key,error:"unexpected_error"});
// var err = new Error();
// err.code = "unexpected_error";
// err.status = 400;
// return reject(err);
// }
//
// return;
// }
// c--;
// if (c === 0) {
// if (!errorReported) {
// runtime.log.audit({event: "context.get",scope:scope,id:id,store:store,key:key});
// resolve(result);
// }
// }
// });
// })
}
} else {
runtime.log.audit({event: "context.delete",scope:scope,id:id,store:store,key:key});
resolve();
}
});
} }
} }