AdminLTE/build/js/Layout.js

240 lines
6.3 KiB
JavaScript
Raw Normal View History

2016-01-16 16:27:23 +00:00
/**
* --------------------------------------------
* AdminLTE Layout.js
* License MIT
* --------------------------------------------
*/
2020-06-02 13:23:22 +00:00
import $ from 'jquery'
2016-01-16 16:27:23 +00:00
2020-06-02 13:23:22 +00:00
/**
* Constants
* ====================================================
*/
const NAME = 'Layout'
const DATA_KEY = 'lte.layout'
const JQUERY_NO_CONFLICT = $.fn[NAME]
const SELECTOR_HEADER = '.main-header'
const SELECTOR_MAIN_SIDEBAR = '.main-sidebar'
const SELECTOR_SIDEBAR = '.main-sidebar .sidebar'
const SELECTOR_CONTENT = '.content-wrapper'
const SELECTOR_CONTROL_SIDEBAR_CONTENT = '.control-sidebar-content'
const SELECTOR_CONTROL_SIDEBAR_BTN = '[data-widget="control-sidebar"]'
const SELECTOR_FOOTER = '.main-footer'
const SELECTOR_PUSHMENU_BTN = '[data-widget="pushmenu"]'
const SELECTOR_LOGIN_BOX = '.login-box'
const SELECTOR_REGISTER_BOX = '.register-box'
const CLASS_NAME_SIDEBAR_FOCUSED = 'sidebar-focused'
const CLASS_NAME_LAYOUT_FIXED = 'layout-fixed'
const CLASS_NAME_CONTROL_SIDEBAR_SLIDE_OPEN = 'control-sidebar-slide-open'
const CLASS_NAME_CONTROL_SIDEBAR_OPEN = 'control-sidebar-open'
2020-06-02 13:23:22 +00:00
const Default = {
scrollbarTheme: 'os-theme-light',
scrollbarAutoHide: 'l',
panelAutoHeight: true,
2020-09-19 12:15:59 +00:00
panelAutoHeightMode: 'min-height',
2020-06-02 13:23:22 +00:00
loginRegisterAutoHeight: true
}
/**
* Class Definition
* ====================================================
*/
class Layout {
constructor(element, config) {
this._config = config
this._element = element
2020-06-02 13:23:22 +00:00
this._init()
2016-01-16 16:27:23 +00:00
}
2020-06-02 13:23:22 +00:00
// Public
2016-01-16 16:27:23 +00:00
2020-06-02 13:23:22 +00:00
fixLayoutHeight(extra = null) {
2020-06-04 18:06:38 +00:00
const $body = $('body')
2020-06-02 13:23:22 +00:00
let controlSidebar = 0
2016-01-16 16:27:23 +00:00
if ($body.hasClass(CLASS_NAME_CONTROL_SIDEBAR_SLIDE_OPEN) || $body.hasClass(CLASS_NAME_CONTROL_SIDEBAR_OPEN) || extra === 'control_sidebar') {
controlSidebar = $(SELECTOR_CONTROL_SIDEBAR_CONTENT).height()
2016-01-16 16:27:23 +00:00
}
2020-06-02 13:23:22 +00:00
const heights = {
window: $(window).height(),
header: $(SELECTOR_HEADER).length !== 0 ? $(SELECTOR_HEADER).outerHeight() : 0,
footer: $(SELECTOR_FOOTER).length !== 0 ? $(SELECTOR_FOOTER).outerHeight() : 0,
sidebar: $(SELECTOR_SIDEBAR).length !== 0 ? $(SELECTOR_SIDEBAR).height() : 0,
2020-06-02 13:23:22 +00:00
controlSidebar
}
const max = this._max(heights)
let offset = this._config.panelAutoHeight
2016-01-16 16:27:23 +00:00
2020-06-02 13:23:22 +00:00
if (offset === true) {
offset = 0
}
const $contentSelector = $(SELECTOR_CONTENT)
2020-06-04 18:06:38 +00:00
2020-06-02 13:23:22 +00:00
if (offset !== false) {
if (max === heights.controlSidebar) {
2020-09-19 12:15:59 +00:00
$contentSelector.css(this._config.panelAutoHeightMode, (max + offset))
2020-06-02 13:23:22 +00:00
} else if (max === heights.window) {
2020-09-19 12:15:59 +00:00
$contentSelector.css(this._config.panelAutoHeightMode, (max + offset) - heights.header - heights.footer)
2020-06-02 13:23:22 +00:00
} else {
2020-09-19 12:15:59 +00:00
$contentSelector.css(this._config.panelAutoHeightMode, (max + offset) - heights.header)
}
2020-06-02 13:23:22 +00:00
if (this._isFooterFixed()) {
2020-09-19 12:15:59 +00:00
$contentSelector.css(this._config.panelAutoHeightMode, parseFloat($contentSelector.css(this._config.panelAutoHeightMode)) + heights.footer)
2018-03-17 17:07:55 +00:00
}
2020-06-02 13:23:22 +00:00
}
if (!$body.hasClass(CLASS_NAME_LAYOUT_FIXED)) {
2020-06-02 13:23:22 +00:00
return
}
2016-10-15 17:18:29 +00:00
2020-06-02 13:23:22 +00:00
if (offset !== false) {
2020-09-19 12:15:59 +00:00
$contentSelector.css(this._config.panelAutoHeightMode, (max + offset) - heights.header - heights.footer)
2020-06-02 13:23:22 +00:00
}
2020-06-02 13:23:22 +00:00
if (typeof $.fn.overlayScrollbars !== 'undefined') {
$(SELECTOR_SIDEBAR).overlayScrollbars({
2020-06-02 13:23:22 +00:00
className: this._config.scrollbarTheme,
sizeAutoCapable: true,
scrollbars: {
autoHide: this._config.scrollbarAutoHide,
clickScrolling: true
}
2020-06-02 13:23:22 +00:00
})
}
}
2020-05-30 13:44:20 +00:00
2020-06-02 13:23:22 +00:00
fixLoginRegisterHeight() {
2020-06-04 18:06:38 +00:00
const $body = $('body')
2020-06-13 12:34:10 +00:00
const $selector = $(`${SELECTOR_LOGIN_BOX}, ${SELECTOR_REGISTER_BOX}`)
2020-06-04 18:06:38 +00:00
if ($selector.length === 0) {
$body.css('height', 'auto')
$('html').css('height', 'auto')
2020-06-02 13:23:22 +00:00
} else {
2020-06-04 18:06:38 +00:00
const boxHeight = $selector.height()
2019-10-12 09:36:52 +00:00
2020-09-19 12:15:59 +00:00
if ($body.css(this._config.panelAutoHeightMode) !== boxHeight) {
$body.css(this._config.panelAutoHeightMode, boxHeight)
2020-05-31 13:45:54 +00:00
}
2020-06-02 13:23:22 +00:00
}
}
2019-10-12 09:36:52 +00:00
2020-06-02 13:23:22 +00:00
// Private
2020-05-31 13:45:54 +00:00
2020-06-02 13:23:22 +00:00
_init() {
// Activate layout height watcher
this.fixLayoutHeight()
if (this._config.loginRegisterAutoHeight === true) {
this.fixLoginRegisterHeight()
} else if (this._config.loginRegisterAutoHeight === parseInt(this._config.loginRegisterAutoHeight, 10)) {
setInterval(this.fixLoginRegisterHeight, this._config.loginRegisterAutoHeight)
2016-01-16 16:27:23 +00:00
}
$(SELECTOR_SIDEBAR)
2020-06-02 13:23:22 +00:00
.on('collapsed.lte.treeview expanded.lte.treeview', () => {
this.fixLayoutHeight()
})
$(SELECTOR_PUSHMENU_BTN)
2020-06-02 13:23:22 +00:00
.on('collapsed.lte.pushmenu shown.lte.pushmenu', () => {
this.fixLayoutHeight()
})
$(SELECTOR_CONTROL_SIDEBAR_BTN)
2020-06-02 13:23:22 +00:00
.on('collapsed.lte.controlsidebar', () => {
this.fixLayoutHeight()
})
.on('expanded.lte.controlsidebar', () => {
this.fixLayoutHeight('control_sidebar')
})
2016-01-16 16:27:23 +00:00
2020-06-02 13:23:22 +00:00
$(window).resize(() => {
2016-01-16 16:27:23 +00:00
this.fixLayoutHeight()
2020-06-02 13:23:22 +00:00
})
setTimeout(() => {
$('body.hold-transition').removeClass('hold-transition')
}, 50)
}
_max(numbers) {
// Calculate the maximum number in a list
let max = 0
2020-06-02 13:23:22 +00:00
Object.keys(numbers).forEach(key => {
if (numbers[key] > max) {
max = numbers[key]
}
2020-06-02 13:23:22 +00:00
})
2020-06-02 13:23:22 +00:00
return max
}
2016-10-22 19:32:28 +00:00
2020-06-02 13:23:22 +00:00
_isFooterFixed() {
return $(SELECTOR_FOOTER).css('position') === 'fixed'
2020-06-02 13:23:22 +00:00
}
2016-01-16 16:27:23 +00:00
2020-06-02 13:23:22 +00:00
// Static
2016-01-16 16:27:23 +00:00
2020-06-02 13:23:22 +00:00
static _jQueryInterface(config = '') {
return this.each(function () {
let data = $(this).data(DATA_KEY)
const _options = $.extend({}, Default, $(this).data())
2016-01-16 16:27:23 +00:00
2020-06-02 13:23:22 +00:00
if (!data) {
data = new Layout($(this), _options)
$(this).data(DATA_KEY, data)
}
2016-01-16 16:27:23 +00:00
2020-06-02 13:23:22 +00:00
if (config === 'init' || config === '') {
data._init()
} else if (config === 'fixLayoutHeight' || config === 'fixLoginRegisterHeight') {
data[config]()
}
})
}
}
2020-06-02 13:23:22 +00:00
/**
* Data API
* ====================================================
*/
2016-01-16 16:27:23 +00:00
2020-06-02 13:23:22 +00:00
$(window).on('load', () => {
Layout._jQueryInterface.call($('body'))
})
2016-01-16 16:27:23 +00:00
2020-06-13 12:34:10 +00:00
$(`${SELECTOR_SIDEBAR} a`).on('focusin', () => {
$(SELECTOR_MAIN_SIDEBAR).addClass(CLASS_NAME_SIDEBAR_FOCUSED)
2020-06-02 13:23:22 +00:00
})
2016-01-16 16:27:23 +00:00
2020-06-13 12:34:10 +00:00
$(`${SELECTOR_SIDEBAR} a`).on('focusout', () => {
$(SELECTOR_MAIN_SIDEBAR).removeClass(CLASS_NAME_SIDEBAR_FOCUSED)
2020-06-02 13:23:22 +00:00
})
2016-01-16 16:27:23 +00:00
2020-06-02 13:23:22 +00:00
/**
* jQuery API
* ====================================================
*/
2016-01-16 16:27:23 +00:00
2020-06-02 13:23:22 +00:00
$.fn[NAME] = Layout._jQueryInterface
$.fn[NAME].Constructor = Layout
$.fn[NAME].noConflict = function () {
$.fn[NAME] = JQUERY_NO_CONFLICT
return Layout._jQueryInterface
}
2018-02-03 23:45:19 +00:00
export default Layout