screenfull.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*!
  2. * screenfull
  3. * v4.2.0 - 2019-04-01
  4. * (c) Sindre Sorhus; MIT License
  5. */
  6. (function () {
  7. 'use strict';
  8. var document = typeof window !== 'undefined' && typeof window.document !== 'undefined' ? window.document : {};
  9. var isCommonjs = typeof module !== 'undefined' && module.exports;
  10. var keyboardAllowed = typeof Element !== 'undefined' && 'ALLOW_KEYBOARD_INPUT' in Element;
  11. var fn = (function () {
  12. var val;
  13. var fnMap = [
  14. [
  15. 'requestFullscreen',
  16. 'exitFullscreen',
  17. 'fullscreenElement',
  18. 'fullscreenEnabled',
  19. 'fullscreenchange',
  20. 'fullscreenerror'
  21. ],
  22. // New WebKit
  23. [
  24. 'webkitRequestFullscreen',
  25. 'webkitExitFullscreen',
  26. 'webkitFullscreenElement',
  27. 'webkitFullscreenEnabled',
  28. 'webkitfullscreenchange',
  29. 'webkitfullscreenerror'
  30. ],
  31. // Old WebKit (Safari 5.1)
  32. [
  33. 'webkitRequestFullScreen',
  34. 'webkitCancelFullScreen',
  35. 'webkitCurrentFullScreenElement',
  36. 'webkitCancelFullScreen',
  37. 'webkitfullscreenchange',
  38. 'webkitfullscreenerror'
  39. ],
  40. [
  41. 'mozRequestFullScreen',
  42. 'mozCancelFullScreen',
  43. 'mozFullScreenElement',
  44. 'mozFullScreenEnabled',
  45. 'mozfullscreenchange',
  46. 'mozfullscreenerror'
  47. ],
  48. [
  49. 'msRequestFullscreen',
  50. 'msExitFullscreen',
  51. 'msFullscreenElement',
  52. 'msFullscreenEnabled',
  53. 'MSFullscreenChange',
  54. 'MSFullscreenError'
  55. ]
  56. ];
  57. var i = 0;
  58. var l = fnMap.length;
  59. var ret = {};
  60. for (; i < l; i++) {
  61. val = fnMap[i];
  62. if (val && val[1] in document) {
  63. for (i = 0; i < val.length; i++) {
  64. ret[fnMap[0][i]] = val[i];
  65. }
  66. return ret;
  67. }
  68. }
  69. return false;
  70. })();
  71. var eventNameMap = {
  72. change: fn.fullscreenchange,
  73. error: fn.fullscreenerror
  74. };
  75. var screenfull = {
  76. request: function (elem) {
  77. return new Promise(function (resolve) {
  78. var request = fn.requestFullscreen;
  79. var onFullScreenEntered = function () {
  80. this.off('change', onFullScreenEntered);
  81. resolve();
  82. }.bind(this);
  83. elem = elem || document.documentElement;
  84. // Work around Safari 5.1 bug: reports support for
  85. // keyboard in fullscreen even though it doesn't.
  86. // Browser sniffing, since the alternative with
  87. // setTimeout is even worse.
  88. if (/ Version\/5\.1(?:\.\d+)? Safari\//.test(navigator.userAgent)) {
  89. elem[request]();
  90. } else {
  91. elem[request](keyboardAllowed ? Element.ALLOW_KEYBOARD_INPUT : {});
  92. }
  93. this.on('change', onFullScreenEntered);
  94. }.bind(this));
  95. },
  96. exit: function () {
  97. return new Promise(function (resolve) {
  98. if (!this.isFullscreen) {
  99. resolve();
  100. return;
  101. }
  102. var onFullScreenExit = function () {
  103. this.off('change', onFullScreenExit);
  104. resolve();
  105. }.bind(this);
  106. document[fn.exitFullscreen]();
  107. this.on('change', onFullScreenExit);
  108. }.bind(this));
  109. },
  110. toggle: function (elem) {
  111. return this.isFullscreen ? this.exit() : this.request(elem);
  112. },
  113. onchange: function (callback) {
  114. this.on('change', callback);
  115. },
  116. onerror: function (callback) {
  117. this.on('error', callback);
  118. },
  119. on: function (event, callback) {
  120. var eventName = eventNameMap[event];
  121. if (eventName) {
  122. document.addEventListener(eventName, callback, false);
  123. }
  124. },
  125. off: function (event, callback) {
  126. var eventName = eventNameMap[event];
  127. if (eventName) {
  128. document.removeEventListener(eventName, callback, false);
  129. }
  130. },
  131. raw: fn
  132. };
  133. if (!fn) {
  134. if (isCommonjs) {
  135. module.exports = false;
  136. } else {
  137. window.screenfull = false;
  138. }
  139. return;
  140. }
  141. Object.defineProperties(screenfull, {
  142. isFullscreen: {
  143. get: function () {
  144. return Boolean(document[fn.fullscreenElement]);
  145. }
  146. },
  147. element: {
  148. enumerable: true,
  149. get: function () {
  150. return document[fn.fullscreenElement];
  151. }
  152. },
  153. enabled: {
  154. enumerable: true,
  155. get: function () {
  156. // Coerce to boolean in case of old WebKit
  157. return Boolean(document[fn.fullscreenEnabled]);
  158. }
  159. }
  160. });
  161. if (isCommonjs) {
  162. module.exports = screenfull;
  163. // TODO: remove this in the next major version
  164. module.exports.default = screenfull;
  165. } else {
  166. window.screenfull = screenfull;
  167. }
  168. })();