You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Layout.js 7.6KB

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