AdminLTE/build/js/ControlSidebar.js

297 lines
8.5 KiB
JavaScript
Raw Normal View History

2016-10-19 17:23:34 +02:00
/**
* --------------------------------------------
* AdminLTE ControlSidebar.js
* License MIT
* --------------------------------------------
*/
2020-06-02 15:23:22 +02:00
import $ from 'jquery'
2016-10-19 17:23:34 +02:00
2020-06-02 15:23:22 +02:00
/**
* Constants
* ====================================================
*/
2016-10-19 17:23:34 +02:00
2020-06-02 15:23:22 +02:00
const NAME = 'ControlSidebar'
const DATA_KEY = 'lte.controlsidebar'
const EVENT_KEY = `.${DATA_KEY}`
const JQUERY_NO_CONFLICT = $.fn[NAME]
const EVENT_COLLAPSED = `collapsed${EVENT_KEY}`
const EVENT_EXPANDED = `expanded${EVENT_KEY}`
const SELECTOR_CONTROL_SIDEBAR = '.control-sidebar'
const SELECTOR_CONTROL_SIDEBAR_CONTENT = '.control-sidebar-content'
const SELECTOR_DATA_TOGGLE = '[data-widget="control-sidebar"]'
const SELECTOR_HEADER = '.main-header'
const SELECTOR_FOOTER = '.main-footer'
const CLASS_NAME_CONTROL_SIDEBAR_ANIMATE = 'control-sidebar-animate'
const CLASS_NAME_CONTROL_SIDEBAR_OPEN = 'control-sidebar-open'
const CLASS_NAME_CONTROL_SIDEBAR_SLIDE = 'control-sidebar-slide-open'
const CLASS_NAME_LAYOUT_FIXED = 'layout-fixed'
const CLASS_NAME_NAVBAR_FIXED = 'layout-navbar-fixed'
const CLASS_NAME_NAVBAR_SM_FIXED = 'layout-sm-navbar-fixed'
const CLASS_NAME_NAVBAR_MD_FIXED = 'layout-md-navbar-fixed'
const CLASS_NAME_NAVBAR_LG_FIXED = 'layout-lg-navbar-fixed'
const CLASS_NAME_NAVBAR_XL_FIXED = 'layout-xl-navbar-fixed'
const CLASS_NAME_FOOTER_FIXED = 'layout-footer-fixed'
const CLASS_NAME_FOOTER_SM_FIXED = 'layout-sm-footer-fixed'
const CLASS_NAME_FOOTER_MD_FIXED = 'layout-md-footer-fixed'
const CLASS_NAME_FOOTER_LG_FIXED = 'layout-lg-footer-fixed'
const CLASS_NAME_FOOTER_XL_FIXED = 'layout-xl-footer-fixed'
2020-06-02 15:23:22 +02:00
const Default = {
controlsidebarSlide: true,
scrollbarTheme: 'os-theme-light',
scrollbarAutoHide: 'l'
}
2016-10-19 17:23:34 +02:00
2020-06-02 15:23:22 +02:00
/**
* Class Definition
* ====================================================
*/
2016-10-19 17:23:34 +02:00
2020-06-02 15:23:22 +02:00
class ControlSidebar {
constructor(element, config) {
this._element = element
this._config = config
this._init()
}
2016-10-19 17:23:34 +02:00
2020-06-02 15:23:22 +02:00
// Public
2020-06-02 15:23:22 +02:00
collapse() {
2020-06-04 20:06:38 +02:00
const $body = $('body')
const $html = $('html')
2020-06-02 15:23:22 +02:00
// Show the control sidebar
if (this._config.controlsidebarSlide) {
$html.addClass(CLASS_NAME_CONTROL_SIDEBAR_ANIMATE)
$body.removeClass(CLASS_NAME_CONTROL_SIDEBAR_SLIDE).delay(300).queue(function () {
$(SELECTOR_CONTROL_SIDEBAR).hide()
$html.removeClass(CLASS_NAME_CONTROL_SIDEBAR_ANIMATE)
2020-06-02 15:23:22 +02:00
$(this).dequeue()
})
} else {
$body.removeClass(CLASS_NAME_CONTROL_SIDEBAR_OPEN)
2016-10-19 17:23:34 +02:00
}
$(this._element).trigger($.Event(EVENT_COLLAPSED))
2020-06-02 15:23:22 +02:00
}
2016-10-19 17:23:34 +02:00
2020-06-02 15:23:22 +02:00
show() {
2020-06-04 20:06:38 +02:00
const $body = $('body')
const $html = $('html')
2020-06-02 15:23:22 +02:00
// Collapse the control sidebar
if (this._config.controlsidebarSlide) {
$html.addClass(CLASS_NAME_CONTROL_SIDEBAR_ANIMATE)
$(SELECTOR_CONTROL_SIDEBAR).show().delay(10).queue(function () {
$body.addClass(CLASS_NAME_CONTROL_SIDEBAR_SLIDE).delay(300).queue(function () {
$html.removeClass(CLASS_NAME_CONTROL_SIDEBAR_ANIMATE)
$(this).dequeue()
})
2020-06-02 15:23:22 +02:00
$(this).dequeue()
})
} else {
$body.addClass(CLASS_NAME_CONTROL_SIDEBAR_OPEN)
2016-10-19 17:23:34 +02:00
}
this._fixHeight()
this._fixScrollHeight()
$(this._element).trigger($.Event(EVENT_EXPANDED))
2020-06-02 15:23:22 +02:00
}
2016-10-19 17:23:34 +02:00
2020-06-02 15:23:22 +02:00
toggle() {
2020-06-04 20:06:38 +02:00
const $body = $('body')
const shouldClose = $body.hasClass(CLASS_NAME_CONTROL_SIDEBAR_OPEN) ||
$body.hasClass(CLASS_NAME_CONTROL_SIDEBAR_SLIDE)
2020-06-04 20:06:38 +02:00
2020-06-02 15:23:22 +02:00
if (shouldClose) {
// Close the control sidebar
this.collapse()
} else {
// Open the control sidebar
this.show()
2016-10-19 17:23:34 +02:00
}
2020-06-02 15:23:22 +02:00
}
2016-10-19 17:23:34 +02:00
2020-06-02 15:23:22 +02:00
// Private
2016-10-19 17:23:34 +02:00
2020-06-02 15:23:22 +02:00
_init() {
this._fixHeight()
this._fixScrollHeight()
$(window).resize(() => {
this._fixHeight()
this._fixScrollHeight()
2020-06-02 15:23:22 +02:00
})
2020-06-02 15:23:22 +02:00
$(window).scroll(() => {
2020-06-04 20:06:38 +02:00
const $body = $('body')
const shouldFixHeight = $body.hasClass(CLASS_NAME_CONTROL_SIDEBAR_OPEN) ||
$body.hasClass(CLASS_NAME_CONTROL_SIDEBAR_SLIDE)
2020-06-04 20:06:38 +02:00
2020-06-05 15:49:56 +02:00
if (shouldFixHeight) {
this._fixScrollHeight()
2020-06-02 15:23:22 +02:00
}
})
}
2020-06-02 15:23:22 +02:00
_fixScrollHeight() {
2020-06-04 20:06:38 +02:00
const $body = $('body')
if (!$body.hasClass(CLASS_NAME_LAYOUT_FIXED)) {
2020-06-02 15:23:22 +02:00
return
}
2020-06-02 15:23:22 +02:00
const heights = {
scroll: $(document).height(),
window: $(window).height(),
header: $(SELECTOR_HEADER).outerHeight(),
footer: $(SELECTOR_FOOTER).outerHeight()
2020-06-02 15:23:22 +02:00
}
const positions = {
bottom: Math.abs((heights.window + $(window).scrollTop()) - heights.scroll),
top: $(window).scrollTop()
}
const navbarFixed = (
$body.hasClass(CLASS_NAME_NAVBAR_FIXED) ||
$body.hasClass(CLASS_NAME_NAVBAR_SM_FIXED) ||
$body.hasClass(CLASS_NAME_NAVBAR_MD_FIXED) ||
$body.hasClass(CLASS_NAME_NAVBAR_LG_FIXED) ||
$body.hasClass(CLASS_NAME_NAVBAR_XL_FIXED)
) && $(SELECTOR_HEADER).css('position') === 'fixed'
2020-05-30 15:44:20 +02:00
const footerFixed = (
$body.hasClass(CLASS_NAME_FOOTER_FIXED) ||
$body.hasClass(CLASS_NAME_FOOTER_SM_FIXED) ||
$body.hasClass(CLASS_NAME_FOOTER_MD_FIXED) ||
$body.hasClass(CLASS_NAME_FOOTER_LG_FIXED) ||
$body.hasClass(CLASS_NAME_FOOTER_XL_FIXED)
) && $(SELECTOR_FOOTER).css('position') === 'fixed'
const $controlSidebar = $(SELECTOR_CONTROL_SIDEBAR)
2020-06-13 14:34:10 +02:00
const $controlsidebarContent = $(`${SELECTOR_CONTROL_SIDEBAR}, ${SELECTOR_CONTROL_SIDEBAR} ${SELECTOR_CONTROL_SIDEBAR_CONTENT}`)
2020-06-04 20:06:38 +02:00
2020-06-02 15:23:22 +02:00
if (positions.top === 0 && positions.bottom === 0) {
2020-06-04 20:06:38 +02:00
$controlSidebar.css({
bottom: heights.footer,
top: heights.header
})
2020-06-04 20:06:38 +02:00
$controlsidebarContent.css('height', heights.window - (heights.header + heights.footer))
2020-06-02 15:23:22 +02:00
} else if (positions.bottom <= heights.footer) {
if (footerFixed === false) {
const top = heights.header - positions.top
$controlSidebar.css('bottom', heights.footer - positions.bottom).css('top', top >= 0 ? top : 0)
2020-06-04 20:06:38 +02:00
$controlsidebarContent.css('height', heights.window - (heights.footer - positions.bottom))
2020-06-02 15:23:22 +02:00
} else {
2020-06-04 20:06:38 +02:00
$controlSidebar.css('bottom', heights.footer)
2020-06-02 15:23:22 +02:00
}
} else if (positions.top <= heights.header) {
if (navbarFixed === false) {
2020-06-04 20:06:38 +02:00
$controlSidebar.css('top', heights.header - positions.top)
$controlsidebarContent.css('height', heights.window - (heights.header - positions.top))
2020-05-31 15:45:54 +02:00
} else {
2020-06-04 20:06:38 +02:00
$controlSidebar.css('top', heights.header)
}
2020-06-02 15:23:22 +02:00
} else if (navbarFixed === false) {
2020-06-04 20:06:38 +02:00
$controlSidebar.css('top', 0)
$controlsidebarContent.css('height', heights.window)
2020-06-02 15:23:22 +02:00
} else {
2020-06-04 20:06:38 +02:00
$controlSidebar.css('top', heights.header)
}
2020-06-02 15:23:22 +02:00
}
2020-06-02 15:23:22 +02:00
_fixHeight() {
2020-06-04 20:06:38 +02:00
const $body = $('body')
if (!$body.hasClass(CLASS_NAME_LAYOUT_FIXED)) {
2020-06-02 15:23:22 +02:00
return
}
2020-05-31 15:45:54 +02:00
2020-06-02 15:23:22 +02:00
const heights = {
window: $(window).height(),
header: $(SELECTOR_HEADER).outerHeight(),
footer: $(SELECTOR_FOOTER).outerHeight()
2020-06-02 15:23:22 +02:00
}
2020-06-02 15:23:22 +02:00
let sidebarHeight = heights.window - heights.header
2020-06-02 15:23:22 +02:00
if (
$body.hasClass(CLASS_NAME_FOOTER_FIXED) ||
$body.hasClass(CLASS_NAME_FOOTER_SM_FIXED) ||
$body.hasClass(CLASS_NAME_FOOTER_MD_FIXED) ||
$body.hasClass(CLASS_NAME_FOOTER_LG_FIXED) ||
$body.hasClass(CLASS_NAME_FOOTER_XL_FIXED)
2020-06-02 15:23:22 +02:00
) {
if ($(SELECTOR_FOOTER).css('position') === 'fixed') {
2020-06-02 15:23:22 +02:00
sidebarHeight = heights.window - heights.header - heights.footer
2020-05-31 15:45:54 +02:00
}
2020-06-02 15:23:22 +02:00
}
2020-06-13 14:34:10 +02:00
const $controlSidebar = $(`${SELECTOR_CONTROL_SIDEBAR} ${SELECTOR_CONTROL_SIDEBAR_CONTENT}`)
2020-06-04 20:06:38 +02:00
$controlSidebar.css('height', sidebarHeight)
2020-05-30 15:44:20 +02:00
2020-06-02 15:23:22 +02:00
if (typeof $.fn.overlayScrollbars !== 'undefined') {
2020-06-04 20:06:38 +02:00
$controlSidebar.overlayScrollbars({
2020-06-02 15:23:22 +02:00
className: this._config.scrollbarTheme,
sizeAutoCapable: true,
scrollbars: {
autoHide: this._config.scrollbarAutoHide,
clickScrolling: true
}
})
2016-10-19 17:23:34 +02:00
}
2020-06-02 15:23:22 +02:00
}
2016-10-19 17:23:34 +02:00
2020-06-02 15:23:22 +02:00
// Static
2016-10-19 17:23:34 +02:00
2020-06-02 15:23:22 +02:00
static _jQueryInterface(operation) {
return this.each(function () {
let data = $(this).data(DATA_KEY)
const _options = $.extend({}, Default, $(this).data())
2016-10-19 17:23:34 +02:00
2020-06-02 15:23:22 +02:00
if (!data) {
data = new ControlSidebar(this, _options)
$(this).data(DATA_KEY, data)
}
2016-10-19 17:23:34 +02:00
2020-06-02 15:23:22 +02:00
if (data[operation] === 'undefined') {
throw new Error(`${operation} is not a function`)
}
2016-10-19 17:23:34 +02:00
2020-06-02 15:23:22 +02:00
data[operation]()
})
2016-10-19 17:23:34 +02:00
}
2020-06-02 15:23:22 +02:00
}
2016-10-19 17:23:34 +02:00
2020-06-02 15:23:22 +02:00
/**
*
* Data Api implementation
* ====================================================
*/
$(document).on('click', SELECTOR_DATA_TOGGLE, function (event) {
2020-06-02 15:23:22 +02:00
event.preventDefault()
2016-10-19 17:23:34 +02:00
2020-06-02 15:23:22 +02:00
ControlSidebar._jQueryInterface.call($(this), 'toggle')
})
2018-02-04 00:45:19 +01:00
2020-06-02 15:23:22 +02:00
/**
* jQuery API
* ====================================================
*/
2020-05-30 15:44:20 +02:00
2020-06-02 15:23:22 +02:00
$.fn[NAME] = ControlSidebar._jQueryInterface
$.fn[NAME].Constructor = ControlSidebar
$.fn[NAME].noConflict = function () {
$.fn[NAME] = JQUERY_NO_CONFLICT
return ControlSidebar._jQueryInterface
}
export default ControlSidebar