-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathTimeline.js
More file actions
370 lines (326 loc) · 11.7 KB
/
Copy pathTimeline.js
File metadata and controls
370 lines (326 loc) · 11.7 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
let BMapLib = window.BMapLib || {};
const prefix = 'BMapLib';
class Timeline {
/**
* @param {TimelineOptions} options
*/
constructor(options) {
/**
* @type {TimelineOptions}
*/
this.options = options;
this.listeners = {};
this._interval = options.interval || 1000;
this._playTimeId = null;
this._playStatus = 'pause';
this._startX; // 初始进度按钮距离视口左边距离
this._progressMax = 0; // 进度条总长度
this._playIndex = 0;
this._progress = 0;
/**
* @type {Step[]}
*/
this._steps = [];
this._times = options.times || [];
this._ctx = document.createElement('canvas').getContext('2d');
this._startScrollIndex = -1;
this._scrollIndex = 1;
this._container = null;
this._playButton = options.playButton ? options.playButton : document.createTextNode('播放');
this._pauseButton = options.pauseButton ? options.pauseButton : document.createTextNode('暂停');
this._initDom(options);
this._drawTime();
}
/**
* @param {TimelineOptions} options
*/
_initDom() {
this.element = document.createElement('div');
this.element.className = `${prefix}-timeline`;
if (this.options.className) {
this.element.className += ` ${this.options.className}`;
}
this._startButton = document.createElement('div');
this._startButton.className = `${prefix}-timeline-play`;
this._startButton.innerHTML = '';
this._startButton.appendChild(this._playButton);
this.element.appendChild(this._startButton);
this._scrollDiv = document.createElement('div');
this._scrollDiv.className = `${prefix}-timeline-main`;
this.element.appendChild(this._scrollDiv);
this._ul = document.createElement('ul');
this._scrollDiv.appendChild(this._ul);
this._progressDiv = document.createElement('div');
this._progressDiv.className = `${prefix}-timeline-progress`;
if (this.options.progressButtonStyle) {
this._applyStyle(this._progressDiv, this.options.progressButtonStyle);
}
this._scrollDiv.appendChild(this._progressDiv);
this._onPlayChange = this._onPlayChange.bind(this);
this._onProgressDragStart = this._onProgressDragStart.bind(this);
this._onProgressDrag = this._onProgressDrag.bind(this);
this._onProgressDragEnd = this._onProgressDragEnd.bind(this);
this._startButton.addEventListener('click', this._onPlayChange);
this._progressDiv.addEventListener('mousedown', this._onProgressDragStart);
if (this.options.customContainer) {
this._container = this.options.customContainer;
this.options.customContainer.appendChild(this.element);
}
else if (this.options.map) {
this._container = this.options.map.getContainer();
this.options.map.getContainer().appendChild(this.element);
}
else {
throw new Error('options.map or options.customContainer is required');
}
this._startX = this._scrollDiv.getBoundingClientRect().left;
}
on(eventName, callback) {
if (!this.listeners[eventName]) {
this.listeners[eventName] = [];
}
this.listeners[eventName].push(callback);
}
un(eventName, callback) {
if (this.listeners[eventName]) {
let index = this.listeners[eventName].indexOf(callback);
if (index > -1) {
this.listeners[eventName].splice(index, 1);
}
}
}
dispatchEvent(event) {
if (this.listeners[event.type]) {
for (let i = 0; i < this.listeners[event.type].length; i++) {
this.listeners[event.type][i](event);
}
}
}
pause() {
if (this._playStatus === 'pause') {
return;
}
this._onPlayChange();
}
play() {
if (this._playStatus === 'play') {
return;
}
this._onPlayChange();
}
/**
* @private
*/
_drawTime() {
let start = 0;
let px = 0;
for (let i = 0; i < this._times.length; i++) {
const time = this._times[i];
const li = document.createElement('li');
const timeItem = document.createElement('div');
const span = document.createElement('span');
span.innerText = time;
if (this.options.timeStyle) {
this._applyStyle(span, this.options.timeStyle);
}
li.appendChild(timeItem);
li.appendChild(span);
timeItem.className = `${prefix}-time-item`;
if (i === 0) {
timeItem.className = `${prefix}-time-item ${prefix}-time-start`;
}
if (i === this._times.length - 1) {
timeItem.className = `${prefix}-time-item ${prefix}-time-end`;
}
if (this.options.scrollStyle) {
this._applyStyle(timeItem, this.options.scrollStyle);
}
const divider = document.createElement('div');
divider.className = `${prefix}-time-divider`;
if (this.options.dividerStyle) {
this._applyStyle(divider, this.options.dividerStyle);
}
li.appendChild(divider);
this._ul.appendChild(li);
this._ctx.font = getComputedStyle(span).font;
const textWidth = this._ctx.measureText(time).width;
const stepWidth = textWidth + 16;
timeItem.style.width = stepWidth + 'px';
let halfWidth = stepWidth / 2;
px += stepWidth;
const timeInfo = {
time,
start,
end: px - halfWidth,
index: i,
};
start = px - halfWidth + 1;
this._steps.push(timeInfo);
}
this._progressMax = px;
this._calcStartScrollIndex();
}
/**
* @private
*/
_calcStartScrollIndex() {
const scrollWidth = this._scrollDiv.clientWidth;
let startScrollIndex = 0;
for (let i = 0; i < this._steps.length - 1; i++) {
if (this._steps[i].end > scrollWidth) {
startScrollIndex = i;
break;
}
}
this._startScrollIndex = startScrollIndex;
// console.log(startScrollIndex, this._steps);
}
/**
* @private
* @param {MouseEvent} e
* @returns
*/
_onProgressDrag(e) {
const x = e.clientX - this._startX + this._scrollDiv.scrollLeft;
if (x >= 0 && x <= this._progressMax) {
this._updateProgress(x);
}
}
// 进度拖拽结束
_onProgressDragEnd(e) {
const x = e.clientX - this._startX;
const step = this._getStepByProgress(x);
if (step) {
this.dispatchEvent({
type: 'change',
time: step.time,
});
this._playIndex = step.index;
} else {
this._playIndex = this._steps.length;
}
document.removeEventListener('mousemove', this._onProgressDrag);
document.removeEventListener('mouseup', this._onProgressDragEnd);
}
// 点击进度按钮
_onProgressDragStart(e) {
clearTimeout(this._playTimeId);
this._playStatus = 'pause';
this._startButton.innerHTML = '';
this._startButton.appendChild(this._playButton);
document.addEventListener('mousemove', this._onProgressDrag);
document.addEventListener('mouseup', this._onProgressDragEnd);
}
// 点击播放按钮
_onPlayChange() {
if (this._playTimeId) {
clearTimeout(this._playTimeId);
}
// 播放到结尾了,重置
if (this._playIndex >= this._steps.length) {
this._playIndex = 0;
this._scrollIndex = 1;
this._scrollDiv.scrollLeft = 0;
this._progressDiv.style.left = 0;
} else {
let dragStep = this._getStepByProgress(this._progress);
if (dragStep) {
this._playIndex = dragStep.index;
}
else {
this._playIndex = this._steps.length;
}
}
if (this._playStatus === 'pause') {
this.dispatchEvent({
type: 'playstart'
});
const loop = () => {
this._playTimeId = setTimeout(loop, this._interval);
let step = this._steps[this._playIndex++];
step && this._updateProgressByStep(step);
if (this._playIndex >= this._steps.length) {
clearTimeout(this._playTimeId);
this._playStatus = 'pause';
this._startButton.innerHTML = '';
this._startButton.appendChild(this._playButton);
this.dispatchEvent({
type: 'playend'
});
}
}
this._playTimeId = setTimeout(loop, this._interval);
this._playStatus = 'play';
this._startButton.innerHTML = '';
this._startButton.appendChild(this._pauseButton);
} else {
this._playStatus = 'pause';
this._startButton.innerHTML = '';
this._startButton.appendChild(this._playButton);
this.dispatchEvent({
type: 'playend'
});
clearTimeout(this._playTimeId);
}
}
/**
* @param {Step} step
*/
_updateProgressByStep(step) {
this.dispatchEvent({
type: 'change',
time: step.time,
});
this._progress = step.end;
this._progressDiv.style.left = this._progress - 5 + 'px';
// console.log(this._scrollIndex);
// 判断是否滚动
if (this._startScrollIndex > 0 && step.end >= this._steps[this._startScrollIndex].end) {
this._scrollDiv.scrollLeft = this._steps[this._scrollIndex++].end;
}
}
_getStepByProgress(progress) {
for (let i = 0; i < this._steps.length; i++) {
const step = this._steps[i];
if (step.start <= progress && step.end >= progress) {
return step;
}
}
}
_updateProgress(progress) {
this._progress = progress;
this._progressDiv.style.left = progress + 'px';
}
_applyStyle(element, style) {
for (let key in style) {
element.style[key] = style[key];
}
}
destroy() {
this._startButton.removeEventListener('click', this._onPlayChange);
this._progressDiv.removeEventListener('mousedown', this._onProgressDragStart);
this._container.removeChild(this.element);
}
}
BMapLib.Timeline = Timeline;
/**
* @typedef {Object} TimelineOptions
* @property {string[]} times - 时间数组
* @property {BMap} map - 地图实例
* @property {HTMLElement} [customContainer] - 自定义容器
* @property {number} [interval=1000] - 播放间隔,单位毫秒
* @property {HTMLElement} [playButton] - 播放按钮
* @property {HTMLElement} [pauseButton] - 暂停按钮
* @property {string} [className] - 类名
* @property {Object} [progressButtonStyle] - 进度按钮样式
* @property {Object} [scrollStyle] - 滚动条样式
* @property {Object} [timeStyle] - 时间样式
* @property {Object} [dividerStyle] - 分割线样式
*/
/**
* @typedef {Object} Step
* @property {number} start - 开始px
* @property {number} end - 结束px
* @property {number} time - 时间
* @property {number} index - 索引
*/