|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908 |
-
-
-
- RED.view = (function() {
- var space_width = 5000,
- space_height = 5000,
- lineCurveScale = 0.75,
- scaleFactor = 1,
- node_width = 100,
- node_height = 30;
-
- var touchLongPressTimeout = 1000,
- startTouchDistance = 0,
- startTouchCenter = [],
- moveTouchCenter = [],
- touchStartTime = 0;
-
- var activeWorkspace = 0;
- var workspaceScrollPositions = {};
-
- var selected_link = null,
- mousedown_link = null,
- mousedown_node = null,
- mousedown_port_type = null,
- mousedown_port_index = 0,
- mouseup_node = null,
- mouse_offset = [0,0],
- mouse_position = null,
- mouse_mode = 0,
- moving_set = [],
- dirty = false,
- lasso = null,
- showStatus = false,
- lastClickNode = null,
- dblClickPrimed = null,
- clickTime = 0,
- clickElapsed = 0;
-
- var clipboard = "";
-
- var status_colours = {
- "red": "#c00",
- "green": "#5a8",
- "yellow": "#F9DF31",
- "blue": "#53A3F3",
- "grey": "#d3d3d3"
- };
-
- var outer = d3.select("#chart")
- .append("svg:svg")
- .attr("width", space_width)
- .attr("height", space_height)
- .attr("pointer-events", "all")
- .style("cursor","crosshair");
-
- var vis = outer
- .append('svg:g')
- .on("dblclick.zoom", null)
- .append('svg:g')
- .on("mousemove", canvasMouseMove)
- .on("mousedown", canvasMouseDown)
- .on("mouseup", canvasMouseUp)
- .on("touchend", function() {
- clearTimeout(touchStartTime);
- touchStartTime = null;
- if (RED.touch.radialMenu.active()) {
- return;
- }
- if (lasso) {
- outer_background.attr("fill","#fff");
- }
- canvasMouseUp.call(this);
- })
- .on("touchcancel", canvasMouseUp)
- .on("touchstart", function() {
- var touch0;
- if (d3.event.touches.length>1) {
- clearTimeout(touchStartTime);
- touchStartTime = null;
- d3.event.preventDefault();
- touch0 = d3.event.touches.item(0);
- var touch1 = d3.event.touches.item(1);
- var a = touch0['pageY']-touch1['pageY'];
- var b = touch0['pageX']-touch1['pageX'];
-
- var offset = $("#chart").offset();
- var scrollPos = [$("#chart").scrollLeft(),$("#chart").scrollTop()];
- startTouchCenter = [
- (touch1['pageX']+(b/2)-offset.left+scrollPos[0])/scaleFactor,
- (touch1['pageY']+(a/2)-offset.top+scrollPos[1])/scaleFactor
- ];
- moveTouchCenter = [
- touch1['pageX']+(b/2),
- touch1['pageY']+(a/2)
- ];
- startTouchDistance = Math.sqrt((a*a)+(b*b));
- } else {
- var obj = d3.select(document.body);
- touch0 = d3.event.touches.item(0);
- var pos = [touch0.pageX,touch0.pageY];
- startTouchCenter = [touch0.pageX,touch0.pageY];
- startTouchDistance = 0;
- var point = d3.touches(this)[0];
- touchStartTime = setTimeout(function() {
- touchStartTime = null;
- showTouchMenu(obj,pos);
-
-
-
-
-
-
-
-
-
-
-
- },touchLongPressTimeout);
- }
- })
- .on("touchmove", function(){
- if (RED.touch.radialMenu.active()) {
- d3.event.preventDefault();
- return;
- }
- var touch0;
- if (d3.event.touches.length<2) {
- if (touchStartTime) {
- touch0 = d3.event.touches.item(0);
- var dx = (touch0.pageX-startTouchCenter[0]);
- var dy = (touch0.pageY-startTouchCenter[1]);
- var d = Math.abs(dx*dx+dy*dy);
- if (d > 64) {
- clearTimeout(touchStartTime);
- touchStartTime = null;
- }
- } else if (lasso) {
- d3.event.preventDefault();
- }
- canvasMouseMove.call(this);
- } else {
- touch0 = d3.event.touches.item(0);
- var touch1 = d3.event.touches.item(1);
- var a = touch0['pageY']-touch1['pageY'];
- var b = touch0['pageX']-touch1['pageX'];
- var offset = $("#chart").offset();
- var scrollPos = [$("#chart").scrollLeft(),$("#chart").scrollTop()];
- var moveTouchDistance = Math.sqrt((a*a)+(b*b));
- var touchCenter = [
- touch1['pageX']+(b/2),
- touch1['pageY']+(a/2)
- ];
-
- if (!isNaN(moveTouchDistance)) {
- oldScaleFactor = scaleFactor;
- scaleFactor = Math.min(2,Math.max(0.3, scaleFactor + (Math.floor(((moveTouchDistance*100)-(startTouchDistance*100)))/10000)));
-
- var deltaTouchCenter = [
- startTouchCenter[0]*(scaleFactor-oldScaleFactor),
- startTouchCenter[1]*(scaleFactor-oldScaleFactor)
- ];
-
- startTouchDistance = moveTouchDistance;
- moveTouchCenter = touchCenter;
-
- $("#chart").scrollLeft(scrollPos[0]+deltaTouchCenter[0]);
- $("#chart").scrollTop(scrollPos[1]+deltaTouchCenter[1]);
- redraw();
- }
- }
- });
-
- var outer_background = vis.append('svg:rect')
- .attr('width', space_width)
- .attr('height', space_height)
- .attr('fill','#fff');
-
- var gridScale = d3.scale.linear().range([0,2000]).domain([0,2000]);
-
-
-
- var drag_line = vis.append("svg:path").attr("class", "drag_line");
-
- var workspace_tabs = RED.tabs.create({
- id: "workspace-tabs",
- onchange: function(tab) {
- if (tab.type == "subflow") {
- $("#workspace-toolbar").show();
- } else {
- $("#workspace-toolbar").hide();
- }
- var chart = $("#chart");
- if (activeWorkspace !== 0) {
- workspaceScrollPositions[activeWorkspace] = {
- left:chart.scrollLeft(),
- top:chart.scrollTop()
- };
- }
- var scrollStartLeft = chart.scrollLeft();
- var scrollStartTop = chart.scrollTop();
-
- activeWorkspace = tab.id;
- if (workspaceScrollPositions[activeWorkspace]) {
- chart.scrollLeft(workspaceScrollPositions[activeWorkspace].left);
- chart.scrollTop(workspaceScrollPositions[activeWorkspace].top);
- } else {
- chart.scrollLeft(0);
- chart.scrollTop(0);
- }
- var scrollDeltaLeft = chart.scrollLeft() - scrollStartLeft;
- var scrollDeltaTop = chart.scrollTop() - scrollStartTop;
- if (mouse_position != null) {
- mouse_position[0] += scrollDeltaLeft;
- mouse_position[1] += scrollDeltaTop;
- }
-
- clearSelection();
- RED.nodes.eachNode(function(n) {
- n.dirty = true;
- });
- redraw();
- },
- ondblclick: function(tab) {
- showRenameWorkspaceDialog(tab.id);
- },
- onadd: function(tab) {
- var menuli = $("<li/>");
- var menuA = $("<a/>",{tabindex:"-1",href:"#"+tab.id}).appendTo(menuli);
- menuA.html(tab.label);
- menuA.on("click",function() {
- workspace_tabs.activateTab(tab.id);
- });
-
- $('#workspace-menu-list').append(menuli);
-
- if (workspace_tabs.count() == 1) {
- $('#btn-workspace-delete').parent().addClass("disabled");
- } else {
- $('#btn-workspace-delete').parent().removeClass("disabled");
- }
- },
- onremove: function(tab) {
- if (workspace_tabs.count() == 1) {
- $('#btn-workspace-delete').parent().addClass("disabled");
- } else {
- $('#btn-workspace-delete').parent().removeClass("disabled");
- }
- $('#workspace-menu-list a[href="#'+tab.id+'"]').parent().remove();
-
- }
- });
-
- var workspaceIndex = 0;
-
- function addWorkspace() {
- var tabId = RED.nodes.id();
- do {
- workspaceIndex += 1;
- } while($("#workspace-tabs a[title='Sheet "+workspaceIndex+"']").size() !== 0);
-
- var ws = {type:"tab",id:tabId,label:"Sheet "+workspaceIndex};
- RED.nodes.addWorkspace(ws);
- workspace_tabs.addTab(ws);
- workspace_tabs.activateTab(tabId);
- RED.history.push({t:'add',workspaces:[ws],dirty:dirty});
- RED.view.dirty(true);
- }
- $('#btn-workspace-add-tab').on("click",addWorkspace);
- $('#btn-workspace-add').on("click",addWorkspace);
- $('#btn-workspace-edit').on("click",function() {
- showRenameWorkspaceDialog(activeWorkspace);
- });
- $('#btn-workspace-delete').on("click",function() {
- deleteWorkspace(activeWorkspace);
- });
-
- function deleteWorkspace(id) {
- if (workspace_tabs.count() == 1) {
- return;
- }
- var ws = RED.nodes.workspace(id);
- $( "#node-dialog-delete-workspace" ).dialog('option','workspace',ws);
- $( "#node-dialog-delete-workspace-name" ).text(ws.label);
- $( "#node-dialog-delete-workspace" ).dialog('open');
- }
-
- function canvasMouseDown() {
- if (!mousedown_node && !mousedown_link) {
- selected_link = null;
- updateSelection();
- }
- if (mouse_mode === 0) {
- if (lasso) {
- lasso.remove();
- lasso = null;
- }
-
- if (!touchStartTime) {
- var point = d3.mouse(this);
- lasso = vis.append('rect')
- .attr("ox",point[0])
- .attr("oy",point[1])
- .attr("rx",2)
- .attr("ry",2)
- .attr("x",point[0])
- .attr("y",point[1])
- .attr("width",0)
- .attr("height",0)
- .attr("class","lasso");
- d3.event.preventDefault();
- }
- }
- }
-
- function canvasMouseMove() {
- mouse_position = d3.touches(this)[0]||d3.mouse(this);
-
-
-
-
-
-
-
-
-
-
-
- if (lasso) {
- var ox = parseInt(lasso.attr("ox"));
- var oy = parseInt(lasso.attr("oy"));
- var x = parseInt(lasso.attr("x"));
- var y = parseInt(lasso.attr("y"));
- var w;
- var h;
- if (mouse_position[0] < ox) {
- x = mouse_position[0];
- w = ox-x;
- } else {
- w = mouse_position[0]-x;
- }
- if (mouse_position[1] < oy) {
- y = mouse_position[1];
- h = oy-y;
- } else {
- h = mouse_position[1]-y;
- }
- lasso
- .attr("x",x)
- .attr("y",y)
- .attr("width",w)
- .attr("height",h)
- ;
- return;
- }
-
- if (mouse_mode != RED.state.IMPORT_DRAGGING && !mousedown_node && selected_link == null) {
- return;
- }
-
- var mousePos;
- if (mouse_mode == RED.state.JOINING) {
-
- drag_line.attr("class", "drag_line");
- mousePos = mouse_position;
- var numOutputs = (mousedown_port_type === 0)?(mousedown_node.outputs || 1):(mousedown_node._def.inputs || 1);
- var sourcePort = mousedown_port_index;
- var portY = -((numOutputs-1)/2)*13 +13*sourcePort;
-
- var sc = (mousedown_port_type === 0)?1:-1;
-
- var dy = mousePos[1]-(mousedown_node.y+portY);
- var dx = mousePos[0]-(mousedown_node.x+sc*mousedown_node.w/2);
- var delta = Math.sqrt(dy*dy+dx*dx);
- var scale = lineCurveScale;
- var scaleY = 0;
-
- if (delta < node_width) {
- scale = 0.75-0.75*((node_width-delta)/node_width);
- }
- if (dx*sc < 0) {
- scale += 2*(Math.min(5*node_width,Math.abs(dx))/(5*node_width));
- if (Math.abs(dy) < 3*node_height) {
- scaleY = ((dy>0)?0.5:-0.5)*(((3*node_height)-Math.abs(dy))/(3*node_height))*(Math.min(node_width,Math.abs(dx))/(node_width)) ;
- }
- }
-
- drag_line.attr("d",
- "M "+(mousedown_node.x+sc*mousedown_node.w/2)+" "+(mousedown_node.y+portY)+
- " C "+(mousedown_node.x+sc*(mousedown_node.w/2+node_width*scale))+" "+(mousedown_node.y+portY+scaleY*node_height)+" "+
- (mousePos[0]-sc*(scale)*node_width)+" "+(mousePos[1]-scaleY*node_height)+" "+
- mousePos[0]+" "+mousePos[1]
- );
- d3.event.preventDefault();
- } else if (mouse_mode == RED.state.MOVING) {
- mousePos = mouse_position;
- var d = (mouse_offset[0]-mousePos[0])*(mouse_offset[0]-mousePos[0]) + (mouse_offset[1]-mousePos[1])*(mouse_offset[1]-mousePos[1]);
- if (d > 2) {
- mouse_mode = RED.state.MOVING_ACTIVE;
- clickElapsed = 0;
- }
- } else if (mouse_mode == RED.state.MOVING_ACTIVE || mouse_mode == RED.state.IMPORT_DRAGGING) {
- mousePos = mouse_position;
- var node;
- var i;
- var minX = 0;
- var minY = 0;
- for (var n = 0; n<moving_set.length; n++) {
- node = moving_set[n];
- if (d3.event.shiftKey) {
- node.n.ox = node.n.x;
- node.n.oy = node.n.y;
- }
- node.n.x = mousePos[0]+node.dx;
- node.n.y = mousePos[1]+node.dy;
- node.n.dirty = true;
- minX = Math.min(node.n.x-node.n.w/2-5,minX);
- minY = Math.min(node.n.y-node.n.h/2-5,minY);
- }
- if (minX !== 0 || minY !== 0) {
- for (i = 0; i<moving_set.length; i++) {
- node = moving_set[i];
- node.n.x -= minX;
- node.n.y -= minY;
- }
- }
- if (d3.event.shiftKey && moving_set.length > 0) {
- var gridOffset = [0,0];
- node = moving_set[0];
- gridOffset[0] = node.n.x-(20*Math.floor((node.n.x-node.n.w/2)/20)+node.n.w/2);
- gridOffset[1] = node.n.y-(20*Math.floor(node.n.y/20));
- if (gridOffset[0] !== 0 || gridOffset[1] !== 0) {
- for (i = 0; i<moving_set.length; i++) {
- node = moving_set[i];
- node.n.x -= gridOffset[0];
- node.n.y -= gridOffset[1];
- if (node.n.x == node.n.ox && node.n.y == node.n.oy) {
- node.dirty = false;
- }
- }
- }
- }
- }
- redraw();
- }
-
- function canvasMouseUp() {
- if (mousedown_node && mouse_mode == RED.state.JOINING) {
- drag_line.attr("class", "drag_line_hidden");
- }
- if (lasso) {
- var x = parseInt(lasso.attr("x"));
- var y = parseInt(lasso.attr("y"));
- var x2 = x+parseInt(lasso.attr("width"));
- var y2 = y+parseInt(lasso.attr("height"));
- if (!d3.event.ctrlKey) {
- clearSelection();
- }
- RED.nodes.eachNode(function(n) {
- if (n.z == activeWorkspace && !n.selected) {
- n.selected = (n.x > x && n.x < x2 && n.y > y && n.y < y2);
- if (n.selected) {
- n.dirty = true;
- moving_set.push({n:n});
- }
- }
- });
- updateSelection();
- lasso.remove();
- lasso = null;
- } else if (mouse_mode == RED.state.DEFAULT && mousedown_link == null) {
- clearSelection();
- updateSelection();
- }
- if (mouse_mode == RED.state.MOVING_ACTIVE) {
- if (moving_set.length > 0) {
- var ns = [];
- for (var j=0;j<moving_set.length;j++) {
- ns.push({n:moving_set[j].n,ox:moving_set[j].ox,oy:moving_set[j].oy});
- }
- RED.history.push({t:'move',nodes:ns,dirty:dirty});
- RED.storage.update();
- }
- }
- if (mouse_mode == RED.state.MOVING || mouse_mode == RED.state.MOVING_ACTIVE) {
- for (var i=0;i<moving_set.length;i++) {
- delete moving_set[i].ox;
- delete moving_set[i].oy;
- }
- }
- if (mouse_mode == RED.state.IMPORT_DRAGGING) {
- RED.keyboard.remove( 27);
- setDirty(true);
- }
- redraw();
-
- resetMouseVars();
- }
-
- $('#btn-zoom-out').click(function() {zoomOut();});
- $('#btn-zoom-zero').click(function() {zoomZero();});
- $('#btn-zoom-in').click(function() {zoomIn();});
- $("#chart").on('DOMMouseScroll mousewheel', function (evt) {
- if ( evt.altKey ) {
- evt.preventDefault();
- evt.stopPropagation();
- var move = -(evt.originalEvent.detail) || evt.originalEvent.wheelDelta;
- if (move <= 0) { zoomOut(); }
- else { zoomIn(); }
- }
- });
- $("#chart").droppable({
- accept:".palette_node",
- drop: function( event, ui ) {
- d3.event = event;
- var selected_tool = ui.draggable[0].type;
- var mousePos = d3.touches(this)[0]||d3.mouse(this);
- mousePos[1] += this.scrollTop;
- mousePos[0] += this.scrollLeft;
- mousePos[1] /= scaleFactor;
- mousePos[0] /= scaleFactor;
-
- var nn = {x: mousePos[0],y:mousePos[1],w:node_width,z:activeWorkspace};
- nn.type = selected_tool;
- nn._def = RED.nodes.getType(nn.type);
- nn.id = RED.nodes.cppName(nn);
-
- nn._def.defaults = nn._def.defaults ? nn._def.defaults : {};
- nn._def.defaults.name = { value: nn.id };
-
- nn.outputs = nn._def.outputs;
- nn.changed = true;
-
- for (var d in nn._def.defaults) {
- if (nn._def.defaults.hasOwnProperty(d)) {
- nn[d] = nn._def.defaults[d].value;
- }
- }
-
- if (nn._def.onadd) {
- nn._def.onadd.call(nn);
- }
-
- nn.h = Math.max(node_height,(nn.outputs||0) * 15);
- RED.history.push({t:'add',nodes:[nn.id],dirty:dirty});
- RED.nodes.add(nn);
- RED.editor.validateNode(nn);
- setDirty(true);
-
- clearSelection();
- nn.selected = true;
- moving_set.push({n:nn});
- updateSelection();
- redraw();
- if (nn._def.autoedit) {
- RED.editor.edit(nn);
- }
- }
- });
-
- function zoomIn() {
- if (scaleFactor < 2) {
- scaleFactor += 0.1;
- redraw();
- }
- }
- function zoomOut() {
- if (scaleFactor > 0.3) {
- scaleFactor -= 0.1;
- redraw();
- }
- }
- function zoomZero() {
- scaleFactor = 1;
- redraw();
- }
-
- function selectAll() {
- RED.nodes.eachNode(function(n) {
- if (n.z == activeWorkspace) {
- if (!n.selected) {
- n.selected = true;
- n.dirty = true;
- moving_set.push({n:n});
- }
- }
- });
- selected_link = null;
- updateSelection();
- redraw();
- }
-
- function clearSelection() {
- for (var i=0;i<moving_set.length;i++) {
- var n = moving_set[i];
- n.n.dirty = true;
- n.n.selected = false;
- }
- moving_set = [];
- selected_link = null;
- }
-
- function updateSelection() {
- if (moving_set.length === 0) {
- $("#li-menu-export").addClass("disabled");
- $("#li-menu-export-clipboard").addClass("disabled");
- $("#li-menu-export-library").addClass("disabled");
- } else {
- $("#li-menu-export").removeClass("disabled");
- $("#li-menu-export-clipboard").removeClass("disabled");
- $("#li-menu-export-library").removeClass("disabled");
- }
- if (moving_set.length === 0 && selected_link == null) {
- RED.keyboard.remove( 8);
- RED.keyboard.remove( 46);
- RED.keyboard.remove( 67);
- RED.keyboard.remove( 88);
- } else {
- RED.keyboard.add( 8,function(){deleteSelection();d3.event.preventDefault();});
- RED.keyboard.add( 46,function(){deleteSelection();d3.event.preventDefault();});
- RED.keyboard.add( 67,{ctrl:true},function(){copySelection();d3.event.preventDefault();});
- RED.keyboard.add( 88,{ctrl:true},function(){copySelection();deleteSelection();d3.event.preventDefault();});
- }
- if (moving_set.length === 0) {
- RED.keyboard.remove( 38);
- RED.keyboard.remove( 40);
- RED.keyboard.remove( 37);
- RED.keyboard.remove( 39);
- } else {
- RED.keyboard.add( 38, function() { if(d3.event.shiftKey){moveSelection( 0,-20)}else{moveSelection( 0,-1);}d3.event.preventDefault();},endKeyboardMove);
- RED.keyboard.add( 40, function() { if(d3.event.shiftKey){moveSelection( 0, 20)}else{moveSelection( 0, 1);}d3.event.preventDefault();},endKeyboardMove);
- RED.keyboard.add( 37, function() { if(d3.event.shiftKey){moveSelection(-20, 0)}else{moveSelection(-1, 0);}d3.event.preventDefault();},endKeyboardMove);
- RED.keyboard.add( 39, function() { if(d3.event.shiftKey){moveSelection( 20, 0)}else{moveSelection( 1, 0);}d3.event.preventDefault();},endKeyboardMove);
- }
- if (moving_set.length == 1) {
- RED.sidebar.info.refresh(moving_set[0].n);
- RED.nodes.selectNode(moving_set[0].n.type);
- } else {
- RED.sidebar.info.clear();
- }
- }
- function endKeyboardMove() {
- var ns = [];
- for (var i=0;i<moving_set.length;i++) {
- ns.push({n:moving_set[i].n,ox:moving_set[i].ox,oy:moving_set[i].oy});
- delete moving_set[i].ox;
- delete moving_set[i].oy;
- }
- RED.history.push({t:'move',nodes:ns,dirty:dirty});
- }
- function moveSelection(dx,dy) {
- var minX = 0;
- var minY = 0;
- var node;
-
- for (var i=0;i<moving_set.length;i++) {
- node = moving_set[i];
- if (node.ox == null && node.oy == null) {
- node.ox = node.n.x;
- node.oy = node.n.y;
- }
- node.n.x += dx;
- node.n.y += dy;
- node.n.dirty = true;
- minX = Math.min(node.n.x-node.n.w/2-5,minX);
- minY = Math.min(node.n.y-node.n.h/2-5,minY);
- }
-
- if (minX !== 0 || minY !== 0) {
- for (var n = 0; n<moving_set.length; n++) {
- node = moving_set[n];
- node.n.x -= minX;
- node.n.y -= minY;
- }
- }
-
- redraw();
- }
- function deleteSelection() {
- var removedNodes = [];
- var removedLinks = [];
- var startDirty = dirty;
- if (moving_set.length > 0) {
- for (var i=0;i<moving_set.length;i++) {
- var node = moving_set[i].n;
- node.selected = false;
- if (node.x < 0) {
- node.x = 25
- }
- var rmlinks = RED.nodes.remove(node.id);
- for (var j=0; j < rmlinks.length; j++) {
- var link = rmlinks[j];
-
-
- if (link.source == node) {
-
- var n = link.targetPort;
- var rect = link.target.inputlist[n];
- rect.on("mousedown", (function(d,n){return function(d){portMouseDown(d,1,n);}})(rect, n))
- .on("touchstart", (function(d,n){return function(d){portMouseDown(d,1,n);}})(rect, n))
- .on("mouseup", (function(d,n){return function(d){portMouseUp(d,1,n);}})(rect, n))
- .on("touchend", (function(d,n){return function(d){portMouseUp(d,1,n);}})(rect, n))
- .on("mouseover",function(d) { var port = d3.select(this); port.classed("port_hovered",(mouse_mode!=RED.state.JOINING || mousedown_port_type != 1 ));})
- .on("mouseout",function(d) { var port = d3.select(this); port.classed("port_hovered",false);})
- }
- }
- removedNodes.push(node);
- removedLinks = removedLinks.concat(rmlinks);
- }
- moving_set = [];
- setDirty(true);
- }
- if (selected_link) {
-
- var n = selected_link.targetPort;
- var rect = selected_link.target.inputlist[n];
- rect.on("mousedown", (function(d,n){return function(d){portMouseDown(d,1,n);}})(rect, n))
- .on("touchstart", (function(d,n){return function(d){portMouseDown(d,1,n);}})(rect, n))
- .on("mouseup", (function(d,n){return function(d){portMouseUp(d,1,n);}})(rect, n))
- .on("touchend", (function(d,n){return function(d){portMouseUp(d,1,n);}})(rect, n))
- .on("mouseover",function(d) { var port = d3.select(this); port.classed("port_hovered",(mouse_mode!=RED.state.JOINING || mousedown_port_type != 1 ));})
- .on("mouseout",function(d) { var port = d3.select(this); port.classed("port_hovered",false);});
- RED.nodes.removeLink(selected_link);
- removedLinks.push(selected_link);
- setDirty(true);
- }
- RED.history.push({t:'delete',nodes:removedNodes,links:removedLinks,dirty:startDirty});
-
- selected_link = null;
- updateSelection();
- redraw();
- }
-
- function copySelection() {
- if (moving_set.length > 0) {
- var nns = [];
- for (var n=0;n<moving_set.length;n++) {
- var node = moving_set[n].n;
- nns.push(RED.nodes.convertNode(node));
- }
- clipboard = JSON.stringify(nns);
- RED.notify(moving_set.length+" node"+(moving_set.length>1?"s":"")+" copied");
- }
- }
-
- function calculateTextWidth(str) {
- var sp = document.createElement("span");
- sp.className = "node_label";
- sp.style.position = "absolute";
- sp.style.top = "-1000px";
- sp.innerHTML = (str||"").replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">");
- document.body.appendChild(sp);
- var w = sp.offsetWidth;
- document.body.removeChild(sp);
- return 50+w;
- }
-
- function resetMouseVars() {
- mousedown_node = null;
- mouseup_node = null;
- mousedown_link = null;
- mouse_mode = 0;
- mousedown_port_type = 0;
- }
-
- function portMouseDown(d,portType,portIndex) {
-
-
- mousedown_node = d;
- selected_link = null;
- mouse_mode = RED.state.JOINING;
- mousedown_port_type = portType;
- mousedown_port_index = portIndex || 0;
- document.body.style.cursor = "crosshair";
- d3.event.preventDefault();
- }
-
- function portMouseUp(d,portType,portIndex) {
- document.body.style.cursor = "";
- if (mouse_mode == RED.state.JOINING && mousedown_node) {
- if (typeof TouchEvent != "undefined" && d3.event instanceof TouchEvent) {
- RED.nodes.eachNode(function(n) {
- if (n.z == activeWorkspace) {
- var hw = n.w/2;
- var hh = n.h/2;
- if (n.x-hw<mouse_position[0] && n.x+hw> mouse_position[0] &&
- n.y-hh<mouse_position[1] && n.y+hh>mouse_position[1]) {
- mouseup_node = n;
- portType = mouseup_node._def.inputs>0?1:0;
- portIndex = 0;
- }
- }
- });
- } else {
- mouseup_node = d;
- }
- if (portType == mousedown_port_type || mouseup_node === mousedown_node) {
- drag_line.attr("class", "drag_line_hidden");
- resetMouseVars();
- return;
- }
- var src,dst,src_port,dst_port;
- if (mousedown_port_type === 0) {
- src = mousedown_node;
- src_port = mousedown_port_index;
- dst = mouseup_node;
- dst_port = portIndex;
- } else if (mousedown_port_type == 1) {
- src = mouseup_node;
- src_port = portIndex;
- dst = mousedown_node;
- dst_port = mousedown_port_index;
- }
-
- var existingLink = false;
- RED.nodes.eachLink(function(d) {
- existingLink = existingLink || (d.source === src && d.target === dst && d.sourcePort == src_port && d.targetPort == dst_port);
- });
- if (!existingLink) {
- var link = {source: src, sourcePort:src_port, target: dst, targetPort: dst_port};
- RED.nodes.addLink(link);
- RED.history.push({t:'add',links:[link],dirty:dirty});
- setDirty(true);
-
- dst.inputlist[dst_port]
- .classed("port_hovered",false)
- .on("mousedown",null)
- .on("touchstart", null)
- .on("mouseup", null)
- .on("touchend", null)
- .on("mouseover", null)
- .on("mouseout", null);
- }
- selected_link = null;
- redraw();
- }
- }
-
- function nodeMouseUp(d) {
- if (dblClickPrimed && mousedown_node == d && clickElapsed > 0 && clickElapsed < 750) {
- RED.editor.edit(d);
- clickElapsed = 0;
- d3.event.stopPropagation();
- return;
- }
- portMouseUp(d, d._def.inputs > 0 ? 1 : 0, 0);
- }
-
- function nodeMouseDown(d) {
-
-
-
- if (mouse_mode == RED.state.IMPORT_DRAGGING) {
- RED.keyboard.remove( 27);
- updateSelection();
- setDirty(true);
- redraw();
- resetMouseVars();
- d3.event.stopPropagation();
- return;
- }
- mousedown_node = d;
- var now = Date.now();
- clickElapsed = now-clickTime;
- clickTime = now;
-
- dblClickPrimed = (lastClickNode == mousedown_node);
- lastClickNode = mousedown_node;
-
- var i;
-
- if (d.selected && d3.event.ctrlKey) {
- d.selected = false;
- for (i=0;i<moving_set.length;i+=1) {
- if (moving_set[i].n === d) {
- moving_set.splice(i,1);
- break;
- }
- }
- } else {
- if (d3.event.shiftKey) {
- clearSelection();
- var cnodes = RED.nodes.getAllFlowNodes(mousedown_node);
- for (var n=0;n<cnodes.length;n++) {
- cnodes[n].selected = true;
- cnodes[n].dirty = true;
- moving_set.push({n:cnodes[n]});
- }
- } else if (!d.selected) {
- if (!d3.event.ctrlKey) {
- clearSelection();
- }
- mousedown_node.selected = true;
- moving_set.push({n:mousedown_node});
- }
- selected_link = null;
- if (d3.event.button != 2) {
- mouse_mode = RED.state.MOVING;
- var mouse = d3.touches(this)[0]||d3.mouse(this);
- mouse[0] += d.x-d.w/2;
- mouse[1] += d.y-d.h/2;
- for (i=0;i<moving_set.length;i++) {
- moving_set[i].ox = moving_set[i].n.x;
- moving_set[i].oy = moving_set[i].n.y;
- moving_set[i].dx = moving_set[i].n.x-mouse[0];
- moving_set[i].dy = moving_set[i].n.y-mouse[1];
- }
- mouse_offset = d3.mouse(document.body);
- if (isNaN(mouse_offset[0])) {
- mouse_offset = d3.touches(document.body)[0];
- }
- }
- }
- d.dirty = true;
- updateSelection();
- redraw();
- d3.event.stopPropagation();
- }
-
- function nodeButtonClicked(d) {
- if (d._def.button.toggle) {
- d[d._def.button.toggle] = !d[d._def.button.toggle];
- d.dirty = true;
- }
- if (d._def.button.onclick) {
- d._def.button.onclick.call(d);
- }
- if (d.dirty) {
- redraw();
- }
- d3.event.preventDefault();
- }
-
- function showTouchMenu(obj,pos) {
- var mdn = mousedown_node;
- var options = [];
- options.push({name:"delete",disabled:(moving_set.length===0),onselect:function() {deleteSelection();}});
- options.push({name:"cut",disabled:(moving_set.length===0),onselect:function() {copySelection();deleteSelection();}});
- options.push({name:"copy",disabled:(moving_set.length===0),onselect:function() {copySelection();}});
- options.push({name:"paste",disabled:(clipboard.length===0),onselect:function() {importNodes(clipboard,true);}});
- options.push({name:"edit",disabled:(moving_set.length != 1),onselect:function() { RED.editor.edit(mdn);}});
- options.push({name:"select",onselect:function() {selectAll();}});
- options.push({name:"undo",disabled:(RED.history.depth() === 0),onselect:function() {RED.history.pop();}});
-
- RED.touch.radialMenu.show(obj,pos,options);
- resetMouseVars();
- }
-
-
- function checkRequirements(d) {
-
- d.requirementError = false;
- d.conflicts = new Array();
- d.requirements = new Array();
- requirements.forEach(function(r) {
- if (r.type == d.type) d.requirements.push(r);
- });
-
-
- d.requirements.forEach(function(r) {
- RED.nodes.eachNode(function (n2) {
- if (n2 != d && n2.requirements != null ) {
- n2.requirements.forEach(function(r2) {
- if (r["resource"] == r2["resource"]) {
- if (r["shareable"] == false ) {
- var msg = "Conflict: shareable '"+r["resource"]+"' "+d.name+" and "+n2.name;
- console.log(msg);
- msg = n2.name + " uses " + r["resource"] + ", too";
- d.conflicts.push(msg);
- d.requirementError = true;
- }
-
- if (r["setting"] != r2["setting"]) {
- var msg = "Conflict: "+ d.name + " setting['"+r["setting"]+"'] and "+n2.name+" setting['"+r2["setting"]+"']";
- console.log(msg);
- msg = n2.name + " has different settings: " + r["setting"] + " ./. " + r2["setting"];
- d.conflicts.push(msg);
- d.requirementError = true;
- }
- }
- });
- }
- });
- });
- }
-
-
- function redraw() {
- vis.attr("transform","scale("+scaleFactor+")");
- outer.attr("width", space_width*scaleFactor).attr("height", space_height*scaleFactor);
-
- if (mouse_mode != RED.state.JOINING) {
-
-
- var node = vis.selectAll(".nodegroup").data(RED.nodes.nodes.filter(function(d) { return d.z == activeWorkspace }),function(d){return d.id});
- node.exit().remove();
-
- var nodeEnter = node.enter().insert("svg:g").attr("class", "node nodegroup");
- nodeEnter.each(function(d,i) {
- var node = d3.select(this);
- node.attr("id",d.id);
-
-
- var l = d.name ? d.name : d.id;
-
- d.w = Math.max(node_width,calculateTextWidth(l)+(d._def.inputs>0?7:0) );
- d.h = Math.max(node_height,(Math.max(d.outputs,d._def.inputs)||0) * 15);
-
- checkRequirements(d);
-
- if (d._def.badge) {
- var badge = node.append("svg:g").attr("class","node_badge_group");
- var badgeRect = badge.append("rect").attr("class","node_badge").attr("rx",5).attr("ry",5).attr("width",40).attr("height",15);
- badge.append("svg:text").attr("class","node_badge_label").attr("x",35).attr("y",11).attr('text-anchor','end').text(d._def.badge());
- if (d._def.onbadgeclick) {
- badgeRect.attr("cursor","pointer")
- .on("click",function(d) { d._def.onbadgeclick.call(d);d3.event.preventDefault();});
- }
- }
-
- if (d._def.button) {
- var nodeButtonGroup = node.append('svg:g')
- .attr("transform",function(d) { return "translate("+((d._def.align == "right") ? 94 : -25)+",2)"; })
- .attr("class",function(d) { return "node_button "+((d._def.align == "right") ? "node_right_button" : "node_left_button"); });
- nodeButtonGroup.append('rect')
- .attr("rx",8)
- .attr("ry",8)
- .attr("width",32)
- .attr("height",node_height-4)
- .attr("fill","#eee");
- nodeButtonGroup.append('rect')
- .attr("x",function(d) { return d._def.align == "right"? 10:5})
- .attr("y",4)
- .attr("rx",5)
- .attr("ry",5)
- .attr("width",16)
- .attr("height",node_height-12)
- .attr("fill",function(d) { return d._def.color;})
- .attr("cursor","pointer")
- .on("mousedown",function(d) {if (!lasso) { d3.select(this).attr("fill-opacity",0.2);d3.event.preventDefault(); d3.event.stopPropagation();}})
- .on("mouseup",function(d) {if (!lasso) { d3.select(this).attr("fill-opacity",0.4);d3.event.preventDefault();d3.event.stopPropagation();}})
- .on("mouseover",function(d) {if (!lasso) { d3.select(this).attr("fill-opacity",0.4);}})
- .on("mouseout",function(d) {if (!lasso) {
- var op = 1;
- if (d._def.button.toggle) {
- op = d[d._def.button.toggle]?1:0.2;
- }
- d3.select(this).attr("fill-opacity",op);
- }})
- .on("click",nodeButtonClicked)
- .on("touchstart",nodeButtonClicked)
- }
-
- var mainRect = node.append("rect")
- .attr("class", "node")
- .classed("node_unknown",function(d) { return d.type == "unknown"; })
- .attr("rx", 6)
- .attr("ry", 6)
- .attr("fill",function(d) { return d._def.color;})
- .on("mouseup",nodeMouseUp)
- .on("mousedown",nodeMouseDown)
- .on("touchstart",function(d) {
- var obj = d3.select(this);
- var touch0 = d3.event.touches.item(0);
- var pos = [touch0.pageX,touch0.pageY];
- startTouchCenter = [touch0.pageX,touch0.pageY];
- startTouchDistance = 0;
- touchStartTime = setTimeout(function() {
- showTouchMenu(obj,pos);
- },touchLongPressTimeout);
- nodeMouseDown.call(this,d)
- })
- .on("touchend", function(d) {
- clearTimeout(touchStartTime);
- touchStartTime = null;
- if (RED.touch.radialMenu.active()) {
- d3.event.stopPropagation();
- return;
- }
- nodeMouseUp.call(this,d);
- })
- .on("mouseover",function(d) {
- if (mouse_mode === 0) {
- var node = d3.select(this);
- node.classed("node_hovered",true);
- }
- })
- .on("mouseout",function(d) {
- var node = d3.select(this);
- node.classed("node_hovered",false);
- });
-
-
-
-
- if (d._def.icon) {
-
- var icon_group = node.append("g")
- .attr("class","node_icon_group")
- .attr("x",0).attr("y",0);
-
- var icon_shade = icon_group.append("rect")
- .attr("x",0).attr("y",0)
- .attr("class","node_icon_shade")
- .attr("width","30")
- .attr("stroke","none")
- .attr("fill","#000")
- .attr("fill-opacity","0.05")
- .attr("height",function(d){return Math.min(50,d.h-4);});
-
- var icon = icon_group.append("image")
- .attr("xlink:href","icons/"+d._def.icon)
- .attr("class","node_icon")
- .attr("x",0)
- .attr("width","30")
- .attr("height","30");
-
- var icon_shade_border = icon_group.append("path")
- .attr("d",function(d) { return "M 30 1 l 0 "+(d.h-2)})
- .attr("class","node_icon_shade_border")
- .attr("stroke-opacity","0.1")
- .attr("stroke","#000")
- .attr("stroke-width","2");
-
- if ("right" == d._def.align) {
- icon_group.attr('class','node_icon_group node_icon_group_'+d._def.align);
- icon_shade_border.attr("d",function(d) { return "M 0 1 l 0 "+(d.h-2)});
-
-
-
- }
-
-
-
-
-
-
-
-
-
-
- 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);
-
-
-
-
-
- };
-
-
- icon_group.style("pointer-events","none");
- }
- var text = node.append('svg:text').attr('class','node_label').attr('x', 38).attr('dy', '.35em').attr('text-anchor','start');
- if (d._def.align) {
- text.attr('class','node_label node_label_'+d._def.align);
- text.attr('text-anchor','end');
- }
-
- var status = node.append("svg:g").attr("class","node_status_group").style("display","none");
-
- var statusRect = status.append("rect").attr("class","node_status")
- .attr("x",6).attr("y",1).attr("width",9).attr("height",9)
- .attr("rx",2).attr("ry",2).attr("stroke-width","3");
-
- var statusLabel = status.append("svg:text")
- .attr("class","node_status_label")
- .attr('x',20).attr('y',9)
- .style({
- 'stroke-width': 0,
- 'fill': '#888',
- 'font-size':'9pt',
- 'stroke':'#000',
- 'text-anchor':'start'
- });
-
-
-
- var numInputs = d._def.inputs;
- var inputlist = [];
- for (var n=0; n < numInputs; n++) {
- var link = RED.nodes.links.filter(function(l){return (l.target == d && l.targetPort == n);});
- var y = (d.h/2)-((numInputs-1)/2)*13;
- y = (y+13*n)-5;
- text.attr("x",38);
- var rect = node.append("rect");
- inputlist[n] = rect;
- rect.attr("class","port port_input").attr("rx",3).attr("ry",3)
- .attr("y",y).attr("x",-5).attr("width",10).attr("height",10).attr("index",n);
- if (link && link.length > 0) {
-
- rect.on("mousedown",null)
- .on("touchstart", null)
- .on("mouseup", null)
- .on("touchend", null)
- .on("mouseover", null)
- .on("mouseout", null)
- .classed("port_hovered",false);
- } else {
-
- rect.on("mousedown", (function(nn){return function(d){portMouseDown(d,1,nn);}})(n))
- .on("touchstart", (function(nn){return function(d){portMouseDown(d,1,nn);}})(n))
- .on("mouseup", (function(nn){return function(d){portMouseUp(d,1,nn);}})(n))
- .on("touchend", (function(nn){return function(d){portMouseUp(d,1,nn);}})(n))
- .on("mouseover",function(d) { var port = d3.select(this); port.classed("port_hovered",(mouse_mode!=RED.state.JOINING || mousedown_port_type != 1 ));})
- .on("mouseout",function(d) { var port = d3.select(this); port.classed("port_hovered",false);})
- }
- }
- d.inputlist = inputlist;
-
-
-
-
-
-
-
- node.append("image").attr("class","node_reqerror hidden").attr("xlink:href","icons/error.png").attr("x",0).attr("y",-12).attr("width",20).attr("height",20);
- });
-
- node.each(function(d,i) {
-
- checkRequirements(d);
-
- if (d.dirty || d.requirementError != undefined) {
-
- if (d.resize) {
-
-
- var l = d.name ? d.name : d.id;
- d.w = Math.max(node_width,calculateTextWidth(l)+(d._def.inputs>0?7:0) );
- d.h = Math.max(node_height,(Math.max(d.outputs,d._def.inputs)||0) * 15);
- }
- var thisNode = d3.select(this);
-
- thisNode.attr("transform", function(d) { return "translate(" + (d.x-d.w/2) + "," + (d.y-d.h/2) + ")"; });
- thisNode.selectAll(".node")
- .attr("width",function(d){return d.w})
- .attr("height",function(d){return d.h})
- .classed("node_selected",function(d) { return d.selected; })
- .classed("node_highlighted",function(d) { return d.highlighted; })
- ;
-
-
-
- thisNode.selectAll(".node_icon_group_right").attr('transform', function(d){return "translate("+(d.w-30)+",0)"});
- thisNode.selectAll(".node_label_right").attr('x', function(d){return d.w-38});
-
-
-
-
-
- var numOutputs = d.outputs;
- var y = (d.h/2)-((numOutputs-1)/2)*13;
- d.ports = d.ports || d3.range(numOutputs);
- d._ports = thisNode.selectAll(".port_output").data(d.ports);
- d._ports.enter().append("rect").attr("class","port port_output").attr("rx",3).attr("ry",3).attr("width",10).attr("height",10)
- .on("mousedown",(function(){var node = d; return function(d,i){portMouseDown(node,0,i);}})() )
- .on("touchstart",(function(){var node = d; return function(d,i){portMouseDown(node,0,i);}})() )
- .on("mouseup",(function(){var node = d; return function(d,i){portMouseUp(node,0,i);}})() )
- .on("touchend",(function(){var node = d; return function(d,i){portMouseUp(node,0,i);}})() )
- .on("mouseover",function(d,i) { var port = d3.select(this); port.classed("port_hovered",(mouse_mode!=RED.state.JOINING || mousedown_port_type !== 0 ));})
- .on("mouseout",function(d,i) { var port = d3.select(this); port.classed("port_hovered",false);});
- d._ports.exit().remove();
- if (d._ports) {
- numOutputs = d.outputs || 1;
- y = (d.h/2)-((numOutputs-1)/2)*13;
- var x = d.w - 5;
- d._ports.each(function(d,i) {
- var port = d3.select(this);
- port.attr("y",(y+13*i)-5).attr("x",x);
- });
- }
- thisNode.selectAll('text.node_label').text(function(d,i){
-
-
- return d.name ? d.name : d.id;
- })
- .attr('y', function(d){return (d.h/2)-1;})
- .attr('class',function(d){
- return 'node_label'+
- (d._def.align?' node_label_'+d._def.align:'')+
- (d._def.label?' '+(typeof d._def.labelStyle == "function" ? d._def.labelStyle.call(d):d._def.labelStyle):'') ;
- });
- thisNode.selectAll(".node_tools").attr("x",function(d){return d.w-35;}).attr("y",function(d){return d.h-20;});
-
- thisNode.selectAll(".node_changed")
- .attr("x",function(d){return d.w-10})
- .classed("hidden",function(d) { return !d.changed; });
-
- thisNode.selectAll(".node_error")
- .attr("x",function(d){return d.w-10-(d.changed?13:0)})
- .classed("hidden",function(d) { return d.valid; });
-
- thisNode.selectAll(".port_input").each(function(d,i) {
- var port = d3.select(this);
- var numInputs = d._def.inputs;
- var y = (d.h/2)-((numInputs-1)/2)*13;
- y = (y+13*i)-5;
- port.attr("y",y)
- });
-
-
- thisNode.selectAll(".node_reqerror")
- .attr("x",function(d){return d.w-25-(d.changed?13:0)})
- .classed("hidden",function(d){ return !d.requirementError; })
- .on("click", function(){RED.notify(
-
- 'Conflicts:<ul><li>'+d.conflicts.join('</li><li>')+'</li></ul>'
-
- )});
-
-
- thisNode.selectAll(".node_icon").attr("y",function(d){return (d.h-d3.select(this).attr("height"))/2;});
- thisNode.selectAll(".node_icon_shade").attr("height",function(d){return d.h;});
- thisNode.selectAll(".node_icon_shade_border").attr("d",function(d){ return "M "+(("right" == d._def.align) ?0:30)+" 1 l 0 "+(d.h-2)});
-
-
- thisNode.selectAll('.node_right_button').attr("transform",function(d){
- var x = d.w-6;
- if (d._def.button.toggle && !d[d._def.button.toggle]) {
- x = x - 8;
- }
- return "translate("+x+",2)";
- });
- thisNode.selectAll('.node_right_button rect').attr("fill-opacity",function(d){
- if (d._def.button.toggle) {
- return d[d._def.button.toggle]?1:0.2;
- }
- return 1;
- });
-
-
-
-
-
- thisNode.selectAll('.node_badge_group').attr("transform",function(d){return "translate("+(d.w-40)+","+(d.h+3)+")";});
- thisNode.selectAll('text.node_badge_label').text(function(d,i) {
-
-
- return d.name ? d.name : d.id;
- });
- if (!showStatus || !d.status) {
- thisNode.selectAll('.node_status_group').style("display","none");
- } else {
- thisNode.selectAll('.node_status_group').style("display","inline").attr("transform","translate(3,"+(d.h+3)+")");
- var fill = status_colours[d.status.fill];
- if (d.status.shape == null && fill == null) {
- thisNode.selectAll('.node_status').style("display","none");
- } else {
- var style;
- if (d.status.shape == null || d.status.shape == "dot") {
- style = {
- display: "inline",
- fill: fill,
- stroke: fill
- };
- } else if (d.status.shape == "ring" ){
- style = {
- display: "inline",
- fill: '#fff',
- stroke: fill
- }
- }
- thisNode.selectAll('.node_status').style(style);
- }
- if (d.status.text) {
- thisNode.selectAll('.node_status_label').text(d.status.text);
- } else {
- thisNode.selectAll('.node_status_label').text("");
- }
- }
-
- d.dirty = false;
- }
- });
-
- }
-
- var link = vis.selectAll(".link").data(RED.nodes.links.filter(function(d) { return d.source.z == activeWorkspace && d.target.z == activeWorkspace }),function(d) { return d.source.id+":"+d.sourcePort+":"+d.target.id+":"+d.targetPort;});
-
- var linkEnter = link.enter().insert("g",".node").attr("class","link");
-
- linkEnter.each(function(d,i) {
- var l = d3.select(this);
- l.append("svg:path").attr("class","link_background link_path")
- .on("mousedown",function(d) {
- mousedown_link = d;
- clearSelection();
- selected_link = mousedown_link;
- updateSelection();
- redraw();
- d3.event.stopPropagation();
- })
- .on("touchstart",function(d) {
- mousedown_link = d;
- clearSelection();
- selected_link = mousedown_link;
- updateSelection();
- redraw();
- d3.event.stopPropagation();
- });
- l.append("svg:path").attr("class","link_outline link_path");
- l.append("svg:path").attr("class","link_line link_path");
- });
-
- link.exit().remove();
-
- var links = vis.selectAll(".link_path");
- links.attr("d",function(d){
- var numOutputs = d.source.outputs || 1;
- var sourcePort = d.sourcePort || 0;
- var ysource = -((numOutputs-1)/2)*13 +13*sourcePort;
- var numInputs = d.target._def.inputs || 1;
- var targetPort = d.targetPort || 0;
- var ytarget = -((numInputs-1)/2)*13 +13*targetPort;
-
- var dy = (d.target.y+ytarget)-(d.source.y+ysource);
- var dx = (d.target.x-d.target.w/2)-(d.source.x+d.source.w/2);
- var delta = Math.sqrt(dy*dy+dx*dx);
- var scale = lineCurveScale;
- var scaleY = 0;
- if (delta < node_width) {
- scale = 0.75-0.75*((node_width-delta)/node_width);
- }
-
- if (dx < 0) {
- scale += 2*(Math.min(5*node_width,Math.abs(dx))/(5*node_width));
- if (Math.abs(dy) < 3*node_height) {
- scaleY = ((dy>0)?0.5:-0.5)*(((3*node_height)-Math.abs(dy))/(3*node_height))*(Math.min(node_width,Math.abs(dx))/(node_width)) ;
- }
- }
-
- d.x1 = d.source.x+d.source.w/2;
- d.y1 = d.source.y+ysource;
- d.x2 = d.target.x-d.target.w/2;
- d.y2 = d.target.y+ytarget;
-
- return "M "+(d.x1)+" "+(d.y1)+
- " C "+(d.x1+scale*node_width)+" "+(d.y1+scaleY*node_height)+" "+
- (d.x2-scale*node_width)+" "+(d.y2-scaleY*node_height)+" "+
- (d.x2)+" "+d.y2;
- });
-
- link.classed("link_selected", function(d) { return d === selected_link || d.selected; });
- link.classed("link_unknown",function(d) { return d.target.type == "unknown" || d.source.type == "unknown"});
-
- if (d3.event) {
- d3.event.preventDefault();
- }
- }
-
- function doSort (arr) {
- arr.sort(function (a, b) {
- var nameA = a.name ? a.name : a.id;
- var nameB = b.name ? b.name : b.id;
- return nameA.localeCompare(nameB, 'en', {numeric: 'true'});
- });
- }
-
- function setNewCoords (lastX, lastY, arr) {
- var x = lastX;
- var y = lastY;
- for (var i = 0; i < arr.length; i++) {
- var node = arr[i];
- var name = node.name ? node.name : node.id;
- var def = node._def;
- var dH = Math.max(RED.view.defaults.height, (Math.max(def.outputs, def.inputs) || 0) * 15);
- x = lastX + Math.max(RED.view.defaults.width, RED.view.calculateTextWidth(name) + (def.inputs > 0 ? 7 : 0));
- node.x = x;
- node.y = y + dH/2;
- y = y + dH + 15;
- node.dirty = true;
- }
- return { x: x, y: y };
- }
-
- function arrangeAll() {
- var ioNoIn = [];
- var ioInOut = [];
- var ioMultiple = [];
- var ioNoOut = [];
- var ioCtrl = [];
-
- RED.nodes.eachNode(function (node) {
-
- if (node._def.inputs == 0 && node._def.outputs == 0) {
- ioCtrl.push(node);
- } else if (node._def.inputs == 0) {
- ioNoIn.push(node);
- } else if (node._def.outputs == 0) {
- ioNoOut.push(node);
- } else if (node._def.inputs == 1 && node._def.outputs == 1) {
- ioInOut.push(node);
- } else if (node._def.inputs > 1) {
- ioMultiple.push(node);
- }
- });
-
- var cols = new Array(ioNoIn, ioInOut, ioMultiple, ioNoOut, ioCtrl);
- var lowestY = 0;
-
- for (var i = 0; i < cols.length; i++) {
- var dX = ((i < cols.length - 1) ? i : 0) * (RED.view.defaults.width * 2) + (RED.view.defaults.width / 2) + 15;
- var dY = ((i < cols.length - 1) ? (RED.view.defaults.height / 4) : lowestY) + 15;
- var startX = 0;
- var startY = 0;
-
- doSort(cols[i]);
- var last = setNewCoords(startX + dX, startY + dY, cols[i]);
- lowestY = Math.max(lowestY, last.y);
- startX = ((i < cols.length - 1) ? last.x : 0) + (RED.view.defaults.width) * 4;
- startY = lowestY + (RED.view.defaults.height * 1.5);
- }
- RED.storage.update();
- redraw();
- }
-
- RED.keyboard.add( 90,{ctrl:true},function(){RED.history.pop();});
- RED.keyboard.add( 79,{ctrl:true},function(){arrangeAll();d3.event.preventDefault();});
- RED.keyboard.add( 65,{ctrl:true},function(){selectAll();d3.event.preventDefault();});
- RED.keyboard.add( 187,{ctrl:true},function(){zoomIn();d3.event.preventDefault();});
- RED.keyboard.add( 189,{ctrl:true},function(){zoomOut();d3.event.preventDefault();});
- RED.keyboard.add( 48,{ctrl:true},function(){zoomZero();d3.event.preventDefault();});
- RED.keyboard.add( 86,{ctrl:true},function(){importNodes(clipboard);d3.event.preventDefault();});
- RED.keyboard.add( 69,{ctrl:true},function(){showExportNodesDialog();d3.event.preventDefault();});
- RED.keyboard.add( 73,{ctrl:true},function(){showImportNodesDialog();d3.event.preventDefault();});
-
-
- function setDirty(d) {
- dirty = d;
- if (dirty) {
- $("#btn-deploy").removeClass("disabled").addClass("btn-danger");
- RED.storage.update();
- } else {
- $("#btn-deploy").addClass("disabled").removeClass("btn-danger");
- }
- }
-
-
-
- function importNodes(newNodesStr,touchImport) {
-
- var createNewIds = true;
- var useStorage = false;
-
- if ($("#node-input-arduino").prop('checked') === true) {
- var nodesJSON = RED.nodes.cppToJSON(newNodesStr);
- if (nodesJSON.count <= 0) {
- var note = "No nodes imported!";
- RED.notify("<strong>Note</strong>: " + note, "warning");
- }
-
- newNodesStr = nodesJSON.data;
- createNewIds = false;
-
- if (useStorage) {
- RED.storage.clear();
- localStorage.setItem("audio_library_guitool", newNodesStr);
- RED.storage.load();
- redraw();
- return;
- }
- }
-
- try {
- var result = RED.nodes.import(newNodesStr,createNewIds);
- if (result) {
- var new_nodes = result[0];
- var new_links = result[1];
- var new_ms = new_nodes.map(function(n) { n.z = activeWorkspace; return {n:n};});
- var new_node_ids = new_nodes.map(function(n){ return n.id; });
-
-
- var root_node = new_ms[0].n;
- var dx = root_node.x;
- var dy = root_node.y;
-
- if (mouse_position == null) {
- mouse_position = [0,0];
- }
-
- var minX = 0;
- var minY = 0;
- var i;
- var node;
-
- for (i=0;i<new_ms.length;i++) {
- node = new_ms[i];
- node.n.selected = true;
- node.n.changed = true;
- node.n.x -= dx - mouse_position[0];
- node.n.y -= dy - mouse_position[1];
- node.dx = node.n.x - mouse_position[0];
- node.dy = node.n.y - mouse_position[1];
- minX = Math.min(node.n.x-node_width/2-5,minX);
- minY = Math.min(node.n.y-node_height/2-5,minY);
- }
- for (i=0;i<new_ms.length;i++) {
- node = new_ms[i];
- node.n.x -= minX;
- node.n.y -= minY;
- node.dx -= minX;
- node.dy -= minY;
- }
- if (!touchImport) {
- mouse_mode = RED.state.IMPORT_DRAGGING;
- }
-
- RED.keyboard.add( 27,function(){
- RED.keyboard.remove( 27);
- clearSelection();
- RED.history.pop();
- mouse_mode = 0;
- });
-
- RED.history.push({t:'add',nodes:new_node_ids,links:new_links,dirty:RED.view.dirty()});
-
- clearSelection();
- moving_set = new_ms;
-
- redraw();
- }
- } catch(error) {
- console.log(error);
- RED.notify("<strong>Error</strong>: "+error,"error");
- }
- }
-
- $('#btn-import').click(function() {showImportNodesDialog();});
- $('#btn-export-clipboard').click(function() {showExportNodesDialog();});
- $('#btn-export-library').click(function() {showExportNodesLibraryDialog();});
-
- function showExportNodesDialog() {
- mouse_mode = RED.state.EXPORT;
- var nns = RED.nodes.createExportableNodeSet(moving_set);
-
- var frm = getForm("dialog-form", "export-clipboard-dialog", function (d, f) {
- $("#node-input-export").val(JSON.stringify(nns)).focus(function() {
- var textarea = $(this);
- textarea.select();
- textarea.mouseup(function() {
- textarea.unbind("mouseup");
- return false;
- });
- }).focus();
- $( "#dialog" ).dialog("option","title","Export nodes to clipboard").dialog( "open" );
- });
- }
-
- function showExportNodesLibraryDialog() {
- mouse_mode = RED.state.EXPORT;
- var nns = RED.nodes.createExportableNodeSet(moving_set);
-
- getForm("dialog-form", "export-library-dialog", function(d, f) {
- $("#node-input-filename").attr('nodes',JSON.stringify(nns));
- $( "#dialog" ).dialog("option","title","Export nodes to library").dialog( "open" );
- });
- }
-
- function showImportNodesDialog() {
- mouse_mode = RED.state.IMPORT;
-
- getForm("dialog-form", "import-dialog", function(d, f) {
- $("#node-input-import").val("");
- $( "#dialog" ).dialog("option","title","Import nodes").dialog( "open" );
- });
- }
-
- function showRenameWorkspaceDialog(id) {
- var ws = RED.nodes.workspace(id);
- $( "#node-dialog-rename-workspace" ).dialog("option","workspace",ws);
-
- if (workspace_tabs.count() == 1) {
- $( "#node-dialog-rename-workspace").next().find(".leftButton")
- .prop('disabled',true)
- .addClass("ui-state-disabled");
- } else {
- $( "#node-dialog-rename-workspace").next().find(".leftButton")
- .prop('disabled',false)
- .removeClass("ui-state-disabled");
- }
-
- $( "#node-input-workspace-name" ).val(ws.label);
- $( "#node-dialog-rename-workspace" ).dialog("open");
- }
-
- function getForm(formId, key, callback) {
-
- var patt = new RegExp(/^[http|https]/);
- var server = false && patt.test(location.protocol);
- var form = $("<h2>No form found.</h2>");
-
- if (!server) {
- data = $("script[data-template-name|='" + key + "']").html();
- form = $("#" + formId);
- $(form).empty();
- $(form).append(data);
- if(typeof callback == 'function') {
- callback.call(this, form);
- }
- } else {
- var frmPlugin = "resources/form/" + key + ".html";
- $.get(frmPlugin, function(data) {
- form = $("#" + formId);
- $(form).empty();
- $(form).append(data);
- if(typeof callback == 'function') {
- callback.call(this, form);
- }
- });
- }
-
- return form;
- }
-
- $("#node-dialog-rename-workspace form" ).submit(function(e) { e.preventDefault();});
- $( "#node-dialog-rename-workspace" ).dialog({
- modal: true,
- autoOpen: false,
- width: 500,
- title: "Rename sheet",
- buttons: [
- {
- class: 'leftButton',
- text: "Delete",
- click: function() {
- var workspace = $(this).dialog('option','workspace');
- $( this ).dialog( "close" );
- deleteWorkspace(workspace.id);
- }
- },
- {
- text: "Ok",
- click: function() {
- var workspace = $(this).dialog('option','workspace');
- var label = $( "#node-input-workspace-name" ).val();
- if (workspace.label != label) {
- workspace.label = label;
- var link = $("#workspace-tabs a[href='#"+workspace.id+"']");
- link.attr("title",label);
- link.text(label);
- RED.view.dirty(true);
- }
- $( this ).dialog( "close" );
- }
- },
- {
- text: "Cancel",
- click: function() {
- $( this ).dialog( "close" );
- }
- }
- ],
- open: function(e) {
- RED.keyboard.disable();
- },
- close: function(e) {
- RED.keyboard.enable();
- }
- });
- $( "#node-dialog-delete-workspace" ).dialog({
- modal: true,
- autoOpen: false,
- width: 500,
- title: "Confirm delete",
- buttons: [
- {
- text: "Ok",
- click: function() {
- var workspace = $(this).dialog('option','workspace');
- RED.view.removeWorkspace(workspace);
- var historyEvent = RED.nodes.removeWorkspace(workspace.id);
- historyEvent.t = 'delete';
- historyEvent.dirty = dirty;
- historyEvent.workspaces = [workspace];
- RED.history.push(historyEvent);
- RED.view.dirty(true);
- $( this ).dialog( "close" );
- }
- },
- {
- text: "Cancel",
- click: function() {
- $( this ).dialog( "close" );
- }
- }
- ],
- open: function(e) {
- RED.keyboard.disable();
- },
- close: function(e) {
- RED.keyboard.enable();
- }
-
- });
-
- return {
- state:function(state) {
- if (state == null) {
- return mouse_mode
- } else {
- mouse_mode = state;
- }
- },
- addWorkspace: function(ws) {
- workspace_tabs.addTab(ws);
- workspace_tabs.resize();
- },
- removeWorkspace: function(ws) {
- workspace_tabs.removeTab(ws.id);
- },
- getWorkspace: function() {
- return activeWorkspace;
- },
- showWorkspace: function(id) {
- workspace_tabs.activateTab(id);
- },
- redraw: redraw,
- dirty: function(d) {
- if (d == null) {
- return dirty;
- } else {
- setDirty(d);
- }
- },
- importNodes: importNodes,
- resize: function() {
- workspace_tabs.resize();
- },
- status: function(s) {
- showStatus = s;
- RED.nodes.eachNode(function(n) { n.dirty = true;});
-
- redraw();
- },
- getForm: getForm,
- calculateTextWidth: calculateTextWidth,
- defaults: {
- width: node_width,
- height: node_height
- }
- };
- })();
|