# All files / src/components SubTree.jsx

86.79% Statements 46/53
70.59% Branches 24/34
58.33% Functions 7/12
86.79% Lines 46/53

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179

    2x

    2x
    2x

    8x
    8x

    8x
    8x
    8x
    8x

    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x

    2x
    2x
    2x
    2x
    2x
    2x
    1x
    1x
    1x
    1x

    2x

    8x

    8x
    8x

    8x

    10x
    10x
    10x

    10x
    10x
    10x

    10x
    10x

    10x

    10x

    2x
    2x

     import { Group } from '@vx/group';
    import React from 'react';
    import PropTypes from 'prop-types';

    import Link from './Link';
    import Node, { DEFAULT_NODE_WIDTH } from './Node';

    import { EVENT_COUNT } from '../constants';
    import getCoordsFromEvent from '../utils/getCoordsFromEvent';
    import { nodeShape } from '../propShapes';

    const propTypes = {
      nodes: PropTypes.objectOf(nodeShape).isRequired,
      nodeSorter: PropTypes.func, // could default to # events

      xScale: PropTypes.func.isRequired,
      yScale: PropTypes.func.isRequired,
      colorScale: PropTypes.func.isRequired,

      getX: PropTypes.func.isRequired,
      getY: PropTypes.func.isRequired,
      getColor: PropTypes.func.isRequired,
      yOffset: PropTypes.number,

      onMouseOver: PropTypes.func,
      onMouseOut: PropTypes.func,
      onClick: PropTypes.func,
    };

    const defaultProps = {
      nodeSorter: (a, b) => b[EVENT_COUNT] - a[EVENT_COUNT],
      onMouseOut: () => {},
      onMouseOver: () => {},
      onClick: () => {},
      yOffset: 0,
    };

    class SubTree extends React.PureComponent {
      constructor(props) {
        super(props);
        this.state = {
          showEventsFor: null,
        };

        this.onClickNode = this.onClickNode.bind(this);
        this.onClickLink = this.onClickLink.bind(this);
        this.onMouseOver = this.onMouseOver.bind(this);
        this.onMouseOut = this.onMouseOut.bind(this);
      }

      onMouseOver(event) {
        const target = event.target;
        const { node, link } = this.getNodeFromTarget(target);
        if (node || link) {
          const coords = getCoordsFromEvent(target, event);
          this.props.onMouseOver({ node, link, coords, event });
        }
      }

      onMouseOut() {
        this.props.onMouseOut();
      }

      onClickNode(event) {
        const target = event.target;
        const { node } = this.getNodeFromTarget(target);
        Eif (node) {
          const coords = getCoordsFromEvent(target, event);
          this.props.onClick({ coords, event, node });
        }
      }

      onClickLink(event) {
        const target = event.target;
        const { link } = this.getNodeFromTarget(target);
        Eif (link) {
          const coords = getCoordsFromEvent(target, event);
          this.props.onClick({ coords, event, node: link.target });
        }
      }

      getNodeFromTarget(target) {
        Eif (target) {
          const result = { node: null, link: null };
          const { nodes } = this.props;
          const hasDataAttr = target.attributes['data-node'] || target.attributes['data-target'];
          const group = hasDataAttr ? target : target.parentElement;
          if (group && group.attributes['data-node']) {
            result.node = nodes[group.attributes['data-node'].value];
          } else Eif (group && group.attributes['data-target']) {
            const targetNode = nodes[group.attributes['data-target'].value];
            result.link = {
              source: targetNode.parent,
              target: targetNode,
            };
          }
          return result;
        }
        return null;
      }

      render() {
        const {
          nodeSorter,
          nodes,
          xScale,
          yScale,
          colorScale,
          getX,
          getY,
          getColor,
          yOffset: parentYOffset,
        } = this.props;

        const sortedNodes = Object.values(nodes).sort(nodeSorter);
        const yOffset = { left: parentYOffset, right: parentYOffset };

        return (
          <Group className="subtree">
            {sortedNodes.map((node) => {
              const offset = node.depth >= 0 ? 'right' : 'left';
              const hasParent = Boolean(node.parent);
              const hasChildren = node.children && Object.keys(node.children).length;

              const top = yOffset[offset];
              const left = xScale(getX(node));
              const parentLeft = hasParent && xScale(getX(node.parent));

              const height = yScale(getY(node));
              const nodeColor = colorScale(getColor(node));

              yOffset[offset] += height;

              return (
                <Group key={node.id} style={{ cursor: 'pointer' }}>
                  {hasChildren &&
                    <SubTree
                      {...this.props}
                      yOffset={top}
                      nodes={node.children}
                    />}
                  {/* link back to the parent */}
                  {hasParent &&
                    <Link
                      source={node.parent}
                      target={node}
                      x={Math.min(left, parentLeft) + (left > parentLeft ? DEFAULT_NODE_WIDTH : 0)}
                      y={top}
                      width={Math.abs(left - parentLeft)}
                      height={Math.max(1, height)}
                      onClick={this.onClickLink}
                      onMouseOver={this.onMouseOver}
                      onMouseOut={this.onMouseOut}
                    />
                  }
                  <Node
                    node={node}
                    x={left}
                    y={top}
                    height={Math.max(1, height)}
                    fill={nodeColor}
                    onClick={this.onClickNode}
                    onMouseOver={this.onMouseOver}
                    onMouseOut={this.onMouseOut}
                    data-node={node.id}
                  />
                </Group>
              );
            })}
          </Group>
        );
      }
    }

    SubTree.propTypes = propTypes;
    SubTree.defaultProps = defaultProps;

    export default SubTree;