edge-container.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import React from 'react';
  2. import { Motion } from 'react-motion';
  3. import { Repeat, fromJS, Map as makeMap } from 'immutable';
  4. import { line, curveBasis } from 'd3-shape';
  5. import { times } from 'lodash';
  6. import { weakSpring } from 'weaveworks-ui-components/lib/utils/animation';
  7. import { NODE_BASE_SIZE, EDGE_WAYPOINTS_CAP } from '../constants/styles';
  8. import Edge from './edge';
  9. const spline = line()
  10. .curve(curveBasis)
  11. .x(d => d.x)
  12. .y(d => d.y);
  13. const transformedEdge = (props, path, thickness) => (
  14. <Edge {...props} path={spline(path)} thickness={thickness} />
  15. );
  16. // Converts a waypoints map of the format { x0: 11, y0: 22, x1: 33, y1: 44 }
  17. // that is used by Motion to an array of waypoints in the format
  18. // [{ x: 11, y: 22 }, { x: 33, y: 44 }] that can be used by D3.
  19. const waypointsMapToArray = (waypointsMap) => {
  20. const waypointsArray = times(EDGE_WAYPOINTS_CAP, () => ({}));
  21. waypointsMap.forEach((value, key) => {
  22. const [axis, index] = [key[0], key.slice(1)];
  23. waypointsArray[index][axis] = value;
  24. });
  25. return waypointsArray;
  26. };
  27. // Converts a waypoints array of the input format [{ x: 11, y: 22 }, { x: 33, y: 44 }]
  28. // to an array of waypoints that is used by Motion in the format { x0: 11, y0: 22, x1: 33, y1: 44 }.
  29. const waypointsArrayToMap = (waypointsArray) => {
  30. let waypointsMap = makeMap();
  31. waypointsArray.forEach((point, index) => {
  32. waypointsMap = waypointsMap.set(`x${index}`, weakSpring(point.get('x')));
  33. waypointsMap = waypointsMap.set(`y${index}`, weakSpring(point.get('y')));
  34. });
  35. return waypointsMap;
  36. };
  37. export default class EdgeContainer extends React.PureComponent {
  38. constructor(props, context) {
  39. super(props, context);
  40. this.state = {
  41. thickness: 1,
  42. waypointsMap: makeMap(),
  43. };
  44. }
  45. componentWillMount() {
  46. this.prepareWaypointsForMotion(this.props);
  47. }
  48. componentWillReceiveProps(nextProps) {
  49. // immutablejs allows us to `===`! \o/
  50. const waypointsChanged = this.props.waypoints !== nextProps.waypoints;
  51. const animationChanged = this.props.isAnimated !== nextProps.isAnimated;
  52. if (waypointsChanged || animationChanged) {
  53. this.prepareWaypointsForMotion(nextProps);
  54. }
  55. // Edge thickness will reflect the zoom scale.
  56. const baseScale = (nextProps.scale * 0.01) * NODE_BASE_SIZE;
  57. const thickness = (nextProps.focused ? 3 : 1) * baseScale;
  58. this.setState({ thickness });
  59. }
  60. render() {
  61. const {
  62. isAnimated, waypoints, scale, ...forwardedProps
  63. } = this.props;
  64. const { thickness, waypointsMap } = this.state;
  65. if (!isAnimated) {
  66. return transformedEdge(forwardedProps, waypoints.toJS(), thickness);
  67. }
  68. return (
  69. // For the Motion interpolation to work, the waypoints need to be in a map format like
  70. // { x0: 11, y0: 22, x1: 33, y1: 44 } that we convert to the array format when rendering.
  71. <Motion style={{
  72. interpolatedThickness: weakSpring(thickness),
  73. ...waypointsMap.toJS(),
  74. }}>
  75. {
  76. ({ interpolatedThickness, ...interpolatedWaypoints }) => transformedEdge(
  77. forwardedProps,
  78. waypointsMapToArray(fromJS(interpolatedWaypoints)),
  79. interpolatedThickness
  80. )
  81. }
  82. </Motion>
  83. );
  84. }
  85. prepareWaypointsForMotion({ waypoints, isAnimated }) {
  86. // Don't update if the edges are not animated.
  87. if (!isAnimated) return;
  88. // The Motion library requires the number of waypoints to be constant, so we fill in for
  89. // the missing ones by reusing the edge source point, which doesn't affect the edge shape
  90. // because of how the curveBasis interpolation is done.
  91. const waypointsMissing = EDGE_WAYPOINTS_CAP - waypoints.size;
  92. if (waypointsMissing > 0) {
  93. waypoints = Repeat(waypoints.get(0), waypointsMissing).concat(waypoints);
  94. }
  95. this.setState({ waypointsMap: waypointsArrayToMap(waypoints) });
  96. }
  97. }