You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
AdminLTE/build/js/Layout.js

258 lines
6.8 KiB

9 years ago
/**
* --------------------------------------------
* AdminLTE Layout.js
* License MIT
* --------------------------------------------
*/
import $ from 'jquery'
9 years ago
/**
* 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'
4 years ago
const SELECTOR_PRELOADER = '.preloader'
const CLASS_NAME_SIDEBAR_COLLAPSED = 'sidebar-collapse'
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'
const Default = {
scrollbarTheme: 'os-theme-light',
scrollbarAutoHide: 'l',
panelAutoHeight: true,
panelAutoHeightMode: 'min-height',
4 years ago
preloadDuration: 200,
loginRegisterAutoHeight: true
}
/**
* Class Definition
* ====================================================
*/
class Layout {
constructor(element, config) {
this._config = config
this._element = element
9 years ago
}
// Public
9 years ago
fixLayoutHeight(extra = null) {
const $body = $('body')
let controlSidebar = 0
9 years ago
if ($body.hasClass(CLASS_NAME_CONTROL_SIDEBAR_SLIDE_OPEN) || $body.hasClass(CLASS_NAME_CONTROL_SIDEBAR_OPEN) || extra === 'control_sidebar') {
controlSidebar = $(SELECTOR_CONTROL_SIDEBAR_CONTENT).outerHeight()
9 years ago
}
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,
controlSidebar
}
const max = this._max(heights)
let offset = this._config.panelAutoHeight
9 years ago
if (offset === true) {
offset = 0
}
const $contentSelector = $(SELECTOR_CONTENT)
if (offset !== false) {
if (max === heights.controlSidebar) {
$contentSelector.css(this._config.panelAutoHeightMode, (max + offset))
} else if (max === heights.window) {
$contentSelector.css(this._config.panelAutoHeightMode, (max + offset) - heights.header - heights.footer)
} else {
$contentSelector.css(this._config.panelAutoHeightMode, (max + offset) - heights.header)
}
if (this._isFooterFixed()) {
$contentSelector.css(this._config.panelAutoHeightMode, parseFloat($contentSelector.css(this._config.panelAutoHeightMode)) + heights.footer)
7 years ago
}
}
if (!$body.hasClass(CLASS_NAME_LAYOUT_FIXED)) {
return
}
if (typeof $.fn.overlayScrollbars !== 'undefined') {
$(SELECTOR_SIDEBAR).overlayScrollbars({
className: this._config.scrollbarTheme,
sizeAutoCapable: true,
scrollbars: {
autoHide: this._config.scrollbarAutoHide,
clickScrolling: true
}
})
} else {
$(SELECTOR_SIDEBAR).css('overflow-y', 'auto')
}
}
fixLoginRegisterHeight() {
const $body = $('body')
const $selector = $(`${SELECTOR_LOGIN_BOX}, ${SELECTOR_REGISTER_BOX}`)
if ($selector.length === 0) {
$body.css('height', 'auto')
$('html').css('height', 'auto')
} else {
const boxHeight = $selector.height()
if ($body.css(this._config.panelAutoHeightMode) !== boxHeight) {
$body.css(this._config.panelAutoHeightMode, boxHeight)
5 years ago
}
}
}
// Private
5 years ago
_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)
9 years ago
}
$(SELECTOR_SIDEBAR)
.on('collapsed.lte.treeview expanded.lte.treeview', () => {
this.fixLayoutHeight()
})
$(SELECTOR_MAIN_SIDEBAR)
.on('mouseenter mouseleave', () => {
if ($('body').hasClass(CLASS_NAME_SIDEBAR_COLLAPSED)) {
this.fixLayoutHeight()
}
})
$(SELECTOR_PUSHMENU_BTN)
.on('collapsed.lte.pushmenu shown.lte.pushmenu', () => {
setTimeout(() => {
this.fixLayoutHeight()
}, 300)
})
$(SELECTOR_CONTROL_SIDEBAR_BTN)
.on('collapsed.lte.controlsidebar', () => {
this.fixLayoutHeight()
})
.on('expanded.lte.controlsidebar', () => {
this.fixLayoutHeight('control_sidebar')
})
9 years ago
$(window).resize(() => {
9 years ago
this.fixLayoutHeight()
})
setTimeout(() => {
$('body.hold-transition').removeClass('hold-transition')
}, 50)
4 years ago
setTimeout(() => {
const $preloader = $(SELECTOR_PRELOADER)
if ($preloader) {
$preloader.css('height', 0)
setTimeout(() => {
$preloader.children().hide()
}, 200)
}
}, this._config.preloadDuration)
}
_max(numbers) {
// Calculate the maximum number in a list
let max = 0
Object.keys(numbers).forEach(key => {
if (numbers[key] > max) {
max = numbers[key]
}
})
return max
}
_isFooterFixed() {
return $(SELECTOR_FOOTER).css('position') === 'fixed'
}
9 years ago
// Static
9 years ago
static _jQueryInterface(config = '') {
return this.each(function () {
let data = $(this).data(DATA_KEY)
const _options = $.extend({}, Default, $(this).data())
9 years ago
if (!data) {
data = new Layout($(this), _options)
$(this).data(DATA_KEY, data)
}
9 years ago
if (config === 'init' || config === '') {
data._init()
} else if (config === 'fixLayoutHeight' || config === 'fixLoginRegisterHeight') {
data[config]()
}
})
}
}
/**
* Data API
* ====================================================
*/
9 years ago
$(window).on('load', () => {
Layout._jQueryInterface.call($('body'))
})
9 years ago
$(`${SELECTOR_SIDEBAR} a`)
.on('focusin', () => {
$(SELECTOR_MAIN_SIDEBAR).addClass(CLASS_NAME_SIDEBAR_FOCUSED)
})
.on('focusout', () => {
$(SELECTOR_MAIN_SIDEBAR).removeClass(CLASS_NAME_SIDEBAR_FOCUSED)
})
9 years ago
/**
* jQuery API
* ====================================================
*/
9 years ago
$.fn[NAME] = Layout._jQueryInterface
$.fn[NAME].Constructor = Layout
$.fn[NAME].noConflict = function () {
$.fn[NAME] = JQUERY_NO_CONFLICT
return Layout._jQueryInterface
}
7 years ago
export default Layout