Fullscreen.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import defined from './defined.js';
  2. import defineProperties from './defineProperties.js';
  3. var _supportsFullscreen;
  4. var _names = {
  5. requestFullscreen : undefined,
  6. exitFullscreen : undefined,
  7. fullscreenEnabled : undefined,
  8. fullscreenElement : undefined,
  9. fullscreenchange : undefined,
  10. fullscreenerror : undefined
  11. };
  12. /**
  13. * Browser-independent functions for working with the standard fullscreen API.
  14. *
  15. * @exports Fullscreen
  16. * @namespace
  17. *
  18. * @see {@link http://dvcs.w3.org/hg/fullscreen/raw-file/tip/Overview.html|W3C Fullscreen Living Specification}
  19. */
  20. var Fullscreen = {};
  21. defineProperties(Fullscreen, {
  22. /**
  23. * The element that is currently fullscreen, if any. To simply check if the
  24. * browser is in fullscreen mode or not, use {@link Fullscreen#fullscreen}.
  25. * @memberof Fullscreen
  26. * @type {Object}
  27. * @readonly
  28. */
  29. element : {
  30. get : function() {
  31. if (!Fullscreen.supportsFullscreen()) {
  32. return undefined;
  33. }
  34. return document[_names.fullscreenElement];
  35. }
  36. },
  37. /**
  38. * The name of the event on the document that is fired when fullscreen is
  39. * entered or exited. This event name is intended for use with addEventListener.
  40. * In your event handler, to determine if the browser is in fullscreen mode or not,
  41. * use {@link Fullscreen#fullscreen}.
  42. * @memberof Fullscreen
  43. * @type {String}
  44. * @readonly
  45. */
  46. changeEventName : {
  47. get : function() {
  48. if (!Fullscreen.supportsFullscreen()) {
  49. return undefined;
  50. }
  51. return _names.fullscreenchange;
  52. }
  53. },
  54. /**
  55. * The name of the event that is fired when a fullscreen error
  56. * occurs. This event name is intended for use with addEventListener.
  57. * @memberof Fullscreen
  58. * @type {String}
  59. * @readonly
  60. */
  61. errorEventName : {
  62. get : function() {
  63. if (!Fullscreen.supportsFullscreen()) {
  64. return undefined;
  65. }
  66. return _names.fullscreenerror;
  67. }
  68. },
  69. /**
  70. * Determine whether the browser will allow an element to be made fullscreen, or not.
  71. * For example, by default, iframes cannot go fullscreen unless the containing page
  72. * adds an "allowfullscreen" attribute (or prefixed equivalent).
  73. * @memberof Fullscreen
  74. * @type {Boolean}
  75. * @readonly
  76. */
  77. enabled : {
  78. get : function() {
  79. if (!Fullscreen.supportsFullscreen()) {
  80. return undefined;
  81. }
  82. return document[_names.fullscreenEnabled];
  83. }
  84. },
  85. /**
  86. * Determines if the browser is currently in fullscreen mode.
  87. * @memberof Fullscreen
  88. * @type {Boolean}
  89. * @readonly
  90. */
  91. fullscreen : {
  92. get : function() {
  93. if (!Fullscreen.supportsFullscreen()) {
  94. return undefined;
  95. }
  96. return Fullscreen.element !== null;
  97. }
  98. }
  99. });
  100. /**
  101. * Detects whether the browser supports the standard fullscreen API.
  102. *
  103. * @returns {Boolean} <code>true</code> if the browser supports the standard fullscreen API,
  104. * <code>false</code> otherwise.
  105. */
  106. Fullscreen.supportsFullscreen = function() {
  107. if (defined(_supportsFullscreen)) {
  108. return _supportsFullscreen;
  109. }
  110. _supportsFullscreen = false;
  111. var body = document.body;
  112. if (typeof body.requestFullscreen === 'function') {
  113. // go with the unprefixed, standard set of names
  114. _names.requestFullscreen = 'requestFullscreen';
  115. _names.exitFullscreen = 'exitFullscreen';
  116. _names.fullscreenEnabled = 'fullscreenEnabled';
  117. _names.fullscreenElement = 'fullscreenElement';
  118. _names.fullscreenchange = 'fullscreenchange';
  119. _names.fullscreenerror = 'fullscreenerror';
  120. _supportsFullscreen = true;
  121. return _supportsFullscreen;
  122. }
  123. //check for the correct combination of prefix plus the various names that browsers use
  124. var prefixes = ['webkit', 'moz', 'o', 'ms', 'khtml'];
  125. var name;
  126. for (var i = 0, len = prefixes.length; i < len; ++i) {
  127. var prefix = prefixes[i];
  128. // casing of Fullscreen differs across browsers
  129. name = prefix + 'RequestFullscreen';
  130. if (typeof body[name] === 'function') {
  131. _names.requestFullscreen = name;
  132. _supportsFullscreen = true;
  133. } else {
  134. name = prefix + 'RequestFullScreen';
  135. if (typeof body[name] === 'function') {
  136. _names.requestFullscreen = name;
  137. _supportsFullscreen = true;
  138. }
  139. }
  140. // disagreement about whether it's "exit" as per spec, or "cancel"
  141. name = prefix + 'ExitFullscreen';
  142. if (typeof document[name] === 'function') {
  143. _names.exitFullscreen = name;
  144. } else {
  145. name = prefix + 'CancelFullScreen';
  146. if (typeof document[name] === 'function') {
  147. _names.exitFullscreen = name;
  148. }
  149. }
  150. // casing of Fullscreen differs across browsers
  151. name = prefix + 'FullscreenEnabled';
  152. if (document[name] !== undefined) {
  153. _names.fullscreenEnabled = name;
  154. } else {
  155. name = prefix + 'FullScreenEnabled';
  156. if (document[name] !== undefined) {
  157. _names.fullscreenEnabled = name;
  158. }
  159. }
  160. // casing of Fullscreen differs across browsers
  161. name = prefix + 'FullscreenElement';
  162. if (document[name] !== undefined) {
  163. _names.fullscreenElement = name;
  164. } else {
  165. name = prefix + 'FullScreenElement';
  166. if (document[name] !== undefined) {
  167. _names.fullscreenElement = name;
  168. }
  169. }
  170. // thankfully, event names are all lowercase per spec
  171. name = prefix + 'fullscreenchange';
  172. // event names do not have 'on' in the front, but the property on the document does
  173. if (document['on' + name] !== undefined) {
  174. //except on IE
  175. if (prefix === 'ms') {
  176. name = 'MSFullscreenChange';
  177. }
  178. _names.fullscreenchange = name;
  179. }
  180. name = prefix + 'fullscreenerror';
  181. if (document['on' + name] !== undefined) {
  182. //except on IE
  183. if (prefix === 'ms') {
  184. name = 'MSFullscreenError';
  185. }
  186. _names.fullscreenerror = name;
  187. }
  188. }
  189. return _supportsFullscreen;
  190. };
  191. /**
  192. * Asynchronously requests the browser to enter fullscreen mode on the given element.
  193. * If fullscreen mode is not supported by the browser, does nothing.
  194. *
  195. * @param {Object} element The HTML element which will be placed into fullscreen mode.
  196. * @param {HMDVRDevice} [vrDevice] The VR device.
  197. *
  198. * @example
  199. * // Put the entire page into fullscreen.
  200. * Cesium.Fullscreen.requestFullscreen(document.body)
  201. *
  202. * // Place only the Cesium canvas into fullscreen.
  203. * Cesium.Fullscreen.requestFullscreen(scene.canvas)
  204. */
  205. Fullscreen.requestFullscreen = function(element, vrDevice) {
  206. if (!Fullscreen.supportsFullscreen()) {
  207. return;
  208. }
  209. element[_names.requestFullscreen]({ vrDisplay: vrDevice });
  210. };
  211. /**
  212. * Asynchronously exits fullscreen mode. If the browser is not currently
  213. * in fullscreen, or if fullscreen mode is not supported by the browser, does nothing.
  214. */
  215. Fullscreen.exitFullscreen = function() {
  216. if (!Fullscreen.supportsFullscreen()) {
  217. return;
  218. }
  219. document[_names.exitFullscreen]();
  220. };
  221. //For unit tests
  222. Fullscreen._names = _names;
  223. export default Fullscreen;