Layout.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * --------------------------------------------
  3. * AdminLTE Layout.js
  4. * License MIT
  5. * --------------------------------------------
  6. */
  7. const Layout = (($) => {
  8. /**
  9. * Constants
  10. * ====================================================
  11. */
  12. const NAME = 'Layout'
  13. const DATA_KEY = 'lte.layout'
  14. const EVENT_KEY = `.${DATA_KEY}`
  15. const JQUERY_NO_CONFLICT = $.fn[NAME]
  16. const Event = {
  17. SIDEBAR: 'sidebar'
  18. }
  19. const Selector = {
  20. HEADER : '.main-header',
  21. MAIN_SIDEBAR : '.main-sidebar',
  22. SIDEBAR : '.main-sidebar .sidebar',
  23. CONTENT : '.content-wrapper',
  24. BRAND : '.brand-link',
  25. CONTENT_HEADER : '.content-header',
  26. WRAPPER : '.wrapper',
  27. CONTROL_SIDEBAR: '.control-sidebar',
  28. LAYOUT_FIXED : '.layout-fixed',
  29. FOOTER : '.main-footer',
  30. PUSHMENU_BTN : '[data-widget="pushmenu"]',
  31. LOGIN_BOX : '.login-box',
  32. REGISTER_BOX : '.register-box'
  33. }
  34. const ClassName = {
  35. HOLD : 'hold-transition',
  36. SIDEBAR : 'main-sidebar',
  37. CONTENT_FIXED : 'content-fixed',
  38. SIDEBAR_FOCUSED: 'sidebar-focused',
  39. LAYOUT_FIXED : 'layout-fixed',
  40. NAVBAR_FIXED : 'layout-navbar-fixed',
  41. FOOTER_FIXED : 'layout-footer-fixed',
  42. LOGIN_PAGE : 'login-page',
  43. REGISTER_PAGE : 'register-page',
  44. }
  45. const Default = {
  46. scrollbarTheme : 'os-theme-light',
  47. scrollbarAutoHide: 'l'
  48. }
  49. /**
  50. * Class Definition
  51. * ====================================================
  52. */
  53. class Layout {
  54. constructor(element, config) {
  55. this._config = config
  56. this._element = element
  57. this._init()
  58. }
  59. // Public
  60. fixLayoutHeight() {
  61. const heights = {
  62. window: $(window).height(),
  63. header: $(Selector.HEADER).length !== 0 ? $(Selector.HEADER).outerHeight() : 0,
  64. footer: $(Selector.FOOTER).length !== 0 ? $(Selector.FOOTER).outerHeight() : 0,
  65. sidebar: $(Selector.SIDEBAR).length !== 0 ? $(Selector.SIDEBAR).height() : 0,
  66. }
  67. const max = this._max(heights)
  68. if (max == heights.window) {
  69. $(Selector.CONTENT).css('min-height', max - heights.header - heights.footer)
  70. } else {
  71. $(Selector.CONTENT).css('min-height', max - heights.header)
  72. }
  73. if ($('body').hasClass(ClassName.LAYOUT_FIXED)) {
  74. $(Selector.CONTENT).css('min-height', max - heights.header - heights.footer)
  75. if (typeof $.fn.overlayScrollbars !== 'undefined') {
  76. $(Selector.SIDEBAR).overlayScrollbars({
  77. className : this._config.scrollbarTheme,
  78. sizeAutoCapable : true,
  79. scrollbars : {
  80. autoHide: this._config.scrollbarAutoHide,
  81. clickScrolling : true
  82. }
  83. })
  84. }
  85. }
  86. }
  87. // Private
  88. _init() {
  89. // Activate layout height watcher
  90. this.fixLayoutHeight()
  91. $(Selector.SIDEBAR)
  92. .on('collapsed.lte.treeview expanded.lte.treeview', () => {
  93. this.fixLayoutHeight()
  94. })
  95. $(Selector.PUSHMENU_BTN)
  96. .on('collapsed.lte.pushmenu shown.lte.pushmenu', () => {
  97. this.fixLayoutHeight()
  98. })
  99. $(window).resize(() => {
  100. this.fixLayoutHeight()
  101. })
  102. if (!$('body').hasClass(ClassName.LOGIN_PAGE) && !$('body').hasClass(ClassName.REGISTER_PAGE)) {
  103. $('body, html').css('height', 'auto')
  104. } else if ($('body').hasClass(ClassName.LOGIN_PAGE) || $('body').hasClass(ClassName.REGISTER_PAGE)) {
  105. let box_height = $(Selector.LOGIN_BOX + ', ' + Selector.REGISTER_BOX).height()
  106. $('body').css('min-height', box_height);
  107. }
  108. $('body.hold-transition').removeClass('hold-transition')
  109. }
  110. _max(numbers) {
  111. // Calculate the maximum number in a list
  112. let max = 0
  113. Object.keys(numbers).forEach((key) => {
  114. if (numbers[key] > max) {
  115. max = numbers[key]
  116. }
  117. })
  118. return max
  119. }
  120. // Static
  121. static _jQueryInterface(config) {
  122. return this.each(function () {
  123. let data = $(this).data(DATA_KEY)
  124. const _config = $.extend({}, Default, $(this).data())
  125. if (!data) {
  126. data = new Layout($(this), _config)
  127. $(this).data(DATA_KEY, data)
  128. }
  129. if (config === 'init') {
  130. data[config]()
  131. }
  132. })
  133. }
  134. }
  135. /**
  136. * Data API
  137. * ====================================================
  138. */
  139. $(window).on('load', () => {
  140. Layout._jQueryInterface.call($('body'))
  141. })
  142. $(Selector.SIDEBAR + ' a').on('focusin', () => {
  143. $(Selector.MAIN_SIDEBAR).addClass(ClassName.SIDEBAR_FOCUSED);
  144. })
  145. $(Selector.SIDEBAR + ' a').on('focusout', () => {
  146. $(Selector.MAIN_SIDEBAR).removeClass(ClassName.SIDEBAR_FOCUSED);
  147. })
  148. /**
  149. * jQuery API
  150. * ====================================================
  151. */
  152. $.fn[NAME] = Layout._jQueryInterface
  153. $.fn[NAME].Constructor = Layout
  154. $.fn[NAME].noConflict = function () {
  155. $.fn[NAME] = JQUERY_NO_CONFLICT
  156. return Layout._jQueryInterface
  157. }
  158. return Layout
  159. })(jQuery)
  160. export default Layout