From b8888a5d46a87e9a260b4daeb1dc34a4b2c0c796 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Tue, 22 Nov 2016 12:57:08 +0000 Subject: [PATCH] Add RED.utils.getNodeLabel utility function --- editor/js/ui/deploy.js | 13 +------------ editor/js/ui/editor.js | 25 +++---------------------- editor/js/ui/search.js | 18 +++++------------- editor/js/ui/tab-config.js | 14 +------------- editor/js/ui/utils.js | 15 ++++++++++++++- editor/js/ui/view.js | 17 ++--------------- 6 files changed, 26 insertions(+), 76 deletions(-) diff --git a/editor/js/ui/deploy.js b/editor/js/ui/deploy.js index 6cba93fae..d2c413521 100644 --- a/editor/js/ui/deploy.js +++ b/editor/js/ui/deploy.js @@ -212,18 +212,7 @@ RED.deploy = (function() { tabLabel = tab.label; } } - var label = ""; - 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; + var label = RED.utils.getNodeLabel(node,node.id); return {tab:tabLabel,type:node.type,label:label}; } function sortNodeInfo(A,B) { diff --git a/editor/js/ui/editor.js b/editor/js/ui/editor.js index de1e691dd..1310f6cbb 100644 --- a/editor/js/ui/editor.js +++ b/editor/js/ui/editor.js @@ -265,17 +265,8 @@ RED.editor = (function() { var configNode = RED.nodes.node(node[property]); var node_def = RED.nodes.getType(type); - if (configNode && node_def.label) { - if (typeof node_def.label == "function") { - 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; - } + if (configNode) { + label = RED.utils.getNodeLabel(configNode,configNode.id); } input.val(label); } @@ -1207,17 +1198,7 @@ RED.editor = (function() { RED.nodes.eachConfig(function(config) { if (config.type == type && (!config.z || config.z === activeWorkspace.id)) { - var label = ""; - 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; - } + var label = RED.utils.getNodeLabel(config,config.id); config.__label__ = label; configNodes.push(config); } diff --git a/editor/js/ui/search.js b/editor/js/ui/search.js index 18565fd13..693154436 100644 --- a/editor/js/ui/search.js +++ b/editor/js/ui/search.js @@ -27,19 +27,11 @@ RED.search = (function() { var results = []; function indexNode(n) { - var l = ""; - if (n._def && n._def.label) { - l = n._def.label; - try { - l = (typeof l === "function" ? l.call(n) : l); - if (l) { - l = (""+l).toLowerCase(); - index[l] = index[l] || {}; - index[l][n.id] = {node:n,label:l} - } - } catch(err) { - console.log("Definition error: "+n.type+".label",err); - } + var l = RED.utils.getNodeLabel(n); + if (l) { + l = (""+l).toLowerCase(); + index[l] = index[l] || {}; + index[l][n.id] = {node:n,label:l} } l = l||n.label||n.name||n.id||""; diff --git a/editor/js/ui/tab-config.js b/editor/js/ui/tab-config.js index 5041d4433..9a4619b28 100644 --- a/editor/js/ui/tab-config.js +++ b/editor/js/ui/tab-config.js @@ -131,19 +131,7 @@ RED.sidebar.config = (function() { } else { var currentType = ""; nodes.forEach(function(node) { - var label = ""; - 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; + var label = RED.utils.getNodeLabel(node,node.id); if (node.type != currentType) { $('
  • '+node.type+'
  • ').appendTo(list); currentType = node.type; diff --git a/editor/js/ui/utils.js b/editor/js/ui/utils.js index 0231fe38b..371698ac0 100644 --- a/editor/js/ui/utils.js +++ b/editor/js/ui/utils.js @@ -389,9 +389,22 @@ RED.utils = (function() { 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 { createObjectElement: buildMessageElement, validatePropertyExpression: validatePropertyExpression, - getNodeIcon: getNodeIcon + getNodeIcon: getNodeIcon, + getNodeLabel: getNodeLabel } })(); diff --git a/editor/js/ui/view.js b/editor/js/ui/view.js index 3c083f9df..4436bbdf2 100644 --- a/editor/js/ui/view.js +++ b/editor/js/ui/view.js @@ -1730,14 +1730,7 @@ RED.view = (function() { var node = d3.select(this); var isLink = d.type === "link in" || d.type === "link out"; node.attr("id",d.id); - var l = d._def.label; - try { - l = (typeof l === "function" ? l.call(d) : l)||""; - } catch(err) { - console.log("Definition error: "+d.type+".label",err); - l = d.type; - } - + var l = RED.utils.getNodeLabel(d); if (isLink) { d.w = node_height; } else { @@ -1927,13 +1920,7 @@ RED.view = (function() { dirtyNodes[d.id] = d; //if (d.x < -50) deleteSelection(); // Delete nodes if dragged back to palette if (!isLink && d.resize) { - var l = d._def.label; - try { - l = (typeof l === "function" ? l.call(d) : l)||""; - } catch(err) { - console.log("Definition error: "+d.type+".label",err); - l = d.type; - } + var l = RED.utils.getNodeLabel(d); 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.h = Math.max(node_height,(d.outputs||0) * 15);