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

Add rest endpoint for add/remove and send updates to editor

This commit is contained in:
Nick O'Leary 2014-08-07 13:46:03 +01:00
parent 15494dda84
commit fde77cec5d
3 changed files with 77 additions and 17 deletions

View File

@ -176,6 +176,27 @@ var RED = function() {
} }
} }
}); });
RED.comms.subscribe("node/#",function(topic,msg) {
if (topic == "node/added") {
for (var i=0;i<msg.length;i++) {
var m = msg[i];
var id = m.id;
$.get('nodes/'+id, function(data) {
$("body").append(data);
var typeList = "<ul><li>"+m.types.join("</li><li>")+"</li></ul>";
RED.notify("Node"+(m.types.length!=1 ? "s":"")+" added to palette:"+typeList,"success");
});
}
} else if (topic == "node/removed") {
if (msg.types) {
for (var i=0;i<msg.types.length;i++) {
RED.palette.remove(msg.types[i]);
}
var typeList = "<ul><li>"+msg.types.join("</li><li>")+"</li></ul>";
RED.notify("Node"+(msg.types.length!=1 ? "s":"")+" removed from palette:"+typeList,"success");
}
}
});
}); });
} }

View File

@ -21,27 +21,31 @@ RED.palette = function() {
function createCategoryContainer(category){ function createCategoryContainer(category){
$("#palette-container").append('\ $("#palette-container").append('<div class="palette-category">'+
<div class="palette-category">\ '<div id="header-'+category+'" class="palette-header"><i class="expanded icon-chevron-down"></i><span>'+category+'</span></div>'+
<div id="header-'+category+'" class="palette-header"><i class="expanded icon-chevron-down"></i><span>'+category+'</span></div>\ '<div class="palette-content" id="palette-base-category-'+category+'">'+
<div class="palette-content" id="palette-base-category-'+category+'">\ '<div id="palette-'+category+'-input"></div>'+
<div id="palette-'+category+'-input"></div>\ '<div id="palette-'+category+'-output"></div>'+
<div id="palette-'+category+'-output"></div>\ '<div id="palette-'+category+'-function"></div>'+
<div id="palette-'+category+'-function"></div>\ '</div>'+
</div>\ '</div>');
</div>');
} }
core.forEach(createCategoryContainer); core.forEach(createCategoryContainer);
function addNodeType(nt,def) { function addNodeType(nt,def) {
if ($("#palette_node_"+nt).length) {
return;
}
if (exclusion.indexOf(def.category)===-1) { if (exclusion.indexOf(def.category)===-1) {
var category = def.category.split("-"); var category = def.category.split("-");
var d = document.createElement("div"); var d = document.createElement("div");
d.id = "pn_"+nt; d.id = "palette_node_"+nt;
d.type = nt; d.type = nt;
var label = /^(.*?)([ -]in|[ -]out)?$/.exec(nt)[1]; var label = /^(.*?)([ -]in|[ -]out)?$/.exec(nt)[1];
@ -71,15 +75,14 @@ RED.palette = function() {
d.appendChild(port); d.appendChild(port);
} }
if($("#palette-base-category-"+category[0]).length == 0){ if ($("#palette-base-category-"+category[0]).length == 0){
createCategoryContainer(category[0]); createCategoryContainer(category[0]);
} }
if($("#palette-"+def.category).length == 0){ if ($("#palette-"+def.category).length == 0) {
$("#palette-base-category-"+category[0]).append('\ $("#palette-base-category-"+category[0]).append('<div id="palette-'+def.category+'"></div>');
<div id="palette-'+def.category+'"></div>');
} }
$("#palette-"+def.category).append(d); $("#palette-"+def.category).append(d);
d.onmousedown = function(e) { e.preventDefault(); } d.onmousedown = function(e) { e.preventDefault(); }
@ -111,6 +114,10 @@ RED.palette = function() {
} }
} }
function removeNodeType(type) {
$("#palette_node_"+type).remove();
}
function filterChange() { function filterChange() {
var val = $("#palette-search-input").val(); var val = $("#palette-search-input").val();
if (val == "") { if (val == "") {
@ -155,6 +162,7 @@ RED.palette = function() {
}); });
return { return {
add:addNodeType add:addNodeType,
remove:removeNodeType
}; };
}(); }();

View File

@ -59,6 +59,37 @@ function createServer(_server,_settings) {
res.send(400,"Invalid Flow"); res.send(400,"Invalid Flow");
} }
); );
app.post("/nodes",
express.json(),
function(req,res) {
var node = req.body;
if (!node.file && !node.module) {
res.send(400,"Invalid request");
return;
}
redNodes.addNode(node).then(function(info) {
comms.publish("node/added",info,false);
res.json(info);
}).otherwise(function(err) {
res.send(400,err.toString());
});
},
function(err,req,res,next) {
res.send(400,err);
}
);
app.get("/nodes/:id", function(req,res) {
var type = req.params.id;
var config = redNodes.getNodeConfig(id);
if (config) {
res.send(config);
} else {
res.send(404);
}
});
} }
function start() { function start() {