Layout.js 6.7 KB

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