Layout.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* Layout()
  2. * ========
  3. * Implements AdminLTE layout.
  4. * Fixes the layout height in case min-height fails.
  5. *
  6. * @usage activated automatically upon window load.
  7. * Configure any options by passing data-option="value"
  8. * to the body tag.
  9. */
  10. +function ($) {
  11. 'use strict'
  12. var DataKey = 'lte.layout'
  13. var Default = {
  14. slimscroll : true,
  15. resetHeight: true
  16. }
  17. var Selector = {
  18. wrapper : '.wrapper',
  19. contentWrapper: '.content-wrapper',
  20. layoutBoxed : '.layout-boxed',
  21. mainFooter : '.main-footer',
  22. mainHeader : '.main-header',
  23. sidebar : '.sidebar',
  24. controlSidebar: '.control-sidebar',
  25. fixed : '.fixed',
  26. sidebarMenu : '.sidebar-menu'
  27. }
  28. var ClassName = {
  29. fixed : 'fixed',
  30. holdTransition: 'hold-transition'
  31. }
  32. var Layout = function (options) {
  33. this.options = options
  34. this.bindedResize = false
  35. this.activate()
  36. }
  37. Layout.prototype.activate = function () {
  38. this.fix()
  39. this.fixSidebar()
  40. $('body').removeClass(ClassName.holdTransition)
  41. if (this.options.resetHeight) {
  42. $('body, html, ' + Selector.wrapper).css('height', 'auto')
  43. }
  44. if (!this.bindedResize) {
  45. $(window).resize(function () {
  46. this.fix()
  47. this.fixSidebar()
  48. }.bind(this))
  49. this.bindedResize = true;
  50. }
  51. $(Selector.sidebarMenu).on('expanded.tree', function () {
  52. this.fix()
  53. this.fixSidebar()
  54. }.bind(this))
  55. $(Selector.sidebarMenu).on('collapsed.tree', function () {
  56. this.fix()
  57. this.fixSidebar()
  58. }.bind(this))
  59. }
  60. Layout.prototype.fix = function () {
  61. // Remove overflow from .wrapper if layout-boxed exists
  62. $(Selector.layoutBoxed + ' > ' + Selector.wrapper).css('overflow', 'hidden')
  63. // Get window height and the wrapper height
  64. var footerHeight = $(Selector.mainFooter).outerHeight() || 0
  65. var neg = $(Selector.mainHeader).outerHeight() + footerHeight
  66. var windowHeight = $(window).height()
  67. var sidebarHeight = $(Selector.sidebar).height() || 0
  68. // Set the min-height of the content and sidebar based on the
  69. // the height of the document.
  70. if ($('body').hasClass(ClassName.fixed)) {
  71. $(Selector.contentWrapper).css('min-height', windowHeight - footerHeight)
  72. } else {
  73. var postSetWidth
  74. if (windowHeight >= sidebarHeight) {
  75. $(Selector.contentWrapper).css('min-height', windowHeight - neg)
  76. postSetWidth = windowHeight - neg
  77. } else {
  78. $(Selector.contentWrapper).css('min-height', sidebarHeight)
  79. postSetWidth = sidebarHeight
  80. }
  81. // Fix for the control sidebar height
  82. var $controlSidebar = $(Selector.controlSidebar)
  83. if (typeof $controlSidebar !== 'undefined') {
  84. if ($controlSidebar.height() > postSetWidth)
  85. $(Selector.contentWrapper).css('min-height', $controlSidebar.height())
  86. }
  87. }
  88. }
  89. Layout.prototype.fixSidebar = function () {
  90. // Make sure the body tag has the .fixed class
  91. if (!$('body').hasClass(ClassName.fixed)) {
  92. if (typeof $.fn.slimScroll != 'undefined') {
  93. $(Selector.sidebar).slimScroll({ destroy: true }).height('auto')
  94. }
  95. return
  96. }
  97. // Enable slimscroll for fixed layout
  98. if (this.options.slimscroll) {
  99. if (typeof $.fn.slimScroll != 'undefined') {
  100. // Destroy if it exists
  101. $(Selector.sidebar).slimScroll({ destroy: true }).height('auto')
  102. // Add slimscroll
  103. $(Selector.sidebar).slimScroll({
  104. height: ($(window).height() - $(Selector.mainHeader).height()) + 'px',
  105. color : 'rgba(0,0,0,0.2)',
  106. size : '3px'
  107. })
  108. }
  109. }
  110. }
  111. // Plugin Definition
  112. // =================
  113. function Plugin(option) {
  114. return this.each(function () {
  115. var $this = $(this)
  116. var data = $this.data(DataKey)
  117. if (!data) {
  118. var options = $.extend({}, Default, $this.data(), typeof option == 'object' && option)
  119. $this.data(DataKey, (data = new Layout(options)))
  120. }
  121. if (typeof option == 'string') {
  122. if (typeof data[option] == 'undefined') {
  123. throw new Error('No method named ' + option)
  124. }
  125. data[option]()
  126. }
  127. })
  128. }
  129. var old = $.fn.layout
  130. $.fn.layout = Plugin
  131. $.fn.layout.Constuctor = Layout
  132. // No conflict mode
  133. // ================
  134. $.fn.layout.noConflict = function () {
  135. $.fn.layout = old
  136. return this
  137. }
  138. // Layout DATA-API
  139. // ===============
  140. $(window).on('load', function () {
  141. Plugin.call($('body'))
  142. })
  143. }(jQuery)