Layout.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /**
  2. * --------------------------------------------
  3. * AdminLTE Layout.js
  4. * License MIT
  5. * --------------------------------------------
  6. */
  7. import $ from 'jquery'
  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 === 'control_sidebar') {
  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. return
  81. }
  82. if (offset !== false) {
  83. $(Selector.CONTENT).css('min-height', (max + offset) - heights.header - heights.footer)
  84. }
  85. if (typeof $.fn.overlayScrollbars !== 'undefined') {
  86. $(Selector.SIDEBAR).overlayScrollbars({
  87. className: this._config.scrollbarTheme,
  88. sizeAutoCapable: true,
  89. scrollbars: {
  90. autoHide: this._config.scrollbarAutoHide,
  91. clickScrolling: true
  92. }
  93. })
  94. }
  95. }
  96. fixLoginRegisterHeight() {
  97. if ($(Selector.LOGIN_BOX + ', ' + Selector.REGISTER_BOX).length === 0) {
  98. $('body, html').css('height', 'auto')
  99. } else {
  100. const boxHeight = $(Selector.LOGIN_BOX + ', ' + Selector.REGISTER_BOX).height()
  101. if ($('body').css('min-height') !== boxHeight) {
  102. $('body').css('min-height', boxHeight)
  103. }
  104. }
  105. }
  106. // Private
  107. _init() {
  108. // Activate layout height watcher
  109. this.fixLayoutHeight()
  110. if (this._config.loginRegisterAutoHeight === true) {
  111. this.fixLoginRegisterHeight()
  112. } else if (this._config.loginRegisterAutoHeight === parseInt(this._config.loginRegisterAutoHeight, 10)) {
  113. setInterval(this.fixLoginRegisterHeight, this._config.loginRegisterAutoHeight)
  114. }
  115. $(Selector.SIDEBAR)
  116. .on('collapsed.lte.treeview expanded.lte.treeview', () => {
  117. this.fixLayoutHeight()
  118. })
  119. $(Selector.PUSHMENU_BTN)
  120. .on('collapsed.lte.pushmenu shown.lte.pushmenu', () => {
  121. this.fixLayoutHeight()
  122. })
  123. $(Selector.CONTROL_SIDEBAR_BTN)
  124. .on('collapsed.lte.controlsidebar', () => {
  125. this.fixLayoutHeight()
  126. })
  127. .on('expanded.lte.controlsidebar', () => {
  128. this.fixLayoutHeight('control_sidebar')
  129. })
  130. $(window).resize(() => {
  131. this.fixLayoutHeight()
  132. })
  133. setTimeout(() => {
  134. $('body.hold-transition').removeClass('hold-transition')
  135. }, 50)
  136. }
  137. _max(numbers) {
  138. // Calculate the maximum number in a list
  139. let max = 0
  140. Object.keys(numbers).forEach(key => {
  141. if (numbers[key] > max) {
  142. max = numbers[key]
  143. }
  144. })
  145. return max
  146. }
  147. _isFooterFixed() {
  148. return $('.main-footer').css('position') === 'fixed'
  149. }
  150. // Static
  151. static _jQueryInterface(config = '') {
  152. return this.each(function () {
  153. let data = $(this).data(DATA_KEY)
  154. const _options = $.extend({}, Default, $(this).data())
  155. if (!data) {
  156. data = new Layout($(this), _options)
  157. $(this).data(DATA_KEY, data)
  158. }
  159. if (config === 'init' || config === '') {
  160. data._init()
  161. } else if (config === 'fixLayoutHeight' || config === 'fixLoginRegisterHeight') {
  162. data[config]()
  163. }
  164. })
  165. }
  166. }
  167. /**
  168. * Data API
  169. * ====================================================
  170. */
  171. $(window).on('load', () => {
  172. Layout._jQueryInterface.call($('body'))
  173. })
  174. $(Selector.SIDEBAR + ' a').on('focusin', () => {
  175. $(Selector.MAIN_SIDEBAR).addClass(ClassName.SIDEBAR_FOCUSED)
  176. })
  177. $(Selector.SIDEBAR + ' a').on('focusout', () => {
  178. $(Selector.MAIN_SIDEBAR).removeClass(ClassName.SIDEBAR_FOCUSED)
  179. })
  180. /**
  181. * jQuery API
  182. * ====================================================
  183. */
  184. $.fn[NAME] = Layout._jQueryInterface
  185. $.fn[NAME].Constructor = Layout
  186. $.fn[NAME].noConflict = function () {
  187. $.fn[NAME] = JQUERY_NO_CONFLICT
  188. return Layout._jQueryInterface
  189. }
  190. export default Layout