2013-09-05 16:02:48 +02:00
|
|
|
/**
|
2015-03-12 01:08:47 +01:00
|
|
|
* Copyright 2013, 2015 IBM Corp.
|
2013-09-05 16:02:48 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2014-07-23 23:07:02 +02:00
|
|
|
**/
|
2014-10-01 14:41:06 +02:00
|
|
|
|
2014-08-08 01:01:35 +02:00
|
|
|
RED.palette = (function() {
|
2014-06-30 17:31:02 +02:00
|
|
|
|
2014-07-23 23:07:02 +02:00
|
|
|
var exclusion = ['config','unknown','deprecated'];
|
2015-02-25 11:09:39 +01:00
|
|
|
var core = ['subflows', 'input', 'output', 'function', 'social', 'storage', 'analysis', 'advanced'];
|
2014-10-01 14:41:06 +02:00
|
|
|
|
2014-07-23 23:07:02 +02:00
|
|
|
function createCategoryContainer(category){
|
2014-08-29 21:37:30 +02:00
|
|
|
var escapedCategory = category.replace(" ","_");
|
2015-02-06 14:16:19 +01:00
|
|
|
var catDiv = $("#palette-container").append('<div id="palette-container-'+category+'" class="palette-category hide">'+
|
|
|
|
'<div id="palette-header-'+category+'" class="palette-header"><i class="expanded fa fa-caret-down"></i><span>'+category.replace("_"," ")+'</span></div>'+
|
2014-08-07 14:46:03 +02:00
|
|
|
'<div class="palette-content" id="palette-base-category-'+category+'">'+
|
2014-08-19 23:58:52 +02:00
|
|
|
'<div id="palette-'+category+'-input"></div>'+
|
|
|
|
'<div id="palette-'+category+'-output"></div>'+
|
|
|
|
'<div id="palette-'+category+'-function"></div>'+
|
2014-08-07 14:46:03 +02:00
|
|
|
'</div>'+
|
|
|
|
'</div>');
|
2014-10-01 14:41:06 +02:00
|
|
|
|
2015-02-06 14:16:19 +01:00
|
|
|
$("#palette-header-"+category).on('click', function(e) {
|
2014-08-28 01:35:07 +02:00
|
|
|
$(this).next().slideToggle();
|
|
|
|
$(this).children("i").toggleClass("expanded");
|
|
|
|
});
|
2014-07-23 23:07:02 +02:00
|
|
|
}
|
2014-10-01 14:41:06 +02:00
|
|
|
|
2014-02-25 00:35:11 +01:00
|
|
|
function setLabel(type, el,label) {
|
|
|
|
var nodeWidth = 80;
|
|
|
|
var nodeHeight = 25;
|
|
|
|
var lineHeight = 20;
|
|
|
|
var portHeight = 10;
|
|
|
|
|
|
|
|
var words = label.split(" ");
|
2014-12-09 15:37:32 +01:00
|
|
|
|
2014-02-25 00:35:11 +01:00
|
|
|
var displayLines = [];
|
2014-12-09 15:37:32 +01:00
|
|
|
|
2014-02-25 00:35:11 +01:00
|
|
|
var currentLine = words[0];
|
|
|
|
var currentLineWidth = RED.view.calculateTextWidth(currentLine, "palette_label", 0);
|
2014-12-09 15:37:32 +01:00
|
|
|
|
2014-02-25 00:35:11 +01:00
|
|
|
for (var i=1;i<words.length;i++) {
|
|
|
|
var newWidth = RED.view.calculateTextWidth(currentLine+" "+words[i], "palette_label", 0);
|
|
|
|
if (newWidth < nodeWidth) {
|
|
|
|
currentLine += " "+words[i];
|
|
|
|
currentLineWidth = newWidth;
|
|
|
|
} else {
|
|
|
|
displayLines.push(currentLine);
|
|
|
|
currentLine = words[i];
|
|
|
|
currentLineWidth = RED.view.calculateTextWidth(currentLine, "palette_label", 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
displayLines.push(currentLine);
|
2014-12-09 15:37:32 +01:00
|
|
|
|
2014-02-25 00:35:11 +01:00
|
|
|
var lines = displayLines.join("<br/>");
|
|
|
|
var multiLineNodeHeight = 8+(lineHeight*displayLines.length);
|
|
|
|
el.css({height:multiLineNodeHeight+"px"});
|
|
|
|
|
|
|
|
var labelElement = el.find(".palette_label");
|
|
|
|
labelElement.html(lines);
|
2014-12-09 15:37:32 +01:00
|
|
|
|
2014-02-25 00:35:11 +01:00
|
|
|
el.find(".palette_port").css({top:(multiLineNodeHeight/2-5)+"px"});
|
2014-12-09 15:37:32 +01:00
|
|
|
|
2014-02-25 00:35:11 +01:00
|
|
|
var popOverContent;
|
|
|
|
try {
|
|
|
|
var l = "<p><b>"+label+"</b></p>";
|
|
|
|
if (label != type) {
|
|
|
|
l = "<p><b>"+label+"</b><br/><i>"+type+"</i></p>";
|
|
|
|
}
|
2015-01-29 22:40:21 +01:00
|
|
|
|
|
|
|
popOverContent = $(l+($("script[data-help-name|='"+type+"']").html()||"<p>no information available</p>").trim())
|
|
|
|
.filter(function(n) {
|
|
|
|
return this.nodeType == 1 || (this.nodeType == 3 && this.textContent.trim().length > 0)
|
|
|
|
}).slice(0,2);
|
2014-02-25 00:35:11 +01:00
|
|
|
} catch(err) {
|
|
|
|
// Malformed HTML may cause errors. TODO: need to understand what can break
|
|
|
|
console.log("Error generating pop-over label for '"+type+"'.");
|
|
|
|
console.log(err.toString());
|
|
|
|
popOverContent = "<p><b>"+label+"</b></p><p>no information available</p>";
|
|
|
|
}
|
2014-10-01 14:41:06 +02:00
|
|
|
|
2014-12-09 15:37:32 +01:00
|
|
|
|
2014-02-25 00:35:11 +01:00
|
|
|
el.data('popover').options.content = popOverContent;
|
|
|
|
}
|
2014-12-09 15:37:32 +01:00
|
|
|
|
2014-02-25 00:35:11 +01:00
|
|
|
function escapeNodeType(nt) {
|
|
|
|
return nt.replace(" ","_").replace(".","_").replace(":","_");
|
|
|
|
}
|
2014-12-09 15:37:32 +01:00
|
|
|
|
2014-02-25 00:35:11 +01:00
|
|
|
function addNodeType(nt,def) {
|
2014-12-09 15:37:32 +01:00
|
|
|
|
2014-02-25 00:35:11 +01:00
|
|
|
var nodeTypeId = escapeNodeType(nt);
|
2014-08-28 01:35:07 +02:00
|
|
|
if ($("#palette_node_"+nodeTypeId).length) {
|
2014-08-07 14:46:03 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-10-01 14:41:06 +02:00
|
|
|
|
2014-07-23 23:07:02 +02:00
|
|
|
if (exclusion.indexOf(def.category)===-1) {
|
2014-10-01 14:41:06 +02:00
|
|
|
|
2014-08-29 21:37:30 +02:00
|
|
|
var category = def.category.replace(" ","_");
|
|
|
|
var rootCategory = category.split("-")[0];
|
2014-10-01 14:41:06 +02:00
|
|
|
|
2014-08-29 21:37:30 +02:00
|
|
|
var d = document.createElement("div");
|
2014-08-28 01:35:07 +02:00
|
|
|
d.id = "palette_node_"+nodeTypeId;
|
2014-08-29 21:37:30 +02:00
|
|
|
d.type = nt;
|
2014-10-01 14:41:06 +02:00
|
|
|
|
2014-10-08 14:54:48 +02:00
|
|
|
var label;
|
2014-10-09 11:05:45 +02:00
|
|
|
|
2014-10-08 14:54:48 +02:00
|
|
|
if (typeof def.paletteLabel === "undefined") {
|
|
|
|
label = /^(.*?)([ -]in|[ -]out)?$/.exec(nt)[1];
|
2014-10-09 11:05:45 +02:00
|
|
|
} else {
|
2014-10-08 14:54:48 +02:00
|
|
|
label = (typeof def.paletteLabel === "function" ? def.paletteLabel.call(def) : def.paletteLabel)||"";
|
|
|
|
}
|
2014-10-09 11:05:45 +02:00
|
|
|
|
2014-02-25 00:35:11 +01:00
|
|
|
d.innerHTML = '<div class="palette_label"></div>';
|
2014-12-09 15:37:32 +01:00
|
|
|
|
2014-08-29 21:37:30 +02:00
|
|
|
d.className="palette_node";
|
|
|
|
if (def.icon) {
|
|
|
|
d.style.backgroundImage = "url(icons/"+def.icon+")";
|
2014-02-25 00:35:11 +01:00
|
|
|
d.style.backgroundSize = "18px 27px";
|
2014-08-29 21:37:30 +02:00
|
|
|
if (def.align == "right") {
|
|
|
|
d.style.backgroundPosition = "95% 50%";
|
|
|
|
} else if (def.inputs > 0) {
|
|
|
|
d.style.backgroundPosition = "10% 50%";
|
|
|
|
}
|
|
|
|
}
|
2014-10-01 14:41:06 +02:00
|
|
|
|
2014-08-29 21:37:30 +02:00
|
|
|
d.style.backgroundColor = def.color;
|
2014-10-01 14:41:06 +02:00
|
|
|
|
2014-08-29 21:37:30 +02:00
|
|
|
if (def.outputs > 0) {
|
|
|
|
var portOut = document.createElement("div");
|
|
|
|
portOut.className = "palette_port palette_port_output";
|
|
|
|
d.appendChild(portOut);
|
|
|
|
}
|
2014-10-01 14:41:06 +02:00
|
|
|
|
2014-08-29 21:37:30 +02:00
|
|
|
if (def.inputs > 0) {
|
|
|
|
var portIn = document.createElement("div");
|
2014-02-25 00:35:11 +01:00
|
|
|
portIn.className = "palette_port palette_port_input";
|
2014-08-29 21:37:30 +02:00
|
|
|
d.appendChild(portIn);
|
|
|
|
}
|
2014-10-01 14:41:06 +02:00
|
|
|
|
2014-08-29 21:37:30 +02:00
|
|
|
if ($("#palette-base-category-"+rootCategory).length === 0) {
|
|
|
|
createCategoryContainer(rootCategory);
|
|
|
|
}
|
2015-02-06 14:16:19 +01:00
|
|
|
$("#palette-container-"+rootCategory).show();
|
2014-10-01 14:41:06 +02:00
|
|
|
|
|
|
|
if ($("#palette-"+category).length === 0) {
|
|
|
|
$("#palette-base-category-"+rootCategory).append('<div id="palette-'+category+'"></div>');
|
2014-08-29 21:37:30 +02:00
|
|
|
}
|
2014-10-01 14:41:06 +02:00
|
|
|
|
2014-08-29 21:37:30 +02:00
|
|
|
$("#palette-"+category).append(d);
|
2014-10-01 14:41:06 +02:00
|
|
|
d.onmousedown = function(e) { e.preventDefault(); };
|
|
|
|
|
2014-08-29 21:37:30 +02:00
|
|
|
$(d).popover({
|
|
|
|
title:d.type,
|
|
|
|
placement:"right",
|
|
|
|
trigger: "hover",
|
|
|
|
delay: { show: 750, hide: 50 },
|
|
|
|
html: true,
|
2014-02-25 00:35:11 +01:00
|
|
|
container:'body'
|
2014-08-29 21:37:30 +02:00
|
|
|
});
|
|
|
|
$(d).click(function() {
|
2015-03-02 23:55:34 +01:00
|
|
|
RED.view.focus();
|
2014-08-29 21:37:30 +02:00
|
|
|
var help = '<div class="node-help">'+($("script[data-help-name|='"+d.type+"']").html()||"")+"</div>";
|
|
|
|
$("#tab-info").html(help);
|
|
|
|
});
|
|
|
|
$(d).draggable({
|
|
|
|
helper: 'clone',
|
|
|
|
appendTo: 'body',
|
|
|
|
revert: true,
|
2015-03-02 23:55:34 +01:00
|
|
|
revertDuration: 50,
|
|
|
|
start: function() {RED.view.focus();}
|
2014-08-29 21:37:30 +02:00
|
|
|
});
|
2014-12-09 15:37:32 +01:00
|
|
|
|
2014-02-25 00:35:11 +01:00
|
|
|
setLabel(nt,$(d),label);
|
2014-07-23 23:07:02 +02:00
|
|
|
}
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|
2014-10-01 14:41:06 +02:00
|
|
|
|
2014-08-28 01:35:07 +02:00
|
|
|
function removeNodeType(nt) {
|
2014-02-25 00:35:11 +01:00
|
|
|
var nodeTypeId = escapeNodeType(nt);
|
2014-08-28 01:35:07 +02:00
|
|
|
$("#palette_node_"+nodeTypeId).remove();
|
|
|
|
}
|
|
|
|
function hideNodeType(nt) {
|
2014-02-25 00:35:11 +01:00
|
|
|
var nodeTypeId = escapeNodeType(nt);
|
2014-08-28 01:35:07 +02:00
|
|
|
$("#palette_node_"+nodeTypeId).hide();
|
|
|
|
}
|
2014-10-01 14:41:06 +02:00
|
|
|
|
2014-08-28 01:35:07 +02:00
|
|
|
function showNodeType(nt) {
|
2014-02-25 00:35:11 +01:00
|
|
|
var nodeTypeId = escapeNodeType(nt);
|
2014-08-28 01:35:07 +02:00
|
|
|
$("#palette_node_"+nodeTypeId).show();
|
2014-08-07 14:46:03 +02:00
|
|
|
}
|
2014-12-09 15:37:32 +01:00
|
|
|
|
2014-02-25 00:35:11 +01:00
|
|
|
function refreshNodeTypes() {
|
|
|
|
RED.nodes.eachSubflow(function(sf) {
|
|
|
|
var paletteNode = $("#palette_node_subflow_"+sf.id.replace(".","_"));
|
|
|
|
var portInput = paletteNode.find(".palette_port_input");
|
|
|
|
var portOutput = paletteNode.find(".palette_port_output");
|
2014-12-09 15:37:32 +01:00
|
|
|
|
2014-02-25 00:35:11 +01:00
|
|
|
if (portInput.length === 0 && sf.in.length > 0) {
|
|
|
|
var portIn = document.createElement("div");
|
|
|
|
portIn.className = "palette_port palette_port_input";
|
|
|
|
paletteNode.append(portIn);
|
|
|
|
} else if (portInput.length !== 0 && sf.in.length === 0) {
|
|
|
|
portInput.remove();
|
|
|
|
}
|
2014-12-09 15:37:32 +01:00
|
|
|
|
2014-11-07 17:12:27 +01:00
|
|
|
if (portOutput.length === 0 && sf.out.length > 0) {
|
2014-02-25 00:35:11 +01:00
|
|
|
var portOut = document.createElement("div");
|
|
|
|
portOut.className = "palette_port palette_port_output";
|
|
|
|
paletteNode.append(portOut);
|
2014-11-07 17:12:27 +01:00
|
|
|
} else if (portOutput.length !== 0 && sf.out.length === 0) {
|
2014-02-25 00:35:11 +01:00
|
|
|
portOutput.remove();
|
2014-12-09 15:37:32 +01:00
|
|
|
}
|
2014-02-25 00:35:11 +01:00
|
|
|
setLabel(sf.type+":"+sf.id,paletteNode,sf.name);
|
|
|
|
});
|
|
|
|
}
|
2014-12-09 15:37:32 +01:00
|
|
|
|
2014-01-19 01:01:27 +01:00
|
|
|
function filterChange() {
|
|
|
|
var val = $("#palette-search-input").val();
|
2014-08-08 01:01:35 +02:00
|
|
|
if (val === "") {
|
2014-01-19 01:01:27 +01:00
|
|
|
$("#palette-search-clear").hide();
|
|
|
|
} else {
|
|
|
|
$("#palette-search-clear").show();
|
|
|
|
}
|
2014-10-01 14:41:06 +02:00
|
|
|
|
2014-12-09 15:37:32 +01:00
|
|
|
var re = new RegExp(val,'i');
|
2014-01-19 01:01:27 +01:00
|
|
|
$(".palette_node").each(function(i,el) {
|
2014-08-08 01:01:35 +02:00
|
|
|
if (val === "" || re.test(el.id)) {
|
2014-01-19 01:01:27 +01:00
|
|
|
$(this).show();
|
|
|
|
} else {
|
|
|
|
$(this).hide();
|
|
|
|
}
|
2013-09-05 16:02:48 +02:00
|
|
|
});
|
2014-01-19 01:01:27 +01:00
|
|
|
}
|
2014-10-01 14:41:06 +02:00
|
|
|
|
2014-11-11 11:15:02 +01:00
|
|
|
function init() {
|
|
|
|
$(".palette-spinner").show();
|
2015-02-06 14:16:19 +01:00
|
|
|
if (RED.settings.paletteCategories) {
|
|
|
|
RED.settings.paletteCategories.forEach(createCategoryContainer);
|
|
|
|
} else {
|
|
|
|
core.forEach(createCategoryContainer);
|
|
|
|
}
|
|
|
|
|
2014-11-11 11:15:02 +01:00
|
|
|
$("#palette-search-input").focus(function(e) {
|
|
|
|
RED.keyboard.disable();
|
|
|
|
});
|
|
|
|
$("#palette-search-input").blur(function(e) {
|
|
|
|
RED.keyboard.enable();
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#palette-search-clear").on("click",function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
$("#palette-search-input").val("");
|
|
|
|
filterChange();
|
|
|
|
$("#palette-search-input").focus();
|
|
|
|
});
|
|
|
|
|
2014-01-19 01:01:27 +01:00
|
|
|
$("#palette-search-input").val("");
|
2014-11-11 11:15:02 +01:00
|
|
|
$("#palette-search-input").on("keyup",function() {
|
|
|
|
filterChange();
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#palette-search-input").on("focus",function() {
|
|
|
|
$("body").one("mousedown",function() {
|
|
|
|
$("#palette-search-input").blur();
|
|
|
|
});
|
2014-01-19 01:01:27 +01:00
|
|
|
});
|
2014-11-11 11:15:02 +01:00
|
|
|
}
|
2014-10-01 14:41:06 +02:00
|
|
|
|
2013-09-05 16:02:48 +02:00
|
|
|
return {
|
2014-11-11 11:15:02 +01:00
|
|
|
init: init,
|
2014-08-07 14:46:03 +02:00
|
|
|
add:addNodeType,
|
2014-08-28 01:35:07 +02:00
|
|
|
remove:removeNodeType,
|
|
|
|
hide:hideNodeType,
|
2014-02-25 00:35:11 +01:00
|
|
|
show:showNodeType,
|
|
|
|
refresh:refreshNodeTypes
|
2013-09-05 16:02:48 +02:00
|
|
|
};
|
2014-08-08 01:01:35 +02:00
|
|
|
})();
|