Layout.js 6.3 KB

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