mxEffects.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. var mxEffects =
  6. {
  7. /**
  8. * Class: mxEffects
  9. *
  10. * Provides animation effects.
  11. */
  12. /**
  13. * Function: animateChanges
  14. *
  15. * Asynchronous animated move operation. See also: <mxMorphing>.
  16. *
  17. * Example:
  18. *
  19. * (code)
  20. * graph.model.addListener(mxEvent.CHANGE, function(sender, evt)
  21. * {
  22. * var changes = evt.getProperty('edit').changes;
  23. *
  24. * if (changes.length < 10)
  25. * {
  26. * mxEffects.animateChanges(graph, changes);
  27. * }
  28. * });
  29. * (end)
  30. *
  31. * Parameters:
  32. *
  33. * graph - <mxGraph> that received the changes.
  34. * changes - Array of changes to be animated.
  35. * done - Optional function argument that is invoked after the
  36. * last step of the animation.
  37. */
  38. animateChanges: function(graph, changes, done)
  39. {
  40. var maxStep = 10;
  41. var step = 0;
  42. var animate = function()
  43. {
  44. var isRequired = false;
  45. for (var i = 0; i < changes.length; i++)
  46. {
  47. var change = changes[i];
  48. if (change instanceof mxGeometryChange ||
  49. change instanceof mxTerminalChange ||
  50. change instanceof mxValueChange ||
  51. change instanceof mxChildChange ||
  52. change instanceof mxStyleChange)
  53. {
  54. var state = graph.getView().getState(change.cell || change.child, false);
  55. if (state != null)
  56. {
  57. isRequired = true;
  58. if (change.constructor != mxGeometryChange || graph.model.isEdge(change.cell))
  59. {
  60. mxUtils.setOpacity(state.shape.node, 100 * step / maxStep);
  61. }
  62. else
  63. {
  64. var scale = graph.getView().scale;
  65. var dx = (change.geometry.x - change.previous.x) * scale;
  66. var dy = (change.geometry.y - change.previous.y) * scale;
  67. var sx = (change.geometry.width - change.previous.width) * scale;
  68. var sy = (change.geometry.height - change.previous.height) * scale;
  69. if (step == 0)
  70. {
  71. state.x -= dx;
  72. state.y -= dy;
  73. state.width -= sx;
  74. state.height -= sy;
  75. }
  76. else
  77. {
  78. state.x += dx / maxStep;
  79. state.y += dy / maxStep;
  80. state.width += sx / maxStep;
  81. state.height += sy / maxStep;
  82. }
  83. graph.cellRenderer.redraw(state);
  84. // Fades all connected edges and children
  85. mxEffects.cascadeOpacity(graph, change.cell, 100 * step / maxStep);
  86. }
  87. }
  88. }
  89. }
  90. if (step < maxStep && isRequired)
  91. {
  92. step++;
  93. window.setTimeout(animate, delay);
  94. }
  95. else if (done != null)
  96. {
  97. done();
  98. }
  99. };
  100. var delay = 30;
  101. animate();
  102. },
  103. /**
  104. * Function: cascadeOpacity
  105. *
  106. * Sets the opacity on the given cell and its descendants.
  107. *
  108. * Parameters:
  109. *
  110. * graph - <mxGraph> that contains the cells.
  111. * cell - <mxCell> to set the opacity for.
  112. * opacity - New value for the opacity in %.
  113. */
  114. cascadeOpacity: function(graph, cell, opacity)
  115. {
  116. // Fades all children
  117. var childCount = graph.model.getChildCount(cell);
  118. for (var i=0; i<childCount; i++)
  119. {
  120. var child = graph.model.getChildAt(cell, i);
  121. var childState = graph.getView().getState(child);
  122. if (childState != null)
  123. {
  124. mxUtils.setOpacity(childState.shape.node, opacity);
  125. mxEffects.cascadeOpacity(graph, child, opacity);
  126. }
  127. }
  128. // Fades all connected edges
  129. var edges = graph.model.getEdges(cell);
  130. if (edges != null)
  131. {
  132. for (var i=0; i<edges.length; i++)
  133. {
  134. var edgeState = graph.getView().getState(edges[i]);
  135. if (edgeState != null)
  136. {
  137. mxUtils.setOpacity(edgeState.shape.node, opacity);
  138. }
  139. }
  140. }
  141. },
  142. /**
  143. * Function: fadeOut
  144. *
  145. * Asynchronous fade-out operation.
  146. */
  147. fadeOut: function(node, from, remove, step, delay, isEnabled)
  148. {
  149. step = step || 40;
  150. delay = delay || 30;
  151. var opacity = from || 100;
  152. mxUtils.setOpacity(node, opacity);
  153. if (isEnabled || isEnabled == null)
  154. {
  155. var f = function()
  156. {
  157. opacity = Math.max(opacity-step, 0);
  158. mxUtils.setOpacity(node, opacity);
  159. if (opacity > 0)
  160. {
  161. window.setTimeout(f, delay);
  162. }
  163. else
  164. {
  165. node.style.visibility = 'hidden';
  166. if (remove && node.parentNode)
  167. {
  168. node.parentNode.removeChild(node);
  169. }
  170. }
  171. };
  172. window.setTimeout(f, delay);
  173. }
  174. else
  175. {
  176. node.style.visibility = 'hidden';
  177. if (remove && node.parentNode)
  178. {
  179. node.parentNode.removeChild(node);
  180. }
  181. }
  182. }
  183. };
  184. __mxOutput.mxEffects = typeof mxEffects !== 'undefined' ? mxEffects : undefined;