tree.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // This is actually for BOTH trees and menus
  2. $axure.internal(function($ax) {
  3. var _tree = $ax.tree = {};
  4. var _menu = $ax.menu = {};
  5. $ax.menu.InitializeSubmenu = function(subMenuId, cellId) {
  6. var $submenudiv = $('#' + subMenuId);
  7. //mouseenter and leave for parent table cell
  8. $('#' + cellId).mouseenter(function(e) {
  9. //show current submenu
  10. $ax.visibility.SetIdVisible(subMenuId, true);
  11. $ax.legacy.BringToFront(subMenuId);
  12. }).mouseleave(function(e) {
  13. var offset = $submenudiv.offset();
  14. var subcontwidth = $submenudiv.width();
  15. var subcontheight = $submenudiv.height();
  16. //If mouse is not within the submenu (added 3 pixel margin to top and left calculations), then close the submenu...
  17. if(e.pageX + 3 < offset.left || e.pageX > offset.left + subcontwidth || e.pageY + 3 < offset.top || e.pageY > offset.top + subcontheight) {
  18. $submenudiv.find('.sub_menu').andSelf().each(function() {
  19. $ax.visibility.SetVisible(this, false);
  20. });
  21. $ax.style.SetWidgetHover(cellId, false);
  22. }
  23. });
  24. $submenudiv.css('display', 'none');
  25. //mouseleave for submenu
  26. $submenudiv.mouseleave(function(e) {
  27. //close this menu and all menus below it
  28. $(this).find('.sub_menu').andSelf().css({ 'visibility': 'hidden', 'display': 'none' });
  29. $ax.style.SetWidgetHover(cellId, false);
  30. });
  31. };
  32. function IsNodeVisible(nodeId) {
  33. var current = window.document.getElementById(nodeId);
  34. var parent = current.parentNode;
  35. //move all the parent's children that are below the node and their annotations
  36. while(!$(current).hasClass("treeroot")) {
  37. if(!$ax.visibility.IsVisible(parent)) return false;
  38. current = parent;
  39. parent = parent.parentNode;
  40. }
  41. return true;
  42. }
  43. $ax.tree.ExpandNode = function(nodeId, childContainerId, plusMinusId) {
  44. var container = window.document.getElementById(childContainerId);
  45. if($ax.visibility.IsVisible(container)) return;
  46. $ax.visibility.SetVisible(container, true);
  47. if(plusMinusId != '') $ax.style.SetWidgetSelected(plusMinusId, true);
  48. var delta = _getExpandCollapseDelta(nodeId, childContainerId);
  49. var isVisible = IsNodeVisible(nodeId);
  50. var current = window.document.getElementById(nodeId);
  51. var parent = current.parentNode;
  52. //move all the parent's children that are below the node and their annotations
  53. while(!$(current).hasClass("treeroot")) {
  54. var after = false;
  55. var i = 0;
  56. for(i = 0; i < parent.childNodes.length; i++) {
  57. var child = parent.childNodes[i];
  58. if(after && child.id && $(child).hasClass("treenode")) {
  59. var elementId = child.id;
  60. child.style.top = Number($(child).css('top').replace("px", "")) + delta + 'px';
  61. var ann = window.document.getElementById(elementId + "_ann");
  62. if(ann) ann.style.top = Number($(ann).css('top').replace("px", "")) + delta + 'px';
  63. }
  64. if(child == current) after = true;
  65. }
  66. current = parent;
  67. parent = parent.parentNode;
  68. if(!isVisible && $ax.visibility.IsVisible(parent)) break;
  69. }
  70. };
  71. $ax.tree.CollapseNode = function(nodeId, childContainerId, plusMinusId) {
  72. var container = window.document.getElementById(childContainerId);
  73. if(!$ax.visibility.IsVisible(container)) return;
  74. if(plusMinusId != '') $ax.style.SetWidgetSelected(plusMinusId, false);
  75. var delta = _getExpandCollapseDelta(nodeId, childContainerId);
  76. //hide it after getting the delta, otherwise the delta can't be calculated (offsetParent is null)
  77. $ax.visibility.SetVisible(container, false);
  78. var isVisible = IsNodeVisible(nodeId);
  79. var current = window.document.getElementById(nodeId);
  80. var parent = current.parentNode;
  81. //move all the parent's children that are below the node and their annotations
  82. while(!$(current).hasClass("treeroot")) {
  83. var after = false;
  84. var i = 0;
  85. for(i = 0; i < parent.childNodes.length; i++) {
  86. var child = parent.childNodes[i];
  87. if(after && child.id && $(child).hasClass("treenode")) {
  88. var elementId = child.id;
  89. child.style.top = Number($(child).css('top').replace("px", "")) - delta + 'px';
  90. var ann = window.document.getElementById(elementId + "_ann");
  91. if(ann) ann.style.top = Number($(ann).css('top').replace("px", "")) - delta + 'px';
  92. }
  93. if(child == current) after = true;
  94. }
  95. current = parent;
  96. parent = current.parentNode;
  97. if(!isVisible && $ax.visibility.IsVisible(parent)) break;
  98. }
  99. };
  100. var _getExpandCollapseDelta = function(nodeId, childContainerId) {
  101. return _getChildContainerHeightHelper(childContainerId);
  102. };
  103. var _getChildContainerHeightHelper = function(childContainerId) {
  104. var height = 0;
  105. $('#' + childContainerId).children().each(function() {
  106. if($(this).hasClass("treenode")) {
  107. height += $(this).height();
  108. var subContainer = window.document.getElementById(this.id + '_children');
  109. if(subContainer && $ax.visibility.IsVisible(subContainer)) {
  110. height += _getChildContainerHeightHelper(subContainer.id);
  111. }
  112. }
  113. });
  114. return height;
  115. };
  116. $ax.tree.InitializeTreeNode = function(nodeId, plusminusid, childContainerId, selectText) {
  117. var childContainer = window.document.getElementById(childContainerId);
  118. if(childContainer) {
  119. //relying on the html generator to put this inline so we know to collapse by default
  120. var isCollapsed = childContainer.style.visibility == "hidden";
  121. if(isCollapsed) $ax.visibility.SetVisible(childContainer, false);
  122. if(!isCollapsed && plusminusid != '') $ax.style.SetWidgetSelected(plusminusid, true);
  123. }
  124. if(plusminusid != '') {
  125. $jobj(plusminusid).click(function() {
  126. var visibleSet = $ax.visibility.IsIdVisible(childContainerId);
  127. if(visibleSet) $ax.tree.CollapseNode(nodeId, childContainerId, plusminusid);
  128. else $ax.tree.ExpandNode(nodeId, childContainerId, plusminusid);
  129. $ax.tree.SelectTreeNode(nodeId, true);
  130. return false;
  131. }).css('cursor', 'default');
  132. }
  133. };
  134. var _getButtonShapeId = function(id) {
  135. var obj = $obj(id);
  136. return obj.type == 'treeNodeObject' ? $ax.getElementIdFromPath([obj.buttonShapeId], { relativeTo: id }) : id;
  137. };
  138. $ax.tree.SelectTreeNode = function(id, selected) {
  139. $ax.style.SetWidgetSelected(_getButtonShapeId(id), selected);
  140. };
  141. });