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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268 | 1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
2×
1×
1×
1×
1×
1×
1×
1×
1×
1×
2×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
2×
2×
2×
4×
2×
| import $ from 'jquery';
import React, { PropTypes } from 'react';
import { Motion, spring } from 'react-motion';
// decorators
import Radium from 'radium';
import '../css/timeline.css';
import HorizontalTimelineButtons from './HorizontalTimelineButtons';
const DAY = 86400000;
const MAX_NORMALISED_SEPERATION = 6;
const MIN_TIMELINE_WIDTH = 750;
const DATE_WIDTH = 85;
let daydiff = (first, second) => Math.round((second - first));
/**
* Determines the minimum distance between events
* @param {[array]} dates [the array containing all the dates]
* @return {[number]} [the minimum distance between events]
*/
const __minDistanceEvents__ = (dates) => {
// determine the minimum distance among events
let dateDistances = [];
for (let i = 1; i < dates.length; i += 1) {
let distance = daydiff(dates[i - 1], dates[i]);
dateDistances.push(distance);
}
// return the minimum distance between two dates but considering that all dates
// are the same then return the distance between 2 days i.e. 86400000
return Math.max(Math.min.apply(null, dateDistances), DAY);
};
/*
* This is the Horizontal Timeline. This component expects an array of dates
* just as strings (e.g. 1/1/1993) and layes them horizontaly on the the screen
* also expects a callback which is activated when that particular index is
* clicked passing that index along
*/
class HorizontalTimeline extends React.Component {
constructor(props) {
super(props);
this.state = {
position: 0,
selected: 0,
filledValue: 0,
maxPosition: 0
};
}
/**
* The expected properties from the parent
* @type {Object}
*/
static propTypes = {
index: PropTypes.number,
// array containing the dates
values: PropTypes.array.isRequired,
// function that takes the index of the array as argument
indexClick: PropTypes.func,
// The minimum distance between consecutive events
eventsMinDistance: PropTypes.number,
styles: PropTypes.object,
fillingMotion: PropTypes.object,
slidingMotion: PropTypes.object
};
/**
* The values that the properties will take if they are not provided
* by the user.
* @type {Object}
*/
static defaultProps = {
eventsMinDistance: 80,
styles: {
background: '#dfdfdf',
foreground: '#7b9d6f',
maxSize: 800
},
fillingMotion: { stiffness: 150, damping: 25 },
slidingMotion: { stiffness: 150, damping: 25 }
};
componentWillMount() {
this.__setUpState__(this.props);
}
componentWillReceiveProps(nextProps) {
this.__setUpState__(nextProps);
}
__setUpState__ = (nextProps) => {
// parsing the dates from all valid formats that the constructor for Date accepts.
let dates = nextProps.values.map((value) => new Date(value));
// Calculating the minimum seperation between events
this.eventsMinLapse = __minDistanceEvents__(dates);
// using dynamic programming to set up the distance from the origin of the timeline.
let distances = new Array(dates.length);
distances[0] = nextProps.eventsMinDistance;
for (let index = 1; index < distances.length; index += 1) {
let distance = daydiff(dates[index - 1], dates[index]);
// NOTE: for now just setting a hard limit on the value of normalised distanceNorm
// i.e. distances will grow linearly and reach a max point then stop to increase
// an elegent mathametical calculation opertunity here.
let distanceFromPrevious = Math.min(Math.round(distance / this.eventsMinLapse) + 1, MAX_NORMALISED_SEPERATION);
// the distance_from_origin(n) = distance_from_origin(n-1) + distance between n and n - 1.
distances[index] = distances[index - 1] + distanceFromPrevious * nextProps.eventsMinDistance;
}
// The new state of the horizontal timeline.
let state = {
// the distances from the origin of the the timeline
distanceFromOrigin: distances,
// parsed format of the dates
timelineDates: dates,
// the exact value of the width of the timeline
timelineTotWidth: Math.max(MIN_TIMELINE_WIDTH, distances[distances.length - 1] + 100)
};
// set selected value only if index value is present
Iif (nextProps.index) {
state.selected = nextProps.index;
}
this.setState(state, () => {
this.__updateFilling__(this.state.selected);
});
};
__updateFilling__ = (selected) => {
// filled value = distane from origin to the selected event + half the space occupied by the date string on screen
let filledValue = (this.state.distanceFromOrigin[selected] + DATE_WIDTH / 2) / this.state.timelineTotWidth;
// right now the filledValue contains the value of the transform
this.setState({
selected: selected,
filledValue: filledValue
});
};
__updateSlide__ = (string) => {
// the width of the timeline component between the two buttons (prev and next)
let wrapperWidth = Number($('.events-wrapper').css('width').replace('px', ''));
// translate the timeline to the left('next')/right('prev')
if (string === 'right') {
this.setState({
position: Math.max(this.state.position - wrapperWidth + this.props.eventsMinDistance,
wrapperWidth - this.state.timelineTotWidth),
maxPosition: wrapperWidth - this.state.timelineTotWidth
});
} else if (string === 'left') {
this.setState({ position: Math.min(0, this.state.position + wrapperWidth - this.props.eventsMinDistance) });
}
};
__handleDateClick__ = (index) => {
this.props.indexClick(index);
this.__updateFilling__(index);
};
render() {
let dots = {
base: {
position: 'absolute',
bottom: -5,
height: 12,
width: 12,
borderRadius: '50%',
zIndex: 2
},
none: {
border: `2px solid ${this.props.styles.background}`,
backgroundColor: '#f8f8f8'
},
older: {
backgroundColor: '#f8f8f8',
border: `2px solid ${this.props.styles.foreground}`
},
selected: {
backgroundColor: this.props.styles.foreground,
borderColor: this.props.styles.foreground
}
};
// creating an array of list items that have an onClick handler into which
// passing the index of the clicked entity.
// NOTE: Improve timeline dates handeling and eventsMinLapse handling
let valuesList = this.props.values.map((date, index) => {
// decide the classname of the events being displayed on the timeline
// older-event: displayed as a circle
return (
<li key={ index } >
<a
className='text-center'
data-date={ date }
onClick={ this.__handleDateClick__.bind(null, index) }
ref={ this.state.timelineDates[index] }
style={{ left: this.state.distanceFromOrigin[index], cursor: 'pointer', width: DATE_WIDTH }} >
{ this.state.timelineDates[index].toDateString().substring(4) }
</a>
<span style={[
dots.base,
{ left: this.state.distanceFromOrigin[index] + (DATE_WIDTH - 2) / 2 },
(this.state.selected < index) && dots.none,
(this.state.selected > index) && dots.older,
(this.state.selected === index) && dots.selected
]}>
</span>
</li>
);
});
return (
<div className='cd-horizontal-timeline loaded'>
<div className='timeline' style={{
maxWidth: this.props.styles.maxSize
}}>
<div className='events-wrapper'>
<Motion style={{ X: spring(this.state.position, this.props.slidingMotion) }}>
{({ X }) =>
<div className='events'
style={{
width: this.state.timelineTotWidth,
WebkitTransform: `translate3d(${X}, 0, 0)px`,
transform: `translate3d(${X}px, 0, 0)`
}}>
<ol style={{ listStyle: 'none' }} >
{ valuesList }
</ol>
<Motion style={{ tX: spring(this.state.filledValue, this.props.fillingMotion) }}>
{({ tX }) =>
<span
aria-hidden='true'
className='filling-line'
style={{
backgroundColor: this.props.styles.foreground,
WebkitTransform: `scaleX(${tX})`,
transform: `scaleX(${tX})`
}}>
</span>
}
</Motion>
</div>
}
</Motion>
</div>
<HorizontalTimelineButtons
maxPosition={this.state.maxPosition}
position={this.state.position}
styles={this.props.styles}
updateSlide={this.__updateSlide__}
/>
</div>
</div>
);
}
}
export default Radium(HorizontalTimeline);
|