This repository has been archived by the owner on Apr 11, 2024. It is now read-only.
/
ChronosTimeSlider.jsx
263 lines (236 loc) · 9.67 KB
/
ChronosTimeSlider.jsx
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
//
// Copyright 2021 Autodesk
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import React, { useState, useEffect, useRef } from "react";
import * as darkGrayHighDensityTheme from "@hig/theme-data/build/esm/darkGrayHighDensityTheme";
import { timeEqual } from "../../shared/Utility";
import { TimeSlider } from "chronos-etu";
/**
* The time slider component based off https://www.npmjs.com/package/chronos-etu.
* @component
* @param {Object} props
* @param {Date} props.startTime The start time for the time range selected in the slider.
* @param {Date} props.endTime The end time for the time range selected in the slider.
* @param {String} props.rangeStart The earliest start date (in ISO string format) for the slider.
* @param {String} props.rangeEnd The latest end date (in ISO string format) for the slider.
* @param {HandleTimeRangeUpdated} props.onTimeRangeUpdated A callback
* function to be invoked when the time selection is updated
* @param {HandleCurrTimeUpdated} props.onCurrTimeUpdated A callback
* handler invoked when the current time marker is updated without
* changing the time selection.
*
* @memberof Autodesk.DataVisualization.UI
* @alias Autodesk.DataVisualization.UI.ChronosTimeSlider
*/
function ChronosTimeSlider(props) {
/** @type {TimeSlider} */
const [timeSliderControl, setTimeSliderControl] = useState(null);
const [timeSliderView, setTimeSliderView] = useState(null);
const [timeSelectionId, setTimeselectionId] = useState(null);
const containerRef = useRef();
/**
* Creates a timeline selection for the provided Date range.
*
* @param {Date} start Start date of the Date range.
* @param {Date} end End date of the Date range.
* @private
*/
function createTimeSelection(start, end) {
//Also zoom the slider to 5 days before and after the selection.
if (start && end) {
if (timeSelectionId) {
removeTimeSelection(timeSelectionId);
}
/** @type {string} */
let tsId = timeSliderControl.addTimeSelection(start.toISOString(), end.toISOString());
var padding = 5; // days
let zoomStart = new Date(start.getTime() - padding * 24 * 60 * 60 * 1000);
let zoomEnd = new Date(end.getTime() + padding * 24 * 60 * 60 * 1000);
timeSliderControl.zoomTo(zoomStart.toISOString(), zoomEnd.toISOString());
timeSliderControl.mStartTime = props.startTime;
timeSliderControl.mEndTime = props.endTime;
setTimeselectionId(tsId);
}
}
/**
* Removes the time selection from the timeSliderControl.
*
* @param {string} tsId The id of the last timeSelection
* @private
*/
function removeTimeSelection(tsId) {
timeSliderControl.removeTimeSelection(tsId);
setTimeselectionId(null);
}
function getCurrentTheme() {
let theme = darkGrayHighDensityTheme;
return theme.resolvedRoles;
}
function updateTimeSliderContainer(parentCntr) {
// the parentCntr is the DOM object that we will use as container to add pixi stage(canvas)
if (parentCntr && parentCntr.children.length <= 0) {
if (timeSliderView) {
parentCntr.appendChild(timeSliderView);
createTimeSelection(props.startTime, props.endTime);
const resizeApp = () => {
const w = parentCntr.clientWidth;
const h = parentCntr.clientHeight;
timeSliderControl.resize(w, h);
};
window.onresize = resizeApp;
}
}
}
/**
* Function to initialize the Timeslider
* @property {TimeSlider} timeSliderControl
* @private
*/
function createTimeSlider() {
const sliderWidth = containerRef.current.clientWidth;
const sliderHeight = containerRef.current.clientHeight;
const theme = getCurrentTheme();
let timeSliderControl = new TimeSlider(
sliderWidth,
sliderHeight,
props.rangeStart,
props.rangeEnd,
theme
);
timeSliderControl._timeFormat = "dddd, MMMM Do YYYY, HH:mm:ss";
timeSliderControl._userDefinedTimeFormat = true;
timeSliderControl.on("appready", () => {
if (timeSliderControl.app && timeSliderControl.view()) {
let timelineCanvas = timeSliderControl.view();
timeSliderControl.off("appready"); // Unsubscribe event
setTimeSliderView(timelineCanvas);
}
function onTimeRangeUpdated(data) {
if (props.onTimeRangeUpdated) {
let start = new Date(data.start);
let end = new Date(data.end);
let current = data.currentTime ? new Date(data.currentTime) : start;
timeSliderControl.mStartTime = start;
timeSliderControl.mEndTime = end;
props.onTimeRangeUpdated(start, end, current);
}
}
function onCurrTimeUpdated(data) {
if (props.onCurrTimeUpdated) {
let time = new Date(data.time);
props.onCurrTimeUpdated(time);
}
}
// Subscribe to other TimeSlider events
timeSliderControl.on("tscreated", onTimeRangeUpdated);
timeSliderControl.on("tsmodified", onTimeRangeUpdated);
timeSliderControl.on("timemarkerchanged", onCurrTimeUpdated);
timeSliderControl.on("playbackmarkerchanged", onCurrTimeUpdated);
timeSliderControl.on("play", () => {
// Calls the timeline.play() method
// This emits a series of playbackmarkerchanged events
});
timeSliderControl.on("pause", onCurrTimeUpdated);
timeSliderControl.on("stop", () => {
// Calls timeline.stop() method
// Moves currentTimeMarker to start of selection.
});
});
timeSliderControl.mStartTime = props.startTime;
timeSliderControl.mEndTime = props.endTime;
setTimeSliderControl(timeSliderControl);
}
/**
* Updates the time selection if the start or end times change.
* @private
*/
function updateSelection() {
if (
containerRef.current &&
timeSliderView &&
timeSliderControl &&
(!timeEqual(timeSliderControl.mStartTime, props.startTime) ||
!timeEqual(timeSliderControl.mEndTime, props.endTime))
) {
timeSliderControl.mStartTime = props.startTime;
timeSliderControl.mEndTime = props.endTime;
// Make sure the Start time is smaller than endTime otherwise addTimeSelection would fail
if (timeSliderControl.mStartTime.getTime() <= timeSliderControl.mEndTime.getTime()) {
createTimeSelection(props.startTime, props.endTime);
}
}
}
useEffect(() => {
//This will be run exactly once to create the actual time slider.
createTimeSlider();
return function cleanUp() {
if (timeSliderControl) {
timeSliderControl.off("tscreated");
timeSliderControl.off("tsdeleted");
timeSliderControl.off("tsmodified");
timeSliderControl.off("timemarkerchanged");
timeSliderControl.off("playbackmarkerchanged");
timeSliderControl.off("play");
timeSliderControl.off("pause");
timeSliderControl.off("stop");
timeSliderControl._app._app.destroy(true); // Destroy the Pixi Application
}
};
}, []);
useEffect(() => {
updateSelection();
}, [props.startTime, props.endTime]);
if (containerRef.current) {
updateTimeSliderContainer(containerRef.current);
}
/**
* The chronos timeslider does not fully support touch event
* Simulate the touch event to pointer events
* Hack? It is very common strategy when something out of our control
*/
let lastDownEvent;
function onTouchStart(event) {
event.touches[0].pointerType = "pen";
let mouseEvent = new PointerEvent("pointerdown", event.touches[0]);
lastDownEvent = event.touches[0];
if (timeSliderView) {
timeSliderView.dispatchEvent(mouseEvent);
}
}
function onTouchMove(event) {
event.touches[0].pointerType = "pen";
lastDownEvent = event.touches[0];
let mouseEvent = new PointerEvent("pointermove", event.touches[0]);
if (timeSliderView) {
timeSliderView.dispatchEvent(mouseEvent);
}
}
function onTouchEnd() {
let mouseEvent = new PointerEvent("pointerup", lastDownEvent);
if (timeSliderView) {
timeSliderView.dispatchEvent(mouseEvent);
}
}
return (
<div
id="timeline_header"
ref={containerRef}
onTouchStart={onTouchStart}
onTouchMove={onTouchMove}
onTouchEnd={onTouchEnd}
/>
);
}
export default ChronosTimeSlider;