routes.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. document.addEventListener('DOMContentLoaded', function () {
  2. function htmlEntities(str) {
  3. return String(str)
  4. .replace(/&/g, '&')
  5. .replace(/</g, '&lt;')
  6. .replace(/>/g, '&gt;')
  7. .replace(/"/g, '&quot;');
  8. }
  9. function foundLazyModuleWithPath(path) {
  10. //path is like app/customers/customers.module#CustomersModule
  11. var split = path.split('#'),
  12. lazyModulePath = split[0],
  13. lazyModuleName = split[1];
  14. return lazyModuleName;
  15. }
  16. function getBB(selection) {
  17. selection.each(function (d) {
  18. d.bbox = this.getBBox();
  19. });
  20. }
  21. var test_cases, test_case, test_case_num, engine;
  22. var tree = ROUTES_INDEX;
  23. function cleanStringChildren(obj) {
  24. for (var property in obj) {
  25. if (obj.hasOwnProperty(property)) {
  26. if (property === 'children' && typeof obj[property] === 'object') {
  27. for (var i = obj[property].length - 1; i >= 0; i--) {
  28. if (typeof obj[property][i] === 'string') {
  29. obj[property].splice(i, 1);
  30. }
  31. }
  32. }
  33. if (typeof obj[property] === 'object') {
  34. cleanStringChildren(obj[property]);
  35. }
  36. }
  37. }
  38. }
  39. cleanStringChildren(tree);
  40. engine = d3.layout.tree().setNodeSizes(true);
  41. engine.spacing(function (a, b) {
  42. return a.parent == b.parent ? 0 : engine.rootXSize();
  43. });
  44. engine.nodeSize(function (d) {
  45. return [
  46. document.getElementById(d.id).getBBox()['height'] + 70,
  47. document.getElementById(d.id).getBBox()['width'] + 30
  48. ];
  49. });
  50. var nodes = d3.layout.hierarchy()(tree),
  51. svg = d3.select('#body-routes').append('svg'),
  52. svg_g = svg.append('g'),
  53. svg_p = svg.append('g'),
  54. last_id = 0,
  55. node = svg_g
  56. .selectAll('.node')
  57. .data(nodes, function (d) {
  58. return d.id || (d.id = ++last_id);
  59. })
  60. .enter()
  61. .append('g')
  62. .attr('class', 'node');
  63. svg.attr('id', 'main');
  64. svg_g.attr('transform', 'translate(20,0)').attr('id', 'main-group');
  65. svg_p.attr('transform', 'translate(20,0)').attr('id', 'paths');
  66. var infos_group = node.append('g').attr({
  67. id: function (d) {
  68. return d.id;
  69. },
  70. dx: 0,
  71. dy: 0
  72. });
  73. //Node icon
  74. infos_group
  75. .append('text')
  76. .attr('font-family', 'Ionicons')
  77. .attr('y', 5)
  78. .attr('x', 0)
  79. .attr('class', function (d) {
  80. return d.children || d._children ? 'icon has-children' : 'icon';
  81. })
  82. .attr('font-size', function (d) {
  83. return '15px';
  84. })
  85. .text(function (d) {
  86. return '\uf183';
  87. });
  88. //node infos
  89. infos_group
  90. .append('svg:text')
  91. .attr('x', function (d) {
  92. return 0;
  93. })
  94. .attr('y', function (d) {
  95. return 10;
  96. })
  97. .attr('dy', '.35em')
  98. .attr('class', 'text')
  99. .attr('text-anchor', function (d) {
  100. return 'start';
  101. })
  102. .html(function (d) {
  103. // if kind === module name + module
  104. // if kind === component component + path
  105. var _name = '';
  106. if (d.kind === 'module') {
  107. if (d.module) {
  108. _name +=
  109. '<tspan x="0" dy="1.4em"><a href="./modules/' +
  110. d.module +
  111. '.html">' +
  112. d.module +
  113. '</a></tspan>';
  114. if (d.name) {
  115. _name += '<tspan x="0" dy="1.4em">' + d.name + '</tspan>';
  116. }
  117. } else {
  118. _name += '<tspan x="0" dy="1.4em">' + htmlEntities(d.name) + '</tspan>';
  119. }
  120. } else if (d.kind === 'component') {
  121. _name += '<tspan x="0" dy="1.4em">' + d.path + '</tspan>';
  122. _name +=
  123. '<tspan x="0" dy="1.4em"><a href="./components/' +
  124. d.component +
  125. '.html">' +
  126. d.component +
  127. '</a></tspan>';
  128. if (d.outlet) {
  129. _name += '<tspan x="0" dy="1.4em">&lt;outlet&gt; : ' + d.outlet + '</tspan>';
  130. }
  131. } else {
  132. _name += '<tspan x="0" dy="1.4em">/' + d.path + '</tspan>';
  133. if (d.component) {
  134. _name +=
  135. '<tspan x="0" dy="1.4em"><a href="./components/' +
  136. d.component +
  137. '.html">' +
  138. d.component +
  139. '</a></tspan>';
  140. }
  141. if (d.loadChildren) {
  142. var moduleName = foundLazyModuleWithPath(d.loadChildren);
  143. _name +=
  144. '<tspan x="0" dy="1.4em"><a href="./modules/' +
  145. moduleName +
  146. '.html">' +
  147. moduleName +
  148. '</a></tspan>';
  149. }
  150. if (d.canActivate) {
  151. _name += '<tspan x="0" dy="1.4em">&#10003; canActivate</tspan>';
  152. }
  153. if (d.canDeactivate) {
  154. _name += '<tspan x="0" dy="1.4em">&#215;&nbsp;&nbsp;canDeactivate</tspan>';
  155. }
  156. if (d.canActivateChild) {
  157. _name += '<tspan x="0" dy="1.4em">&#10003; canActivateChild</tspan>';
  158. }
  159. if (d.canLoad) {
  160. _name += '<tspan x="0" dy="1.4em">&#8594; canLoad</tspan>';
  161. }
  162. if (d.redirectTo) {
  163. _name += '<tspan x="0" dy="1.4em">&rarr; ' + d.redirectTo + '</tspan>';
  164. }
  165. if (d.pathMatch) {
  166. _name += '<tspan x="0" dy="1.4em">&gt; ' + d.pathMatch + '</tspan>';
  167. }
  168. if (d.outlet) {
  169. _name += '<tspan x="0" dy="1.4em">&lt;outlet&gt; : ' + d.outlet + '</tspan>';
  170. }
  171. }
  172. return _name;
  173. })
  174. .call(getBB);
  175. //
  176. // Node lazy loaded ?
  177. //
  178. infos_group
  179. .append('svg:text')
  180. .attr('y', function (d) {
  181. return 45;
  182. })
  183. .attr('x', function (d) {
  184. return -18;
  185. })
  186. .attr('font-family', 'Ionicons')
  187. .attr('class', function (d) {
  188. return 'icon';
  189. })
  190. .attr('font-size', function (d) {
  191. return '15px';
  192. })
  193. .text(function (d) {
  194. var _text = '';
  195. if (d.loadChildren || d.loadComponent) {
  196. _text = '\uf4c1';
  197. }
  198. if (d.guarded) {
  199. _text = '\uf1b0';
  200. }
  201. return _text;
  202. });
  203. //Node text background
  204. infos_group
  205. .insert('rect', 'text')
  206. .attr('width', function (d) {
  207. return d.bbox.width;
  208. })
  209. .attr('height', function (d) {
  210. return d.bbox.height;
  211. })
  212. .attr('y', function (d) {
  213. return 15;
  214. })
  215. .style('fill', 'white')
  216. .style('fill-opacity', 0.75);
  217. nodes = engine.nodes(tree);
  218. function node_extents(n) {
  219. return [n.x - n.x_size / 2, n.y, n.x + n.x_size / 2, n.y + n.y_size];
  220. }
  221. var root_extents = node_extents(nodes[0]);
  222. var xmin = root_extents[0],
  223. ymin = root_extents[1],
  224. xmax = root_extents[2],
  225. ymax = root_extents[3],
  226. area_sum = (xmax - xmin) * (ymax - ymin),
  227. x_size_min = nodes[0].x_size,
  228. y_size_min = nodes[0].y_size;
  229. nodes.slice(1).forEach(function (n) {
  230. var ne = node_extents(n);
  231. xmin = Math.min(xmin, ne[0]);
  232. ymin = Math.min(ymin, ne[1]);
  233. xmax = Math.max(xmax, ne[2]);
  234. ymax = Math.max(ymax, ne[3]);
  235. area_sum += (ne[2] - ne[0]) * (ne[3] - ne[1]);
  236. x_size_min = Math.min(x_size_min, n.x_size);
  237. y_size_min = Math.min(y_size_min, n.y_size);
  238. });
  239. var area_ave = area_sum / nodes.length;
  240. var scale = 80 / Math.sqrt(area_ave);
  241. function svg_x(node_y) {
  242. return node_y - ymin;
  243. }
  244. function svg_y(node_x) {
  245. return (node_x - xmin) * scale;
  246. }
  247. var nodebox_right_margin = Math.min(x_size_min * scale, 10);
  248. var nodebox_vertical_margin = Math.min(y_size_min * scale, 3);
  249. node.attr('transform', function (d) {
  250. return 'translate(' + svg_x(d.y) + ',' + svg_y(d.x) + ')';
  251. });
  252. var diagonal = d3.svg.diagonal().projection(function (d) {
  253. return [svg_x(d.y), svg_y(d.x)];
  254. });
  255. var links = engine.links(nodes);
  256. var links = svg_p
  257. .selectAll('.link')
  258. .data(links)
  259. .enter()
  260. .append('path')
  261. .attr('class', 'link')
  262. .attr('d', diagonal);
  263. var _svg = document.getElementById('main'),
  264. main_g = _svg.childNodes[0];
  265. _svg.removeChild(main_g);
  266. _svg.appendChild(main_g);
  267. svg.attr({
  268. width: document.getElementById('main-group').getBBox()['width'] + 30,
  269. height: document.getElementById('main-group').getBBox()['height'] + 50
  270. });
  271. });