Allow node icon definition to be a function

This commit is contained in:
Nick O'Leary 2015-06-02 22:07:45 +01:00
parent f8853af902
commit 871f764e98
2 changed files with 25 additions and 1 deletions

View File

@ -127,8 +127,9 @@ RED.palette = (function() {
if (def.icon) {
var icon_url = (typeof def.icon === "function" ? def.icon.call({}) : def.icon);
var iconContainer = $('<div/>',{class:"palette_icon_container"+(def.align=="right"?" palette_icon_container_right":"")}).appendTo(d);
$('<div/>',{class:"palette_icon",style:"background-image: url(icons/"+def.icon+")"}).appendTo(iconContainer);
$('<div/>',{class:"palette_icon",style:"background-image: url(icons/"+icon_url+")"}).appendTo(iconContainer);
}
d.style.backgroundColor = def.color;

View File

@ -1459,6 +1459,29 @@ RED.view = (function() {
(d._def.align?' node_label_'+d._def.align:'')+
(d._def.labelStyle?' '+(typeof d._def.labelStyle == "function" ? d._def.labelStyle.call(d):d._def.labelStyle):'') ;
});
if (d._def.icon) {
icon = thisNode.select(".node_icon");
var current_url = icon.attr("xlink:href");
var icon_url;
if (typeof d._def.icon == "function") {
icon_url = d._def.icon.call(d);
} else {
icon_url = d._def.icon;
}
if ("icons/"+icon_url != current_url) {
icon.attr("xlink:href","icons/"+icon_url);
var img = new Image();
img.src = "icons/"+d._def.icon;
img.onload = function() {
icon.attr("width",Math.min(img.width,30));
icon.attr("height",Math.min(img.height,30));
icon.attr("x",15-Math.min(img.width,30)/2);
}
}
}
thisNode.selectAll(".node_tools").attr("x",function(d){return d.w-35;}).attr("y",function(d){return d.h-20;});
thisNode.selectAll(".node_changed")