Layout.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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_COLLAPSED = 'sidebar-collapse'
  26. const CLASS_NAME_SIDEBAR_FOCUSED = 'sidebar-focused'
  27. const CLASS_NAME_LAYOUT_FIXED = 'layout-fixed'
  28. const CLASS_NAME_CONTROL_SIDEBAR_SLIDE_OPEN = 'control-sidebar-slide-open'
  29. const CLASS_NAME_CONTROL_SIDEBAR_OPEN = 'control-sidebar-open'
  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. }
  46. // Public
  47. fixLayoutHeight(extra = null) {
  48. const $body = $('body')
  49. let controlSidebar = 0
  50. if ($body.hasClass(CLASS_NAME_CONTROL_SIDEBAR_SLIDE_OPEN) || $body.hasClass(CLASS_NAME_CONTROL_SIDEBAR_OPEN) || extra === 'control_sidebar') {
  51. controlSidebar = $(SELECTOR_CONTROL_SIDEBAR_CONTENT).outerHeight()
  52. }
  53. const heights = {
  54. window: $(window).height(),
  55. header: $(SELECTOR_HEADER).length > 0 ? $(SELECTOR_HEADER).outerHeight() : 0,
  56. footer: $(SELECTOR_FOOTER).length > 0 ? $(SELECTOR_FOOTER).outerHeight() : 0,
  57. sidebar: $(SELECTOR_SIDEBAR).length > 0 ? $(SELECTOR_SIDEBAR).height() : 0,
  58. controlSidebar
  59. }
  60. const max = this._max(heights)
  61. let offset = this._config.panelAutoHeight
  62. if (offset === true) {
  63. offset = 0
  64. }
  65. const $contentSelector = $(SELECTOR_CONTENT)
  66. if (offset !== false) {
  67. if (max === heights.controlSidebar) {
  68. $contentSelector.css(this._config.panelAutoHeightMode, (max + offset))
  69. } else if (max === heights.window) {
  70. $contentSelector.css(this._config.panelAutoHeightMode, (max + offset) - heights.header - heights.footer)
  71. } else {
  72. $contentSelector.css(this._config.panelAutoHeightMode, (max + offset) - heights.header)
  73. }
  74. if (this._isFooterFixed()) {
  75. $contentSelector.css(this._config.panelAutoHeightMode, parseFloat($contentSelector.css(this._config.panelAutoHeightMode)) + heights.footer)
  76. }
  77. }
  78. if (!$body.hasClass(CLASS_NAME_LAYOUT_FIXED)) {
  79. return
  80. }
  81. if (typeof $.fn.overlayScrollbars !== 'undefined') {
  82. $(SELECTOR_SIDEBAR).overlayScrollbars({
  83. className: this._config.scrollbarTheme,
  84. sizeAutoCapable: true,
  85. scrollbars: {
  86. autoHide: this._config.scrollbarAutoHide,
  87. clickScrolling: true
  88. }
  89. })
  90. } else {
  91. $(SELECTOR_SIDEBAR).css('overflow-y', 'auto')
  92. }
  93. }
  94. fixLoginRegisterHeight() {
  95. const $body = $('body')
  96. const $selector = $(`${SELECTOR_LOGIN_BOX}, ${SELECTOR_REGISTER_BOX}`)
  97. if ($selector.length === 0) {
  98. $body.css('height', 'auto')
  99. $('html').css('height', 'auto')
  100. } else {
  101. const boxHeight = $selector.height()
  102. if ($body.css(this._config.panelAutoHeightMode) !== boxHeight) {
  103. $body.css(this._config.panelAutoHeightMode, boxHeight)
  104. }
  105. }
  106. }
  107. // Private
  108. _init() {
  109. // Activate layout height watcher
  110. this.fixLayoutHeight()
  111. if (this._config.loginRegisterAutoHeight === true) {
  112. this.fixLoginRegisterHeight()
  113. } else if (this._config.loginRegisterAutoHeight === parseInt(this._config.loginRegisterAutoHeight, 10)) {
  114. setInterval(this.fixLoginRegisterHeight, this._config.loginRegisterAutoHeight)
  115. }
  116. $(SELECTOR_SIDEBAR)
  117. .on('collapsed.lte.treeview expanded.lte.treeview', () => {
  118. this.fixLayoutHeight()
  119. })
  120. $(SELECTOR_MAIN_SIDEBAR)
  121. .on('mouseenter mouseleave', () => {
  122. if ($('body').hasClass(CLASS_NAME_SIDEBAR_COLLAPSED)) {
  123. this.fixLayoutHeight()
  124. }
  125. })
  126. $(SELECTOR_PUSHMENU_BTN)
  127. .on('collapsed.lte.pushmenu shown.lte.pushmenu', () => {
  128. setTimeout(() => {
  129. this.fixLayoutHeight()
  130. }, 300)
  131. })
  132. $(SELECTOR_CONTROL_SIDEBAR_BTN)
  133. .on('collapsed.lte.controlsidebar', () => {
  134. this.fixLayoutHeight()
  135. })
  136. .on('expanded.lte.controlsidebar', () => {
  137. this.fixLayoutHeight('control_sidebar')
  138. })
  139. $(window).resize(() => {
  140. this.fixLayoutHeight()
  141. })
  142. setTimeout(() => {
  143. $('body.hold-transition').removeClass('hold-transition')
  144. }, 50)
  145. }
  146. _max(numbers) {
  147. // Calculate the maximum number in a list
  148. let max = 0
  149. Object.keys(numbers).forEach(key => {
  150. if (numbers[key] > max) {
  151. max = numbers[key]
  152. }
  153. })
  154. return max
  155. }
  156. _isFooterFixed() {
  157. return $(SELECTOR_FOOTER).css('position') === 'fixed'
  158. }
  159. // Static
  160. static _jQueryInterface(config = '') {
  161. return this.each(function () {
  162. let data = $(this).data(DATA_KEY)
  163. const _options = $.extend({}, Default, $(this).data())
  164. if (!data) {
  165. data = new Layout($(this), _options)
  166. $(this).data(DATA_KEY, data)
  167. }
  168. if (config === 'init' || config === '') {
  169. data._init()
  170. } else if (config === 'fixLayoutHeight' || config === 'fixLoginRegisterHeight') {
  171. data[config]()
  172. }
  173. })
  174. }
  175. }
  176. /**
  177. * Data API
  178. * ====================================================
  179. */
  180. $(window).on('load', () => {
  181. Layout._jQueryInterface.call($('body'))
  182. })
  183. $(`${SELECTOR_SIDEBAR} a`)
  184. .on('focusin', () => {
  185. $(SELECTOR_MAIN_SIDEBAR).addClass(CLASS_NAME_SIDEBAR_FOCUSED)
  186. })
  187. .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