Layout.js 7.1 KB

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