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

Add RED.utils.getNodeLabel utility function

This commit is contained in:
Nick O'Leary 2016-11-22 12:57:08 +00:00
parent 0857f979ff
commit b8888a5d46
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
6 changed files with 26 additions and 76 deletions

View File

@ -212,18 +212,7 @@ RED.deploy = (function() {
tabLabel = tab.label; tabLabel = tab.label;
} }
} }
var label = ""; var label = RED.utils.getNodeLabel(node,node.id);
if (typeof node._def.label == "function") {
try {
label = node._def.label.call(node);
} catch(err) {
console.log("Definition error: "+node_def.type+".label",err);
label = node_def.type;
}
} else {
label = node._def.label;
}
label = label || node.id;
return {tab:tabLabel,type:node.type,label:label}; return {tab:tabLabel,type:node.type,label:label};
} }
function sortNodeInfo(A,B) { function sortNodeInfo(A,B) {

View File

@ -265,17 +265,8 @@ RED.editor = (function() {
var configNode = RED.nodes.node(node[property]); var configNode = RED.nodes.node(node[property]);
var node_def = RED.nodes.getType(type); var node_def = RED.nodes.getType(type);
if (configNode && node_def.label) { if (configNode) {
if (typeof node_def.label == "function") { label = RED.utils.getNodeLabel(configNode,configNode.id);
try {
label = node_def.label.call(configNode);
} catch(err) {
console.log("Definition error: "+node_def.type+".label",err);
label = node_def.type;
}
} else {
label = node_def.label;
}
} }
input.val(label); input.val(label);
} }
@ -1207,17 +1198,7 @@ RED.editor = (function() {
RED.nodes.eachConfig(function(config) { RED.nodes.eachConfig(function(config) {
if (config.type == type && (!config.z || config.z === activeWorkspace.id)) { if (config.type == type && (!config.z || config.z === activeWorkspace.id)) {
var label = ""; var label = RED.utils.getNodeLabel(config,config.id);
if (typeof node_def.label == "function") {
try {
label = node_def.label.call(config);
} catch(err) {
console.log("Definition error: "+node_def.type+".label",err);
label = node_def.type;
}
} else {
label = node_def.label;
}
config.__label__ = label; config.__label__ = label;
configNodes.push(config); configNodes.push(config);
} }

View File

@ -27,20 +27,12 @@ RED.search = (function() {
var results = []; var results = [];
function indexNode(n) { function indexNode(n) {
var l = ""; var l = RED.utils.getNodeLabel(n);
if (n._def && n._def.label) {
l = n._def.label;
try {
l = (typeof l === "function" ? l.call(n) : l);
if (l) { if (l) {
l = (""+l).toLowerCase(); l = (""+l).toLowerCase();
index[l] = index[l] || {}; index[l] = index[l] || {};
index[l][n.id] = {node:n,label:l} index[l][n.id] = {node:n,label:l}
} }
} catch(err) {
console.log("Definition error: "+n.type+".label",err);
}
}
l = l||n.label||n.name||n.id||""; l = l||n.label||n.name||n.id||"";

View File

@ -131,19 +131,7 @@ RED.sidebar.config = (function() {
} else { } else {
var currentType = ""; var currentType = "";
nodes.forEach(function(node) { nodes.forEach(function(node) {
var label = ""; var label = RED.utils.getNodeLabel(node,node.id);
if (typeof node._def.label == "function") {
try {
label = node._def.label.call(node);
} catch(err) {
console.log("Definition error: "+node._def.type+".label",err);
label = node._def.type;
}
} else {
label = node._def.label;
}
label = label || node.id;
if (node.type != currentType) { if (node.type != currentType) {
$('<li class="config_node_type">'+node.type+'</li>').appendTo(list); $('<li class="config_node_type">'+node.type+'</li>').appendTo(list);
currentType = node.type; currentType = node.type;

View File

@ -389,9 +389,22 @@ RED.utils = (function() {
return "icons/"+def.set.module+"/"+icon_url; return "icons/"+def.set.module+"/"+icon_url;
} }
function getNodeLabel(node,defaultLabel) {
defaultLabel = defaultLabel||"";
var l = node._def.label;
try {
l = (typeof l === "function" ? l.call(node) : l)||defaultLabel;
} catch(err) {
console.log("Definition error: "+node.type+".label",err);
l = defaultLabel;
}
return RED.text.bidi.enforceTextDirectionWithUCC(l);
}
return { return {
createObjectElement: buildMessageElement, createObjectElement: buildMessageElement,
validatePropertyExpression: validatePropertyExpression, validatePropertyExpression: validatePropertyExpression,
getNodeIcon: getNodeIcon getNodeIcon: getNodeIcon,
getNodeLabel: getNodeLabel
} }
})(); })();

View File

@ -1730,14 +1730,7 @@ RED.view = (function() {
var node = d3.select(this); var node = d3.select(this);
var isLink = d.type === "link in" || d.type === "link out"; var isLink = d.type === "link in" || d.type === "link out";
node.attr("id",d.id); node.attr("id",d.id);
var l = d._def.label; var l = RED.utils.getNodeLabel(d);
try {
l = (typeof l === "function" ? l.call(d) : l)||"";
} catch(err) {
console.log("Definition error: "+d.type+".label",err);
l = d.type;
}
if (isLink) { if (isLink) {
d.w = node_height; d.w = node_height;
} else { } else {
@ -1927,13 +1920,7 @@ RED.view = (function() {
dirtyNodes[d.id] = d; dirtyNodes[d.id] = d;
//if (d.x < -50) deleteSelection(); // Delete nodes if dragged back to palette //if (d.x < -50) deleteSelection(); // Delete nodes if dragged back to palette
if (!isLink && d.resize) { if (!isLink && d.resize) {
var l = d._def.label; var l = RED.utils.getNodeLabel(d);
try {
l = (typeof l === "function" ? l.call(d) : l)||"";
} catch(err) {
console.log("Definition error: "+d.type+".label",err);
l = d.type;
}
var ow = d.w; var ow = d.w;
d.w = Math.max(node_width,gridSize*(Math.ceil((calculateTextWidth(l, "node_label", 50)+(d._def.inputs>0?7:0))/gridSize)) ); d.w = Math.max(node_width,gridSize*(Math.ceil((calculateTextWidth(l, "node_label", 50)+(d._def.inputs>0?7:0))/gridSize)) );
d.h = Math.max(node_height,(d.outputs||0) * 15); d.h = Math.max(node_height,(d.outputs||0) * 15);