Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add a player option keepTimeTooltipInSeekBar to prevent time tooltip overflow #7913

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ <h2>Navigation</h2>
<li><a href="sandbox/hls.html">Hls Demo</a></li>
<li><a href="sandbox/autoplay-tests.html">Autoplay Tests</a></li>
<li><a href="sandbox/noUITitleAttributes.html">noUITitleAttributes Demo</a></li>
<li><a href="sandbox/keepTimeTooltipInSeekBar.html">keepTimeTooltipInSeekBar Demo</a></li>
<li><a href="sandbox/debug.html">Videojs debug build test page</a></li>
</ul>

Expand Down
38 changes: 38 additions & 0 deletions sandbox/keepTimeTooltipInSeekBar.html.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Video.js keep time tooltip in seek bar example</title>
<link href="../dist/video-js.css" rel="stylesheet" type="text/css">
<script src="../dist/video.js"></script>
</head>
<body>
<div style="background-color:#eee; border: 1px solid #777; padding: 10px; margin-bottom: 20px; font-size: .8em; line-height: 1.5em; font-family: Verdana, sans-serif;">
<p>You can use /sandbox/ for writing and testing your own code. Nothing in /sandbox/ will get checked into the repo, except files that end in .example (so don't edit or add those files). To get started run `npm start` and open the index.html</p>
<pre>npm start</pre>
<pre>open http://localhost:9999/sandbox/keepTimeTooltipInSeekBar.html</pre>
</div>

<video-js
id="vid1"
controls
preload="auto"
width="640"
height="264"
poster="https://vjs.zencdn.net/v/oceans.png">
<source src="https://vjs.zencdn.net/v/oceans.mp4" type="video/mp4">
<source src="https://vjs.zencdn.net/v/oceans.webm" type="video/webm">
<source src="https://vjs.zencdn.net/v/oceans.ogv" type="video/ogg">
<track kind="captions" src="../docs/examples/shared/example-captions.vtt" srclang="en" label="English">
<p class="vjs-no-js">To view this video please enable JavaScript, and consider upgrading to a web browser that <a href="https://videojs.com/html5-video-support/" target="_blank">supports HTML5 video</a></p>
</video-js>

<script>
var vid = document.getElementById('vid1');
var player = videojs(vid, {
keepTimeTooltipInSeekBar: true
});
</script>

</body>
</html>
58 changes: 42 additions & 16 deletions src/js/control-bar/progress-control/time-tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,34 +66,60 @@
// of the player. We calculate any gap between the left edge of the player
// and the left edge of the `SeekBar` and add the number of pixels in the
// `SeekBar` before hitting the `seekBarPoint`
const spaceLeftOfPoint = (seekBarRect.left - playerRect.left) + seekBarPointPx;
let spaceLeftOfPoint = (seekBarRect.left - playerRect.left) + seekBarPointPx;

// This is the space right of the `seekBarPoint` available within the bounds
// of the player. We calculate the number of pixels from the `seekBarPoint`
// to the right edge of the `SeekBar` and add to that any gap between the
// right edge of the `SeekBar` and the player.
const spaceRightOfPoint = (seekBarRect.width - seekBarPointPx) +
let spaceRightOfPoint = (seekBarRect.width - seekBarPointPx) +
(playerRect.right - seekBarRect.right);

if (this.options_.playerOptions.keepTimeTooltipInSeekBar) {
// This is the space right of the `seekBarPoint` in the `SeekBar`
spaceRightOfPoint = seekBarRect.width - seekBarPointPx;

Check warning on line 80 in src/js/control-bar/progress-control/time-tooltip.js

View check run for this annotation

Codecov / codecov/patch

src/js/control-bar/progress-control/time-tooltip.js#L80

Added line #L80 was not covered by tests

// This is the space left of the `seekBarPoint` in the `SeekBar`
spaceLeftOfPoint = seekBarRect.width - spaceRightOfPoint;

Check warning on line 83 in src/js/control-bar/progress-control/time-tooltip.js

View check run for this annotation

Codecov / codecov/patch

src/js/control-bar/progress-control/time-tooltip.js#L83

Added line #L83 was not covered by tests
}

// This is the number of pixels by which the tooltip will need to be pulled
// further to the right to center it over the `seekBarPoint`.
let pullTooltipBy = tooltipRect.width / 2;

// Adjust the `pullTooltipBy` distance to the left or right depending on
// the results of the space calculations above.
if (spaceLeftOfPoint < pullTooltipBy) {
pullTooltipBy += pullTooltipBy - spaceLeftOfPoint;
} else if (spaceRightOfPoint < pullTooltipBy) {
pullTooltipBy = spaceRightOfPoint;
}
if (this.options_.playerOptions.keepTimeTooltipInSeekBar) {
// The center of `seekBar`
const centerPosition = seekBarRect.width / 2;

Check warning on line 92 in src/js/control-bar/progress-control/time-tooltip.js

View check run for this annotation

Codecov / codecov/patch

src/js/control-bar/progress-control/time-tooltip.js#L92

Added line #L92 was not covered by tests

// Offset value of the `centerPosition`
const centerOffsetOfPoint = centerPosition - seekBarPointPx;

Check warning on line 95 in src/js/control-bar/progress-control/time-tooltip.js

View check run for this annotation

Codecov / codecov/patch

src/js/control-bar/progress-control/time-tooltip.js#L95

Added line #L95 was not covered by tests

// If `tooltipRect` is greater than `seekBarRect` then center the tooltip,
// else patch the offset value of the tooltip overflow space.
if (tooltipRect.width > seekBarRect.width) {
pullTooltipBy += centerOffsetOfPoint;
} else if (spaceLeftOfPoint < pullTooltipBy) {
pullTooltipBy += pullTooltipBy - spaceLeftOfPoint;
} else if (spaceRightOfPoint < pullTooltipBy) {
pullTooltipBy -= pullTooltipBy - spaceRightOfPoint;

Check warning on line 104 in src/js/control-bar/progress-control/time-tooltip.js

View check run for this annotation

Codecov / codecov/patch

src/js/control-bar/progress-control/time-tooltip.js#L99-L104

Added lines #L99 - L104 were not covered by tests
}
} else {
// Adjust the `pullTooltipBy` distance to the left or right depending on
// the results of the space calculations above.
if (spaceLeftOfPoint < pullTooltipBy) {
pullTooltipBy += pullTooltipBy - spaceLeftOfPoint;

Check warning on line 110 in src/js/control-bar/progress-control/time-tooltip.js

View check run for this annotation

Codecov / codecov/patch

src/js/control-bar/progress-control/time-tooltip.js#L110

Added line #L110 was not covered by tests
} else if (spaceRightOfPoint < pullTooltipBy) {
pullTooltipBy = spaceRightOfPoint;

Check warning on line 112 in src/js/control-bar/progress-control/time-tooltip.js

View check run for this annotation

Codecov / codecov/patch

src/js/control-bar/progress-control/time-tooltip.js#L112

Added line #L112 was not covered by tests
}

// Due to the imprecision of decimal/ratio based calculations and varying
// rounding behaviors, there are cases where the spacing adjustment is off
// by a pixel or two. This adds insurance to these calculations.
if (pullTooltipBy < 0) {
pullTooltipBy = 0;
} else if (pullTooltipBy > tooltipRect.width) {
pullTooltipBy = tooltipRect.width;
// Due to the imprecision of decimal/ratio based calculations and varying
// rounding behaviors, there are cases where the spacing adjustment is off
// by a pixel or two. This adds insurance to these calculations.
if (pullTooltipBy < 0) {
pullTooltipBy = 0;

Check warning on line 119 in src/js/control-bar/progress-control/time-tooltip.js

View check run for this annotation

Codecov / codecov/patch

src/js/control-bar/progress-control/time-tooltip.js#L119

Added line #L119 was not covered by tests
} else if (pullTooltipBy > tooltipRect.width) {
pullTooltipBy = tooltipRect.width;

Check warning on line 121 in src/js/control-bar/progress-control/time-tooltip.js

View check run for this annotation

Codecov / codecov/patch

src/js/control-bar/progress-control/time-tooltip.js#L121

Added line #L121 was not covered by tests
}
}

// prevent small width fluctuations within 0.4px from
Expand Down