-
Notifications
You must be signed in to change notification settings - Fork 7.5k
/
Copy pathseek-bar.js
556 lines (471 loc) · 15.8 KB
/
seek-bar.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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/**
* @file seek-bar.js
*/
import Slider from '../../slider/slider.js';
import Component from '../../component.js';
import {IS_IOS, IS_ANDROID} from '../../utils/browser.js';
import * as Dom from '../../utils/dom.js';
import * as Fn from '../../utils/fn.js';
import {formatTime} from '../../utils/time.js';
import {silencePromise} from '../../utils/promise';
import {merge} from '../../utils/obj';
import document from 'global/document';
/** @import Player from '../../player' */
import './load-progress-bar.js';
import './play-progress-bar.js';
import './mouse-time-display.js';
// The number of seconds the `step*` functions move the timeline.
const STEP_SECONDS = 5;
// The multiplier of STEP_SECONDS that PgUp/PgDown move the timeline.
const PAGE_KEY_MULTIPLIER = 12;
/**
* Seek bar and container for the progress bars. Uses {@link PlayProgressBar}
* as its `bar`.
*
* @extends Slider
*/
class SeekBar extends Slider {
/**
* Creates an instance of this class.
*
* @param {Player} player
* The `Player` that this class should be attached to.
*
* @param {Object} [options]
* The key/value store of player options.
*/
constructor(player, options) {
options = merge(SeekBar.prototype.options_, options);
// Avoid mutating the prototype's `children` array by creating a copy
options.children = [...options.children];
const shouldDisableSeekWhileScrubbingOnMobile = player.options_.disableSeekWhileScrubbingOnMobile && (IS_IOS || IS_ANDROID);
// Add the TimeTooltip as a child if we are on desktop, or on mobile with `disableSeekWhileScrubbingOnMobile: true`
if ((!IS_IOS && !IS_ANDROID) || shouldDisableSeekWhileScrubbingOnMobile) {
options.children.splice(1, 0, 'mouseTimeDisplay');
}
super(player, options);
this.shouldDisableSeekWhileScrubbingOnMobile_ = shouldDisableSeekWhileScrubbingOnMobile;
this.pendingSeekTime_ = null;
this.setEventHandlers_();
}
/**
* Sets the event handlers
*
* @private
*/
setEventHandlers_() {
this.update_ = Fn.bind_(this, this.update);
this.update = Fn.throttle(this.update_, Fn.UPDATE_REFRESH_INTERVAL);
this.on(this.player_, ['durationchange', 'timeupdate'], this.update);
this.on(this.player_, ['ended'], this.update_);
if (this.player_.liveTracker) {
this.on(this.player_.liveTracker, 'liveedgechange', this.update);
}
// when playing, let's ensure we smoothly update the play progress bar
// via an interval
this.updateInterval = null;
this.enableIntervalHandler_ = (e) => this.enableInterval_(e);
this.disableIntervalHandler_ = (e) => this.disableInterval_(e);
this.on(this.player_, ['playing'], this.enableIntervalHandler_);
this.on(this.player_, ['ended', 'pause', 'waiting'], this.disableIntervalHandler_);
// we don't need to update the play progress if the document is hidden,
// also, this causes the CPU to spike and eventually crash the page on IE11.
if ('hidden' in document && 'visibilityState' in document) {
this.on(document, 'visibilitychange', this.toggleVisibility_);
}
}
toggleVisibility_(e) {
if (document.visibilityState === 'hidden') {
this.cancelNamedAnimationFrame('SeekBar#update');
this.cancelNamedAnimationFrame('Slider#update');
this.disableInterval_(e);
} else {
if (!this.player_.ended() && !this.player_.paused()) {
this.enableInterval_();
}
// we just switched back to the page and someone may be looking, so, update ASAP
this.update();
}
}
enableInterval_() {
if (this.updateInterval) {
return;
}
this.updateInterval = this.setInterval(this.update, Fn.UPDATE_REFRESH_INTERVAL);
}
disableInterval_(e) {
if (this.player_.liveTracker && this.player_.liveTracker.isLive() && e && e.type !== 'ended') {
return;
}
if (!this.updateInterval) {
return;
}
this.clearInterval(this.updateInterval);
this.updateInterval = null;
}
/**
* Create the `Component`'s DOM element
*
* @return {Element}
* The element that was created.
*/
createEl() {
return super.createEl('div', {
className: 'vjs-progress-holder'
}, {
'aria-label': this.localize('Progress Bar')
});
}
/**
* This function updates the play progress bar and accessibility
* attributes to whatever is passed in.
*
* @param {Event} [event]
* The `timeupdate` or `ended` event that caused this to run.
*
* @listens Player#timeupdate
*
* @return {number}
* The current percent at a number from 0-1
*/
update(event) {
// ignore updates while the tab is hidden
if (document.visibilityState === 'hidden') {
return;
}
const percent = super.update();
this.requestNamedAnimationFrame('SeekBar#update', () => {
const currentTime = this.player_.ended() ?
this.player_.duration() : this.getCurrentTime_();
const liveTracker = this.player_.liveTracker;
let duration = this.player_.duration();
if (liveTracker && liveTracker.isLive()) {
duration = this.player_.liveTracker.liveCurrentTime();
}
if (this.percent_ !== percent) {
// machine readable value of progress bar (percentage complete)
this.el_.setAttribute('aria-valuenow', (percent * 100).toFixed(2));
this.percent_ = percent;
}
if (this.currentTime_ !== currentTime || this.duration_ !== duration) {
// human readable value of progress bar (time complete)
this.el_.setAttribute(
'aria-valuetext',
this.localize(
'progress bar timing: currentTime={1} duration={2}',
[formatTime(currentTime, duration),
formatTime(duration, duration)],
'{1} of {2}'
)
);
this.currentTime_ = currentTime;
this.duration_ = duration;
}
// update the progress bar time tooltip with the current time
if (this.bar) {
this.bar.update(Dom.getBoundingClientRect(this.el()), this.getProgress());
}
});
return percent;
}
/**
* Prevent liveThreshold from causing seeks to seem like they
* are not happening from a user perspective.
*
* @param {number} ct
* current time to seek to
*/
userSeek_(ct) {
if (this.player_.liveTracker && this.player_.liveTracker.isLive()) {
this.player_.liveTracker.nextSeekedFromUser();
}
this.player_.currentTime(ct);
}
/**
* Get the value of current time but allows for smooth scrubbing,
* when player can't keep up.
*
* @return {number}
* The current time value to display
*
* @private
*/
getCurrentTime_() {
return (this.player_.scrubbing()) ?
this.player_.getCache().currentTime :
this.player_.currentTime();
}
/**
* Get the percentage of media played so far.
*
* @return {number}
* The percentage of media played so far (0 to 1).
*/
getPercent() {
// If we have a pending seek time, we are scrubbing on mobile and should set the slider percent
// to reflect the current scrub location.
if (this.pendingSeekTime_) {
return this.pendingSeekTime_ / this.player_.duration();
}
const currentTime = this.getCurrentTime_();
let percent;
const liveTracker = this.player_.liveTracker;
if (liveTracker && liveTracker.isLive()) {
percent = (currentTime - liveTracker.seekableStart()) / liveTracker.liveWindow();
// prevent the percent from changing at the live edge
if (liveTracker.atLiveEdge()) {
percent = 1;
}
} else {
percent = currentTime / this.player_.duration();
}
return percent;
}
/**
* Handle mouse down on seek bar
*
* @param {MouseEvent} event
* The `mousedown` event that caused this to run.
*
* @listens mousedown
*/
handleMouseDown(event) {
if (!Dom.isSingleLeftClick(event)) {
return;
}
// Stop event propagation to prevent double fire in progress-control.js
event.stopPropagation();
this.videoWasPlaying = !this.player_.paused();
// Don't pause if we are on mobile and `disableSeekWhileScrubbingOnMobile: true`.
// In that case, playback should continue while the player scrubs to a new location.
if (!this.shouldDisableSeekWhileScrubbingOnMobile_) {
this.player_.pause();
}
super.handleMouseDown(event);
}
/**
* Handle mouse move on seek bar
*
* @param {MouseEvent} event
* The `mousemove` event that caused this to run.
* @param {boolean} mouseDown this is a flag that should be set to true if `handleMouseMove` is called directly. It allows us to skip things that should not happen if coming from mouse down but should happen on regular mouse move handler. Defaults to false
*
* @listens mousemove
*/
handleMouseMove(event, mouseDown = false) {
if (!Dom.isSingleLeftClick(event) || isNaN(this.player_.duration())) {
return;
}
if (!mouseDown && !this.player_.scrubbing()) {
this.player_.scrubbing(true);
}
let newTime;
const distance = this.calculateDistance(event);
const liveTracker = this.player_.liveTracker;
if (!liveTracker || !liveTracker.isLive()) {
newTime = distance * this.player_.duration();
// Don't let video end while scrubbing.
if (newTime === this.player_.duration()) {
newTime = newTime - 0.1;
}
} else {
if (distance >= 0.99) {
liveTracker.seekToLiveEdge();
return;
}
const seekableStart = liveTracker.seekableStart();
const seekableEnd = liveTracker.liveCurrentTime();
newTime = seekableStart + (distance * liveTracker.liveWindow());
// Don't let video end while scrubbing.
if (newTime >= seekableEnd) {
newTime = seekableEnd;
}
// Compensate for precision differences so that currentTime is not less
// than seekable start
if (newTime <= seekableStart) {
newTime = seekableStart + 0.1;
}
// On android seekableEnd can be Infinity sometimes,
// this will cause newTime to be Infinity, which is
// not a valid currentTime.
if (newTime === Infinity) {
return;
}
}
// if on mobile and `disableSeekWhileScrubbingOnMobile: true`, keep track of the desired seek point but we won't initiate the seek until 'touchend'
if (this.shouldDisableSeekWhileScrubbingOnMobile_) {
this.pendingSeekTime_ = newTime;
} else {
this.userSeek_(newTime);
}
if (this.player_.options_.enableSmoothSeeking) {
this.update();
}
}
enable() {
super.enable();
const mouseTimeDisplay = this.getChild('mouseTimeDisplay');
if (!mouseTimeDisplay) {
return;
}
mouseTimeDisplay.show();
}
disable() {
super.disable();
const mouseTimeDisplay = this.getChild('mouseTimeDisplay');
if (!mouseTimeDisplay) {
return;
}
mouseTimeDisplay.hide();
}
/**
* Handle mouse up on seek bar
*
* @param {MouseEvent} event
* The `mouseup` event that caused this to run.
*
* @listens mouseup
*/
handleMouseUp(event) {
super.handleMouseUp(event);
// Stop event propagation to prevent double fire in progress-control.js
if (event) {
event.stopPropagation();
}
this.player_.scrubbing(false);
// If we have a pending seek time, then we have finished scrubbing on mobile and should initiate a seek.
if (this.pendingSeekTime_) {
this.userSeek_(this.pendingSeekTime_);
this.pendingSeekTime_ = null;
}
/**
* Trigger timeupdate because we're done seeking and the time has changed.
* This is particularly useful for if the player is paused to time the time displays.
*
* @event Tech#timeupdate
* @type {Event}
*/
this.player_.trigger({ type: 'timeupdate', target: this, manuallyTriggered: true });
if (this.videoWasPlaying) {
silencePromise(this.player_.play());
} else {
// We're done seeking and the time has changed.
// If the player is paused, make sure we display the correct time on the seek bar.
this.update_();
}
}
/**
* Move more quickly fast forward for keyboard-only users
*/
stepForward() {
this.userSeek_(this.player_.currentTime() + STEP_SECONDS);
}
/**
* Move more quickly rewind for keyboard-only users
*/
stepBack() {
this.userSeek_(this.player_.currentTime() - STEP_SECONDS);
}
/**
* Toggles the playback state of the player
* This gets called when enter or space is used on the seekbar
*
* @param {KeyboardEvent} event
* The `keydown` event that caused this function to be called
*
*/
handleAction(event) {
if (this.player_.paused()) {
this.player_.play();
} else {
this.player_.pause();
}
}
/**
* Called when this SeekBar has focus and a key gets pressed down.
* Supports the following keys:
*
* Space or Enter key fire a click event
* Home key moves to start of the timeline
* End key moves to end of the timeline
* Digit "0" through "9" keys move to 0%, 10% ... 80%, 90% of the timeline
* PageDown key moves back a larger step than ArrowDown
* PageUp key moves forward a large step
*
* @param {KeyboardEvent} event
* The `keydown` event that caused this function to be called.
*
* @listens keydown
*/
handleKeyDown(event) {
const liveTracker = this.player_.liveTracker;
if (event.key === ' ' || event.key === 'Enter') {
event.preventDefault();
event.stopPropagation();
this.handleAction(event);
} else if (event.key === 'Home') {
event.preventDefault();
event.stopPropagation();
this.userSeek_(0);
} else if (event.key === 'End') {
event.preventDefault();
event.stopPropagation();
if (liveTracker && liveTracker.isLive()) {
this.userSeek_(liveTracker.liveCurrentTime());
} else {
this.userSeek_(this.player_.duration());
}
} else if (/^[0-9]$/.test(event.key)) {
event.preventDefault();
event.stopPropagation();
const gotoFraction = parseInt(event.key, 10) * 0.1;
if (liveTracker && liveTracker.isLive()) {
this.userSeek_(liveTracker.seekableStart() + (liveTracker.liveWindow() * gotoFraction));
} else {
this.userSeek_(this.player_.duration() * gotoFraction);
}
} else if (event.key === 'PageDown') {
event.preventDefault();
event.stopPropagation();
this.userSeek_(this.player_.currentTime() - (STEP_SECONDS * PAGE_KEY_MULTIPLIER));
} else if (event.key === 'PageUp') {
event.preventDefault();
event.stopPropagation();
this.userSeek_(this.player_.currentTime() + (STEP_SECONDS * PAGE_KEY_MULTIPLIER));
} else {
// Pass keydown handling up for unsupported keys
super.handleKeyDown(event);
}
}
dispose() {
this.disableInterval_();
this.off(this.player_, ['durationchange', 'timeupdate'], this.update);
this.off(this.player_, ['ended'], this.update_);
if (this.player_.liveTracker) {
this.off(this.player_.liveTracker, 'liveedgechange', this.update);
}
this.off(this.player_, ['playing'], this.enableIntervalHandler_);
this.off(this.player_, ['ended', 'pause', 'waiting'], this.disableIntervalHandler_);
// we don't need to update the play progress if the document is hidden,
// also, this causes the CPU to spike and eventually crash the page on IE11.
if ('hidden' in document && 'visibilityState' in document) {
this.off(document, 'visibilitychange', this.toggleVisibility_);
}
super.dispose();
}
}
/**
* Default options for the `SeekBar`
*
* @type {Object}
* @private
*/
SeekBar.prototype.options_ = {
children: [
'loadProgressBar',
'playProgressBar'
],
barName: 'playProgressBar'
};
Component.registerComponent('SeekBar', SeekBar);
export default SeekBar;