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

Merge pull request #434 from anna2130/palette-multi-line-nodes

Node expands for multi-line node names
This commit is contained in:
Nick O'Leary 2014-10-08 13:23:54 +01:00
commit 483c4352d3

View File

@ -13,12 +13,12 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
**/ **/
RED.palette = (function() { RED.palette = (function() {
var exclusion = ['config','unknown','deprecated']; var exclusion = ['config','unknown','deprecated'];
var core = ['input', 'output', 'function', 'social', 'storage', 'analysis', 'advanced']; var core = ['input', 'output', 'function', 'social', 'storage', 'analysis', 'advanced'];
function createCategoryContainer(category){ function createCategoryContainer(category){
var escapedCategory = category.replace(" ","_"); var escapedCategory = category.replace(" ","_");
$("#palette-container").append('<div class="palette-category">'+ $("#palette-container").append('<div class="palette-category">'+
@ -29,71 +29,103 @@ RED.palette = (function() {
'<div id="palette-'+category+'-function"></div>'+ '<div id="palette-'+category+'-function"></div>'+
'</div>'+ '</div>'+
'</div>'); '</div>');
$("#header-"+category).on('click', function(e) { $("#header-"+category).on('click', function(e) {
$(this).next().slideToggle(); $(this).next().slideToggle();
$(this).children("i").toggleClass("expanded"); $(this).children("i").toggleClass("expanded");
}); });
} }
core.forEach(createCategoryContainer); core.forEach(createCategoryContainer);
function addNodeType(nt,def) { function addNodeType(nt,def) {
var nodeTypeId = nt.replace(" ","_"); var nodeTypeId = nt.replace(" ","_");
if ($("#palette_node_"+nodeTypeId).length) { if ($("#palette_node_"+nodeTypeId).length) {
return; return;
} }
if (exclusion.indexOf(def.category)===-1) { if (exclusion.indexOf(def.category)===-1) {
var category = def.category.replace(" ","_"); var category = def.category.replace(" ","_");
var rootCategory = category.split("-")[0]; var rootCategory = category.split("-")[0];
var d = document.createElement("div"); var d = document.createElement("div");
d.id = "palette_node_"+nodeTypeId; d.id = "palette_node_"+nodeTypeId;
d.type = nt; d.type = nt;
// calculate width of label text
$.fn.textWidth = function(text, font) {
if (!$.fn.textWidth.fakeEl) {
$.fn.textWidth.fakeEl = $('<span>').hide().appendTo(document.body);
}
$.fn.textWidth.fakeEl.text(text || this.val() || this.text()).css('font', font || this.css('font'));
return $.fn.textWidth.fakeEl.width();
};
var label = /^(.*?)([ -]in|[ -]out)?$/.exec(nt)[1]; var label = /^(.*?)([ -]in|[ -]out)?$/.exec(nt)[1];
var pixels = $.fn.textWidth(label, '13px helvetica');
d.innerHTML = '<div class="palette_label">'+label+"</div>"; var nodeWidth = 90;
var labelWidth = nodeWidth - 10;
var numLines = Math.ceil(pixels / nodeWidth);
var multiLine = numLines > 1;
// styles matching with style.css
var nodeHeight = 25;
var lineHeight = 16;
var portHeight = 10;
var multiLineNodeHeight = lineHeight * numLines + (nodeHeight - lineHeight);
d.innerHTML = '<div class="palette_label"'+
(multiLine ? 'style="line-height: '+
lineHeight + 'px; margin-top: 5px"' : '')+
'>'+label+"</div>";
d.className="palette_node"; d.className="palette_node";
if (def.icon) { if (def.icon) {
d.style.backgroundImage = "url(icons/"+def.icon+")"; d.style.backgroundImage = "url(icons/"+def.icon+")";
if (multiLine) {
d.style.backgroundSize = "18px 27px";
}
if (def.align == "right") { if (def.align == "right") {
d.style.backgroundPosition = "95% 50%"; d.style.backgroundPosition = "95% 50%";
} else if (def.inputs > 0) { } else if (def.inputs > 0) {
d.style.backgroundPosition = "10% 50%"; d.style.backgroundPosition = "10% 50%";
} }
} }
d.style.backgroundColor = def.color; d.style.backgroundColor = def.color;
d.style.height = multiLineNodeHeight + "px";
if (def.outputs > 0) { if (def.outputs > 0) {
var portOut = document.createElement("div"); var portOut = document.createElement("div");
portOut.className = "palette_port palette_port_output"; portOut.className = "palette_port palette_port_output";
if (multiLine) {
portOut.style.top = ((multiLineNodeHeight - portHeight) / 2) + "px";
}
d.appendChild(portOut); d.appendChild(portOut);
} }
if (def.inputs > 0) { if (def.inputs > 0) {
var portIn = document.createElement("div"); var portIn = document.createElement("div");
portIn.className = "palette_port"; portIn.className = "palette_port";
if (multiLine) {
portIn.style.top = ((multiLineNodeHeight - portHeight) / 2) + "px";
}
d.appendChild(portIn); d.appendChild(portIn);
} }
if ($("#palette-base-category-"+rootCategory).length === 0) { if ($("#palette-base-category-"+rootCategory).length === 0) {
createCategoryContainer(rootCategory); createCategoryContainer(rootCategory);
} }
if ($("#palette-"+category).length === 0) { if ($("#palette-"+category).length === 0) {
$("#palette-base-category-"+rootCategory).append('<div id="palette-'+category+'"></div>'); $("#palette-base-category-"+rootCategory).append('<div id="palette-'+category+'"></div>');
} }
$("#palette-"+category).append(d); $("#palette-"+category).append(d);
d.onmousedown = function(e) { e.preventDefault(); } d.onmousedown = function(e) { e.preventDefault(); };
$(d).popover({ $(d).popover({
title:d.type, title:d.type,
placement:"right", placement:"right",
@ -101,7 +133,7 @@ RED.palette = (function() {
delay: { show: 750, hide: 50 }, delay: { show: 750, hide: 50 },
html: true, html: true,
container:'body', container:'body',
content: $(($("script[data-help-name|='"+nt+"']").html()||"<p>no information available</p>").trim())[0] content: $(($("script[data-help-name|='"+nt+"']").html()||"<p>no information available</p>").trim())[0]
}); });
$(d).click(function() { $(d).click(function() {
var help = '<div class="node-help">'+($("script[data-help-name|='"+d.type+"']").html()||"")+"</div>"; var help = '<div class="node-help">'+($("script[data-help-name|='"+d.type+"']").html()||"")+"</div>";
@ -115,7 +147,7 @@ RED.palette = (function() {
}); });
} }
} }
function removeNodeType(nt) { function removeNodeType(nt) {
var nodeTypeId = nt.replace(" ","_"); var nodeTypeId = nt.replace(" ","_");
$("#palette_node_"+nodeTypeId).remove(); $("#palette_node_"+nodeTypeId).remove();
@ -124,12 +156,12 @@ RED.palette = (function() {
var nodeTypeId = nt.replace(" ","_"); var nodeTypeId = nt.replace(" ","_");
$("#palette_node_"+nodeTypeId).hide(); $("#palette_node_"+nodeTypeId).hide();
} }
function showNodeType(nt) { function showNodeType(nt) {
var nodeTypeId = nt.replace(" ","_"); var nodeTypeId = nt.replace(" ","_");
$("#palette_node_"+nodeTypeId).show(); $("#palette_node_"+nodeTypeId).show();
} }
function filterChange() { function filterChange() {
var val = $("#palette-search-input").val(); var val = $("#palette-search-input").val();
if (val === "") { if (val === "") {
@ -137,7 +169,7 @@ RED.palette = (function() {
} else { } else {
$("#palette-search-clear").show(); $("#palette-search-clear").show();
} }
var re = new RegExp(val); var re = new RegExp(val);
$(".palette_node").each(function(i,el) { $(".palette_node").each(function(i,el) {
if (val === "" || re.test(el.id)) { if (val === "" || re.test(el.id)) {
@ -147,21 +179,21 @@ RED.palette = (function() {
} }
}); });
} }
$("#palette-search-input").focus(function(e) { $("#palette-search-input").focus(function(e) {
RED.keyboard.disable(); RED.keyboard.disable();
}); });
$("#palette-search-input").blur(function(e) { $("#palette-search-input").blur(function(e) {
RED.keyboard.enable(); RED.keyboard.enable();
}); });
$("#palette-search-clear").on("click",function(e) { $("#palette-search-clear").on("click",function(e) {
e.preventDefault(); e.preventDefault();
$("#palette-search-input").val(""); $("#palette-search-input").val("");
filterChange(); filterChange();
$("#palette-search-input").focus(); $("#palette-search-input").focus();
}); });
$("#palette-search-input").val(""); $("#palette-search-input").val("");
$("#palette-search-input").on("keyup",function() { $("#palette-search-input").on("keyup",function() {
filterChange(); filterChange();
@ -172,7 +204,7 @@ RED.palette = (function() {
$("#palette-search-input").blur(); $("#palette-search-input").blur();
}); });
}); });
return { return {
add:addNodeType, add:addNodeType,
remove:removeNodeType, remove:removeNodeType,