tree.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. document.addEventListener('DOMContentLoaded', function() {
  2. var tabs = document.getElementsByClassName('nav-tabs')[0],
  3. tabsCollection = tabs.getElementsByTagName('A'),
  4. treeTab;
  5. var len = tabsCollection.length;
  6. for(var i = 0; i < len; i++) {
  7. if (tabsCollection[i].getAttribute('id') === 'tree-tab') {
  8. treeTab = tabsCollection[i];
  9. }
  10. }
  11. // short-circuit if no tree tab
  12. if (!treeTab) return;
  13. var handler = new Tautologistics.NodeHtmlParser.HtmlBuilder(function(error, dom) {
  14. if (error) {
  15. console.log('handler ko');
  16. }
  17. }),
  18. parser = new Tautologistics.NodeHtmlParser.Parser(handler),
  19. currentLocation = window.location;
  20. parser.parseComplete(COMPONENT_TEMPLATE);
  21. var newNodes = [],
  22. newEdges = [],
  23. parsedHtml = handler.dom[0],
  24. nodeCount = 0,
  25. nodeLevel = 0;
  26. newNodes.push({
  27. _id: 0,
  28. label: parsedHtml.name,
  29. type: parsedHtml.type
  30. })
  31. //Add id for nodes
  32. var traverseIds = function(o) {
  33. for (i in o) {
  34. if (!!o[i] && typeof(o[i]) == "object") {
  35. if (!o[i].length && o[i].type === 'tag') {
  36. nodeCount += 1;
  37. o[i]._id = nodeCount;
  38. }
  39. traverseIds(o[i]);
  40. }
  41. }
  42. }
  43. parsedHtml._id = 0;
  44. traverseIds(parsedHtml);
  45. var DeepIterator = deepIterator.default,
  46. it = DeepIterator(parsedHtml);
  47. for (let {
  48. value,
  49. parent,
  50. parentNode,
  51. key,
  52. type
  53. } of it) {
  54. if (type === 'NonIterableObject' && typeof key !== 'undefined' && value.type === 'tag') {
  55. var newNode = {
  56. id: value._id,
  57. label: value.name,
  58. type: value.type
  59. };
  60. for(var i = 0; i < COMPONENTS.length; i++) {
  61. if (COMPONENTS[i].selector === value.name) {
  62. newNode.font = {
  63. multi: 'html'
  64. };
  65. newNode.label = '<b>' + newNode.label + '</b>';
  66. newNode.color = '#FB7E81';
  67. newNode.name = COMPONENTS[i].name;
  68. }
  69. }
  70. for(var i = 0; i < DIRECTIVES.length; i++) {
  71. if (value.attributes) {
  72. for(attr in value.attributes) {
  73. if (DIRECTIVES[i].selector.indexOf(attr) !== -1) {
  74. newNode.font = {
  75. multi: 'html'
  76. };
  77. newNode.label = '<b>' + newNode.label + '</b>';
  78. newNode.color = '#FF9800';
  79. newNode.name = DIRECTIVES[i].name;
  80. }
  81. }
  82. }
  83. }
  84. newNodes.push(newNode);
  85. newEdges.push({
  86. from: parentNode._parent._id,
  87. to: value._id,
  88. arrows: 'to'
  89. });
  90. }
  91. }
  92. newNodes.shift();
  93. var container = document.getElementById('tree-container'),
  94. data = {
  95. nodes: newNodes,
  96. edges: newEdges
  97. },
  98. options = {
  99. layout: {
  100. hierarchical: {
  101. sortMethod: 'directed',
  102. enabled: true
  103. }
  104. },
  105. nodes: {
  106. shape: 'ellipse',
  107. fixed: true
  108. }
  109. },
  110. handleClickNode = function(params) {
  111. var clickeNodeId;
  112. if (params.nodes.length > 0) {
  113. clickeNodeId = params.nodes[0];
  114. for(var i = 0; i < newNodes.length; i++) {
  115. if (newNodes[i].id === clickeNodeId) {
  116. for(var j = 0; j < COMPONENTS.length; j++) {
  117. if (COMPONENTS[j].name === newNodes[i].name) {
  118. document.location.href = currentLocation.origin + currentLocation.pathname.replace(ACTUAL_COMPONENT.name, newNodes[i].name);
  119. }
  120. }
  121. }
  122. }
  123. }
  124. },
  125. loadTree = function () {
  126. setTimeout(function() {
  127. container.style.height = document.getElementsByClassName('content')[0].offsetHeight - 140 + 'px';
  128. var network = new vis.Network(container, data, options);
  129. network.on('click', handleClickNode);
  130. }, 200); // Fade is 0.150
  131. };
  132. loadTree();
  133. treeTab.addEventListener('click', function() {
  134. loadTree();
  135. });
  136. });