screenfull.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*!
  2. * screenfull
  3. * v3.3.2 - 2017-10-27
  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. var request = fn.requestFullscreen;
  78. elem = elem || document.documentElement;
  79. // Work around Safari 5.1 bug: reports support for
  80. // keyboard in fullscreen even though it doesn't.
  81. // Browser sniffing, since the alternative with
  82. // setTimeout is even worse.
  83. if (/ Version\/5\.1(?:\.\d+)? Safari\//.test(navigator.userAgent)) {
  84. elem[request]();
  85. } else {
  86. elem[request](keyboardAllowed && Element.ALLOW_KEYBOARD_INPUT);
  87. }
  88. },
  89. exit: function () {
  90. document[fn.exitFullscreen]();
  91. },
  92. toggle: function (elem) {
  93. if (this.isFullscreen) {
  94. this.exit();
  95. } else {
  96. this.request(elem);
  97. }
  98. },
  99. onchange: function (callback) {
  100. this.on('change', callback);
  101. },
  102. onerror: function (callback) {
  103. this.on('error', callback);
  104. },
  105. on: function (event, callback) {
  106. var eventName = eventNameMap[event];
  107. if (eventName) {
  108. document.addEventListener(eventName, callback, false);
  109. }
  110. },
  111. off: function (event, callback) {
  112. var eventName = eventNameMap[event];
  113. if (eventName) {
  114. document.removeEventListener(eventName, callback, false);
  115. }
  116. },
  117. raw: fn
  118. };
  119. if (!fn) {
  120. if (isCommonjs) {
  121. module.exports = false;
  122. } else {
  123. window.screenfull = false;
  124. }
  125. return;
  126. }
  127. Object.defineProperties(screenfull, {
  128. isFullscreen: {
  129. get: function () {
  130. return Boolean(document[fn.fullscreenElement]);
  131. }
  132. },
  133. element: {
  134. enumerable: true,
  135. get: function () {
  136. return document[fn.fullscreenElement];
  137. }
  138. },
  139. enabled: {
  140. enumerable: true,
  141. get: function () {
  142. // Coerce to boolean in case of old WebKit
  143. return Boolean(document[fn.fullscreenEnabled]);
  144. }
  145. }
  146. });
  147. if (isCommonjs) {
  148. module.exports = screenfull;
  149. } else {
  150. window.screenfull = screenfull;
  151. }
  152. })();