jquery.fullscreen.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * jQuery fullscreen plugin v2.0.0-git (9f8f97d127)
  3. * https://github.com/theopolisme/jquery-fullscreen
  4. *
  5. * Copyright (c) 2013 Theopolisme <theopolismewiki@gmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. ( function ( $ ) {
  22. var setupFullscreen,
  23. fsClass = 'jq-fullscreened';
  24. /**
  25. * On fullscreenchange, trigger a jq-fullscreen-change event
  26. * The event is given an object, which contains the fullscreened DOM element (element), if any
  27. * and a boolean value (fullscreen) indicating if we've entered or exited fullscreen mode
  28. * Also remove the 'fullscreened' class from elements that are no longer fullscreen
  29. */
  30. function handleFullscreenChange () {
  31. var fullscreenElement = document.fullscreenElement ||
  32. document.mozFullScreenElement ||
  33. document.webkitFullscreenElement ||
  34. document.msFullscreenElement;
  35. if ( !fullscreenElement ) {
  36. $( '.' + fsClass ).data( 'isFullscreened', false ).removeClass( fsClass );
  37. }
  38. $( document ).trigger( $.Event( 'jq-fullscreen-change', { element: fullscreenElement, fullscreen: !!fullscreenElement } ) );
  39. }
  40. /**
  41. * Enters full screen with the "this" element in focus.
  42. * Check the .data( 'isFullscreened' ) of the return value to check
  43. * success or failure, if you're into that sort of thing.
  44. * @chainable
  45. * @return {jQuery}
  46. */
  47. function enterFullscreen () {
  48. var element = this.get(0),
  49. $element = this.first();
  50. if ( element ) {
  51. if ( element.requestFullscreen ) {
  52. element.requestFullscreen();
  53. } else if ( element.mozRequestFullScreen ) {
  54. element.mozRequestFullScreen();
  55. } else if ( element.webkitRequestFullscreen ) {
  56. element.webkitRequestFullscreen();
  57. } else if ( element.msRequestFullscreen ) {
  58. element.msRequestFullscreen();
  59. } else {
  60. // Unable to make fullscreen
  61. $element.data( 'isFullscreened', false );
  62. return this;
  63. }
  64. // Add the fullscreen class and data attribute to `element`
  65. $element.addClass( fsClass ).data( 'isFullscreened', true );
  66. return this;
  67. } else {
  68. $element.data( 'isFullscreened', false );
  69. return this;
  70. }
  71. }
  72. /**
  73. * Brings the "this" element out of fullscreen.
  74. * Check the .data( 'isFullscreened' ) of the return value to check
  75. * success or failure, if you're into that sort of thing.
  76. * @chainable
  77. * @return {jQuery}
  78. */
  79. function exitFullscreen () {
  80. var fullscreenElement = ( document.fullscreenElement ||
  81. document.mozFullScreenElement ||
  82. document.webkitFullscreenElement ||
  83. document.msFullscreenElement );
  84. // Ensure that we only exit fullscreen if exitFullscreen() is being called on the same element that is currently fullscreen
  85. if ( fullscreenElement && this.get(0) === fullscreenElement ) {
  86. if ( document.exitFullscreen ) {
  87. document.exitFullscreen();
  88. } else if ( document.mozCancelFullScreen ) {
  89. document.mozCancelFullScreen();
  90. } else if ( document.webkitCancelFullScreen ) {
  91. document.webkitCancelFullScreen();
  92. } else if ( document.msExitFullscreen ) {
  93. document.msExitFullscreen();
  94. } else {
  95. // Unable to cancel fullscreen mode
  96. return this;
  97. }
  98. // We don't need to remove the fullscreen class here,
  99. // because it will be removed in handleFullscreenChange.
  100. // But we should change the data on the element so the
  101. // caller can check for success.
  102. this.first().data( 'isFullscreened', false );
  103. }
  104. return this;
  105. }
  106. /**
  107. * Set up fullscreen handling and install necessary event handlers.
  108. * Return false if fullscreen is not supported.
  109. */
  110. setupFullscreen = function () {
  111. if ( $.support.fullscreen ) {
  112. // When the fullscreen mode is changed, trigger the
  113. // fullscreen events (and when exiting,
  114. // also remove the fullscreen class)
  115. $( document ).on( 'fullscreenchange webkitfullscreenchange mozfullscreenchange MSFullscreenChange', handleFullscreenChange);
  116. // Convenience wrapper so that one only needs to listen for
  117. // 'fullscreenerror', not all of the prefixed versions
  118. $( document ).on( 'webkitfullscreenerror mozfullscreenerror MSFullscreenError', function () {
  119. $( document ).trigger( $.Event( 'fullscreenerror' ) );
  120. } );
  121. // Fullscreen has been set up, so always return true
  122. setupFullscreen = function () { return true; };
  123. return true;
  124. } else {
  125. // Always return false from now on, since fullscreen is not supported
  126. setupFullscreen = function () { return false; };
  127. return false;
  128. }
  129. };
  130. /**
  131. * Set up fullscreen handling if necessary, then make the first element
  132. * matching the given selector fullscreen
  133. * @chainable
  134. * @return {jQuery}
  135. */
  136. $.fn.enterFullscreen = function () {
  137. if ( setupFullscreen() ) {
  138. $.fn.enterFullscreen = enterFullscreen;
  139. return this.enterFullscreen();
  140. } else {
  141. $.fn.enterFullscreen = function () { return this; };
  142. return this;
  143. }
  144. };
  145. /**
  146. * Set up fullscreen handling if necessary, then cancel fullscreen mode
  147. * for the first element matching the given selector.
  148. * @chainable
  149. * @return {jQuery}
  150. */
  151. $.fn.exitFullscreen = function () {
  152. if ( setupFullscreen() ) {
  153. $.fn.exitFullscreen = exitFullscreen;
  154. return this.exitFullscreen();
  155. } else {
  156. $.fn.exitFullscreen = function () { return this; };
  157. return this;
  158. }
  159. };
  160. $.support.fullscreen = document.fullscreenEnabled ||
  161. document.webkitFullscreenEnabled ||
  162. document.mozFullScreenEnabled ||
  163. document.msFullscreenEnabled;
  164. }( jQuery ) );