lbry-desktop/ui/util/full-screen.js

50 lines
1.7 KiB
JavaScript
Raw Normal View History

2019-05-31 06:35:37 +02:00
/*
Polyfill functions for the HTML5 fullscreen api:
https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API
*/
2019-05-31 06:11:33 +02:00
const prefixes = {
exitFullscreen: ['exitFullscreen', 'msExitFullscreen', 'mozCancelFullScreen', 'webkitExitFullscreen'],
fullscreenChange: ['fullscreenChange', 'MSFullscreenChange', 'mozfullscreenchange', 'webkitfullscreenchange'],
2019-05-31 06:35:37 +02:00
fullscreenEnabled: ['fullscreenEnabled', 'msFullscreenEnabled', 'mozFullScreenEnabled', 'webkitFullscreenEnabled'],
fullscreenElement: ['fullscreenElement', 'msFullscreenElement', 'mozFullScreenElement', 'webkitFullscreenElement'],
requestFullscreen: ['requestFullscreen', 'msRequestFullscreen', 'mozRequestFullScreen', 'webkitRequestFullscreen'],
2019-05-31 06:11:33 +02:00
};
const getPrefix = () => {
let prefixIndex = 0;
// validate prefix
prefixes.fullscreenEnabled.some((prefix, index) => {
if (document[prefix] || document[prefix] === false) {
prefixIndex = index;
return true;
}
});
// prefix vendor index
return prefixIndex;
};
export const fullscreenElement = () => {
const index = getPrefix();
2019-06-05 04:26:57 +02:00
const prefix = prefixes.fullscreenElement[index];
return document[prefix];
2019-05-31 06:11:33 +02:00
};
export const requestFullscreen = elem => {
const index = getPrefix();
const prefix = prefixes.requestFullscreen[index];
elem[prefix] && elem[prefix]();
};
export const exitFullscreen = () => {
const index = getPrefix();
const prefix = prefixes.exitFullscreen[index];
document[prefix] && document[prefix]();
};
2019-10-14 02:04:58 +02:00
export const onFullscreenChange = (target, action, callback) => {
2019-05-31 06:11:33 +02:00
const index = getPrefix();
const prefix = prefixes.fullscreenChange[index];
2019-10-14 02:04:58 +02:00
target[`${action}EventListener`](prefix, callback, false);
2019-05-31 06:11:33 +02:00
};