No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

1853 líneas
60KB

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