polyfill.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. namespace polyfill {
  2. interface API {
  3. request: string;
  4. changeEvent: string;
  5. }
  6. const VARIANTS = [
  7. {
  8. request: 'requestFullscreen',
  9. changeEvent: 'fullscreenchange'
  10. },
  11. {
  12. request: 'mozRequestFullScreen',
  13. changeEvent: 'mozfullscreenchange',
  14. },
  15. {
  16. request: 'webkitRequestFullscreen',
  17. changeEvent: 'webkitfullscreenchange',
  18. },
  19. {
  20. request: 'msRequestFullscreen',
  21. changeEvent: 'MSFullscreenChange',
  22. },
  23. ];
  24. class FullscreenPolyfill {
  25. private api: API | undefined;
  26. constructor() {
  27. this.api = VARIANTS.find((variant) =>
  28. // @ts-ignore
  29. document.firstChild[variant.request] !== undefined
  30. );
  31. }
  32. request(element: HTMLElement) {
  33. if (this.api !== undefined) {
  34. // @ts-ignore
  35. element[this.api.request]();
  36. }
  37. }
  38. addEventListener(listener: () => void) {
  39. if (this.api !== undefined) {
  40. document.addEventListener(this.api.changeEvent, listener);
  41. }
  42. }
  43. }
  44. export const fullscreen = new FullscreenPolyfill();
  45. }