Layout.js 6.6 KB

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