Layout.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * --------------------------------------------
  3. * AdminLTE Layout.js
  4. * License MIT
  5. * --------------------------------------------
  6. */
  7. const Layout = (($) => {
  8. /**
  9. * Constants
  10. * ====================================================
  11. */
  12. const NAME = 'Layout'
  13. const DATA_KEY = 'lte.layout'
  14. const EVENT_KEY = `.${DATA_KEY}`
  15. const JQUERY_NO_CONFLICT = $.fn[NAME]
  16. const Event = {
  17. SIDEBAR: 'sidebar'
  18. }
  19. const Selector = {
  20. HEADER : '.main-header',
  21. SIDEBAR : '.main-sidebar .sidebar',
  22. CONTENT : '.content-wrapper',
  23. CONTENT_HEADER : '.content-header',
  24. WRAPPER : '.wrapper',
  25. CONTROL_SIDEBAR: '.control-sidebar',
  26. LAYOUT_FIXED : '.layout-fixed',
  27. FOOTER : '.main-footer'
  28. }
  29. const ClassName = {
  30. HOLD : 'hold-transition',
  31. SIDEBAR : 'main-sidebar',
  32. LAYOUT_FIXED: 'layout-fixed'
  33. }
  34. /**
  35. * Class Definition
  36. * ====================================================
  37. */
  38. class Layout {
  39. constructor(element) {
  40. this._element = element
  41. this._init()
  42. }
  43. // Public
  44. fixLayoutHeight() {
  45. const heights = {
  46. window : $(window).height(),
  47. header : $(Selector.HEADER).outerHeight(),
  48. footer : $(Selector.FOOTER).outerHeight(),
  49. sidebar : $(Selector.SIDEBAR).height(),
  50. }
  51. console.log(heights);
  52. const max = this._max(heights)
  53. $(Selector.CONTENT).css('min-height', max - heights.header - heights.footer)
  54. $(Selector.SIDEBAR).css('min-height', max - heights.header)
  55. if (!$('body').hasClass(ClassName.LAYOUT_FIXED)) {
  56. if (typeof $.fn.slimScroll !== 'undefined') {
  57. $(Selector.SIDEBAR)
  58. .slimScroll({ destroy: true })
  59. .slimScroll({ height: max - heights.header });
  60. }
  61. }
  62. }
  63. // Private
  64. _init() {
  65. // Enable transitions
  66. $('body').removeClass(ClassName.HOLD)
  67. // Activate layout height watcher
  68. this.fixLayoutHeight()
  69. $(Selector.SIDEBAR)
  70. .on('collapsed.lte.treeview expanded.lte.treeview collapsed.lte.pushmenu expanded.lte.pushmenu', () => {
  71. this.fixLayoutHeight()
  72. })
  73. $(window).resize(() => {
  74. console.log('resized');
  75. this.fixLayoutHeight()
  76. })
  77. $('body, html').css('height', 'auto')
  78. }
  79. _max(numbers) {
  80. // Calculate the maximum number in a list
  81. let max = 0
  82. Object.keys(numbers).forEach((key) => {
  83. if (numbers[key] > max) {
  84. max = numbers[key]
  85. }
  86. })
  87. return max
  88. }
  89. // Static
  90. static _jQueryInterface(operation) {
  91. return this.each(function () {
  92. let data = $(this)
  93. .data(DATA_KEY)
  94. if (!data) {
  95. data = new Layout(this)
  96. $(this).data(DATA_KEY, data)
  97. }
  98. if (operation) {
  99. data[operation]()
  100. }
  101. })
  102. }
  103. }
  104. /**
  105. * Data API
  106. * ====================================================
  107. */
  108. $(window).on('load', () => {
  109. Layout._jQueryInterface.call($('body'))
  110. })
  111. /**
  112. * jQuery API
  113. * ====================================================
  114. */
  115. $.fn[NAME] = Layout._jQueryInterface
  116. $.fn[NAME].Constructor = Layout
  117. $.fn[NAME].noConflict = function () {
  118. $.fn[NAME] = JQUERY_NO_CONFLICT
  119. return Layout._jQueryInterface
  120. }
  121. return Layout
  122. })(jQuery)
  123. export default Layout