Guest User

Untitled

a guest
Oct 17th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. // Each DIV has data-parent="parentid" (except the top)
  2. // Go through all steps, make hierarchy of parent to children
  3. var childrenIds = {};
  4. var topId;
  5. $('.step').each(function() {
  6. if($(this).hasClass('notree')) {
  7. return
  8. }
  9. var id = $(this).attr('id');
  10. var parentId = $(this).attr('data-parent');
  11. if (!parentId) {
  12. topId = id;
  13. console.log("Found topId " + topId)
  14. $(this).attr({
  15. 'data-depth': 0,
  16. 'data-size': 1
  17. });
  18. } else {
  19. if (!childrenIds[parentId]) {
  20. childrenIds[parentId] = [];
  21. }
  22. childrenIds[parentId].push(id);
  23. }
  24. });
  25.  
  26. console.log(childrenIds);
  27.  
  28. var sizeY = 1000;
  29. var sizeX = 1000;
  30. calculateSize(topId);
  31.  
  32. function calculateSize(stepId) {
  33. var size = 0;
  34. var childDepth = parseInt($('#' + stepId).attr('data-depth'), 10) + 1;
  35.  
  36. var children = childrenIds[stepId];
  37. if (!children) {
  38. $('#' + stepId).attr('data-size', size);
  39. return 1;
  40. }
  41. for (var i = 0; i < children.length; i++) {
  42. $('#' + children[i]).attr({
  43. 'data-depth': childDepth,
  44. 'data-siblings': children.length,
  45. 'data-sibling-index': i
  46. });
  47. size += calculateSize(children[i]);
  48. }
  49. $('#' + stepId).attr('data-size', size);
  50. return size;
  51. }
  52.  
  53. var startX = 0;
  54. var startY = 0;
  55. positionStep(topId, startX);
  56.  
  57. function positionStep(stepId, parentX, parentSize) {
  58. var stepDom = $('#' + stepId);
  59.  
  60. // x is the complicated part
  61. var xUnits = $(stepDom).attr('data-size');
  62. var xSiblings = $(stepDom).attr('data-siblings') || 0;
  63. var xIndex = $(stepDom).attr('data-sibling-index') || 0;
  64. var xOffset = 0
  65. if (xSiblings && parentSize) {
  66. xOffset = (xIndex - (xSiblings - 1)/2) * parentSize / xSiblings
  67. }
  68. var x = parentX + (xOffset * sizeX);
  69.  
  70. // y is always based on distance from topId
  71. var y = sizeY * parseInt($(stepDom).attr('data-depth')) + startY;
  72.  
  73. stepDom.attr('data-x', x);
  74. stepDom.attr('data-y', y);
  75. //stepDom.css({width: sizeY + 'px', height: sizeY + 'px'});
  76. //stepDom.css({position: 'absolute', top: x + 'px', left: y + 'px'});
  77. console.log("Positioning step " + stepId + " at " + x + ", " + y);
  78.  
  79. var children = childrenIds[stepId];
  80. if (!children) return;
  81. for (var i = 0; i < children.length; i++) {
  82. positionStep(children[i], x, xUnits);
  83. }
  84. }
  85. // now init to draw at new positions
Add Comment
Please, Sign In to add comment