Source: lib/polyfill/fullscreen.js

  1. /**
  2. * @license
  3. * Copyright 2016 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. goog.provide('shaka.polyfill.Fullscreen');
  18. goog.require('shaka.polyfill.register');
  19. /**
  20. * @namespace shaka.polyfill.Fullscreen
  21. *
  22. * @summary A polyfill to unify fullscreen APIs across browsers.
  23. * Many browsers have prefixed fullscreen methods on Element and document.
  24. * See {@link http://goo.gl/n7TYl0 Using fullscreen mode} on MDN for more
  25. * information.
  26. */
  27. /**
  28. * Install the polyfill if needed.
  29. */
  30. shaka.polyfill.Fullscreen.install = function() {
  31. if (!window.Document) {
  32. // Avoid errors on very old browsers.
  33. return;
  34. }
  35. var proto = Element.prototype;
  36. proto.requestFullscreen = proto.requestFullscreen ||
  37. proto.mozRequestFullScreen ||
  38. proto.msRequestFullscreen ||
  39. proto.webkitRequestFullscreen;
  40. proto = Document.prototype;
  41. proto.exitFullscreen = proto.exitFullscreen ||
  42. proto.mozCancelFullScreen ||
  43. proto.msExitFullscreen ||
  44. proto.webkitExitFullscreen;
  45. if (!('fullscreenElement' in document)) {
  46. Object.defineProperty(document, 'fullscreenElement', {
  47. get: function() {
  48. return document.mozFullScreenElement ||
  49. document.msFullscreenElement ||
  50. document.webkitFullscreenElement;
  51. }
  52. });
  53. Object.defineProperty(document, 'fullscreenEnabled', {
  54. get: function() {
  55. return document.mozFullScreenEnabled ||
  56. document.msFullscreenEnabled ||
  57. document.webkitFullscreenEnabled;
  58. }
  59. });
  60. }
  61. var proxy = shaka.polyfill.Fullscreen.proxyEvent_;
  62. document.addEventListener('webkitfullscreenchange', proxy);
  63. document.addEventListener('webkitfullscreenerror', proxy);
  64. document.addEventListener('mozfullscreenchange', proxy);
  65. document.addEventListener('mozfullscreenerror', proxy);
  66. document.addEventListener('MSFullscreenChange', proxy);
  67. document.addEventListener('MSFullscreenError', proxy);
  68. };
  69. /**
  70. * Proxy fullscreen events after changing their name.
  71. * @param {!Event} event
  72. * @private
  73. */
  74. shaka.polyfill.Fullscreen.proxyEvent_ = function(event) {
  75. var eventType = event.type.replace(/^(webkit|moz|MS)/, '').toLowerCase();
  76. var newEvent;
  77. // IE 11 does not have an Event constructor
  78. if (typeof(Event) === 'function') {
  79. newEvent = new Event(eventType, /** @type {EventInit} */(event));
  80. } else {
  81. newEvent = document.createEvent('Event');
  82. newEvent.initEvent(eventType, event.bubbles, event.cancelable);
  83. }
  84. event.target.dispatchEvent(newEvent);
  85. };
  86. shaka.polyfill.register(shaka.polyfill.Fullscreen.install);