Layout.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. SIDEBAR : '.main-sidebar',
  21. HEADER : '.main-header',
  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. const max = this._max(heights)
  52. $(Selector.CONTENT).css('min-height', max - (heights.header))
  53. $(Selector.SIDEBAR).css('min-height', max - heights.header)
  54. }
  55. // Private
  56. _init() {
  57. // Enable transitions
  58. $('body').removeClass(ClassName.HOLD)
  59. // Activate layout height watcher
  60. this.fixLayoutHeight()
  61. $(Selector.SIDEBAR)
  62. .on('collapsed.lte.treeview expanded.lte.treeview collapsed.lte.pushmenu expanded.lte.pushmenu', () => {
  63. this.fixLayoutHeight()
  64. })
  65. $(window).resize(() => {
  66. this.fixLayoutHeight()
  67. })
  68. $('body, html').css('height', 'auto')
  69. }
  70. _max(numbers) {
  71. // Calculate the maximum number in a list
  72. let max = 0
  73. Object.keys(numbers).forEach((key) => {
  74. if (numbers[key] > max) {
  75. max = numbers[key]
  76. }
  77. })
  78. return max
  79. }
  80. // Static
  81. static _jQueryInterface(operation) {
  82. return this.each(function () {
  83. let data = $(this)
  84. .data(DATA_KEY)
  85. if (!data) {
  86. data = new Layout(this)
  87. $(this).data(DATA_KEY, data)
  88. }
  89. if (operation) {
  90. data[operation]()
  91. }
  92. })
  93. }
  94. }
  95. /**
  96. * Data API
  97. * ====================================================
  98. */
  99. $(window).on('load', () => {
  100. Layout._jQueryInterface.call($('body'))
  101. })
  102. /**
  103. * jQuery API
  104. * ====================================================
  105. */
  106. $.fn[NAME] = Layout._jQueryInterface
  107. $.fn[NAME].Constructor = Layout
  108. $.fn[NAME].noConflict = function () {
  109. $.fn[NAME] = JQUERY_NO_CONFLICT
  110. return Layout._jQueryInterface
  111. }
  112. return Layout
  113. })(jQuery)
  114. export default Layout