Fullscreen.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * --------------------------------------------
  3. * AdminLTE Fullscreen.js
  4. * License MIT
  5. * --------------------------------------------
  6. */
  7. import $ from 'jquery'
  8. /**
  9. * Constants
  10. * ====================================================
  11. */
  12. const NAME = 'Fullscreen'
  13. const DATA_KEY = 'lte.fullscreen'
  14. const JQUERY_NO_CONFLICT = $.fn[NAME]
  15. const SELECTOR_DATA_WIDGET = '[data-widget="fullscreen"]'
  16. const SELECTOR_ICON = `${SELECTOR_DATA_WIDGET} i`
  17. const EVENT_FULLSCREEN_CHANGE = 'webkitfullscreenchange mozfullscreenchange fullscreenchange MSFullscreenChange'
  18. const Default = {
  19. minimizeIcon: 'fa-compress-arrows-alt',
  20. maximizeIcon: 'fa-expand-arrows-alt'
  21. }
  22. /**
  23. * Class Definition
  24. * ====================================================
  25. */
  26. class Fullscreen {
  27. constructor(_element, _options) {
  28. this.element = _element
  29. this.options = _options
  30. }
  31. // Public
  32. toggle() {
  33. if (document.fullscreenElement ||
  34. document.mozFullScreenElement ||
  35. document.webkitFullscreenElement ||
  36. document.msFullscreenElement) {
  37. this.windowed()
  38. } else {
  39. this.fullscreen()
  40. }
  41. }
  42. toggleIcon() {
  43. if (document.fullscreenElement ||
  44. document.mozFullScreenElement ||
  45. document.webkitFullscreenElement ||
  46. document.msFullscreenElement) {
  47. $(SELECTOR_ICON).removeClass(this.options.maximizeIcon).addClass(this.options.minimizeIcon)
  48. } else {
  49. $(SELECTOR_ICON).removeClass(this.options.minimizeIcon).addClass(this.options.maximizeIcon)
  50. }
  51. }
  52. fullscreen() {
  53. if (document.documentElement.requestFullscreen) {
  54. document.documentElement.requestFullscreen()
  55. } else if (document.documentElement.webkitRequestFullscreen) {
  56. document.documentElement.webkitRequestFullscreen()
  57. } else if (document.documentElement.msRequestFullscreen) {
  58. document.documentElement.msRequestFullscreen()
  59. }
  60. }
  61. windowed() {
  62. if (document.exitFullscreen) {
  63. document.exitFullscreen()
  64. } else if (document.webkitExitFullscreen) {
  65. document.webkitExitFullscreen()
  66. } else if (document.msExitFullscreen) {
  67. document.msExitFullscreen()
  68. }
  69. }
  70. // Static
  71. static _jQueryInterface(config) {
  72. return this.each(function () {
  73. let data = $(this).data(DATA_KEY)
  74. const _config = $.extend({}, Default, typeof config === 'object' ? config : $(this).data())
  75. if (!data) {
  76. data = new Fullscreen($(this), _config)
  77. $(this).data(DATA_KEY, data)
  78. } else if (typeof config === 'string') {
  79. if (typeof data[config] === 'undefined') {
  80. throw new TypeError(`No method named "${config}"`)
  81. }
  82. data[config]()
  83. }
  84. })
  85. }
  86. }
  87. /**
  88. * Data API
  89. * ====================================================
  90. */
  91. $(document).on('click', SELECTOR_DATA_WIDGET, function () {
  92. Fullscreen._jQueryInterface.call($(this), 'toggle')
  93. })
  94. $(document).on(EVENT_FULLSCREEN_CHANGE, () => {
  95. Fullscreen._jQueryInterface.call($(SELECTOR_DATA_WIDGET), 'toggleIcon')
  96. })
  97. /**
  98. * jQuery API
  99. * ====================================================
  100. */
  101. $.fn[NAME] = Fullscreen._jQueryInterface
  102. $.fn[NAME].Constructor = Fullscreen
  103. $.fn[NAME].noConflict = function () {
  104. $.fn[NAME] = JQUERY_NO_CONFLICT
  105. return Fullscreen._jQueryInterface
  106. }
  107. export default Fullscreen