Layout.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. CONTROL_SIDEBAR_CONTENT: '.control-sidebar-content',
  29. CONTROL_SIDEBAR_BTN: '[data-widget="control-sidebar"]',
  30. LAYOUT_FIXED : '.layout-fixed',
  31. FOOTER : '.main-footer',
  32. PUSHMENU_BTN : '[data-widget="pushmenu"]',
  33. LOGIN_BOX : '.login-box',
  34. REGISTER_BOX : '.register-box'
  35. }
  36. const ClassName = {
  37. HOLD : 'hold-transition',
  38. SIDEBAR : 'main-sidebar',
  39. CONTENT_FIXED : 'content-fixed',
  40. SIDEBAR_FOCUSED: 'sidebar-focused',
  41. LAYOUT_FIXED : 'layout-fixed',
  42. NAVBAR_FIXED : 'layout-navbar-fixed',
  43. FOOTER_FIXED : 'layout-footer-fixed',
  44. LOGIN_PAGE : 'login-page',
  45. REGISTER_PAGE : 'register-page',
  46. CONTROL_SIDEBAR_SLIDE_OPEN: 'control-sidebar-slide-open',
  47. CONTROL_SIDEBAR_OPEN: 'control-sidebar-open',
  48. }
  49. const Default = {
  50. scrollbarTheme : 'os-theme-light',
  51. scrollbarAutoHide: 'l'
  52. }
  53. /**
  54. * Class Definition
  55. * ====================================================
  56. */
  57. class Layout {
  58. constructor(element, config) {
  59. this._config = config
  60. this._element = element
  61. this._init()
  62. }
  63. // Public
  64. fixLayoutHeight(extra = null) {
  65. let control_sidebar = 0
  66. if ($('body').hasClass(ClassName.CONTROL_SIDEBAR_SLIDE_OPEN) || $('body').hasClass(ClassName.CONTROL_SIDEBAR_OPEN) || extra == 'control_sidebar') {
  67. control_sidebar = $(Selector.CONTROL_SIDEBAR_CONTENT).height()
  68. }
  69. const heights = {
  70. window: $(window).height(),
  71. header: $(Selector.HEADER).length !== 0 ? $(Selector.HEADER).outerHeight() : 0,
  72. footer: $(Selector.FOOTER).length !== 0 ? $(Selector.FOOTER).outerHeight() : 0,
  73. sidebar: $(Selector.SIDEBAR).length !== 0 ? $(Selector.SIDEBAR).height() : 0,
  74. control_sidebar: control_sidebar,
  75. }
  76. const max = this._max(heights)
  77. if (max == heights.control_sidebar) {
  78. $(Selector.CONTENT).css('min-height', max)
  79. } else if (max == heights.window) {
  80. $(Selector.CONTENT).css('min-height', max - heights.header - heights.footer)
  81. } else {
  82. $(Selector.CONTENT).css('min-height', max - heights.header)
  83. }
  84. if ($('body').hasClass(ClassName.LAYOUT_FIXED)) {
  85. $(Selector.CONTENT).css('min-height', max - heights.header - heights.footer)
  86. if (typeof $.fn.overlayScrollbars !== 'undefined') {
  87. $(Selector.SIDEBAR).overlayScrollbars({
  88. className : this._config.scrollbarTheme,
  89. sizeAutoCapable : true,
  90. scrollbars : {
  91. autoHide: this._config.scrollbarAutoHide,
  92. clickScrolling : true
  93. }
  94. })
  95. }
  96. }
  97. }
  98. // Private
  99. _init() {
  100. // Activate layout height watcher
  101. this.fixLayoutHeight()
  102. $(Selector.SIDEBAR)
  103. .on('collapsed.lte.treeview expanded.lte.treeview', () => {
  104. this.fixLayoutHeight()
  105. })
  106. $(Selector.PUSHMENU_BTN)
  107. .on('collapsed.lte.pushmenu shown.lte.pushmenu', () => {
  108. this.fixLayoutHeight()
  109. })
  110. $(Selector.CONTROL_SIDEBAR_BTN)
  111. .on('collapsed.lte.controlsidebar', () => {
  112. this.fixLayoutHeight()
  113. })
  114. .on('expanded.lte.controlsidebar', () => {
  115. this.fixLayoutHeight('control_sidebar')
  116. })
  117. $(window).resize(() => {
  118. this.fixLayoutHeight()
  119. })
  120. if ($(Selector.LOGIN_BOX + ', ' + Selector.REGISTER_BOX).length === 0) {
  121. $('body, html').css('height', 'auto')
  122. } else if ($(Selector.LOGIN_BOX + ', ' + Selector.REGISTER_BOX).length !== 0) {
  123. let box_height = $(Selector.LOGIN_BOX + ', ' + Selector.REGISTER_BOX).height()
  124. $('body').css('min-height', box_height);
  125. }
  126. $('body.hold-transition').removeClass('hold-transition')
  127. }
  128. _max(numbers) {
  129. // Calculate the maximum number in a list
  130. let max = 0
  131. Object.keys(numbers).forEach((key) => {
  132. if (numbers[key] > max) {
  133. max = numbers[key]
  134. }
  135. })
  136. return max
  137. }
  138. // Static
  139. static _jQueryInterface(config = '') {
  140. return this.each(function () {
  141. let data = $(this).data(DATA_KEY)
  142. const _options = $.extend({}, Default, $(this).data())
  143. if (!data) {
  144. data = new Layout($(this), _options)
  145. $(this).data(DATA_KEY, data)
  146. }
  147. if (config === 'init' || config === '') {
  148. data['_init']()
  149. }
  150. })
  151. }
  152. }
  153. /**
  154. * Data API
  155. * ====================================================
  156. */
  157. $(window).on('load', () => {
  158. Layout._jQueryInterface.call($('body'))
  159. })
  160. $(Selector.SIDEBAR + ' a').on('focusin', () => {
  161. $(Selector.MAIN_SIDEBAR).addClass(ClassName.SIDEBAR_FOCUSED);
  162. })
  163. $(Selector.SIDEBAR + ' a').on('focusout', () => {
  164. $(Selector.MAIN_SIDEBAR).removeClass(ClassName.SIDEBAR_FOCUSED);
  165. })
  166. /**
  167. * jQuery API
  168. * ====================================================
  169. */
  170. $.fn[NAME] = Layout._jQueryInterface
  171. $.fn[NAME].Constructor = Layout
  172. $.fn[NAME].noConflict = function () {
  173. $.fn[NAME] = JQUERY_NO_CONFLICT
  174. return Layout._jQueryInterface
  175. }
  176. return Layout
  177. })(jQuery)
  178. export default Layout