Layout.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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({
  43. 'height' : 'auto',
  44. 'min-height': '100%'
  45. })
  46. }
  47. if (!this.bindedResize) {
  48. $(window).resize(function () {
  49. this.fix()
  50. this.fixSidebar()
  51. }.bind(this))
  52. this.bindedResize = true
  53. }
  54. $(Selector.sidebarMenu).on('expanded.tree', function () {
  55. this.fix()
  56. this.fixSidebar()
  57. }.bind(this))
  58. $(Selector.sidebarMenu).on('collapsed.tree', function () {
  59. this.fix()
  60. this.fixSidebar()
  61. }.bind(this))
  62. }
  63. Layout.prototype.fix = function () {
  64. // Remove overflow from .wrapper if layout-boxed exists
  65. $(Selector.layoutBoxed + ' > ' + Selector.wrapper).css('overflow', 'hidden')
  66. // Get window height and the wrapper height
  67. var footerHeight = $(Selector.mainFooter).outerHeight() || 0
  68. var neg = $(Selector.mainHeader).outerHeight() + footerHeight
  69. var windowHeight = $(window).height()
  70. var sidebarHeight = $(Selector.sidebar).height() || 0
  71. // Set the min-height of the content and sidebar based on the
  72. // the height of the document.
  73. if ($('body').hasClass(ClassName.fixed)) {
  74. $(Selector.contentWrapper).css('min-height', windowHeight - footerHeight)
  75. } else {
  76. var postSetWidth
  77. if (windowHeight >= sidebarHeight) {
  78. $(Selector.contentWrapper).css('min-height', windowHeight - neg)
  79. postSetWidth = windowHeight - neg
  80. } else {
  81. $(Selector.contentWrapper).css('min-height', sidebarHeight)
  82. postSetWidth = sidebarHeight
  83. }
  84. // Fix for the control sidebar height
  85. var $controlSidebar = $(Selector.controlSidebar)
  86. if (typeof $controlSidebar !== 'undefined') {
  87. if ($controlSidebar.height() > postSetWidth)
  88. $(Selector.contentWrapper).css('min-height', $controlSidebar.height())
  89. }
  90. }
  91. }
  92. Layout.prototype.fixSidebar = function () {
  93. // Make sure the body tag has the .fixed class
  94. if (!$('body').hasClass(ClassName.fixed)) {
  95. if (typeof $.fn.slimScroll !== 'undefined') {
  96. $(Selector.sidebar).slimScroll({ destroy: true }).height('auto')
  97. }
  98. return
  99. }
  100. // Enable slimscroll for fixed layout
  101. if (this.options.slimscroll) {
  102. if (typeof $.fn.slimScroll !== 'undefined') {
  103. // Destroy if it exists
  104. $(Selector.sidebar).slimScroll({ destroy: true }).height('auto')
  105. // Add slimscroll
  106. $(Selector.sidebar).slimScroll({
  107. height: ($(window).height() - $(Selector.mainHeader).height()) + 'px',
  108. color : 'rgba(0,0,0,0.2)',
  109. size : '3px'
  110. })
  111. }
  112. }
  113. }
  114. // Plugin Definition
  115. // =================
  116. function Plugin(option) {
  117. return this.each(function () {
  118. var $this = $(this)
  119. var data = $this.data(DataKey)
  120. if (!data) {
  121. var options = $.extend({}, Default, $this.data(), typeof option === 'object' && option)
  122. $this.data(DataKey, (data = new Layout(options)))
  123. }
  124. if (typeof option === 'string') {
  125. if (typeof data[option] === 'undefined') {
  126. throw new Error('No method named ' + option)
  127. }
  128. data[option]()
  129. }
  130. })
  131. }
  132. var old = $.fn.layout
  133. $.fn.layout = Plugin
  134. $.fn.layout.Constuctor = Layout
  135. // No conflict mode
  136. // ================
  137. $.fn.layout.noConflict = function () {
  138. $.fn.layout = old
  139. return this
  140. }
  141. // Layout DATA-API
  142. // ===============
  143. $(window).on('load', function () {
  144. Plugin.call($('body'))
  145. })
  146. }(jQuery)