fullscreen.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright 2012 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Functions for managing full screen status of the DOM.
  16. *
  17. */
  18. goog.provide('goog.dom.fullscreen');
  19. goog.provide('goog.dom.fullscreen.EventType');
  20. goog.require('goog.dom');
  21. goog.require('goog.userAgent');
  22. /**
  23. * Event types for full screen.
  24. * @enum {string}
  25. */
  26. goog.dom.fullscreen.EventType = {
  27. /** Dispatched by the Document when the fullscreen status changes. */
  28. CHANGE: (function() {
  29. if (goog.userAgent.WEBKIT) {
  30. return 'webkitfullscreenchange';
  31. }
  32. if (goog.userAgent.GECKO) {
  33. return 'mozfullscreenchange';
  34. }
  35. if (goog.userAgent.IE) {
  36. return 'MSFullscreenChange';
  37. }
  38. // Opera 12-14, and W3C standard (Draft):
  39. // https://dvcs.w3.org/hg/fullscreen/raw-file/tip/Overview.html
  40. return 'fullscreenchange';
  41. })()
  42. };
  43. /**
  44. * Determines if full screen is supported.
  45. * @param {!goog.dom.DomHelper=} opt_domHelper The DomHelper for the DOM being
  46. * queried. If not provided, use the current DOM.
  47. * @return {boolean} True iff full screen is supported.
  48. */
  49. goog.dom.fullscreen.isSupported = function(opt_domHelper) {
  50. var doc = goog.dom.fullscreen.getDocument_(opt_domHelper);
  51. var body = doc.body;
  52. return !!(
  53. body.webkitRequestFullscreen ||
  54. (body.mozRequestFullScreen && doc.mozFullScreenEnabled) ||
  55. (body.msRequestFullscreen && doc.msFullscreenEnabled) ||
  56. (body.requestFullscreen && doc.fullscreenEnabled));
  57. };
  58. /**
  59. * Requests putting the element in full screen.
  60. * @param {!Element} element The element to put full screen.
  61. */
  62. goog.dom.fullscreen.requestFullScreen = function(element) {
  63. if (element.webkitRequestFullscreen) {
  64. element.webkitRequestFullscreen();
  65. } else if (element.mozRequestFullScreen) {
  66. element.mozRequestFullScreen();
  67. } else if (element.msRequestFullscreen) {
  68. element.msRequestFullscreen();
  69. } else if (element.requestFullscreen) {
  70. element.requestFullscreen();
  71. }
  72. };
  73. /**
  74. * Requests putting the element in full screen with full keyboard access.
  75. * @param {!Element} element The element to put full screen.
  76. */
  77. goog.dom.fullscreen.requestFullScreenWithKeys = function(element) {
  78. if (element.mozRequestFullScreenWithKeys) {
  79. element.mozRequestFullScreenWithKeys();
  80. } else if (element.webkitRequestFullscreen) {
  81. element.webkitRequestFullscreen();
  82. } else {
  83. goog.dom.fullscreen.requestFullScreen(element);
  84. }
  85. };
  86. /**
  87. * Exits full screen.
  88. * @param {!goog.dom.DomHelper=} opt_domHelper The DomHelper for the DOM being
  89. * queried. If not provided, use the current DOM.
  90. */
  91. goog.dom.fullscreen.exitFullScreen = function(opt_domHelper) {
  92. var doc = goog.dom.fullscreen.getDocument_(opt_domHelper);
  93. if (doc.webkitCancelFullScreen) {
  94. doc.webkitCancelFullScreen();
  95. } else if (doc.mozCancelFullScreen) {
  96. doc.mozCancelFullScreen();
  97. } else if (doc.msExitFullscreen) {
  98. doc.msExitFullscreen();
  99. } else if (doc.exitFullscreen) {
  100. doc.exitFullscreen();
  101. }
  102. };
  103. /**
  104. * Determines if the document is full screen.
  105. * @param {!goog.dom.DomHelper=} opt_domHelper The DomHelper for the DOM being
  106. * queried. If not provided, use the current DOM.
  107. * @return {boolean} Whether the document is full screen.
  108. */
  109. goog.dom.fullscreen.isFullScreen = function(opt_domHelper) {
  110. var doc = goog.dom.fullscreen.getDocument_(opt_domHelper);
  111. // IE 11 doesn't have similar boolean property, so check whether
  112. // document.msFullscreenElement is null instead.
  113. return !!(
  114. doc.webkitIsFullScreen || doc.mozFullScreen || doc.msFullscreenElement ||
  115. doc.fullscreenElement);
  116. };
  117. /**
  118. * Get the root element in full screen mode.
  119. * @param {!goog.dom.DomHelper=} opt_domHelper The DomHelper for the DOM being
  120. * queried. If not provided, use the current DOM.
  121. * @return {?Element} The root element in full screen mode.
  122. */
  123. goog.dom.fullscreen.getFullScreenElement = function(opt_domHelper) {
  124. var doc = goog.dom.fullscreen.getDocument_(opt_domHelper);
  125. var element_list = [
  126. doc.webkitFullscreenElement, doc.mozFullScreenElement,
  127. doc.msFullscreenElement, doc.fullscreenElement
  128. ];
  129. for (var i = 0; i < element_list.length; i++) {
  130. if (element_list[i] != null) {
  131. return element_list[i];
  132. }
  133. }
  134. return null;
  135. };
  136. /**
  137. * Gets the document object of the dom.
  138. * @param {!goog.dom.DomHelper=} opt_domHelper The DomHelper for the DOM being
  139. * queried. If not provided, use the current DOM.
  140. * @return {!Document} The dom document.
  141. * @private
  142. */
  143. goog.dom.fullscreen.getDocument_ = function(opt_domHelper) {
  144. return opt_domHelper ? opt_domHelper.getDocument() :
  145. goog.dom.getDomHelper().getDocument();
  146. };