Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

1718 rindas
56KB

  1. /** Modified from original Node-Red source, for audio system visualization
  2. * vim: set ts=4:
  3. * Copyright 2013, 2014 IBM Corp.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. **/
  17. RED.view = (function() {
  18. var space_width = 5000,
  19. space_height = 5000,
  20. lineCurveScale = 0.75,
  21. scaleFactor = 1,
  22. node_width = 100,
  23. node_height = 30;
  24. var touchLongPressTimeout = 1000,
  25. startTouchDistance = 0,
  26. startTouchCenter = [],
  27. moveTouchCenter = [],
  28. touchStartTime = 0;
  29. var activeWorkspace = 0;
  30. var workspaceScrollPositions = {};
  31. var selected_link = null,
  32. mousedown_link = null,
  33. mousedown_node = null,
  34. mousedown_port_type = null,
  35. mousedown_port_index = 0,
  36. mouseup_node = null,
  37. mouse_offset = [0,0],
  38. mouse_position = null,
  39. mouse_mode = 0,
  40. moving_set = [],
  41. dirty = false,
  42. lasso = null,
  43. showStatus = false,
  44. lastClickNode = null,
  45. dblClickPrimed = null,
  46. clickTime = 0,
  47. clickElapsed = 0;
  48. var clipboard = "";
  49. var status_colours = {
  50. "red": "#c00",
  51. "green": "#5a8",
  52. "yellow": "#F9DF31",
  53. "blue": "#53A3F3",
  54. "grey": "#d3d3d3"
  55. }
  56. var outer = d3.select("#chart")
  57. .append("svg:svg")
  58. .attr("width", space_width)
  59. .attr("height", space_height)
  60. .attr("pointer-events", "all")
  61. .style("cursor","crosshair");
  62. var vis = outer
  63. .append('svg:g')
  64. .on("dblclick.zoom", null)
  65. .append('svg:g')
  66. .on("mousemove", canvasMouseMove)
  67. .on("mousedown", canvasMouseDown)
  68. .on("mouseup", canvasMouseUp)
  69. .on("touchend", function() {
  70. clearTimeout(touchStartTime);
  71. touchStartTime = null;
  72. if (RED.touch.radialMenu.active()) {
  73. return;
  74. }
  75. if (lasso) {
  76. outer_background.attr("fill","#fff");
  77. }
  78. canvasMouseUp.call(this);
  79. })
  80. .on("touchcancel", canvasMouseUp)
  81. .on("touchstart", function() {
  82. var touch0;
  83. if (d3.event.touches.length>1) {
  84. clearTimeout(touchStartTime);
  85. touchStartTime = null;
  86. d3.event.preventDefault();
  87. touch0 = d3.event.touches.item(0);
  88. var touch1 = d3.event.touches.item(1);
  89. var a = touch0['pageY']-touch1['pageY'];
  90. var b = touch0['pageX']-touch1['pageX'];
  91. var offset = $("#chart").offset();
  92. var scrollPos = [$("#chart").scrollLeft(),$("#chart").scrollTop()];
  93. startTouchCenter = [
  94. (touch1['pageX']+(b/2)-offset.left+scrollPos[0])/scaleFactor,
  95. (touch1['pageY']+(a/2)-offset.top+scrollPos[1])/scaleFactor
  96. ];
  97. moveTouchCenter = [
  98. touch1['pageX']+(b/2),
  99. touch1['pageY']+(a/2)
  100. ]
  101. startTouchDistance = Math.sqrt((a*a)+(b*b));
  102. } else {
  103. var obj = d3.select(document.body);
  104. touch0 = d3.event.touches.item(0);
  105. var pos = [touch0.pageX,touch0.pageY];
  106. startTouchCenter = [touch0.pageX,touch0.pageY];
  107. startTouchDistance = 0;
  108. var point = d3.touches(this)[0];
  109. touchStartTime = setTimeout(function() {
  110. touchStartTime = null;
  111. showTouchMenu(obj,pos);
  112. //lasso = vis.append('rect')
  113. // .attr("ox",point[0])
  114. // .attr("oy",point[1])
  115. // .attr("rx",2)
  116. // .attr("ry",2)
  117. // .attr("x",point[0])
  118. // .attr("y",point[1])
  119. // .attr("width",0)
  120. // .attr("height",0)
  121. // .attr("class","lasso");
  122. //outer_background.attr("fill","#e3e3f3");
  123. },touchLongPressTimeout);
  124. }
  125. })
  126. .on("touchmove", function(){
  127. if (RED.touch.radialMenu.active()) {
  128. d3.event.preventDefault();
  129. return;
  130. }
  131. var touch0;
  132. if (d3.event.touches.length<2) {
  133. if (touchStartTime) {
  134. touch0 = d3.event.touches.item(0);
  135. var dx = (touch0.pageX-startTouchCenter[0]);
  136. var dy = (touch0.pageY-startTouchCenter[1]);
  137. var d = Math.abs(dx*dx+dy*dy);
  138. if (d > 64) {
  139. clearTimeout(touchStartTime);
  140. touchStartTime = null;
  141. }
  142. } else if (lasso) {
  143. d3.event.preventDefault();
  144. }
  145. canvasMouseMove.call(this);
  146. } else {
  147. touch0 = d3.event.touches.item(0);
  148. var touch1 = d3.event.touches.item(1);
  149. var a = touch0['pageY']-touch1['pageY'];
  150. var b = touch0['pageX']-touch1['pageX'];
  151. var offset = $("#chart").offset();
  152. var scrollPos = [$("#chart").scrollLeft(),$("#chart").scrollTop()];
  153. var moveTouchDistance = Math.sqrt((a*a)+(b*b));
  154. var touchCenter = [
  155. touch1['pageX']+(b/2),
  156. touch1['pageY']+(a/2)
  157. ];
  158. if (!isNaN(moveTouchDistance)) {
  159. oldScaleFactor = scaleFactor;
  160. scaleFactor = Math.min(2,Math.max(0.3, scaleFactor + (Math.floor(((moveTouchDistance*100)-(startTouchDistance*100)))/10000)));
  161. var deltaTouchCenter = [ // Try to pan whilst zooming - not 100%
  162. startTouchCenter[0]*(scaleFactor-oldScaleFactor),//-(touchCenter[0]-moveTouchCenter[0]),
  163. startTouchCenter[1]*(scaleFactor-oldScaleFactor) //-(touchCenter[1]-moveTouchCenter[1])
  164. ];
  165. startTouchDistance = moveTouchDistance;
  166. moveTouchCenter = touchCenter;
  167. $("#chart").scrollLeft(scrollPos[0]+deltaTouchCenter[0]);
  168. $("#chart").scrollTop(scrollPos[1]+deltaTouchCenter[1]);
  169. redraw();
  170. }
  171. }
  172. });
  173. var outer_background = vis.append('svg:rect')
  174. .attr('width', space_width)
  175. .attr('height', space_height)
  176. .attr('fill','#fff');
  177. //var gridScale = d3.scale.linear().range([0,2000]).domain([0,2000]);
  178. //var grid = vis.append('g');
  179. //
  180. //grid.selectAll("line.horizontal").data(gridScale.ticks(100)).enter()
  181. // .append("line")
  182. // .attr(
  183. // {
  184. // "class":"horizontal",
  185. // "x1" : 0,
  186. // "x2" : 2000,
  187. // "y1" : function(d){ return gridScale(d);},
  188. // "y2" : function(d){ return gridScale(d);},
  189. // "fill" : "none",
  190. // "shape-rendering" : "crispEdges",
  191. // "stroke" : "#eee",
  192. // "stroke-width" : "1px"
  193. // });
  194. //grid.selectAll("line.vertical").data(gridScale.ticks(100)).enter()
  195. // .append("line")
  196. // .attr(
  197. // {
  198. // "class":"vertical",
  199. // "y1" : 0,
  200. // "y2" : 2000,
  201. // "x1" : function(d){ return gridScale(d);},
  202. // "x2" : function(d){ return gridScale(d);},
  203. // "fill" : "none",
  204. // "shape-rendering" : "crispEdges",
  205. // "stroke" : "#eee",
  206. // "stroke-width" : "1px"
  207. // });
  208. var drag_line = vis.append("svg:path").attr("class", "drag_line");
  209. var workspace_tabs = RED.tabs.create({
  210. id: "workspace-tabs",
  211. onchange: function(tab) {
  212. if (tab.type == "subflow") {
  213. $("#workspace-toolbar").show();
  214. } else {
  215. $("#workspace-toolbar").hide();
  216. }
  217. var chart = $("#chart");
  218. if (activeWorkspace !== 0) {
  219. workspaceScrollPositions[activeWorkspace] = {
  220. left:chart.scrollLeft(),
  221. top:chart.scrollTop()
  222. };
  223. }
  224. var scrollStartLeft = chart.scrollLeft();
  225. var scrollStartTop = chart.scrollTop();
  226. activeWorkspace = tab.id;
  227. if (workspaceScrollPositions[activeWorkspace]) {
  228. chart.scrollLeft(workspaceScrollPositions[activeWorkspace].left);
  229. chart.scrollTop(workspaceScrollPositions[activeWorkspace].top);
  230. } else {
  231. chart.scrollLeft(0);
  232. chart.scrollTop(0);
  233. }
  234. var scrollDeltaLeft = chart.scrollLeft() - scrollStartLeft;
  235. var scrollDeltaTop = chart.scrollTop() - scrollStartTop;
  236. if (mouse_position != null) {
  237. mouse_position[0] += scrollDeltaLeft;
  238. mouse_position[1] += scrollDeltaTop;
  239. }
  240. clearSelection();
  241. RED.nodes.eachNode(function(n) {
  242. n.dirty = true;
  243. });
  244. redraw();
  245. },
  246. ondblclick: function(tab) {
  247. showRenameWorkspaceDialog(tab.id);
  248. },
  249. onadd: function(tab) {
  250. var menuli = $("<li/>");
  251. var menuA = $("<a/>",{tabindex:"-1",href:"#"+tab.id}).appendTo(menuli);
  252. menuA.html(tab.label);
  253. menuA.on("click",function() {
  254. workspace_tabs.activateTab(tab.id);
  255. });
  256. $('#workspace-menu-list').append(menuli);
  257. if (workspace_tabs.count() == 1) {
  258. $('#btn-workspace-delete').parent().addClass("disabled");
  259. } else {
  260. $('#btn-workspace-delete').parent().removeClass("disabled");
  261. }
  262. },
  263. onremove: function(tab) {
  264. if (workspace_tabs.count() == 1) {
  265. $('#btn-workspace-delete').parent().addClass("disabled");
  266. } else {
  267. $('#btn-workspace-delete').parent().removeClass("disabled");
  268. }
  269. $('#workspace-menu-list a[href="#'+tab.id+'"]').parent().remove();
  270. }
  271. });
  272. var workspaceIndex = 0;
  273. function addWorkspace() {
  274. var tabId = RED.nodes.id();
  275. do {
  276. workspaceIndex += 1;
  277. } while($("#workspace-tabs a[title='Sheet "+workspaceIndex+"']").size() !== 0);
  278. var ws = {type:"tab",id:tabId,label:"Sheet "+workspaceIndex};
  279. RED.nodes.addWorkspace(ws);
  280. workspace_tabs.addTab(ws);
  281. workspace_tabs.activateTab(tabId);
  282. RED.history.push({t:'add',workspaces:[ws],dirty:dirty});
  283. RED.view.dirty(true);
  284. }
  285. $('#btn-workspace-add-tab').on("click",addWorkspace);
  286. $('#btn-workspace-add').on("click",addWorkspace);
  287. $('#btn-workspace-edit').on("click",function() {
  288. showRenameWorkspaceDialog(activeWorkspace);
  289. });
  290. $('#btn-workspace-delete').on("click",function() {
  291. deleteWorkspace(activeWorkspace);
  292. });
  293. function deleteWorkspace(id) {
  294. if (workspace_tabs.count() == 1) {
  295. return;
  296. }
  297. var ws = RED.nodes.workspace(id);
  298. $( "#node-dialog-delete-workspace" ).dialog('option','workspace',ws);
  299. $( "#node-dialog-delete-workspace-name" ).text(ws.label);
  300. $( "#node-dialog-delete-workspace" ).dialog('open');
  301. }
  302. function canvasMouseDown() {
  303. if (!mousedown_node && !mousedown_link) {
  304. selected_link = null;
  305. updateSelection();
  306. }
  307. if (mouse_mode === 0) {
  308. if (lasso) {
  309. lasso.remove();
  310. lasso = null;
  311. }
  312. if (!touchStartTime) {
  313. var point = d3.mouse(this);
  314. lasso = vis.append('rect')
  315. .attr("ox",point[0])
  316. .attr("oy",point[1])
  317. .attr("rx",2)
  318. .attr("ry",2)
  319. .attr("x",point[0])
  320. .attr("y",point[1])
  321. .attr("width",0)
  322. .attr("height",0)
  323. .attr("class","lasso");
  324. d3.event.preventDefault();
  325. }
  326. }
  327. }
  328. function canvasMouseMove() {
  329. mouse_position = d3.touches(this)[0]||d3.mouse(this);
  330. // Prevent touch scrolling...
  331. //if (d3.touches(this)[0]) {
  332. // d3.event.preventDefault();
  333. //}
  334. // TODO: auto scroll the container
  335. //var point = d3.mouse(this);
  336. //if (point[0]-container.scrollLeft < 30 && container.scrollLeft > 0) { container.scrollLeft -= 15; }
  337. //console.log(d3.mouse(this),container.offsetWidth,container.offsetHeight,container.scrollLeft,container.scrollTop);
  338. if (lasso) {
  339. var ox = parseInt(lasso.attr("ox"));
  340. var oy = parseInt(lasso.attr("oy"));
  341. var x = parseInt(lasso.attr("x"));
  342. var y = parseInt(lasso.attr("y"));
  343. var w;
  344. var h;
  345. if (mouse_position[0] < ox) {
  346. x = mouse_position[0];
  347. w = ox-x;
  348. } else {
  349. w = mouse_position[0]-x;
  350. }
  351. if (mouse_position[1] < oy) {
  352. y = mouse_position[1];
  353. h = oy-y;
  354. } else {
  355. h = mouse_position[1]-y;
  356. }
  357. lasso
  358. .attr("x",x)
  359. .attr("y",y)
  360. .attr("width",w)
  361. .attr("height",h)
  362. ;
  363. return;
  364. }
  365. if (mouse_mode != RED.state.IMPORT_DRAGGING && !mousedown_node && selected_link == null) {
  366. return;
  367. }
  368. var mousePos;
  369. if (mouse_mode == RED.state.JOINING) {
  370. // update drag line
  371. drag_line.attr("class", "drag_line");
  372. mousePos = mouse_position;
  373. var numOutputs = (mousedown_port_type === 0)?(mousedown_node.outputs || 1):(mousedown_node._def.inputs || 1);
  374. var sourcePort = mousedown_port_index;
  375. var portY = -((numOutputs-1)/2)*13 +13*sourcePort;
  376. var sc = (mousedown_port_type === 0)?1:-1;
  377. var dy = mousePos[1]-(mousedown_node.y+portY);
  378. var dx = mousePos[0]-(mousedown_node.x+sc*mousedown_node.w/2);
  379. var delta = Math.sqrt(dy*dy+dx*dx);
  380. var scale = lineCurveScale;
  381. var scaleY = 0;
  382. if (delta < node_width) {
  383. scale = 0.75-0.75*((node_width-delta)/node_width);
  384. }
  385. if (dx*sc < 0) {
  386. scale += 2*(Math.min(5*node_width,Math.abs(dx))/(5*node_width));
  387. if (Math.abs(dy) < 3*node_height) {
  388. 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)) ;
  389. }
  390. }
  391. drag_line.attr("d",
  392. "M "+(mousedown_node.x+sc*mousedown_node.w/2)+" "+(mousedown_node.y+portY)+
  393. " C "+(mousedown_node.x+sc*(mousedown_node.w/2+node_width*scale))+" "+(mousedown_node.y+portY+scaleY*node_height)+" "+
  394. (mousePos[0]-sc*(scale)*node_width)+" "+(mousePos[1]-scaleY*node_height)+" "+
  395. mousePos[0]+" "+mousePos[1]
  396. );
  397. d3.event.preventDefault();
  398. } else if (mouse_mode == RED.state.MOVING) {
  399. mousePos = mouse_position;
  400. var d = (mouse_offset[0]-mousePos[0])*(mouse_offset[0]-mousePos[0]) + (mouse_offset[1]-mousePos[1])*(mouse_offset[1]-mousePos[1]);
  401. if (d > 2) {
  402. mouse_mode = RED.state.MOVING_ACTIVE;
  403. clickElapsed = 0;
  404. }
  405. } else if (mouse_mode == RED.state.MOVING_ACTIVE || mouse_mode == RED.state.IMPORT_DRAGGING) {
  406. mousePos = mouse_position;
  407. var node;
  408. var i;
  409. var minX = 0;
  410. var minY = 0;
  411. for (var n = 0; n<moving_set.length; n++) {
  412. node = moving_set[n];
  413. if (d3.event.shiftKey) {
  414. node.n.ox = node.n.x;
  415. node.n.oy = node.n.y;
  416. }
  417. node.n.x = mousePos[0]+node.dx;
  418. node.n.y = mousePos[1]+node.dy;
  419. node.n.dirty = true;
  420. minX = Math.min(node.n.x-node.n.w/2-5,minX);
  421. minY = Math.min(node.n.y-node.n.h/2-5,minY);
  422. }
  423. if (minX !== 0 || minY !== 0) {
  424. for (i = 0; i<moving_set.length; i++) {
  425. node = moving_set[i];
  426. node.n.x -= minX;
  427. node.n.y -= minY;
  428. }
  429. }
  430. if (d3.event.shiftKey && moving_set.length > 0) {
  431. var gridOffset = [0,0];
  432. node = moving_set[0];
  433. gridOffset[0] = node.n.x-(20*Math.floor((node.n.x-node.n.w/2)/20)+node.n.w/2);
  434. gridOffset[1] = node.n.y-(20*Math.floor(node.n.y/20));
  435. if (gridOffset[0] !== 0 || gridOffset[1] !== 0) {
  436. for (i = 0; i<moving_set.length; i++) {
  437. node = moving_set[i];
  438. node.n.x -= gridOffset[0];
  439. node.n.y -= gridOffset[1];
  440. if (node.n.x == node.n.ox && node.n.y == node.n.oy) {
  441. node.dirty = false;
  442. }
  443. }
  444. }
  445. }
  446. }
  447. redraw();
  448. }
  449. function canvasMouseUp() {
  450. if (mousedown_node && mouse_mode == RED.state.JOINING) {
  451. drag_line.attr("class", "drag_line_hidden");
  452. }
  453. if (lasso) {
  454. var x = parseInt(lasso.attr("x"));
  455. var y = parseInt(lasso.attr("y"));
  456. var x2 = x+parseInt(lasso.attr("width"));
  457. var y2 = y+parseInt(lasso.attr("height"));
  458. if (!d3.event.ctrlKey) {
  459. clearSelection();
  460. }
  461. RED.nodes.eachNode(function(n) {
  462. if (n.z == activeWorkspace && !n.selected) {
  463. n.selected = (n.x > x && n.x < x2 && n.y > y && n.y < y2);
  464. if (n.selected) {
  465. n.dirty = true;
  466. moving_set.push({n:n});
  467. }
  468. }
  469. });
  470. updateSelection();
  471. lasso.remove();
  472. lasso = null;
  473. } else if (mouse_mode == RED.state.DEFAULT && mousedown_link == null) {
  474. clearSelection();
  475. updateSelection();
  476. }
  477. if (mouse_mode == RED.state.MOVING_ACTIVE) {
  478. if (moving_set.length > 0) {
  479. var ns = [];
  480. for (var j=0;j<moving_set.length;j++) {
  481. ns.push({n:moving_set[j].n,ox:moving_set[j].ox,oy:moving_set[j].oy});
  482. }
  483. RED.history.push({t:'move',nodes:ns,dirty:dirty});
  484. RED.storage.update();
  485. }
  486. }
  487. if (mouse_mode == RED.state.MOVING || mouse_mode == RED.state.MOVING_ACTIVE) {
  488. for (var i=0;i<moving_set.length;i++) {
  489. delete moving_set[i].ox;
  490. delete moving_set[i].oy;
  491. }
  492. }
  493. if (mouse_mode == RED.state.IMPORT_DRAGGING) {
  494. RED.keyboard.remove(/* ESCAPE */ 27);
  495. setDirty(true);
  496. }
  497. redraw();
  498. // clear mouse event vars
  499. resetMouseVars();
  500. }
  501. $('#btn-zoom-out').click(function() {zoomOut();});
  502. $('#btn-zoom-zero').click(function() {zoomZero();});
  503. $('#btn-zoom-in').click(function() {zoomIn();});
  504. $("#chart").on('DOMMouseScroll mousewheel', function (evt) {
  505. if ( evt.altKey ) {
  506. evt.preventDefault();
  507. evt.stopPropagation();
  508. var move = -(evt.originalEvent.detail) || evt.originalEvent.wheelDelta;
  509. if (move <= 0) { zoomOut(); }
  510. else { zoomIn(); }
  511. }
  512. });
  513. $("#chart").droppable({
  514. accept:".palette_node",
  515. drop: function( event, ui ) {
  516. d3.event = event;
  517. var selected_tool = ui.draggable[0].type;
  518. var mousePos = d3.touches(this)[0]||d3.mouse(this);
  519. mousePos[1] += this.scrollTop;
  520. mousePos[0] += this.scrollLeft;
  521. mousePos[1] /= scaleFactor;
  522. mousePos[0] /= scaleFactor;
  523. var nn = {x: mousePos[0],y:mousePos[1],w:node_width,z:activeWorkspace};
  524. nn.type = selected_tool;
  525. nn._def = RED.nodes.getType(nn.type);
  526. nn.id = RED.nodes.cppName(nn);
  527. nn.outputs = nn._def.outputs;
  528. nn.changed = true;
  529. for (var d in nn._def.defaults) {
  530. if (nn._def.defaults.hasOwnProperty(d)) {
  531. nn[d] = nn._def.defaults[d].value;
  532. }
  533. }
  534. if (nn._def.onadd) {
  535. nn._def.onadd.call(nn);
  536. }
  537. nn.h = Math.max(node_height,(nn.outputs||0) * 15);
  538. RED.history.push({t:'add',nodes:[nn.id],dirty:dirty});
  539. RED.nodes.add(nn);
  540. RED.editor.validateNode(nn);
  541. setDirty(true);
  542. // auto select dropped node - so info shows (if visible)
  543. clearSelection();
  544. nn.selected = true;
  545. moving_set.push({n:nn});
  546. updateSelection();
  547. redraw();
  548. if (nn._def.autoedit) {
  549. RED.editor.edit(nn);
  550. }
  551. }
  552. });
  553. function zoomIn() {
  554. if (scaleFactor < 2) {
  555. scaleFactor += 0.1;
  556. redraw();
  557. }
  558. }
  559. function zoomOut() {
  560. if (scaleFactor > 0.3) {
  561. scaleFactor -= 0.1;
  562. redraw();
  563. }
  564. }
  565. function zoomZero() {
  566. scaleFactor = 1;
  567. redraw();
  568. }
  569. function selectAll() {
  570. RED.nodes.eachNode(function(n) {
  571. if (n.z == activeWorkspace) {
  572. if (!n.selected) {
  573. n.selected = true;
  574. n.dirty = true;
  575. moving_set.push({n:n});
  576. }
  577. }
  578. });
  579. selected_link = null;
  580. updateSelection();
  581. redraw();
  582. }
  583. function clearSelection() {
  584. for (var i=0;i<moving_set.length;i++) {
  585. var n = moving_set[i];
  586. n.n.dirty = true;
  587. n.n.selected = false;
  588. }
  589. moving_set = [];
  590. selected_link = null;
  591. }
  592. function updateSelection() {
  593. if (moving_set.length === 0) {
  594. $("#li-menu-export").addClass("disabled");
  595. $("#li-menu-export-clipboard").addClass("disabled");
  596. $("#li-menu-export-library").addClass("disabled");
  597. } else {
  598. $("#li-menu-export").removeClass("disabled");
  599. $("#li-menu-export-clipboard").removeClass("disabled");
  600. $("#li-menu-export-library").removeClass("disabled");
  601. }
  602. if (moving_set.length === 0 && selected_link == null) {
  603. RED.keyboard.remove(/* backspace */ 8);
  604. RED.keyboard.remove(/* delete */ 46);
  605. RED.keyboard.remove(/* c */ 67);
  606. RED.keyboard.remove(/* x */ 88);
  607. } else {
  608. RED.keyboard.add(/* backspace */ 8,function(){deleteSelection();d3.event.preventDefault();});
  609. RED.keyboard.add(/* delete */ 46,function(){deleteSelection();d3.event.preventDefault();});
  610. RED.keyboard.add(/* c */ 67,{ctrl:true},function(){copySelection();d3.event.preventDefault();});
  611. RED.keyboard.add(/* x */ 88,{ctrl:true},function(){copySelection();deleteSelection();d3.event.preventDefault();});
  612. }
  613. if (moving_set.length === 0) {
  614. RED.keyboard.remove(/* up */ 38);
  615. RED.keyboard.remove(/* down */ 40);
  616. RED.keyboard.remove(/* left */ 37);
  617. RED.keyboard.remove(/* right*/ 39);
  618. } else {
  619. RED.keyboard.add(/* up */ 38, function() { if(d3.event.shiftKey){moveSelection( 0,-20)}else{moveSelection( 0,-1);}d3.event.preventDefault();},endKeyboardMove);
  620. RED.keyboard.add(/* down */ 40, function() { if(d3.event.shiftKey){moveSelection( 0, 20)}else{moveSelection( 0, 1);}d3.event.preventDefault();},endKeyboardMove);
  621. RED.keyboard.add(/* left */ 37, function() { if(d3.event.shiftKey){moveSelection(-20, 0)}else{moveSelection(-1, 0);}d3.event.preventDefault();},endKeyboardMove);
  622. RED.keyboard.add(/* right*/ 39, function() { if(d3.event.shiftKey){moveSelection( 20, 0)}else{moveSelection( 1, 0);}d3.event.preventDefault();},endKeyboardMove);
  623. }
  624. if (moving_set.length == 1) {
  625. RED.sidebar.info.refresh(moving_set[0].n);
  626. RED.nodes.selectNode(moving_set[0].n.type);
  627. } else {
  628. RED.sidebar.info.clear();
  629. }
  630. }
  631. function endKeyboardMove() {
  632. var ns = [];
  633. for (var i=0;i<moving_set.length;i++) {
  634. ns.push({n:moving_set[i].n,ox:moving_set[i].ox,oy:moving_set[i].oy});
  635. delete moving_set[i].ox;
  636. delete moving_set[i].oy;
  637. }
  638. RED.history.push({t:'move',nodes:ns,dirty:dirty});
  639. }
  640. function moveSelection(dx,dy) {
  641. var minX = 0;
  642. var minY = 0;
  643. var node;
  644. for (var i=0;i<moving_set.length;i++) {
  645. node = moving_set[i];
  646. if (node.ox == null && node.oy == null) {
  647. node.ox = node.n.x;
  648. node.oy = node.n.y;
  649. }
  650. node.n.x += dx;
  651. node.n.y += dy;
  652. node.n.dirty = true;
  653. minX = Math.min(node.n.x-node.n.w/2-5,minX);
  654. minY = Math.min(node.n.y-node.n.h/2-5,minY);
  655. }
  656. if (minX !== 0 || minY !== 0) {
  657. for (var n = 0; n<moving_set.length; n++) {
  658. node = moving_set[n];
  659. node.n.x -= minX;
  660. node.n.y -= minY;
  661. }
  662. }
  663. redraw();
  664. }
  665. function deleteSelection() {
  666. var removedNodes = [];
  667. var removedLinks = [];
  668. var startDirty = dirty;
  669. if (moving_set.length > 0) {
  670. for (var i=0;i<moving_set.length;i++) {
  671. var node = moving_set[i].n;
  672. node.selected = false;
  673. if (node.x < 0) {
  674. node.x = 25
  675. }
  676. var rmlinks = RED.nodes.remove(node.id);
  677. for (var i=0; i < rmlinks.length; i++) {
  678. var link = rmlinks[i];
  679. //console.log("delete link: " + link.source.id + ":" + link.sourcePort
  680. // + " -> " + link.target.id + ":" + link.targetPort);
  681. if (link.source == node) {
  682. // reenable input port
  683. var n = link.targetPort;
  684. var rect = link.target.inputlist[n];
  685. rect.on("mousedown", (function(d,n){return function(d){portMouseDown(d,1,n);}})(rect, n))
  686. .on("touchstart", (function(d,n){return function(d){portMouseDown(d,1,n);}})(rect, n))
  687. .on("mouseup", (function(d,n){return function(d){portMouseUp(d,1,n);}})(rect, n))
  688. .on("touchend", (function(d,n){return function(d){portMouseUp(d,1,n);}})(rect, n))
  689. .on("mouseover",function(d) { var port = d3.select(this); port.classed("port_hovered",(mouse_mode!=RED.state.JOINING || mousedown_port_type != 1 ));})
  690. .on("mouseout",function(d) { var port = d3.select(this); port.classed("port_hovered",false);})
  691. }
  692. }
  693. removedNodes.push(node);
  694. removedLinks = removedLinks.concat(rmlinks);
  695. }
  696. moving_set = [];
  697. setDirty(true);
  698. }
  699. if (selected_link) {
  700. // reenable input port
  701. var n = selected_link.targetPort;
  702. var rect = selected_link.target.inputlist[n];
  703. rect.on("mousedown", (function(d,n){return function(d){portMouseDown(d,1,n);}})(rect, n))
  704. .on("touchstart", (function(d,n){return function(d){portMouseDown(d,1,n);}})(rect, n))
  705. .on("mouseup", (function(d,n){return function(d){portMouseUp(d,1,n);}})(rect, n))
  706. .on("touchend", (function(d,n){return function(d){portMouseUp(d,1,n);}})(rect, n))
  707. .on("mouseover",function(d) { var port = d3.select(this); port.classed("port_hovered",(mouse_mode!=RED.state.JOINING || mousedown_port_type != 1 ));})
  708. .on("mouseout",function(d) { var port = d3.select(this); port.classed("port_hovered",false);})
  709. RED.nodes.removeLink(selected_link);
  710. removedLinks.push(selected_link);
  711. setDirty(true);
  712. }
  713. RED.history.push({t:'delete',nodes:removedNodes,links:removedLinks,dirty:startDirty});
  714. selected_link = null;
  715. updateSelection();
  716. redraw();
  717. }
  718. function copySelection() {
  719. if (moving_set.length > 0) {
  720. var nns = [];
  721. for (var n=0;n<moving_set.length;n++) {
  722. var node = moving_set[n].n;
  723. nns.push(RED.nodes.convertNode(node));
  724. }
  725. clipboard = JSON.stringify(nns);
  726. RED.notify(moving_set.length+" node"+(moving_set.length>1?"s":"")+" copied");
  727. }
  728. }
  729. function calculateTextWidth(str) {
  730. var sp = document.createElement("span");
  731. sp.className = "node_label";
  732. sp.style.position = "absolute";
  733. sp.style.top = "-1000px";
  734. sp.innerHTML = (str||"").replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
  735. document.body.appendChild(sp);
  736. var w = sp.offsetWidth;
  737. document.body.removeChild(sp);
  738. return 50+w;
  739. }
  740. function resetMouseVars() {
  741. mousedown_node = null;
  742. mouseup_node = null;
  743. mousedown_link = null;
  744. mouse_mode = 0;
  745. mousedown_port_type = 0;
  746. }
  747. function portMouseDown(d,portType,portIndex) {
  748. // disable zoom
  749. //vis.call(d3.behavior.zoom().on("zoom"), null);
  750. mousedown_node = d;
  751. selected_link = null;
  752. mouse_mode = RED.state.JOINING;
  753. mousedown_port_type = portType;
  754. mousedown_port_index = portIndex || 0;
  755. document.body.style.cursor = "crosshair";
  756. d3.event.preventDefault();
  757. }
  758. function portMouseUp(d,portType,portIndex) {
  759. document.body.style.cursor = "";
  760. if (mouse_mode == RED.state.JOINING && mousedown_node) {
  761. if (typeof TouchEvent != "undefined" && d3.event instanceof TouchEvent) {
  762. RED.nodes.eachNode(function(n) {
  763. if (n.z == activeWorkspace) {
  764. var hw = n.w/2;
  765. var hh = n.h/2;
  766. if (n.x-hw<mouse_position[0] && n.x+hw> mouse_position[0] &&
  767. n.y-hh<mouse_position[1] && n.y+hh>mouse_position[1]) {
  768. mouseup_node = n;
  769. portType = mouseup_node._def.inputs>0?1:0;
  770. portIndex = 0;
  771. }
  772. }
  773. });
  774. } else {
  775. mouseup_node = d;
  776. }
  777. if (portType == mousedown_port_type || mouseup_node === mousedown_node) {
  778. drag_line.attr("class", "drag_line_hidden");
  779. resetMouseVars();
  780. return;
  781. }
  782. var src,dst,src_port,dst_port;
  783. if (mousedown_port_type === 0) {
  784. src = mousedown_node;
  785. src_port = mousedown_port_index;
  786. dst = mouseup_node;
  787. dst_port = portIndex;
  788. } else if (mousedown_port_type == 1) {
  789. src = mouseup_node;
  790. src_port = portIndex;
  791. dst = mousedown_node;
  792. dst_port = mousedown_port_index;
  793. }
  794. var existingLink = false;
  795. RED.nodes.eachLink(function(d) {
  796. existingLink = existingLink || (d.source === src && d.target === dst && d.sourcePort == src_port && d.targetPort == dst_port);
  797. });
  798. if (!existingLink) {
  799. var link = {source: src, sourcePort:src_port, target: dst, targetPort: dst_port};
  800. RED.nodes.addLink(link);
  801. RED.history.push({t:'add',links:[link],dirty:dirty});
  802. setDirty(true);
  803. // disallow new links to this destination - each input can have only a single link
  804. dst.inputlist[dst_port]
  805. .classed("port_hovered",false)
  806. .on("mousedown",null)
  807. .on("touchstart", null)
  808. .on("mouseup", null)
  809. .on("touchend", null)
  810. .on("mouseover", null)
  811. .on("mouseout", null);
  812. }
  813. selected_link = null;
  814. redraw();
  815. }
  816. }
  817. function nodeMouseUp(d) {
  818. if (dblClickPrimed && mousedown_node == d && clickElapsed > 0 && clickElapsed < 750) {
  819. RED.editor.edit(d);
  820. clickElapsed = 0;
  821. d3.event.stopPropagation();
  822. return;
  823. }
  824. portMouseUp(d, d._def.inputs > 0 ? 1 : 0, 0);
  825. }
  826. function nodeMouseDown(d) {
  827. //var touch0 = d3.event;
  828. //var pos = [touch0.pageX,touch0.pageY];
  829. //RED.touch.radialMenu.show(d3.select(this),pos);
  830. if (mouse_mode == RED.state.IMPORT_DRAGGING) {
  831. RED.keyboard.remove(/* ESCAPE */ 27);
  832. updateSelection();
  833. setDirty(true);
  834. redraw();
  835. resetMouseVars();
  836. d3.event.stopPropagation();
  837. return;
  838. }
  839. mousedown_node = d;
  840. var now = Date.now();
  841. clickElapsed = now-clickTime;
  842. clickTime = now;
  843. dblClickPrimed = (lastClickNode == mousedown_node);
  844. lastClickNode = mousedown_node;
  845. var i;
  846. if (d.selected && d3.event.ctrlKey) {
  847. d.selected = false;
  848. for (i=0;i<moving_set.length;i+=1) {
  849. if (moving_set[i].n === d) {
  850. moving_set.splice(i,1);
  851. break;
  852. }
  853. }
  854. } else {
  855. if (d3.event.shiftKey) {
  856. clearSelection();
  857. var cnodes = RED.nodes.getAllFlowNodes(mousedown_node);
  858. for (var n=0;n<cnodes.length;n++) {
  859. cnodes[n].selected = true;
  860. cnodes[n].dirty = true;
  861. moving_set.push({n:cnodes[n]});
  862. }
  863. } else if (!d.selected) {
  864. if (!d3.event.ctrlKey) {
  865. clearSelection();
  866. }
  867. mousedown_node.selected = true;
  868. moving_set.push({n:mousedown_node});
  869. }
  870. selected_link = null;
  871. if (d3.event.button != 2) {
  872. mouse_mode = RED.state.MOVING;
  873. var mouse = d3.touches(this)[0]||d3.mouse(this);
  874. mouse[0] += d.x-d.w/2;
  875. mouse[1] += d.y-d.h/2;
  876. for (i=0;i<moving_set.length;i++) {
  877. moving_set[i].ox = moving_set[i].n.x;
  878. moving_set[i].oy = moving_set[i].n.y;
  879. moving_set[i].dx = moving_set[i].n.x-mouse[0];
  880. moving_set[i].dy = moving_set[i].n.y-mouse[1];
  881. }
  882. mouse_offset = d3.mouse(document.body);
  883. if (isNaN(mouse_offset[0])) {
  884. mouse_offset = d3.touches(document.body)[0];
  885. }
  886. }
  887. }
  888. d.dirty = true;
  889. updateSelection();
  890. redraw();
  891. d3.event.stopPropagation();
  892. }
  893. function nodeButtonClicked(d) {
  894. if (d._def.button.toggle) {
  895. d[d._def.button.toggle] = !d[d._def.button.toggle];
  896. d.dirty = true;
  897. }
  898. if (d._def.button.onclick) {
  899. d._def.button.onclick.call(d);
  900. }
  901. if (d.dirty) {
  902. redraw();
  903. }
  904. d3.event.preventDefault();
  905. }
  906. function showTouchMenu(obj,pos) {
  907. var mdn = mousedown_node;
  908. var options = [];
  909. options.push({name:"delete",disabled:(moving_set.length===0),onselect:function() {deleteSelection();}});
  910. options.push({name:"cut",disabled:(moving_set.length===0),onselect:function() {copySelection();deleteSelection();}});
  911. options.push({name:"copy",disabled:(moving_set.length===0),onselect:function() {copySelection();}});
  912. options.push({name:"paste",disabled:(clipboard.length===0),onselect:function() {importNodes(clipboard,true);}});
  913. options.push({name:"edit",disabled:(moving_set.length != 1),onselect:function() { RED.editor.edit(mdn);}});
  914. options.push({name:"select",onselect:function() {selectAll();}});
  915. options.push({name:"undo",disabled:(RED.history.depth() === 0),onselect:function() {RED.history.pop();}});
  916. RED.touch.radialMenu.show(obj,pos,options);
  917. resetMouseVars();
  918. }
  919. function redraw() {
  920. vis.attr("transform","scale("+scaleFactor+")");
  921. outer.attr("width", space_width*scaleFactor).attr("height", space_height*scaleFactor);
  922. if (mouse_mode != RED.state.JOINING) {
  923. // Don't bother redrawing nodes if we're drawing links
  924. var node = vis.selectAll(".nodegroup").data(RED.nodes.nodes.filter(function(d) { return d.z == activeWorkspace }),function(d){return d.id});
  925. node.exit().remove();
  926. var nodeEnter = node.enter().insert("svg:g").attr("class", "node nodegroup");
  927. nodeEnter.each(function(d,i) {
  928. var node = d3.select(this);
  929. node.attr("id",d.id);
  930. //var l = d._def.label;
  931. //l = (typeof l === "function" ? l.call(d) : l)||"";
  932. var l = d.id;
  933. d.w = Math.max(node_width,calculateTextWidth(l)+(d._def.inputs>0?7:0) );
  934. d.h = Math.max(node_height,(Math.max(d.outputs,d._def.inputs)||0) * 15);
  935. if (d._def.badge) {
  936. var badge = node.append("svg:g").attr("class","node_badge_group");
  937. var badgeRect = badge.append("rect").attr("class","node_badge").attr("rx",5).attr("ry",5).attr("width",40).attr("height",15);
  938. badge.append("svg:text").attr("class","node_badge_label").attr("x",35).attr("y",11).attr('text-anchor','end').text(d._def.badge());
  939. if (d._def.onbadgeclick) {
  940. badgeRect.attr("cursor","pointer")
  941. .on("click",function(d) { d._def.onbadgeclick.call(d);d3.event.preventDefault();});
  942. }
  943. }
  944. if (d._def.button) {
  945. var nodeButtonGroup = node.append('svg:g')
  946. .attr("transform",function(d) { return "translate("+((d._def.align == "right") ? 94 : -25)+",2)"; })
  947. .attr("class",function(d) { return "node_button "+((d._def.align == "right") ? "node_right_button" : "node_left_button"); });
  948. nodeButtonGroup.append('rect')
  949. .attr("rx",8)
  950. .attr("ry",8)
  951. .attr("width",32)
  952. .attr("height",node_height-4)
  953. .attr("fill","#eee");//function(d) { return d._def.color;})
  954. nodeButtonGroup.append('rect')
  955. .attr("x",function(d) { return d._def.align == "right"? 10:5})
  956. .attr("y",4)
  957. .attr("rx",5)
  958. .attr("ry",5)
  959. .attr("width",16)
  960. .attr("height",node_height-12)
  961. .attr("fill",function(d) { return d._def.color;})
  962. .attr("cursor","pointer")
  963. .on("mousedown",function(d) {if (!lasso) { d3.select(this).attr("fill-opacity",0.2);d3.event.preventDefault(); d3.event.stopPropagation();}})
  964. .on("mouseup",function(d) {if (!lasso) { d3.select(this).attr("fill-opacity",0.4);d3.event.preventDefault();d3.event.stopPropagation();}})
  965. .on("mouseover",function(d) {if (!lasso) { d3.select(this).attr("fill-opacity",0.4);}})
  966. .on("mouseout",function(d) {if (!lasso) {
  967. var op = 1;
  968. if (d._def.button.toggle) {
  969. op = d[d._def.button.toggle]?1:0.2;
  970. }
  971. d3.select(this).attr("fill-opacity",op);
  972. }})
  973. .on("click",nodeButtonClicked)
  974. .on("touchstart",nodeButtonClicked)
  975. }
  976. var mainRect = node.append("rect")
  977. .attr("class", "node")
  978. .classed("node_unknown",function(d) { return d.type == "unknown"; })
  979. .attr("rx", 6)
  980. .attr("ry", 6)
  981. .attr("fill",function(d) { return d._def.color;})
  982. .on("mouseup",nodeMouseUp)
  983. .on("mousedown",nodeMouseDown)
  984. .on("touchstart",function(d) {
  985. var obj = d3.select(this);
  986. var touch0 = d3.event.touches.item(0);
  987. var pos = [touch0.pageX,touch0.pageY];
  988. startTouchCenter = [touch0.pageX,touch0.pageY];
  989. startTouchDistance = 0;
  990. touchStartTime = setTimeout(function() {
  991. showTouchMenu(obj,pos);
  992. },touchLongPressTimeout);
  993. nodeMouseDown.call(this,d)
  994. })
  995. .on("touchend", function(d) {
  996. clearTimeout(touchStartTime);
  997. touchStartTime = null;
  998. if (RED.touch.radialMenu.active()) {
  999. d3.event.stopPropagation();
  1000. return;
  1001. }
  1002. nodeMouseUp.call(this,d);
  1003. })
  1004. .on("mouseover",function(d) {
  1005. if (mouse_mode === 0) {
  1006. var node = d3.select(this);
  1007. node.classed("node_hovered",true);
  1008. }
  1009. })
  1010. .on("mouseout",function(d) {
  1011. var node = d3.select(this);
  1012. node.classed("node_hovered",false);
  1013. });
  1014. //node.append("rect").attr("class", "node-gradient-top").attr("rx", 6).attr("ry", 6).attr("height",30).attr("stroke","none").attr("fill","url(#gradient-top)").style("pointer-events","none");
  1015. //node.append("rect").attr("class", "node-gradient-bottom").attr("rx", 6).attr("ry", 6).attr("height",30).attr("stroke","none").attr("fill","url(#gradient-bottom)").style("pointer-events","none");
  1016. if (d._def.icon) {
  1017. var icon_group = node.append("g")
  1018. .attr("class","node_icon_group")
  1019. .attr("x",0).attr("y",0);
  1020. var icon_shade = icon_group.append("rect")
  1021. .attr("x",0).attr("y",0)
  1022. .attr("class","node_icon_shade")
  1023. .attr("width","30")
  1024. .attr("stroke","none")
  1025. .attr("fill","#000")
  1026. .attr("fill-opacity","0.05")
  1027. .attr("height",function(d){return Math.min(50,d.h-4);});
  1028. var icon = icon_group.append("image")
  1029. .attr("xlink:href","icons/"+d._def.icon)
  1030. .attr("class","node_icon")
  1031. .attr("x",0)
  1032. .attr("width","30")
  1033. .attr("height","30");
  1034. var icon_shade_border = icon_group.append("path")
  1035. .attr("d",function(d) { return "M 30 1 l 0 "+(d.h-2)})
  1036. .attr("class","node_icon_shade_border")
  1037. .attr("stroke-opacity","0.1")
  1038. .attr("stroke","#000")
  1039. .attr("stroke-width","2");
  1040. if ("right" == d._def.align) {
  1041. icon_group.attr('class','node_icon_group node_icon_group_'+d._def.align);
  1042. icon_shade_border.attr("d",function(d) { return "M 0 1 l 0 "+(d.h-2)})
  1043. //icon.attr('class','node_icon node_icon_'+d._def.align);
  1044. //icon.attr('class','node_icon_shade node_icon_shade_'+d._def.align);
  1045. //icon.attr('class','node_icon_shade_border node_icon_shade_border_'+d._def.align);
  1046. }
  1047. //if (d._def.inputs > 0 && d._def.align == null) {
  1048. // icon_shade.attr("width",35);
  1049. // icon.attr("transform","translate(5,0)");
  1050. // icon_shade_border.attr("transform","translate(5,0)");
  1051. //}
  1052. //if (d._def.outputs > 0 && "right" == d._def.align) {
  1053. // icon_shade.attr("width",35); //icon.attr("x",5);
  1054. //}
  1055. var img = new Image();
  1056. img.src = "icons/"+d._def.icon;
  1057. img.onload = function() {
  1058. icon.attr("width",Math.min(img.width,30));
  1059. icon.attr("height",Math.min(img.height,30));
  1060. icon.attr("x",15-Math.min(img.width,30)/2);
  1061. //if ("right" == d._def.align) {
  1062. // icon.attr("x",function(d){return d.w-img.width-1-(d.outputs>0?5:0);});
  1063. // icon_shade.attr("x",function(d){return d.w-30});
  1064. // icon_shade_border.attr("d",function(d){return "M "+(d.w-30)+" 1 l 0 "+(d.h-2);});
  1065. //}
  1066. }
  1067. //icon.style("pointer-events","none");
  1068. icon_group.style("pointer-events","none");
  1069. }
  1070. var text = node.append('svg:text').attr('class','node_label').attr('x', 38).attr('dy', '.35em').attr('text-anchor','start');
  1071. if (d._def.align) {
  1072. text.attr('class','node_label node_label_'+d._def.align);
  1073. text.attr('text-anchor','end');
  1074. }
  1075. var status = node.append("svg:g").attr("class","node_status_group").style("display","none");
  1076. var statusRect = status.append("rect").attr("class","node_status")
  1077. .attr("x",6).attr("y",1).attr("width",9).attr("height",9)
  1078. .attr("rx",2).attr("ry",2).attr("stroke-width","3");
  1079. var statusLabel = status.append("svg:text")
  1080. .attr("class","node_status_label")
  1081. .attr('x',20).attr('y',9)
  1082. .style({
  1083. 'stroke-width': 0,
  1084. 'fill': '#888',
  1085. 'font-size':'9pt',
  1086. 'stroke':'#000',
  1087. 'text-anchor':'start'
  1088. });
  1089. //node.append("circle").attr({"class":"centerDot","cx":0,"cy":0,"r":5});
  1090. var numInputs = d._def.inputs;
  1091. var inputlist = [];
  1092. for (var n=0; n < numInputs; n++) {
  1093. var link = RED.nodes.links.filter(function(l){return (l.target == d && l.targetPort == n);});
  1094. var y = (d.h/2)-((numInputs-1)/2)*13;
  1095. y = (y+13*n)-5;
  1096. text.attr("x",38);
  1097. var rect = node.append("rect");
  1098. inputlist[n] = rect;
  1099. rect.attr("class","port port_input").attr("rx",3).attr("ry",3)
  1100. .attr("y",y).attr("x",-5).attr("width",10).attr("height",10).attr("index",n);
  1101. if (link && link.length > 0) {
  1102. // this input already has a link connected, so disallow new links
  1103. rect.on("mousedown",null)
  1104. .on("touchstart", null)
  1105. .on("mouseup", null)
  1106. .on("touchend", null)
  1107. .on("mouseover", null)
  1108. .on("mouseout", null)
  1109. .classed("port_hovered",false);
  1110. } else {
  1111. // allow new link on inputs without any link connected
  1112. rect.on("mousedown", (function(nn){return function(d){portMouseDown(d,1,nn);}})(n))
  1113. .on("touchstart", (function(nn){return function(d){portMouseDown(d,1,nn);}})(n))
  1114. .on("mouseup", (function(nn){return function(d){portMouseUp(d,1,nn);}})(n))
  1115. .on("touchend", (function(nn){return function(d){portMouseUp(d,1,nn);}})(n))
  1116. .on("mouseover",function(d) { var port = d3.select(this); port.classed("port_hovered",(mouse_mode!=RED.state.JOINING || mousedown_port_type != 1 ));})
  1117. .on("mouseout",function(d) { var port = d3.select(this); port.classed("port_hovered",false);})
  1118. }
  1119. }
  1120. d.inputlist = inputlist;
  1121. // never show these little status icons
  1122. // people try clicking on them, thinking they're buttons
  1123. // or some sort of user interface widget
  1124. //node.append("path").attr("class","node_error").attr("d","M 3,-3 l 10,0 l -5,-8 z");
  1125. //node.append("image").attr("class","node_error hidden").attr("xlink:href","icons/node-error.png").attr("x",0).attr("y",-6).attr("width",10).attr("height",9);
  1126. //node.append("image").attr("class","node_changed hidden").attr("xlink:href","icons/node-changed.png").attr("x",12).attr("y",-6).attr("width",10).attr("height",10);
  1127. });
  1128. node.each(function(d,i) {
  1129. if (d.dirty) {
  1130. //if (d.x < -50) deleteSelection(); // Delete nodes if dragged back to palette
  1131. if (d.resize) {
  1132. //var l = d._def.label;
  1133. //l = (typeof l === "function" ? l.call(d) : l)||"";
  1134. var l = d.id;
  1135. d.w = Math.max(node_width,calculateTextWidth(l)+(d._def.inputs>0?7:0) );
  1136. d.h = Math.max(node_height,(Math.max(d.outputs,d._def.inputs)||0) * 15);
  1137. }
  1138. var thisNode = d3.select(this);
  1139. //thisNode.selectAll(".centerDot").attr({"cx":function(d) { return d.w/2;},"cy":function(d){return d.h/2}});
  1140. thisNode.attr("transform", function(d) { return "translate(" + (d.x-d.w/2) + "," + (d.y-d.h/2) + ")"; });
  1141. thisNode.selectAll(".node")
  1142. .attr("width",function(d){return d.w})
  1143. .attr("height",function(d){return d.h})
  1144. .classed("node_selected",function(d) { return d.selected; })
  1145. .classed("node_highlighted",function(d) { return d.highlighted; })
  1146. ;
  1147. //thisNode.selectAll(".node-gradient-top").attr("width",function(d){return d.w});
  1148. //thisNode.selectAll(".node-gradient-bottom").attr("width",function(d){return d.w}).attr("y",function(d){return d.h-30});
  1149. thisNode.selectAll(".node_icon_group_right").attr('transform', function(d){return "translate("+(d.w-30)+",0)"});
  1150. thisNode.selectAll(".node_label_right").attr('x', function(d){return d.w-38});
  1151. //thisNode.selectAll(".node_icon_right").attr("x",function(d){return d.w-d3.select(this).attr("width")-1-(d.outputs>0?5:0);});
  1152. //thisNode.selectAll(".node_icon_shade_right").attr("x",function(d){return d.w-30;});
  1153. //thisNode.selectAll(".node_icon_shade_border_right").attr("d",function(d){return "M "+(d.w-30)+" 1 l 0 "+(d.h-2)});
  1154. var numOutputs = d.outputs;
  1155. var y = (d.h/2)-((numOutputs-1)/2)*13;
  1156. d.ports = d.ports || d3.range(numOutputs);
  1157. d._ports = thisNode.selectAll(".port_output").data(d.ports);
  1158. d._ports.enter().append("rect").attr("class","port port_output").attr("rx",3).attr("ry",3).attr("width",10).attr("height",10)
  1159. .on("mousedown",(function(){var node = d; return function(d,i){portMouseDown(node,0,i);}})() )
  1160. .on("touchstart",(function(){var node = d; return function(d,i){portMouseDown(node,0,i);}})() )
  1161. .on("mouseup",(function(){var node = d; return function(d,i){portMouseUp(node,0,i);}})() )
  1162. .on("touchend",(function(){var node = d; return function(d,i){portMouseUp(node,0,i);}})() )
  1163. .on("mouseover",function(d,i) { var port = d3.select(this); port.classed("port_hovered",(mouse_mode!=RED.state.JOINING || mousedown_port_type !== 0 ));})
  1164. .on("mouseout",function(d,i) { var port = d3.select(this); port.classed("port_hovered",false);});
  1165. d._ports.exit().remove();
  1166. if (d._ports) {
  1167. numOutputs = d.outputs || 1;
  1168. y = (d.h/2)-((numOutputs-1)/2)*13;
  1169. var x = d.w - 5;
  1170. d._ports.each(function(d,i) {
  1171. var port = d3.select(this);
  1172. port.attr("y",(y+13*i)-5).attr("x",x);
  1173. });
  1174. }
  1175. thisNode.selectAll('text.node_label').text(function(d,i){
  1176. /* if (d._def.label) {
  1177. if (typeof d._def.label == "function") {
  1178. return d._def.label.call(d);
  1179. } else {
  1180. return d._def.label;
  1181. }
  1182. }
  1183. return ""; */
  1184. return d.id;
  1185. })
  1186. .attr('y', function(d){return (d.h/2)-1;})
  1187. .attr('class',function(d){
  1188. return 'node_label'+
  1189. (d._def.align?' node_label_'+d._def.align:'')+
  1190. (d._def.label?' '+(typeof d._def.labelStyle == "function" ? d._def.labelStyle.call(d):d._def.labelStyle):'') ;
  1191. });
  1192. thisNode.selectAll(".node_tools").attr("x",function(d){return d.w-35;}).attr("y",function(d){return d.h-20;});
  1193. thisNode.selectAll(".node_changed")
  1194. .attr("x",function(d){return d.w-10})
  1195. .classed("hidden",function(d) { return !d.changed; });
  1196. thisNode.selectAll(".node_error")
  1197. .attr("x",function(d){return d.w-10-(d.changed?13:0)})
  1198. .classed("hidden",function(d) { return d.valid; });
  1199. thisNode.selectAll(".port_input").each(function(d,i) {
  1200. var port = d3.select(this);
  1201. var numInputs = d._def.inputs;
  1202. var y = (d.h/2)-((numInputs-1)/2)*13;
  1203. y = (y+13*i)-5;
  1204. port.attr("y",y)
  1205. });
  1206. thisNode.selectAll(".node_icon").attr("y",function(d){return (d.h-d3.select(this).attr("height"))/2;});
  1207. thisNode.selectAll(".node_icon_shade").attr("height",function(d){return d.h;});
  1208. thisNode.selectAll(".node_icon_shade_border").attr("d",function(d){ return "M "+(("right" == d._def.align) ?0:30)+" 1 l 0 "+(d.h-2)});
  1209. thisNode.selectAll('.node_right_button').attr("transform",function(d){
  1210. var x = d.w-6;
  1211. if (d._def.button.toggle && !d[d._def.button.toggle]) {
  1212. x = x - 8;
  1213. }
  1214. return "translate("+x+",2)";
  1215. });
  1216. thisNode.selectAll('.node_right_button rect').attr("fill-opacity",function(d){
  1217. if (d._def.button.toggle) {
  1218. return d[d._def.button.toggle]?1:0.2;
  1219. }
  1220. return 1;
  1221. });
  1222. //thisNode.selectAll('.node_right_button').attr("transform",function(d){return "translate("+(d.w - d._def.button.width.call(d))+","+0+")";}).attr("fill",function(d) {
  1223. // return typeof d._def.button.color === "function" ? d._def.button.color.call(d):(d._def.button.color != null ? d._def.button.color : d._def.color)
  1224. //});
  1225. thisNode.selectAll('.node_badge_group').attr("transform",function(d){return "translate("+(d.w-40)+","+(d.h+3)+")";});
  1226. thisNode.selectAll('text.node_badge_label').text(function(d,i) {
  1227. /* if (d._def.badge) {
  1228. if (typeof d._def.badge == "function") {
  1229. return d._def.badge.call(d);
  1230. } else {
  1231. return d._def.badge;
  1232. }
  1233. }
  1234. return ""; */
  1235. return d.id;
  1236. });
  1237. if (!showStatus || !d.status) {
  1238. thisNode.selectAll('.node_status_group').style("display","none");
  1239. } else {
  1240. thisNode.selectAll('.node_status_group').style("display","inline").attr("transform","translate(3,"+(d.h+3)+")");
  1241. var fill = status_colours[d.status.fill]; // Only allow our colours for now
  1242. if (d.status.shape == null && fill == null) {
  1243. thisNode.selectAll('.node_status').style("display","none");
  1244. } else {
  1245. var style;
  1246. if (d.status.shape == null || d.status.shape == "dot") {
  1247. style = {
  1248. display: "inline",
  1249. fill: fill,
  1250. stroke: fill
  1251. };
  1252. } else if (d.status.shape == "ring" ){
  1253. style = {
  1254. display: "inline",
  1255. fill: '#fff',
  1256. stroke: fill
  1257. }
  1258. }
  1259. thisNode.selectAll('.node_status').style(style);
  1260. }
  1261. if (d.status.text) {
  1262. thisNode.selectAll('.node_status_label').text(d.status.text);
  1263. } else {
  1264. thisNode.selectAll('.node_status_label').text("");
  1265. }
  1266. }
  1267. d.dirty = false;
  1268. }
  1269. });
  1270. }
  1271. 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;});
  1272. var linkEnter = link.enter().insert("g",".node").attr("class","link");
  1273. linkEnter.each(function(d,i) {
  1274. var l = d3.select(this);
  1275. l.append("svg:path").attr("class","link_background link_path")
  1276. .on("mousedown",function(d) {
  1277. mousedown_link = d;
  1278. clearSelection();
  1279. selected_link = mousedown_link;
  1280. updateSelection();
  1281. redraw();
  1282. d3.event.stopPropagation();
  1283. })
  1284. .on("touchstart",function(d) {
  1285. mousedown_link = d;
  1286. clearSelection();
  1287. selected_link = mousedown_link;
  1288. updateSelection();
  1289. redraw();
  1290. d3.event.stopPropagation();
  1291. });
  1292. l.append("svg:path").attr("class","link_outline link_path");
  1293. l.append("svg:path").attr("class","link_line link_path");
  1294. });
  1295. link.exit().remove();
  1296. var links = vis.selectAll(".link_path")
  1297. links.attr("d",function(d){
  1298. var numOutputs = d.source.outputs || 1;
  1299. var sourcePort = d.sourcePort || 0;
  1300. var ysource = -((numOutputs-1)/2)*13 +13*sourcePort;
  1301. var numInputs = d.target._def.inputs || 1;
  1302. var targetPort = d.targetPort || 0;
  1303. var ytarget = -((numInputs-1)/2)*13 +13*targetPort;
  1304. var dy = (d.target.y+ytarget)-(d.source.y+ysource);
  1305. var dx = (d.target.x-d.target.w/2)-(d.source.x+d.source.w/2);
  1306. var delta = Math.sqrt(dy*dy+dx*dx);
  1307. var scale = lineCurveScale;
  1308. var scaleY = 0;
  1309. if (delta < node_width) {
  1310. scale = 0.75-0.75*((node_width-delta)/node_width);
  1311. }
  1312. if (dx < 0) {
  1313. scale += 2*(Math.min(5*node_width,Math.abs(dx))/(5*node_width));
  1314. if (Math.abs(dy) < 3*node_height) {
  1315. 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)) ;
  1316. }
  1317. }
  1318. d.x1 = d.source.x+d.source.w/2;
  1319. d.y1 = d.source.y+ysource;
  1320. d.x2 = d.target.x-d.target.w/2;
  1321. d.y2 = d.target.y+ytarget;
  1322. return "M "+(d.x1)+" "+(d.y1)+
  1323. " C "+(d.x1+scale*node_width)+" "+(d.y1+scaleY*node_height)+" "+
  1324. (d.x2-scale*node_width)+" "+(d.y2-scaleY*node_height)+" "+
  1325. (d.x2)+" "+d.y2;
  1326. })
  1327. link.classed("link_selected", function(d) { return d === selected_link || d.selected; });
  1328. link.classed("link_unknown",function(d) { return d.target.type == "unknown" || d.source.type == "unknown"});
  1329. if (d3.event) {
  1330. d3.event.preventDefault();
  1331. }
  1332. }
  1333. RED.keyboard.add(/* z */ 90,{ctrl:true},function(){RED.history.pop();});
  1334. RED.keyboard.add(/* a */ 65,{ctrl:true},function(){selectAll();d3.event.preventDefault();});
  1335. RED.keyboard.add(/* = */ 187,{ctrl:true},function(){zoomIn();d3.event.preventDefault();});
  1336. RED.keyboard.add(/* - */ 189,{ctrl:true},function(){zoomOut();d3.event.preventDefault();});
  1337. RED.keyboard.add(/* 0 */ 48,{ctrl:true},function(){zoomZero();d3.event.preventDefault();});
  1338. RED.keyboard.add(/* v */ 86,{ctrl:true},function(){importNodes(clipboard);d3.event.preventDefault();});
  1339. RED.keyboard.add(/* e */ 69,{ctrl:true},function(){showExportNodesDialog();d3.event.preventDefault();});
  1340. RED.keyboard.add(/* i */ 73,{ctrl:true},function(){showImportNodesDialog();d3.event.preventDefault();});
  1341. // TODO: 'dirty' should be a property of RED.nodes - with an event callback for ui hooks
  1342. function setDirty(d) {
  1343. dirty = d;
  1344. if (dirty) {
  1345. $("#btn-deploy").removeClass("disabled").addClass("btn-danger");
  1346. RED.storage.update();
  1347. } else {
  1348. $("#btn-deploy").addClass("disabled").removeClass("btn-danger");
  1349. }
  1350. }
  1351. /**
  1352. * Imports a new collection of nodes from a JSON String.
  1353. * - all get new IDs assigned
  1354. * - all 'selected'
  1355. * - attached to mouse for placing - 'IMPORT_DRAGGING'
  1356. */
  1357. function importNodes(newNodesStr,touchImport) {
  1358. try {
  1359. var result = RED.nodes.import(newNodesStr,true);
  1360. if (result) {
  1361. var new_nodes = result[0];
  1362. var new_links = result[1];
  1363. var new_ms = new_nodes.map(function(n) { n.z = activeWorkspace; return {n:n};});
  1364. var new_node_ids = new_nodes.map(function(n){ return n.id; });
  1365. // TODO: pick a more sensible root node
  1366. var root_node = new_ms[0].n;
  1367. var dx = root_node.x;
  1368. var dy = root_node.y;
  1369. if (mouse_position == null) {
  1370. mouse_position = [0,0];
  1371. }
  1372. var minX = 0;
  1373. var minY = 0;
  1374. var i;
  1375. var node;
  1376. for (i=0;i<new_ms.length;i++) {
  1377. node = new_ms[i];
  1378. node.n.selected = true;
  1379. node.n.changed = true;
  1380. node.n.x -= dx - mouse_position[0];
  1381. node.n.y -= dy - mouse_position[1];
  1382. node.dx = node.n.x - mouse_position[0];
  1383. node.dy = node.n.y - mouse_position[1];
  1384. minX = Math.min(node.n.x-node_width/2-5,minX);
  1385. minY = Math.min(node.n.y-node_height/2-5,minY);
  1386. }
  1387. for (i=0;i<new_ms.length;i++) {
  1388. node = new_ms[i];
  1389. node.n.x -= minX;
  1390. node.n.y -= minY;
  1391. node.dx -= minX;
  1392. node.dy -= minY;
  1393. }
  1394. if (!touchImport) {
  1395. mouse_mode = RED.state.IMPORT_DRAGGING;
  1396. }
  1397. RED.keyboard.add(/* ESCAPE */ 27,function(){
  1398. RED.keyboard.remove(/* ESCAPE */ 27);
  1399. clearSelection();
  1400. RED.history.pop();
  1401. mouse_mode = 0;
  1402. });
  1403. RED.history.push({t:'add',nodes:new_node_ids,links:new_links,dirty:RED.view.dirty()});
  1404. clearSelection();
  1405. moving_set = new_ms;
  1406. redraw();
  1407. }
  1408. } catch(error) {
  1409. console.log(error);
  1410. RED.notify("<strong>Error</strong>: "+error,"error");
  1411. }
  1412. }
  1413. $('#btn-import').click(function() {showImportNodesDialog();});
  1414. $('#btn-export-clipboard').click(function() {showExportNodesDialog();});
  1415. $('#btn-export-library').click(function() {showExportNodesLibraryDialog();});
  1416. function showExportNodesDialog() {
  1417. mouse_mode = RED.state.EXPORT;
  1418. var nns = RED.nodes.createExportableNodeSet(moving_set);
  1419. $("#dialog-form").html($("script[data-template-name='export-clipboard-dialog']").html());
  1420. $("#node-input-export").val(JSON.stringify(nns));
  1421. $("#node-input-export").focus(function() {
  1422. var textarea = $(this);
  1423. textarea.select();
  1424. textarea.mouseup(function() {
  1425. textarea.unbind("mouseup");
  1426. return false;
  1427. });
  1428. });
  1429. $( "#dialog" ).dialog("option","title","Export nodes to clipboard").dialog( "open" );
  1430. $("#node-input-export").focus();
  1431. }
  1432. function showExportNodesLibraryDialog() {
  1433. mouse_mode = RED.state.EXPORT;
  1434. var nns = RED.nodes.createExportableNodeSet(moving_set);
  1435. $("#dialog-form").html($("script[data-template-name='export-library-dialog']").html());
  1436. $("#node-input-filename").attr('nodes',JSON.stringify(nns));
  1437. $( "#dialog" ).dialog("option","title","Export nodes to library").dialog( "open" );
  1438. }
  1439. function showImportNodesDialog() {
  1440. mouse_mode = RED.state.IMPORT;
  1441. $("#dialog-form").html($("script[data-template-name='import-dialog']").html());
  1442. $("#node-input-import").val("");
  1443. $( "#dialog" ).dialog("option","title","Import nodes").dialog( "open" );
  1444. }
  1445. function showRenameWorkspaceDialog(id) {
  1446. var ws = RED.nodes.workspace(id);
  1447. $( "#node-dialog-rename-workspace" ).dialog("option","workspace",ws);
  1448. if (workspace_tabs.count() == 1) {
  1449. $( "#node-dialog-rename-workspace").next().find(".leftButton")
  1450. .prop('disabled',true)
  1451. .addClass("ui-state-disabled");
  1452. } else {
  1453. $( "#node-dialog-rename-workspace").next().find(".leftButton")
  1454. .prop('disabled',false)
  1455. .removeClass("ui-state-disabled");
  1456. }
  1457. $( "#node-input-workspace-name" ).val(ws.label);
  1458. $( "#node-dialog-rename-workspace" ).dialog("open");
  1459. }
  1460. $("#node-dialog-rename-workspace form" ).submit(function(e) { e.preventDefault();});
  1461. $( "#node-dialog-rename-workspace" ).dialog({
  1462. modal: true,
  1463. autoOpen: false,
  1464. width: 500,
  1465. title: "Rename sheet",
  1466. buttons: [
  1467. {
  1468. class: 'leftButton',
  1469. text: "Delete",
  1470. click: function() {
  1471. var workspace = $(this).dialog('option','workspace');
  1472. $( this ).dialog( "close" );
  1473. deleteWorkspace(workspace.id);
  1474. }
  1475. },
  1476. {
  1477. text: "Ok",
  1478. click: function() {
  1479. var workspace = $(this).dialog('option','workspace');
  1480. var label = $( "#node-input-workspace-name" ).val();
  1481. if (workspace.label != label) {
  1482. workspace.label = label;
  1483. var link = $("#workspace-tabs a[href='#"+workspace.id+"']");
  1484. link.attr("title",label);
  1485. link.text(label);
  1486. RED.view.dirty(true);
  1487. }
  1488. $( this ).dialog( "close" );
  1489. }
  1490. },
  1491. {
  1492. text: "Cancel",
  1493. click: function() {
  1494. $( this ).dialog( "close" );
  1495. }
  1496. }
  1497. ],
  1498. open: function(e) {
  1499. RED.keyboard.disable();
  1500. },
  1501. close: function(e) {
  1502. RED.keyboard.enable();
  1503. }
  1504. });
  1505. $( "#node-dialog-delete-workspace" ).dialog({
  1506. modal: true,
  1507. autoOpen: false,
  1508. width: 500,
  1509. title: "Confirm delete",
  1510. buttons: [
  1511. {
  1512. text: "Ok",
  1513. click: function() {
  1514. var workspace = $(this).dialog('option','workspace');
  1515. RED.view.removeWorkspace(workspace);
  1516. var historyEvent = RED.nodes.removeWorkspace(workspace.id);
  1517. historyEvent.t = 'delete';
  1518. historyEvent.dirty = dirty;
  1519. historyEvent.workspaces = [workspace];
  1520. RED.history.push(historyEvent);
  1521. RED.view.dirty(true);
  1522. $( this ).dialog( "close" );
  1523. }
  1524. },
  1525. {
  1526. text: "Cancel",
  1527. click: function() {
  1528. $( this ).dialog( "close" );
  1529. }
  1530. }
  1531. ],
  1532. open: function(e) {
  1533. RED.keyboard.disable();
  1534. },
  1535. close: function(e) {
  1536. RED.keyboard.enable();
  1537. }
  1538. });
  1539. return {
  1540. state:function(state) {
  1541. if (state == null) {
  1542. return mouse_mode
  1543. } else {
  1544. mouse_mode = state;
  1545. }
  1546. },
  1547. addWorkspace: function(ws) {
  1548. workspace_tabs.addTab(ws);
  1549. workspace_tabs.resize();
  1550. },
  1551. removeWorkspace: function(ws) {
  1552. workspace_tabs.removeTab(ws.id);
  1553. },
  1554. getWorkspace: function() {
  1555. return activeWorkspace;
  1556. },
  1557. showWorkspace: function(id) {
  1558. workspace_tabs.activateTab(id);
  1559. },
  1560. redraw:redraw,
  1561. dirty: function(d) {
  1562. if (d == null) {
  1563. return dirty;
  1564. } else {
  1565. setDirty(d);
  1566. }
  1567. },
  1568. importNodes: importNodes,
  1569. resize: function() {
  1570. workspace_tabs.resize();
  1571. },
  1572. status: function(s) {
  1573. showStatus = s;
  1574. RED.nodes.eachNode(function(n) { n.dirty = true;});
  1575. //TODO: subscribe/unsubscribe here
  1576. redraw();
  1577. }
  1578. };
  1579. })();