diff options
author | Som Meaden <som@theprojectsomething.com> | 2020-04-04 13:43:51 +1000 |
---|---|---|
committer | Som Meaden <som@theprojectsomething.com> | 2020-04-04 13:43:51 +1000 |
commit | 49ed2cac4eff3ff3eae7a2c72e5280a302906f7d (patch) | |
tree | 3f7809731d8ab0e4e513b128d9a78b8c25b60916 /dist/plyr.mjs | |
parent | 44ef0bbc8754a11b0f6411e32e5383fd8573a88c (diff) | |
download | plyr-49ed2cac4eff3ff3eae7a2c72e5280a302906f7d.tar.lz plyr-49ed2cac4eff3ff3eae7a2c72e5280a302906f7d.tar.xz plyr-49ed2cac4eff3ff3eae7a2c72e5280a302906f7d.zip |
This is a PR to allow for contextual content to be included in fullscreen (or fallback) mode. This means arbitrary elements (extensions to the basic player UI) can be overlaid and remain visible when the player switches to fullscreen.
Example use-cases include:
- display of video title or other metadata (see the included demo)
- alternative access to menu items, such as a searchable captions list (in cases where many hundreds of languages are available)
- custom share dialogs
- integrated playlists with 'playing next' overlays
This approach / PR is just an example of how this feature could work and aims to keep Plyr complexity to a minimum (while enabling some fairly interesting integrations). It utilises a single config option, and does away with the need for injecting bespoke APIs or elements into the player context on a per-project basis. Or trying to mess with what is a pretty slick, but tightly coupled system.
For the user: A new `fullscreen.container` attribute is used to provide a container selector. The container must be an ancestor of the player, otherwise it's ignored. When toggling fullscreen mode, this container is now used in place of the player. Hovering over any children of the container is the same as hovering over the controls. The exception is where the player and the child share a common ancestor (that's not the fullscreen container) ... sounds complex but it's not. You can also gain pretty fine control this way with pointer events.
Under the hood: it adds a `utils/elements/closest` helper method to find the right ancestor. If found this is returned as the fullscreen target in place of the player container. Fullscreen is instantiated slightly earlier in the setup so this container is available for the `listeners.controls` call. In here we add some more 'mouseenter/mouseleave' listeners to any direct descendants of the container, that aren't also ancestors of the player. And that's it. No extra classes, nothing else. There are some style changes to the demo (top margin on the player) but these would be project specific.
Thanks for reading.
Diffstat (limited to 'dist/plyr.mjs')
-rw-r--r-- | dist/plyr.mjs | 57 |
1 files changed, 47 insertions, 10 deletions
diff --git a/dist/plyr.mjs b/dist/plyr.mjs index 59572923..c7a3d6a5 100644 --- a/dist/plyr.mjs +++ b/dist/plyr.mjs @@ -782,6 +782,25 @@ function matches$1(element, selector) { var method = prototype.matches || prototype.webkitMatchesSelector || prototype.mozMatchesSelector || prototype.msMatchesSelector || match; return method.call(element, selector); +} // Closest ancestor element matching selector (also tests element itself) + +function closest(element, selector) { + var _Element2 = Element, + prototype = _Element2.prototype; // https://developer.mozilla.org/en-US/docs/Web/API/Element/closest#Polyfill + + function closestElement() { + var el = this; + + do { + if (matches$1.matches(el, selector)) return el; + el = el.parentElement || el.parentNode; + } while (el !== null && el.nodeType === 1); + + return null; + } + + var method = prototype.closest || closestElement; + return method.call(element, selector); } // Find all elements function getElements(selector) { @@ -1290,7 +1309,7 @@ function dedupe(array) { }); } // Get the closest value in an array -function closest(array, value) { +function closest$1(array, value) { if (!is$1.array(array) || !array.length) { return null; } @@ -3627,6 +3646,9 @@ var defaults$1 = { fallback: true, // Fallback using full viewport/window iosNative: false // Use the native fullscreen in iOS (disables custom controls) + // Selector for the fullscreen container so contextual / non-player content can remain visible in fullscreen mode + // Non-ancestors of the player element will be ignored + // container: null, // defaults to the player element }, // Local storage @@ -3983,7 +4005,10 @@ var Fullscreen = /*#__PURE__*/function () { y: 0 }; // Force the use of 'full window/browser' rather than fullscreen - this.forceFallback = player.config.fullscreen.fallback === 'force'; // Register event listeners + this.forceFallback = player.config.fullscreen.fallback === 'force'; // Get the fullscreen element + // Checks container is an ancestor, defaults to null + + this.player.elements.fullscreen = player.config.fullscreen.container && closest(this.player.elements.container, player.config.fullscreen.container); // Register event listeners // Handle event (incase user presses escape etc) on.call(this.player, document, this.prefix === 'ms' ? 'MSFullscreenChange' : "".concat(this.prefix, "fullscreenchange"), function () { @@ -4209,7 +4234,7 @@ var Fullscreen = /*#__PURE__*/function () { }, { key: "target", get: function get() { - return browser.isIos && this.player.config.fullscreen.iosNative ? this.player.media : this.player.elements.container; + return browser.isIos && this.player.config.fullscreen.iosNative ? this.player.media : this.player.elements.fullscreen || this.player.elements.container; } }], [{ key: "native", @@ -5200,7 +5225,18 @@ var Listeners = /*#__PURE__*/function () { this.bind(elements.controls, 'mouseenter mouseleave', function (event) { elements.controls.hover = !player.touch && event.type === 'mouseenter'; - }); // Update controls.pressed state (used for ui.toggleControls to avoid hiding when interacting) + }); // Also update controls.hover state for any non-player children of fullscreen element (as above) + + if (elements.fullscreen) { + for (var i = 0; i < elements.fullscreen.children.length; i++) { + if (!elements.fullscreen.children[i].contains(elements.container)) { + this.bind(elements.fullscreen.children[i], 'mouseenter mouseleave', function (event) { + elements.controls.hover = !player.touch && event.type === 'mouseenter'; + }); + } + } + } // Update controls.pressed state (used for ui.toggleControls to avoid hiding when interacting) + this.bind(elements.controls, 'mousedown mouseup touchstart touchend touchcancel', function (event) { elements.controls.pressed = ['mousedown', 'touchstart'].includes(event.type); @@ -7958,6 +7994,7 @@ var Plyr = /*#__PURE__*/function () { this.elements = { container: null, + fullscreen: null, captions: null, buttons: {}, display: {}, @@ -8143,9 +8180,11 @@ var Plyr = /*#__PURE__*/function () { on.call(this, this.elements.container, this.config.events.join(' '), function (event) { _this.debug.log("event: ".concat(event.type)); }); - } // Setup interface - // If embed but not fully supported, build interface now to avoid flash of controls + } // Setup fullscreen + + this.fullscreen = new Fullscreen(this); // Setup interface + // If embed but not fully supported, build interface now to avoid flash of controls if (this.isHTML5 || this.isEmbed && !this.supported.ui) { ui.build.call(this); @@ -8154,9 +8193,7 @@ var Plyr = /*#__PURE__*/function () { this.listeners.container(); // Global listeners - this.listeners.global(); // Setup fullscreen - - this.fullscreen = new Fullscreen(this); // Setup ads if provided + this.listeners.global(); // Setup ads if provided if (this.config.ads.enabled) { this.ads = new Ads(this); @@ -8855,7 +8892,7 @@ var Plyr = /*#__PURE__*/function () { var updateStorage = true; if (!options.includes(quality)) { - var value = closest(options, quality); + var value = closest$1(options, quality); this.debug.warn("Unsupported quality option: ".concat(quality, ", using ").concat(value, " instead")); quality = value; // Don't update storage if quality is not supported |