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. }
  54. var proxy = shaka.polyfill.Fullscreen.proxyEvent_;
  55. document.addEventListener('webkitfullscreenchange', proxy);
  56. document.addEventListener('webkitfullscreenerror', proxy);
  57. document.addEventListener('mozfullscreenchange', proxy);
  58. document.addEventListener('mozfullscreenerror', proxy);
  59. document.addEventListener('MSFullscreenChange', proxy);
  60. document.addEventListener('MSFullscreenError', proxy);
  61. };
  62. /**
  63. * Proxy fullscreen events after changing their name.
  64. * @param {!Event} event
  65. * @private
  66. * @suppress {unnecessaryCasts}
  67. */
  68. shaka.polyfill.Fullscreen.proxyEvent_ = function(event) {
  69. var type2 = event.type.replace(/^(webkit|moz|MS)/, '').toLowerCase();
  70. var event2 = new Event(type2, /** @type {EventInit} */(event));
  71. event.target.dispatchEvent(event2);
  72. };
  73. shaka.polyfill.register(shaka.polyfill.Fullscreen.install);