Layout.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. const $body = $('body')
  52. let controlSidebar = 0
  53. if ($body.hasClass(ClassName.CONTROL_SIDEBAR_SLIDE_OPEN) || $body.hasClass(ClassName.CONTROL_SIDEBAR_OPEN) || extra === 'control_sidebar') {
  54. controlSidebar = $(Selector.CONTROL_SIDEBAR_CONTENT).height()
  55. }
  56. const heights = {
  57. window: $(window).height(),
  58. header: $(Selector.HEADER).length !== 0 ? $(Selector.HEADER).outerHeight() : 0,
  59. footer: $(Selector.FOOTER).length !== 0 ? $(Selector.FOOTER).outerHeight() : 0,
  60. sidebar: $(Selector.SIDEBAR).length !== 0 ? $(Selector.SIDEBAR).height() : 0,
  61. controlSidebar
  62. }
  63. const max = this._max(heights)
  64. let offset = this._config.panelAutoHeight
  65. if (offset === true) {
  66. offset = 0
  67. }
  68. const $contentSelector = $(Selector.CONTENT)
  69. if (offset !== false) {
  70. if (max === heights.controlSidebar) {
  71. $contentSelector.css('min-height', (max + offset))
  72. } else if (max === heights.window) {
  73. $contentSelector.css('min-height', (max + offset) - heights.header - heights.footer)
  74. } else {
  75. $contentSelector.css('min-height', (max + offset) - heights.header)
  76. }
  77. if (this._isFooterFixed()) {
  78. $contentSelector.css('min-height', parseFloat($contentSelector.css('min-height')) + heights.footer)
  79. }
  80. }
  81. if (!$body.hasClass(ClassName.LAYOUT_FIXED)) {
  82. return
  83. }
  84. if (offset !== false) {
  85. $contentSelector.css('min-height', (max + offset) - heights.header - heights.footer)
  86. }
  87. if (typeof $.fn.overlayScrollbars !== 'undefined') {
  88. $(Selector.SIDEBAR).overlayScrollbars({
  89. className: this._config.scrollbarTheme,
  90. sizeAutoCapable: true,
  91. scrollbars: {
  92. autoHide: this._config.scrollbarAutoHide,
  93. clickScrolling: true
  94. }
  95. })
  96. }
  97. }
  98. fixLoginRegisterHeight() {
  99. const $body = $('body')
  100. const $selector = $(Selector.LOGIN_BOX + ', ' + Selector.REGISTER_BOX)
  101. if ($selector.length === 0) {
  102. $body.css('height', 'auto')
  103. $('html').css('height', 'auto')
  104. } else {
  105. const boxHeight = $selector.height()
  106. if ($body.css('min-height') !== boxHeight) {
  107. $body.css('min-height', boxHeight)
  108. }
  109. }
  110. }
  111. // Private
  112. _init() {
  113. // Activate layout height watcher
  114. this.fixLayoutHeight()
  115. if (this._config.loginRegisterAutoHeight === true) {
  116. this.fixLoginRegisterHeight()
  117. } else if (this._config.loginRegisterAutoHeight === parseInt(this._config.loginRegisterAutoHeight, 10)) {
  118. setInterval(this.fixLoginRegisterHeight, this._config.loginRegisterAutoHeight)
  119. }
  120. $(Selector.SIDEBAR)
  121. .on('collapsed.lte.treeview expanded.lte.treeview', () => {
  122. this.fixLayoutHeight()
  123. })
  124. $(Selector.PUSHMENU_BTN)
  125. .on('collapsed.lte.pushmenu shown.lte.pushmenu', () => {
  126. this.fixLayoutHeight()
  127. })
  128. $(Selector.CONTROL_SIDEBAR_BTN)
  129. .on('collapsed.lte.controlsidebar', () => {
  130. this.fixLayoutHeight()
  131. })
  132. .on('expanded.lte.controlsidebar', () => {
  133. this.fixLayoutHeight('control_sidebar')
  134. })
  135. $(window).resize(() => {
  136. this.fixLayoutHeight()
  137. })
  138. setTimeout(() => {
  139. $('body.hold-transition').removeClass('hold-transition')
  140. }, 50)
  141. }
  142. _max(numbers) {
  143. // Calculate the maximum number in a list
  144. let max = 0
  145. Object.keys(numbers).forEach(key => {
  146. if (numbers[key] > max) {
  147. max = numbers[key]
  148. }
  149. })
  150. return max
  151. }
  152. _isFooterFixed() {
  153. return $(Selector.FOOTER).css('position') === 'fixed'
  154. }
  155. // Static
  156. static _jQueryInterface(config = '') {
  157. return this.each(function () {
  158. let data = $(this).data(DATA_KEY)
  159. const _options = $.extend({}, Default, $(this).data())
  160. if (!data) {
  161. data = new Layout($(this), _options)
  162. $(this).data(DATA_KEY, data)
  163. }
  164. if (config === 'init' || config === '') {
  165. data._init()
  166. } else if (config === 'fixLayoutHeight' || config === 'fixLoginRegisterHeight') {
  167. data[config]()
  168. }
  169. })
  170. }
  171. }
  172. /**
  173. * Data API
  174. * ====================================================
  175. */
  176. $(window).on('load', () => {
  177. Layout._jQueryInterface.call($('body'))
  178. })
  179. $(Selector.SIDEBAR + ' a').on('focusin', () => {
  180. $(Selector.MAIN_SIDEBAR).addClass(ClassName.SIDEBAR_FOCUSED)
  181. })
  182. $(Selector.SIDEBAR + ' a').on('focusout', () => {
  183. $(Selector.MAIN_SIDEBAR).removeClass(ClassName.SIDEBAR_FOCUSED)
  184. })
  185. /**
  186. * jQuery API
  187. * ====================================================
  188. */
  189. $.fn[NAME] = Layout._jQueryInterface
  190. $.fn[NAME].Constructor = Layout
  191. $.fn[NAME].noConflict = function () {
  192. $.fn[NAME] = JQUERY_NO_CONFLICT
  193. return Layout._jQueryInterface
  194. }
  195. export default Layout