Layout.js 4.9 KB
Newer Older
A
Abdullah Almsaeed 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/**
 * --------------------------------------------
 * AdminLTE Layout.js
 * License MIT
 * --------------------------------------------
 */

const Layout = (($) => {
  /**
   * Constants
   * ====================================================
   */

A
Abdullah Almsaeed 已提交
14 15 16
  const NAME               = 'Layout'
  const DATA_KEY           = 'lte.layout'
  const EVENT_KEY          = `.${DATA_KEY}`
A
Abdullah Almsaeed 已提交
17 18 19 20 21 22 23
  const JQUERY_NO_CONFLICT = $.fn[NAME]

  const Event = {
    SIDEBAR: 'sidebar'
  }

  const Selector = {
A
Abdullah Almsaeed 已提交
24
    HEADER         : '.main-header',
J
Jorge Vilaça 已提交
25
    SIDEBAR        : '.main-sidebar .sidebar',
A
Abdullah Almsaeed 已提交
26
    CONTENT        : '.content-wrapper',
R
REJack 已提交
27
    BRAND          : '.brand-link',
A
Abdullah Almsaeed 已提交
28 29
    CONTENT_HEADER : '.content-header',
    WRAPPER        : '.wrapper',
A
Abdullah Almsaeed 已提交
30
    CONTROL_SIDEBAR: '.control-sidebar',
A
Abdullah Almsaeed 已提交
31 32
    LAYOUT_FIXED   : '.layout-fixed',
    FOOTER         : '.main-footer'
A
Abdullah Almsaeed 已提交
33 34 35
  }

  const ClassName = {
A
Abdullah Almsaeed 已提交
36 37
    HOLD        : 'hold-transition',
    SIDEBAR     : 'main-sidebar',
R
REJack 已提交
38 39 40 41 42 43 44 45
    LAYOUT_FIXED: 'layout-fixed',
    NAVBAR_FIXED: 'layout-navbar-fixed',
    FOOTER_FIXED: 'layout-footer-fixed',
  }

  const Default = {
    scrollbarTheme : 'os-theme-light',
    scrollbarAutoHide: 'l'
A
Abdullah Almsaeed 已提交
46 47 48 49 50 51 52 53
  }

  /**
   * Class Definition
   * ====================================================
   */

  class Layout {
R
REJack 已提交
54 55
    constructor(element, config) {
      this._config  = config
A
Abdullah Almsaeed 已提交
56 57 58 59 60 61 62 63
      this._element = element

      this._init()
    }

    // Public

    fixLayoutHeight() {
A
Abdullah Almsaeed 已提交
64
      const heights = {
J
Jorge Vilaça 已提交
65 66 67 68
        window     : $(window).height(),
        header     : $(Selector.HEADER).outerHeight(),
        footer     : $(Selector.FOOTER).outerHeight(),
        sidebar    : $(Selector.SIDEBAR).height(),
A
Abdullah Almsaeed 已提交
69
      }
R
REJack 已提交
70

J
Jorge Vilaça 已提交
71
      const max = this._max(heights)
A
Abdullah Almsaeed 已提交
72

J
Jorge Vilaça 已提交
73

R
REJack 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
      if ($('body').hasClass(ClassName.LAYOUT_FIXED)) {
        $(Selector.CONTENT).css('min-height', max - heights.header - heights.footer)
        // $(Selector.SIDEBAR).css('min-height', max - heights.header)
        $(Selector.CONTROL_SIDEBAR + ' .control-sidebar-content').css('height', max - heights.header)
        
        if (typeof $.fn.overlayScrollbars !== 'undefined') {
          $(Selector.SIDEBAR).overlayScrollbars({
            className       : this._config.scrollbarTheme,
            sizeAutoCapable : true,
            scrollbars : {
              autoHide: this._config.scrollbarAutoHide, 
              clickScrolling : true
            }
          })
          $(Selector.CONTROL_SIDEBAR + ' .control-sidebar-content').overlayScrollbars({
            className       : this._config.scrollbarTheme,
            sizeAutoCapable : true,
            scrollbars : {
              autoHide: this._config.scrollbarAutoHide, 
              clickScrolling : true
            }
          })
        }
      } else {
        if (heights.window > heights.sidebar) {
          $(Selector.CONTENT).css('min-height', heights.window - heights.header - heights.footer)
        } else {
          $(Selector.CONTENT).css('min-height', heights.sidebar - heights.header)
J
Jorge Vilaça 已提交
102 103
        }
      }
R
REJack 已提交
104 105 106 107 108 109 110 111
      if ($('body').hasClass(ClassName.NAVBAR_FIXED)) {
          $(Selector.BRAND).css('height', heights.header)
          $(Selector.SIDEBAR).css('margin-top', heights.header)
          $(Selector.SIDEBAR).css('margin-top', heights.header)
      }
      if ($('body').hasClass(ClassName.FOOTER_FIXED)) {
        $(Selector.CONTENT).css('margin-bottom', heights.footer)
      } 
A
Abdullah Almsaeed 已提交
112 113 114 115 116
    }

    // Private

    _init() {
117
      // Enable transitions
A
Abdullah Almsaeed 已提交
118 119
      $('body').removeClass(ClassName.HOLD)

120
      // Activate layout height watcher
A
Abdullah Almsaeed 已提交
121
      this.fixLayoutHeight()
A
Abdullah Almsaeed 已提交
122 123 124 125 126
      $(Selector.SIDEBAR)
        .on('collapsed.lte.treeview expanded.lte.treeview collapsed.lte.pushmenu expanded.lte.pushmenu', () => {
          this.fixLayoutHeight()
        })

J
Jorge Vilaça 已提交
127
      $(window).resize(() => {
A
Abdullah Almsaeed 已提交
128 129
        this.fixLayoutHeight()
      })
A
Abdullah Almsaeed 已提交
130

A
Abdullah Almsaeed 已提交
131
      $('body, html').css('height', 'auto')
A
Abdullah Almsaeed 已提交
132 133 134
    }

    _max(numbers) {
135
      // Calculate the maximum number in a list
A
Abdullah Almsaeed 已提交
136 137
      let max = 0

A
Abdullah Almsaeed 已提交
138 139 140
      Object.keys(numbers).forEach((key) => {
        if (numbers[key] > max) {
          max = numbers[key]
A
Abdullah Almsaeed 已提交
141 142 143 144 145 146 147 148
        }
      })

      return max
    }

    // Static

R
REJack 已提交
149
    static _jQueryInterface(config) {
A
Abdullah Almsaeed 已提交
150
      return this.each(function () {
R
REJack 已提交
151 152
        let data      = $(this).data(DATA_KEY)
        const _config = $.extend({}, Default, $(this).data())
A
Abdullah Almsaeed 已提交
153 154

        if (!data) {
R
REJack 已提交
155
          data = new Layout($(this), _config)
A
Abdullah Almsaeed 已提交
156 157 158
          $(this).data(DATA_KEY, data)
        }

R
REJack 已提交
159 160
        if (config === 'init') {
          data[config]()
A
Abdullah Almsaeed 已提交
161 162 163 164 165
        }
      })
    }
  }

166 167 168 169
  /**
   * Data API
   * ====================================================
   */
R
REJack 已提交
170

171 172
  $(window).on('load', () => {
    Layout._jQueryInterface.call($('body'))
A
Abdullah Almsaeed 已提交
173
  })
174

A
Abdullah Almsaeed 已提交
175 176 177 178 179 180 181
  /**
   * jQuery API
   * ====================================================
   */

  $.fn[NAME] = Layout._jQueryInterface
  $.fn[NAME].Constructor = Layout
J
Jorge Vilaça 已提交
182
  $.fn[NAME].noConflict = function () {
A
Abdullah Almsaeed 已提交
183 184 185 186 187 188
    $.fn[NAME] = JQUERY_NO_CONFLICT
    return Layout._jQueryInterface
  }

  return Layout
})(jQuery)
A
Abdullah Almsaeed 已提交
189

190
export default Layout