mxCellStatePreview.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. /**
  6. *
  7. * Class: mxCellStatePreview
  8. *
  9. * Implements a live preview for moving cells.
  10. *
  11. * Constructor: mxCellStatePreview
  12. *
  13. * Constructs a move preview for the given graph.
  14. *
  15. * Parameters:
  16. *
  17. * graph - Reference to the enclosing <mxGraph>.
  18. */
  19. function mxCellStatePreview(graph)
  20. {
  21. this.deltas = new mxDictionary();
  22. this.graph = graph;
  23. };
  24. /**
  25. * Variable: graph
  26. *
  27. * Reference to the enclosing <mxGraph>.
  28. */
  29. mxCellStatePreview.prototype.graph = null;
  30. /**
  31. * Variable: deltas
  32. *
  33. * Reference to the enclosing <mxGraph>.
  34. */
  35. mxCellStatePreview.prototype.deltas = null;
  36. /**
  37. * Variable: count
  38. *
  39. * Contains the number of entries in the map.
  40. */
  41. mxCellStatePreview.prototype.count = 0;
  42. /**
  43. * Function: isEmpty
  44. *
  45. * Returns true if this contains no entries.
  46. */
  47. mxCellStatePreview.prototype.isEmpty = function()
  48. {
  49. return this.count == 0;
  50. };
  51. /**
  52. * Function: moveState
  53. */
  54. mxCellStatePreview.prototype.moveState = function(state, dx, dy, add, includeEdges)
  55. {
  56. add = (add != null) ? add : true;
  57. includeEdges = (includeEdges != null) ? includeEdges : true;
  58. var delta = this.deltas.get(state.cell);
  59. if (delta == null)
  60. {
  61. // Note: Deltas stores the point and the state since the key is a string.
  62. delta = {point: new mxPoint(dx, dy), state: state};
  63. this.deltas.put(state.cell, delta);
  64. this.count++;
  65. }
  66. else if (add)
  67. {
  68. delta.point.x += dx;
  69. delta.point.y += dy;
  70. }
  71. else
  72. {
  73. delta.point.x = dx;
  74. delta.point.y = dy;
  75. }
  76. if (includeEdges)
  77. {
  78. this.addEdges(state);
  79. }
  80. return delta.point;
  81. };
  82. /**
  83. * Function: show
  84. */
  85. mxCellStatePreview.prototype.show = function(visitor)
  86. {
  87. this.deltas.visit(mxUtils.bind(this, function(key, delta)
  88. {
  89. this.translateState(delta.state, delta.point.x, delta.point.y);
  90. }));
  91. this.deltas.visit(mxUtils.bind(this, function(key, delta)
  92. {
  93. this.revalidateState(delta.state, delta.point.x, delta.point.y, visitor);
  94. }));
  95. };
  96. /**
  97. * Function: translateState
  98. */
  99. mxCellStatePreview.prototype.translateState = function(state, dx, dy)
  100. {
  101. if (state != null)
  102. {
  103. var model = this.graph.getModel();
  104. if (model.isVertex(state.cell))
  105. {
  106. state.view.updateCellState(state);
  107. var geo = model.getGeometry(state.cell);
  108. // Moves selection cells and non-relative vertices in
  109. // the first phase so that edge terminal points will
  110. // be updated in the second phase
  111. if ((dx != 0 || dy != 0) && geo != null && (!geo.relative || this.deltas.get(state.cell) != null))
  112. {
  113. state.x += dx;
  114. state.y += dy;
  115. }
  116. }
  117. var childCount = model.getChildCount(state.cell);
  118. for (var i = 0; i < childCount; i++)
  119. {
  120. this.translateState(state.view.getState(model.getChildAt(state.cell, i)), dx, dy);
  121. }
  122. }
  123. };
  124. /**
  125. * Function: revalidateState
  126. */
  127. mxCellStatePreview.prototype.revalidateState = function(state, dx, dy, visitor)
  128. {
  129. if (state != null)
  130. {
  131. var model = this.graph.getModel();
  132. // Updates the edge terminal points and restores the
  133. // (relative) positions of any (relative) children
  134. if (model.isEdge(state.cell))
  135. {
  136. state.view.updateCellState(state);
  137. }
  138. var geo = this.graph.getCellGeometry(state.cell);
  139. var pState = state.view.getState(model.getParent(state.cell));
  140. // Moves selection vertices which are relative
  141. if ((dx != 0 || dy != 0) && geo != null && geo.relative &&
  142. model.isVertex(state.cell) && (pState == null ||
  143. model.isVertex(pState.cell) || this.deltas.get(state.cell) != null))
  144. {
  145. state.x += dx;
  146. state.y += dy;
  147. }
  148. this.graph.cellRenderer.redraw(state);
  149. // Invokes the visitor on the given state
  150. if (visitor != null)
  151. {
  152. visitor(state);
  153. }
  154. var childCount = model.getChildCount(state.cell);
  155. for (var i = 0; i < childCount; i++)
  156. {
  157. this.revalidateState(this.graph.view.getState(model.getChildAt(state.cell, i)), dx, dy, visitor);
  158. }
  159. }
  160. };
  161. /**
  162. * Function: addEdges
  163. */
  164. mxCellStatePreview.prototype.addEdges = function(state)
  165. {
  166. var model = this.graph.getModel();
  167. var edgeCount = model.getEdgeCount(state.cell);
  168. for (var i = 0; i < edgeCount; i++)
  169. {
  170. var s = state.view.getState(model.getEdgeAt(state.cell, i));
  171. if (s != null)
  172. {
  173. this.moveState(s, 0, 0);
  174. }
  175. }
  176. };
  177. __mxOutput.mxCellStatePreview = typeof mxCellStatePreview !== 'undefined' ? mxCellStatePreview : undefined;