-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventsBar.js
131 lines (123 loc) · 3.33 KB
/
EventsBar.js
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
import React from "react";
import { Motion, spring } from "react-motion";
import EventLine from "./EventLine";
import Events from "./Events";
import PropTypes from "prop-types";
//implement updateSlide
class EventsBar extends React.Component {
constructor(props) {
super(props);
this.state = {
position: 0
};
this.handleSpaceBar = this.handleSpaceBar.bind(this);
}
componentWillMount() {
document.body.addEventListener("keydown", this.handleSpaceBar);
}
componentWillReceiveProps() {
const selectedEvent = this.props.events[this.props.index];
if (selectedEvent) {
this.setState({
position: selectedEvent.distance
});
}
}
handleSpaceBar(event) {
if (event.keyCode === 32 && this.props.keyBoardEnabled) {
return this.props.spaceBarClick();
}
}
render() {
const {
events,
index,
totalWidth,
barPaddingLeft,
barPaddingRight,
styles,
fillingMotion,
spaceBarClick
} = this.props;
const filledValue =
index >= events.length
? events[events.length - 1] - barPaddingLeft
: events[index].distance - barPaddingLeft;
const eventLineWidth = totalWidth - barPaddingLeft - barPaddingRight;
const line1Styles = {
left: barPaddingLeft,
width: eventLineWidth,
backgroundColor: styles.outline
};
const line2Styles = {
left: barPaddingLeft,
width: filledValue,
backgroundColor: styles.foreground
};
return (
<div>
<div
className="events-wrapper"
style={{
position: "fixed",
margin: "0 auto",
top: "5%",
left: "15%",
right: "15%"
}}
>
<Motion
style={{
x: spring(this.state.position, fillingMotion)
}}
>
{value => (
<div
className="events"
style={{
position: "absolute",
left: "50%",
marginLeft: -totalWidth / 2,
top: 0,
height: 2,
width: totalWidth
}}
>
<EventLine styles={line1Styles} fillingMotion={fillingMotion} />
<EventLine styles={line2Styles} fillingMotion={fillingMotion} />
<Events
events={events}
selectedIndex={index}
styles={styles}
spaceBarClick={spaceBarClick}
/>
</div>
)}
</Motion>
</div>
</div>
);
}
}
EventsBar.propTypes = {
barPaddingLeft: PropTypes.number.isRequired,
barPaddingRight: PropTypes.number.isRequired,
height: PropTypes.number.isRequired,
index: PropTypes.number.isRequired,
totalWidth: PropTypes.number.isRequired,
width: PropTypes.number.isRequired,
keyBoardEnabled: PropTypes.bool.isRequired,
styles: PropTypes.object.isRequired,
spaceBarClick: PropTypes.func.isRequired,
fillingMotion: PropTypes.shape({
damping: PropTypes.number.isRequired,
stiffness: PropTypes.number.isRequired
}),
events: PropTypes.arrayOf(
PropTypes.shape({
distance: PropTypes.number.isRequired,
time: PropTypes.number.isRequired
})
)
};
export default EventsBar;