Fullscreen.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 Default = {
  18. minimizeIcon: 'fa-compress-arrows-alt',
  19. maximizeIcon: 'fa-expand-arrows-alt'
  20. }
  21. /**
  22. * Class Definition
  23. * ====================================================
  24. */
  25. class Fullscreen {
  26. constructor(_element, _options) {
  27. this.element = _element
  28. this.options = $.extend({}, Default, _options)
  29. }
  30. // Public
  31. toggle() {
  32. if (document.fullscreenElement ||
  33. document.mozFullScreenElement ||
  34. document.webkitFullscreenElement ||
  35. document.msFullscreenElement) {
  36. this.windowed()
  37. } else {
  38. this.fullscreen()
  39. }
  40. }
  41. fullscreen() {
  42. if (document.documentElement.requestFullscreen) {
  43. document.documentElement.requestFullscreen()
  44. } else if (document.documentElement.webkitRequestFullscreen) {
  45. document.documentElement.webkitRequestFullscreen()
  46. } else if (document.documentElement.msRequestFullscreen) {
  47. document.documentElement.msRequestFullscreen()
  48. }
  49. $(SELECTOR_ICON).removeClass(this.options.maximizeIcon).addClass(this.options.minimizeIcon)
  50. }
  51. windowed() {
  52. if (document.exitFullscreen) {
  53. document.exitFullscreen()
  54. } else if (document.webkitExitFullscreen) {
  55. document.webkitExitFullscreen()
  56. } else if (document.msExitFullscreen) {
  57. document.msExitFullscreen()
  58. }
  59. $(SELECTOR_ICON).removeClass(this.options.minimizeIcon).addClass(this.options.maximizeIcon)
  60. }
  61. // Static
  62. static _jQueryInterface(config) {
  63. let data = $(this).data(DATA_KEY)
  64. if (!data) {
  65. data = $(this).data()
  66. }
  67. const _options = $.extend({}, Default, typeof config === 'object' ? config : data)
  68. const plugin = new Fullscreen($(this), _options)
  69. $(this).data(DATA_KEY, typeof config === 'object' ? config : data)
  70. if (typeof config === 'string' && config.match(/toggle|fullscreen|windowed/)) {
  71. plugin[config]()
  72. } else {
  73. plugin.init()
  74. }
  75. }
  76. }
  77. /**
  78. * Data API
  79. * ====================================================
  80. */
  81. $(document).on('click', SELECTOR_DATA_WIDGET, function () {
  82. Fullscreen._jQueryInterface.call($(this), 'toggle')
  83. })
  84. /**
  85. * jQuery API
  86. * ====================================================
  87. */
  88. $.fn[NAME] = Fullscreen._jQueryInterface
  89. $.fn[NAME].Constructor = Fullscreen
  90. $.fn[NAME].noConflict = function () {
  91. $.fn[NAME] = JQUERY_NO_CONFLICT
  92. return Fullscreen._jQueryInterface
  93. }
  94. export default Fullscreen