JAVASCRIPT   4

constants.js

Guest on 20th July 2021 03:27:04 PM

  1. 'use strict';
  2.  
  3. import window from 'global/window';
  4. import document from 'global/document';
  5. import mejs from '../core/mejs';
  6.  
  7. export const NAV = window.navigator;
  8. export const UA = NAV.userAgent.toLowerCase();
  9. export const IS_IPAD = /ipad/i.test(UA) && !window.MSStream;
  10. export const IS_IPHONE = /iphone/i.test(UA) && !window.MSStream;
  11. export const IS_IPOD = /ipod/i.test(UA) && !window.MSStream;
  12. export const IS_IOS = /ipad|iphone|ipod/i.test(UA) && !window.MSStream;
  13. export const IS_ANDROID = /android/i.test(UA);
  14. export const IS_IE = /(trident|microsoft)/i.test(NAV.appName);
  15. export const IS_EDGE = ('msLaunchUri' in NAV && !('documentMode' in document));
  16. export const IS_CHROME = /chrome/i.test(UA);
  17. export const IS_FIREFOX = /firefox/i.test(UA);
  18. export const IS_SAFARI = /safari/i.test(UA) && !IS_CHROME;
  19. export const IS_STOCK_ANDROID = /^mozilla\/\d+\.\d+\s\(linux;\su;/i.test(UA);
  20. export const HAS_MSE = ('MediaSource' in window);
  21. export const SUPPORT_POINTER_EVENTS = (() => {
  22.         const
  23.                 element = document.createElement('x'),
  24.                 documentElement = document.documentElement,
  25.                 getComputedStyle = window.getComputedStyle
  26.         ;
  27.  
  28.         if (!('pointerEvents' in element.style)) {
  29.                 return false;
  30.         }
  31.  
  32.         element.style.pointerEvents = 'auto';
  33.         element.style.pointerEvents = 'x';
  34.         documentElement.appendChild(element);
  35.         let supports = getComputedStyle && (getComputedStyle(element, '') || {}).pointerEvents === 'auto';
  36.         element.remove();
  37.         return !!supports;
  38. })();
  39.  
  40. // Test via a getter in the options object to see if the passive property is accessed
  41. export const SUPPORT_PASSIVE_EVENT = (() => {
  42.         let supportsPassive = false;
  43.         try {
  44.                 const opts = Object.defineProperty({}, 'passive', {
  45.                         get: function() {
  46.                                 supportsPassive = true;
  47.                         }
  48.                 });
  49.                 window.addEventListener('test', null, opts);
  50.         } catch (e) {}
  51.  
  52.         return supportsPassive;
  53. })();
  54.  
  55. // for IE
  56. const html5Elements = ['source', 'track', 'audio', 'video'];
  57. let video;
  58.  
  59. for (let i = 0, total = html5Elements.length; i < total; i++) {
  60.         video = document.createElement(html5Elements[i]);
  61. }
  62.  
  63. // Test if browsers support HLS natively (right now Safari, Android's Chrome and Stock browsers, and MS Edge)
  64. export const SUPPORTS_NATIVE_HLS = (IS_SAFARI || (IS_IE && /edge/i.test(UA)));
  65.  
  66. // Detect native JavaScript fullscreen (Safari/Firefox only, Chrome still fails)
  67.  
  68. // iOS
  69. let hasiOSFullScreen = (video.webkitEnterFullscreen !== undefined);
  70.  
  71. // W3C
  72. let hasNativeFullscreen = (video.requestFullscreen !== undefined);
  73.  
  74. // OS X 10.5 can't do this even if it says it can :(
  75. if (hasiOSFullScreen && /mac os x 10_5/i.test(UA)) {
  76.         hasNativeFullscreen = false;
  77.         hasiOSFullScreen = false;
  78. }
  79.  
  80. // webkit/firefox/IE11+
  81. const hasWebkitNativeFullScreen = (video.webkitRequestFullScreen !== undefined);
  82. const hasMozNativeFullScreen = (video.mozRequestFullScreen !== undefined);
  83. const hasMsNativeFullScreen = (video.msRequestFullscreen !== undefined);
  84. const hasTrueNativeFullScreen = (hasWebkitNativeFullScreen || hasMozNativeFullScreen || hasMsNativeFullScreen);
  85. let nativeFullScreenEnabled = hasTrueNativeFullScreen;
  86. let fullScreenEventName = '';
  87. let isFullScreen, requestFullScreen, cancelFullScreen;
  88.  
  89. // Enabled?
  90. if (hasMozNativeFullScreen) {
  91.         nativeFullScreenEnabled = document.mozFullScreenEnabled;
  92. } else if (hasMsNativeFullScreen) {
  93.         nativeFullScreenEnabled = document.msFullscreenEnabled;
  94. }
  95.  
  96. if (IS_CHROME) {
  97.         hasiOSFullScreen = false;
  98. }
  99.  
  100. if (hasTrueNativeFullScreen) {
  101.         if (hasWebkitNativeFullScreen) {
  102.                 fullScreenEventName = 'webkitfullscreenchange';
  103.         } else if (hasMozNativeFullScreen) {
  104.                 fullScreenEventName = 'fullscreenchange';
  105.         } else if (hasMsNativeFullScreen) {
  106.                 fullScreenEventName = 'MSFullscreenChange';
  107.         }
  108.  
  109.         isFullScreen = () =>  {
  110.                 if (hasMozNativeFullScreen) {
  111.                         return document.mozFullScreen;
  112.  
  113.                 } else if (hasWebkitNativeFullScreen) {
  114.                         return document.webkitIsFullScreen;
  115.  
  116.                 } else if (hasMsNativeFullScreen) {
  117.                         return document.msFullscreenElement !== null;
  118.                 }
  119.         };
  120.  
  121.         requestFullScreen = (el) => {
  122.                 if (hasWebkitNativeFullScreen) {
  123.                         el.webkitRequestFullScreen();
  124.                 } else if (hasMozNativeFullScreen) {
  125.                         el.mozRequestFullScreen();
  126.                 } else if (hasMsNativeFullScreen) {
  127.                         el.msRequestFullscreen();
  128.                 }
  129.         };
  130.  
  131.         cancelFullScreen = () => {
  132.                 if (hasWebkitNativeFullScreen) {
  133.                         document.webkitCancelFullScreen();
  134.  
  135.                 } else if (hasMozNativeFullScreen) {
  136.                         document.mozCancelFullScreen();
  137.  
  138.                 } else if (hasMsNativeFullScreen) {
  139.                         document.msExitFullscreen();
  140.  
  141.                 }
  142.         };
  143. }
  144.  
  145. export const HAS_NATIVE_FULLSCREEN = hasNativeFullscreen;
  146. export const HAS_WEBKIT_NATIVE_FULLSCREEN = hasWebkitNativeFullScreen;
  147. export const HAS_MOZ_NATIVE_FULLSCREEN = hasMozNativeFullScreen;
  148. export const HAS_MS_NATIVE_FULLSCREEN = hasMsNativeFullScreen;
  149. export const HAS_IOS_FULLSCREEN = hasiOSFullScreen;
  150. export const HAS_TRUE_NATIVE_FULLSCREEN = hasTrueNativeFullScreen;
  151. export const HAS_NATIVE_FULLSCREEN_ENABLED = nativeFullScreenEnabled;
  152. export const FULLSCREEN_EVENT_NAME = fullScreenEventName;
  153. export {isFullScreen, requestFullScreen, cancelFullScreen};
  154.  
  155. mejs.Features = mejs.Features || {};
  156. mejs.Features.isiPad = IS_IPAD;
  157. mejs.Features.isiPod = IS_IPOD;
  158. mejs.Features.isiPhone = IS_IPHONE;
  159. mejs.Features.isiOS = mejs.Features.isiPhone || mejs.Features.isiPad;
  160. mejs.Features.isAndroid = IS_ANDROID;
  161. mejs.Features.isIE = IS_IE;
  162. mejs.Features.isEdge = IS_EDGE;
  163. mejs.Features.isChrome = IS_CHROME;
  164. mejs.Features.isFirefox = IS_FIREFOX;
  165. mejs.Features.isSafari = IS_SAFARI;
  166. mejs.Features.isStockAndroid = IS_STOCK_ANDROID;
  167. mejs.Features.hasMSE = HAS_MSE;
  168. mejs.Features.supportsNativeHLS = SUPPORTS_NATIVE_HLS;
  169. mejs.Features.supportsPointerEvents = SUPPORT_POINTER_EVENTS;
  170. mejs.Features.supportsPassiveEvent = SUPPORT_PASSIVE_EVENT;
  171. mejs.Features.hasiOSFullScreen = HAS_IOS_FULLSCREEN;
  172. mejs.Features.hasNativeFullscreen = HAS_NATIVE_FULLSCREEN;
  173. mejs.Features.hasWebkitNativeFullScreen = HAS_WEBKIT_NATIVE_FULLSCREEN;
  174. mejs.Features.hasMozNativeFullScreen = HAS_MOZ_NATIVE_FULLSCREEN;
  175. mejs.Features.hasMsNativeFullScreen = HAS_MS_NATIVE_FULLSCREEN;
  176. mejs.Features.hasTrueNativeFullScreen = HAS_TRUE_NATIVE_FULLSCREEN;
  177. mejs.Features.nativeFullScreenEnabled = HAS_NATIVE_FULLSCREEN_ENABLED;
  178. mejs.Features.fullScreenEventName = FULLSCREEN_EVENT_NAME;
  179. mejs.Features.isFullScreen = isFullScreen;
  180. mejs.Features.requestFullScreen = requestFullScreen;
  181. mejs.Features.cancelFullScreen = cancelFullScreen;

Raw Paste


Login or Register to edit or fork this paste. It's free.