main.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. function addNode(parentId, nodeId, nodeLable, position) {
  2. var panel = d3.select("#" + parentId);
  3. panel.append('div')
  4. .style('position', 'absolute')
  5. .style('top', position.y).style('left', position.x)
  6. .style('border', 'none').attr('align', 'center')
  7. // .style('background','#4169E1')
  8. .style('border-radius', '9px')
  9. // .style('cursor', 'pointer')
  10. .style('color', '#4169E1')
  11. .style('font-size', '25px')
  12. .style('padding-left', '10px')
  13. .style('padding-right', '10px')
  14. .style('padding-top', '5px')
  15. .style('padding-bottom', '5px')
  16. .style("margin", '0 auto')
  17. .attr('id', nodeId).classed('node', true)
  18. // .attr('data-toggle','modal')
  19. // .attr('data-target',"#myModal")
  20. // .attr('onclick', 'openA(\'' + nodeId + '\')')
  21. .text(nodeLable);
  22. panel.append('div')
  23. .style('position', 'absolute')
  24. .style('top', (parseInt(position.y.split("px")[0]) + 40).toString() + 'px').style('left', position.x)
  25. .style('border', 'none').attr('align', 'center')
  26. // .style('background','#4169E1')
  27. .style('width', (document.getElementById(nodeId).offsetWidth).toString() + 'px')
  28. .style('text-align', 'center')
  29. .style('border-radius', '9px')
  30. .style('cursor', 'pointer')
  31. .style('color', '#4169E1')
  32. .style('font-size', '25px')
  33. .style('padding-left', '10px')
  34. .style('padding-right', '10px')
  35. .style('padding-top', '5px')
  36. .style('padding-bottom', '5px')
  37. .style("margin", '0 auto')
  38. .classed('node', true)
  39. // .attr('data-toggle','modal')
  40. // .attr('data-target',"#myModal")
  41. .attr('onclick', 'openA(\'' + nodeId + '\')')
  42. .text("+");
  43. return jsPlumb.getSelector('#' + nodeId)[0];
  44. }
  45. var aaa = 1;
  46. function bbb() {
  47. if (event.keyCode == 13) {
  48. aaa = aaa + 1;
  49. var name = "types" + aaa.toString();
  50. var str = '<div class="form-group"><label style="vertical-align: middle;display:inline-block;width: 100%;">' + aaa + '<input type="text" id="' + name + '" class="form-control" placeholder="请输入节点类别名字" style="width: 92%;display:inline-block;margin-left: 5px;" onkeydown="bbb()"></label></div>'
  51. $("#bbb").append(str);
  52. var inputs = document.getElementById("bbb").getElementsByTagName("input");
  53. inputs[inputs.length-1].focus();
  54. }
  55. }
  56. function addInput() {
  57. if (event.keyCode == 13) {
  58. aaa = aaa + 1;
  59. var name = "names" + aaa.toString();
  60. var str = '<div class="form-group"><label style="vertical-align: middle;display:inline-block;width: 100%;">' + aaa + '<input type="text" id="' + name + '" class="form-control" placeholder="请输入节点名字" style="width: 92%;display:inline-block;margin-left: 5px;" onkeydown="addInput()"></label></div>';
  61. $("#aaa").append(str);
  62. var inputs = document.getElementById("aaa").getElementsByTagName("input");
  63. inputs[inputs.length-1].focus();
  64. }
  65. }
  66. function openA(id) {
  67. $("#myModal").modal('show');
  68. $("#column").val(id.split("node")[1])
  69. }
  70. function addNodeA(parentId, nodeId, nodeLable, position) {
  71. var panel = d3.select("#" + parentId);
  72. panel.append('div')
  73. .style('position', 'absolute')
  74. .style('top', position.y).style('left', position.x)
  75. .style('border', '1px #4169E1 solid').attr('align', 'center')
  76. .style('background', '#4169E1')
  77. .style('border-radius', '9px')
  78. .style('color', 'white')
  79. .style('font-size', '25px')
  80. .style('padding-left', '10px')
  81. .style('padding-right', '10px')
  82. .style('padding-top', '5px')
  83. .style('padding-bottom', '5px')
  84. .style("margin", '0 auto')
  85. .attr('id', nodeId).classed('node', true)
  86. .text(nodeLable);
  87. return jsPlumb.getSelector('#' + nodeId)[0];
  88. }
  89. function addNodeB(parentId, nodeId, nodeLable, position) {
  90. var panel = d3.select("#" + parentId);
  91. panel.append('div')
  92. .style('position', 'absolute')
  93. .style('top', position.y).style('left', position.x)
  94. .style('border', '1px #4169E1 solid').attr('align', 'center')
  95. .style('background', '#ffffff')
  96. .style('border-radius', '9px')
  97. .style('color', '#4169E1')
  98. .style('font-size', '25px')
  99. .style('padding-left', '10px')
  100. .style('padding-right', '10px')
  101. .style('padding-top', '5px')
  102. .style('padding-bottom', '5px')
  103. .style("margin", '0 auto')
  104. .attr('id', nodeId).classed('node', true)
  105. .text(nodeLable);
  106. return jsPlumb.getSelector('#' + nodeId)[0];
  107. }
  108. function SubmitB() {
  109. for (var i = 1; i <= aaa; i++) {
  110. if (i == 1) {
  111. var b = 20;
  112. var a = addNode('flow-panel', 'node1', $('#types' + i).val(), { x: '80px', y: b.toString() + 'px' });
  113. }
  114. else if (i == 2) {
  115. var b = 20;
  116. var a = addNode('flow-panel', 'node2', $('#types' + i).val(), { x: '380px', y: b.toString() + 'px' });
  117. }
  118. else if (i == 3) {
  119. var b = 20;
  120. var a = addNode('flow-panel', 'node3', $('#types' + i).val(), { x: '680px', y: b.toString() + 'px' });
  121. }
  122. else {
  123. var b = 20;
  124. var a = addNode('flow-panel', 'node4', $('#types' + i).val(), { x: '980px', y: b.toString() + 'px' });
  125. }
  126. $('#types' + i).val('');
  127. }
  128. $("#bbb").html("");
  129. aaa = 1;
  130. var str = '<div class="form-group"><label style="vertical-align: middle;display:inline-block;width: 100%;">1<input type="text" id="types1" class="form-control" placeholder="请输入节点类别名字" style="width: 92%;display:inline-block;" onkeydown="bbb()"></label></div>'
  131. $("#bbb").append(str);
  132. $("#Modal").modal('hide');
  133. }
  134. function SubmitA(instance) {
  135. var column = $('#column').val();
  136. if (column == 1) {
  137. for (var i = 1; i <= aaa; i++) {
  138. var b = 20 + i * 80;
  139. var a = addNodeA('flow-panel', 'nodeA' + i.toString(), $('#names' + i.toString()).val(), { x: '80px', y: b.toString() + 'px' });
  140. addPorts(instance, a, ['out'], 'output');
  141. $('#names' + i).val('')
  142. }
  143. }
  144. else if (column == 2) {
  145. for (var i = 1; i <= aaa; i++) {
  146. var b = 20 + i * 80;
  147. var a = addNodeB('flow-panel', 'nodeB' + i.toString(), $('#names' + i.toString()).val(), { x: '380px', y: b.toString() + 'px' });
  148. addPorts(instance, a, ['in'], 'input');
  149. addPorts(instance, a, ['out'], 'output');
  150. $('#names' + i).val('')
  151. }
  152. }
  153. else if (column == 3) {
  154. for (var i = 1; i <= aaa; i++) {
  155. var b = 20 + i * 80;
  156. var a = addNodeB('flow-panel', 'nodeC' + i.toString(), $('#names' + i.toString()).val(), { x: '680px', y: b.toString() + 'px' });
  157. addPorts(instance, a, ['in'], 'input');
  158. addPorts(instance, a, ['out'], 'output');
  159. $('#names' + i).val('')
  160. }
  161. }
  162. else {
  163. for (var i = 1; i <= aaa; i++) {
  164. var b = 20 + i * 80;
  165. var a = addNodeA('flow-panel', 'nodeD' + i.toString(), $('#names' + i.toString()).val(), { x: '980px', y: b.toString() + 'px' });
  166. addPorts(instance, a, ['in'], 'input');
  167. $('#names' + i).val('')
  168. }
  169. }
  170. $('#column').val('');
  171. $("#aaa").html("");
  172. aaa = 1;
  173. var str = '<div class="form-group"><label style="vertical-align: middle;display:inline-block;width: 100%;">1 <input type="text" id="names1" class="form-control" placeholder="请输入节点名字" style="width: 92%;display:inline-block;" onkeydown="addInput()"></label></div>'
  174. $("#aaa").append(str);
  175. $("#myModal").modal('hide');
  176. }
  177. function addPorts(instance, node, ports, type) {
  178. //Assume horizental layout
  179. var number_of_ports = ports.length;
  180. var i = 0;
  181. var height = $(node).height(); //Note, jquery does not include border for height
  182. var y_offset = 1 / (number_of_ports + 1);
  183. var y = 0;
  184. var connectorStyle = {
  185. // 端点的样式
  186. // paintStyle: {
  187. // fill: '#7AB02C',
  188. // radius: 7
  189. // },
  190. // endpointStyle: { fill: 'lightgray', outlineStroke: 'darkgray', outlineWidth: 2 },
  191. // // // 鼠标移上样式
  192. // // hoverPaintStyle: {
  193. // // fill: '#216477',
  194. // // stroke: '#216477'
  195. // // },
  196. // // // 连线类型
  197. // connector: ['Straight', {
  198. // stub: [40, 60],
  199. // gap: 10,
  200. // cornerRadius: 5,
  201. // alwaysRespectStubs: true
  202. // }],
  203. // // // 连线样式
  204. // connectorStyle: {
  205. // strokeWidth: 3,
  206. // stroke: '#4169E1',
  207. // // joinstyle: 'round',
  208. // // outlineStroke: 'none',
  209. // // // 线外边的宽,值越大,线的点击范围越大
  210. // // outlineWidth: 10
  211. // },
  212. // connectorHoverStyle: {
  213. // stroke: 'green'
  214. // },
  215. connectorOverlays: [
  216. // 箭头
  217. ['Arrow', { width: 12, length: 12, location: 1 }]
  218. ]
  219. }
  220. for (; i < number_of_ports; i++) {
  221. var anchor = [0, 0, 0, 0];
  222. var paintStyle = { radius: 4, fillStyle: '#7FFFAA' };
  223. var isSource = false, isTarget = false;
  224. if (type === 'output') {
  225. anchor[0] = 1;
  226. paintStyle.fillStyle = '#7FFFAA';
  227. isSource = true;
  228. } else {
  229. isTarget = true;
  230. }
  231. anchor[1] = y + y_offset;
  232. y = anchor[1];
  233. instance.addEndpoint(node, {
  234. uuid: node.getAttribute("id") + "-" + ports[i],
  235. paintStyle: paintStyle,
  236. anchor: anchor,
  237. maxConnections: -1,
  238. isSource: isSource,
  239. isTarget: isTarget
  240. }, connectorStyle);
  241. }
  242. }
  243. function connectPorts(instance, node1, port1, node2, port2) {
  244. // declare some common values:
  245. var color = "gray";
  246. var arrowCommon = { foldback: 0.8, fillStyle: color, width: 5 },
  247. // use three-arg spec to create two different arrows with the common values:
  248. overlays = [
  249. ["Arrow", { location: 0.8 }, arrowCommon],
  250. ["Arrow", { location: 0.2, direction: -1 }, arrowCommon]
  251. ];
  252. var uuid_source = node1.getAttribute("id") + "-" + port1;
  253. var uuid_target = node2.getAttribute("id") + "-" + port2;
  254. instance.connect({ uuids: [uuid_source, uuid_target] });
  255. }
  256. function getTreeData() {
  257. var tree = [
  258. {
  259. text: "Nodes",
  260. nodes: [
  261. {
  262. text: "Node1",
  263. },
  264. {
  265. text: "Node2"
  266. }
  267. ]
  268. }
  269. ];
  270. return tree;
  271. }
  272. jsPlumb.ready(function () {
  273. console.log("jsPlumb is ready to use");
  274. //Initialize JsPlumb
  275. var color = "#4169E1";
  276. var instance = jsPlumb.getInstance({
  277. // notice the 'curviness' argument to this Bezier curve. the curves on this page are far smoother
  278. // than the curves on the first demo, which use the default curviness value.
  279. Connector: ["Straight", { curviness: 50 }],
  280. DragOptions: { cursor: "pointer", zIndex: 2000 },
  281. PaintStyle: { strokeStyle: color, lineWidth: 2 },
  282. EndpointStyle: { radius: 4, fillStyle: color },
  283. HoverPaintStyle: { strokeStyle: "#4169E1" },
  284. EndpointHoverStyle: { fillStyle: "#7FFFAA" },
  285. Container: "flow-panel"
  286. });
  287. //Initialize Control Tree View
  288. $('#control-panel').treeview({ data: getTreeData() });
  289. //Handle drag and drop
  290. $('.list-group-item').attr('draggable', 'true').on('dragstart', function (ev) {
  291. //ev.dataTransfer.setData("text", ev.target.id);
  292. ev.originalEvent.dataTransfer.setData('text', ev.target.textContent);
  293. console.log('drag start');
  294. });
  295. $('#flow-panel').on('drop', function (ev) {
  296. //avoid event conlict for jsPlumb
  297. if (ev.target.className.indexOf('_jsPlumb') >= 0) {
  298. return;
  299. }
  300. ev.preventDefault();
  301. var mx = '' + ev.originalEvent.offsetX + 'px';
  302. var my = '' + ev.originalEvent.offsetY + 'px';
  303. console.log('on drop : ' + ev.originalEvent.dataTransfer.getData('text'));
  304. var uid = new Date().getTime();
  305. var node = addNode('flow-panel', 'node' + uid, 'node', { x: mx, y: my });
  306. addPorts(instance, node, ['out'], 'output');
  307. addPorts(instance, node, ['in1', 'in2'], 'input');
  308. instance.draggable($(node));
  309. }).on('dragover', function (ev) {
  310. ev.preventDefault();
  311. console.log('on drag over');
  312. });
  313. $("#SubmitA").on('click', function () {
  314. SubmitA(instance)
  315. });
  316. $("#SubmitB").on('click', function () {
  317. SubmitB()
  318. });
  319. // for (var i = 0;i < 4;i++){
  320. // $("#node" + i).on('click', function(){
  321. // open(i)
  322. // });
  323. // }
  324. instance.doWhileSuspended(function () {
  325. // declare some common values:
  326. // var arrowCommon = { foldback:0.8, fillStyle:color, width:5 },
  327. // // use three-arg spec to create two different arrows with the common values:
  328. // overlays = [
  329. // [ "Arrow", { location:0.8 }, arrowCommon ],
  330. // [ "Arrow", { location:0.2, direction:-1 }, arrowCommon ]
  331. // ];
  332. // var node1 = addNode('flow-panel','node1', 'node1', {x:'80px',y:'20px'});
  333. // var node2 = addNode('flow-panel','node2', 'node2', {x:'280px',y:'20px'});
  334. // var node3 = addNode('flow-panel','node3', 'node3', {x:'500px',y:'20px'});
  335. // var node4 = addNode('flow-panel','node4', 'node4', {x:'700px',y:'20px'});
  336. // var node5 = addNode('flow-panel','node5', 'node5', {x:'80px',y:'100px'});
  337. // var node6 = addNode('flow-panel','node6', 'node6', {x:'700px',y:'100px'});
  338. // addPorts(instance, node1, ['out'],'output');
  339. // addPorts(instance, node2, ['in'],'input');
  340. // addPorts(instance, node2, ['out'],'output');
  341. // addPorts(instance, node3, ['in'],'input');
  342. // addPorts(instance, node3, ['out'],'output');
  343. // addPorts(instance, node4, ['in'],'input');
  344. // addPorts(instance, node5, ['out'],'output');
  345. // addPorts(instance, node6, ['in'],'input');
  346. // connectPorts(instance, node1, 'out', node2, 'in');
  347. instance.draggable($('.node'));
  348. });
  349. jsPlumb.fire("jsFlowLoaded", instance);
  350. });