# All files / components/TimeScrubber/components TimeScrubberTimeline.js

0% Statements 0/52
0% Branches 0/24
0% Functions 0/12
0% Lines 0/23

    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

     import React from 'react';
    import styled from 'styled-components';
    import themeGet from 'extensions/themeGet';

    const Bar = styled.div`
      display: flex;
      flex-direction: row;
      position: absolute;
      bottom: 0;
      top: 0;
      user-select: none;
      transition: ${props => (props.dragging ? '0' : '0.2s ease all')};

      & > * {
        margin-top: auto;
        margin-right: ${props => props.tickSpacing - 1}px;
      }
    `;

    const Tick = styled.div`
      border-left: 1px solid ${themeGet('colors.border.medium')};
      height: ${props => (props.primary ? '10px' : '5px')};
      position: relative;

      &::after {
        content: '${props => props.label}';
        font-size: 12px;
        position: absolute;
        bottom: 70%;
        transform: translateX(-50%);
        white-space: pre;
        opacity: ${props => (props.enabled ? 1 : 0.5)};
      }
    `;

    const getTickLabel = idx => {
      if (idx % 4 !== 0) {
        return '';
      }

      const hour = idx / 4;
      const pm = hour > 11 && hour < 24;
      const displayHour = hour % 12 === 0 ? 12 : hour % 12;
      return `${displayHour} ${pm ? 'PM' : 'AM'}`;
    };

    const rangeContains = (range, value) => value >= range[0] && value <= range[1];

    /**
     * Draws a timeline with times, tickmarks
     */
    class Timeline extends React.PureComponent {
      static defaultProps = {
        tickSpacing: 11,
        allowedRange: [0, 23.75],
      };

      render() {
        const { tickSpacing, allowedRange, ...rest } = this.props;
        return (
          <Bar tickSpacing={tickSpacing} {...rest}>
            {new Array(97).fill(null).map((_, idx) => (
              <Tick
                key={idx}
                primary={idx % 4 === 0}
                label={getTickLabel(idx)}
                enabled={idx !== 96 && rangeContains(allowedRange, idx / 4.0)}
              />
            ))}
          </Bar>
        );
      }
    }

    export default Timeline;