mxEdgeLabelLayout.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxEdgeLabelLayout
  7. *
  8. * Extends <mxGraphLayout> to implement an edge label layout. This layout
  9. * makes use of cell states, which means the graph must be validated in
  10. * a graph view (so that the label bounds are available) before this layout
  11. * can be executed.
  12. *
  13. * Example:
  14. *
  15. * (code)
  16. * var layout = new mxEdgeLabelLayout(graph);
  17. * layout.execute(graph.getDefaultParent());
  18. * (end)
  19. *
  20. * Constructor: mxEdgeLabelLayout
  21. *
  22. * Constructs a new edge label layout.
  23. *
  24. * Arguments:
  25. *
  26. * graph - <mxGraph> that contains the cells.
  27. */
  28. function mxEdgeLabelLayout(graph, radius)
  29. {
  30. mxGraphLayout.call(this, graph);
  31. };
  32. /**
  33. * Extends mxGraphLayout.
  34. */
  35. mxEdgeLabelLayout.prototype = new mxGraphLayout();
  36. mxEdgeLabelLayout.prototype.constructor = mxEdgeLabelLayout;
  37. /**
  38. * Function: execute
  39. *
  40. * Implements <mxGraphLayout.execute>.
  41. */
  42. mxEdgeLabelLayout.prototype.execute = function(parent)
  43. {
  44. var view = this.graph.view;
  45. var model = this.graph.getModel();
  46. // Gets all vertices and edges inside the parent
  47. var edges = [];
  48. var vertices = [];
  49. var childCount = model.getChildCount(parent);
  50. for (var i = 0; i < childCount; i++)
  51. {
  52. var cell = model.getChildAt(parent, i);
  53. var state = view.getState(cell);
  54. if (state != null)
  55. {
  56. if (!this.isVertexIgnored(cell))
  57. {
  58. vertices.push(state);
  59. }
  60. else if (!this.isEdgeIgnored(cell))
  61. {
  62. edges.push(state);
  63. }
  64. }
  65. }
  66. this.placeLabels(vertices, edges);
  67. };
  68. /**
  69. * Function: placeLabels
  70. *
  71. * Places the labels of the given edges.
  72. */
  73. mxEdgeLabelLayout.prototype.placeLabels = function(v, e)
  74. {
  75. var model = this.graph.getModel();
  76. // Moves the vertices to build a circle. Makes sure the
  77. // radius is large enough for the vertices to not
  78. // overlap
  79. model.beginUpdate();
  80. try
  81. {
  82. for (var i = 0; i < e.length; i++)
  83. {
  84. var edge = e[i];
  85. if (edge != null && edge.text != null &&
  86. edge.text.boundingBox != null)
  87. {
  88. for (var j = 0; j < v.length; j++)
  89. {
  90. var vertex = v[j];
  91. if (vertex != null)
  92. {
  93. this.avoid(edge, vertex);
  94. }
  95. }
  96. }
  97. }
  98. }
  99. finally
  100. {
  101. model.endUpdate();
  102. }
  103. };
  104. /**
  105. * Function: avoid
  106. *
  107. * Places the labels of the given edges.
  108. */
  109. mxEdgeLabelLayout.prototype.avoid = function(edge, vertex)
  110. {
  111. var model = this.graph.getModel();
  112. var labRect = edge.text.boundingBox;
  113. if (mxUtils.intersects(labRect, vertex))
  114. {
  115. var dy1 = -labRect.y - labRect.height + vertex.y;
  116. var dy2 = -labRect.y + vertex.y + vertex.height;
  117. var dy = (Math.abs(dy1) < Math.abs(dy2)) ? dy1 : dy2;
  118. var dx1 = -labRect.x - labRect.width + vertex.x;
  119. var dx2 = -labRect.x + vertex.x + vertex.width;
  120. var dx = (Math.abs(dx1) < Math.abs(dx2)) ? dx1 : dx2;
  121. if (Math.abs(dx) < Math.abs(dy))
  122. {
  123. dy = 0;
  124. }
  125. else
  126. {
  127. dx = 0;
  128. }
  129. var g = model.getGeometry(edge.cell);
  130. if (g != null)
  131. {
  132. g = g.clone();
  133. if (g.offset != null)
  134. {
  135. g.offset.x += dx;
  136. g.offset.y += dy;
  137. }
  138. else
  139. {
  140. g.offset = new mxPoint(dx, dy);
  141. }
  142. model.setGeometry(edge.cell, g);
  143. }
  144. }
  145. };
  146. __mxOutput.mxEdgeLabelLayout = typeof mxEdgeLabelLayout !== 'undefined' ? mxEdgeLabelLayout : undefined;