demo.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // Generated by CoffeeScript 1.4.0
  2. /*
  3. # Code to render a parsed xdot file as an SVG
  4. */
  5. var renderCurrent, renderDotGraph, renderToSvg;
  6. renderCurrent = function() {
  7. window.ast = DotParser.parse(document.querySelector('textarea').value);
  8. console.log(ast);
  9. window.graph = new DotGraph(ast);
  10. console.log(graph);
  11. return graph.walk();
  12. };
  13. renderToSvg = function(graph) {
  14. var createElm, createElmNS, dragging, floatList, g, getCoords, i, k, label, n, offsetx, offsety, oldx, oldy, parser, path, points, pos, rect, rx, ry, text, tspan, x, xml, _ref, _ref1;
  15. floatList = function(l) {
  16. var v, _i, _len, _ref, _results;
  17. _ref = l.split(',');
  18. _results = [];
  19. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  20. v = _ref[_i];
  21. _results.push(parseFloat(v));
  22. }
  23. return _results;
  24. };
  25. createElm = function(name, attrs, parent) {
  26. var elm, k, v;
  27. if (attrs == null) {
  28. attrs = {};
  29. }
  30. elm = document.createElement(name);
  31. for (k in attrs) {
  32. v = attrs[k];
  33. elm.setAttribute(k, v);
  34. }
  35. if (parent != null) {
  36. parent.appendChild(elm);
  37. }
  38. return elm;
  39. };
  40. createElmNS = function(name, attrs, parent) {
  41. var elm, k, v;
  42. if (attrs == null) {
  43. attrs = {};
  44. }
  45. elm = document.createElementNS("http://www.w3.org/2000/svg", name);
  46. for (k in attrs) {
  47. v = attrs[k];
  48. elm.setAttribute(k, v);
  49. }
  50. if (parent != null) {
  51. parent.appendChild(elm);
  52. }
  53. return elm;
  54. };
  55. window.div = createElm('div', {}, document.body);
  56. window.svg = createElmNS('svg', {
  57. width: 700,
  58. height: 500
  59. }, div);
  60. window.bb = createElmNS('g', {
  61. transform: "translate(0,0)"
  62. }, svg);
  63. parser = new DOMParser();
  64. _ref = graph.nodes;
  65. for (k in _ref) {
  66. n = _ref[k];
  67. if (n.attrs.pos) {
  68. pos = floatList(n.attrs.pos);
  69. label = n.attrs.label;
  70. try {
  71. label = label.replace("\\N", k);
  72. } catch (e) {
  73. '';
  74. }
  75. if (label.value) {
  76. try {
  77. xml = parser.parseFromString("<root>" + label.value + "</root>", "text/xml");
  78. label = ((function() {
  79. var _i, _len, _ref1, _results;
  80. _ref1 = xml.querySelectorAll('font');
  81. _results = [];
  82. for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
  83. x = _ref1[_i];
  84. _results.push(x.textContent);
  85. }
  86. return _results;
  87. })()).join('\n');
  88. } catch (e) {
  89. console.log(label.value);
  90. label = '' + label.value;
  91. }
  92. }
  93. rx = parseFloat(n.attrs.width) * 36;
  94. ry = parseFloat(n.attrs.height) * 36;
  95. rect = createElmNS('rect', {
  96. id: k,
  97. x: pos[0] - rx,
  98. y: pos[1] - ry,
  99. width: rx * 2,
  100. height: ry * 2,
  101. stroke: 'black',
  102. fill: 'none',
  103. rx: 10
  104. }, bb);
  105. g = createElmNS('g', {
  106. transform: "translate(" + pos[0] + "," + pos[1] + ")"
  107. }, bb);
  108. text = createElmNS('text', {
  109. x: 0,
  110. y: 0,
  111. 'text-anchor': 'middle',
  112. 'font-family': 'sans',
  113. 'font-size': 12,
  114. fill: 'red'
  115. }, g);
  116. tspan = createElmNS('tspan', {
  117. x: 0,
  118. y: -5
  119. }, text);
  120. tspan.textContent = label.split('\n')[0];
  121. tspan = createElmNS('tspan', {
  122. x: 0,
  123. y: 12,
  124. fill: 'blue'
  125. }, text);
  126. tspan.textContent = '' + ('' + label).split('\n')[1];
  127. }
  128. }
  129. _ref1 = graph.edges;
  130. for (k in _ref1) {
  131. e = _ref1[k];
  132. e = e[0];
  133. if (e.attrs.pos) {
  134. points = e.attrs.pos.slice(2).split(' ').map(floatList);
  135. path = "M " + points[1][0] + " " + points[1][1];
  136. i = 2;
  137. while (i < points.length) {
  138. path += " C " + [points[i][0], points[i][1], points[i + 1][0], points[i + 1][1], points[i + 2][0], points[i + 2][1]].join(' ');
  139. i += 3;
  140. }
  141. createElmNS('path', {
  142. d: path,
  143. stroke: 'blue',
  144. fill: 'none'
  145. }, bb);
  146. }
  147. }
  148. dragging = false;
  149. oldx = oldy = 0;
  150. offsetx = offsety = 0;
  151. getCoords = function(evt) {
  152. rect = svg.getBoundingClientRect();
  153. return [evt.pageX - div.offsetLeft, evt.pageY - div.offsetTop];
  154. return [evt.clientX - rect.left, evt.clientY - rect.top];
  155. };
  156. svg.onmousedown = function(evt) {
  157. var y, _ref2;
  158. _ref2 = getCoords(evt), x = _ref2[0], y = _ref2[1];
  159. oldx = x;
  160. oldy = y;
  161. return dragging = true;
  162. };
  163. svg.onmouseup = function(evt) {
  164. return dragging = false;
  165. };
  166. return svg.onmousemove = function(evt) {
  167. var diffx, diffy, y, _ref2;
  168. if (dragging) {
  169. _ref2 = getCoords(evt), x = _ref2[0], y = _ref2[1];
  170. diffx = x - oldx;
  171. diffy = y - oldy;
  172. offsetx += diffx;
  173. offsety += diffy;
  174. bb.setAttribute('transform', "translate(" + offsetx + "," + offsety + ")");
  175. oldx = x;
  176. return oldy = y;
  177. }
  178. };
  179. };
  180. renderDotGraph = function(canvasElm, graph) {
  181. var bb, ctx, floatList, g, i, k, label, n, points, pos, rx, ry, _ref, _ref1, _ref2, _results;
  182. floatList = function(l) {
  183. var v, _i, _len, _ref, _results;
  184. _ref = l.split(',');
  185. _results = [];
  186. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  187. v = _ref[_i];
  188. _results.push(parseFloat(v));
  189. }
  190. return _results;
  191. };
  192. ctx = canvasElm.getContext("2d");
  193. ctx.fillStyle = 'white';
  194. ctx.rect(0, 0, 10000, 10000);
  195. ctx.fill();
  196. ctx.fillStyle = 'black';
  197. ctx.strokeStyle = 'red';
  198. _ref = graph.graphs;
  199. for (k in _ref) {
  200. g = _ref[k];
  201. if (g.attrs.bb) {
  202. bb = floatList(g.attrs.bb);
  203. ctx.beginPath();
  204. ctx.rect(bb[0], bb[1], bb[2] - bb[0], bb[3] - bb[1]);
  205. ctx.stroke();
  206. }
  207. }
  208. ctx.textAlign = 'center';
  209. ctx.textBaseline = 'middle';
  210. _ref1 = graph.nodes;
  211. for (k in _ref1) {
  212. n = _ref1[k];
  213. if (n.attrs.pos) {
  214. pos = floatList(n.attrs.pos);
  215. label = n.attrs.label;
  216. try {
  217. label = label.replace("\\N", k);
  218. } catch (e) {
  219. console.log(label);
  220. }
  221. ctx.fillText(label, pos[0], pos[1]);
  222. rx = parseFloat(n.attrs.width) * 36;
  223. ry = parseFloat(n.attrs.height) * 36;
  224. ctx.beginPath();
  225. ctx.ellipse(pos[0] - rx, pos[1] - ry, rx * 2, ry * 2);
  226. ctx.stroke();
  227. }
  228. }
  229. ctx.strokeStyle = 'blue';
  230. _ref2 = graph.edges;
  231. _results = [];
  232. for (k in _ref2) {
  233. e = _ref2[k];
  234. e = e[0];
  235. if (e.attrs.pos) {
  236. points = e.attrs.pos.slice(2).split(' ').map(floatList);
  237. ctx.beginPath();
  238. ctx.moveTo(points[1][0], points[1][1]);
  239. i = 2;
  240. while (i < points.length) {
  241. ctx.bezierCurveTo(points[i][0], points[i][1], points[i + 1][0], points[i + 1][1], points[i + 2][0], points[i + 2][1]);
  242. i += 3;
  243. }
  244. ctx.stroke();
  245. ctx.beginPath();
  246. canvas_arrow(ctx, points[points.length - 1][0], points[points.length - 1][1], points[0][0], points[0][1]);
  247. _results.push(ctx.stroke());
  248. } else {
  249. _results.push(void 0);
  250. }
  251. }
  252. return _results;
  253. };
  254. window.onload = function() {
  255. renderCurrent();
  256. return renderToSvg(graph);
  257. };