AdminLTE/build/js/PushMenu.js

228 lines
5.3 KiB
JavaScript
Raw Normal View History

2015-11-11 19:29:54 +00:00
/**
* --------------------------------------------
* AdminLTE PushMenu.js
* License MIT
* --------------------------------------------
*/
2020-06-02 13:23:22 +00:00
import $ from 'jquery'
2018-03-17 17:07:55 +00:00
2020-06-02 13:23:22 +00:00
/**
* Constants
* ====================================================
*/
2015-11-11 19:29:54 +00:00
2020-06-02 13:23:22 +00:00
const NAME = 'PushMenu'
const DATA_KEY = 'lte.pushmenu'
const EVENT_KEY = `.${DATA_KEY}`
const JQUERY_NO_CONFLICT = $.fn[NAME]
const EVENT_COLLAPSED = `collapsed${EVENT_KEY}`
const EVENT_SHOWN = `shown${EVENT_KEY}`
const SELECTOR_TOGGLE_BUTTON = '[data-widget="pushmenu"]'
const SELECTOR_BODY = 'body'
const SELECTOR_OVERLAY = '#sidebar-overlay'
const SELECTOR_WRAPPER = '.wrapper'
const CLASS_NAME_COLLAPSED = 'sidebar-collapse'
const CLASS_NAME_OPEN = 'sidebar-open'
const CLASS_NAME_IS_OPENING = 'sidebar-is-opening'
const CLASS_NAME_CLOSED = 'sidebar-closed'
2020-06-02 13:23:22 +00:00
const Default = {
autoCollapseSize: 992,
enableRemember: false,
noTransitionAfterReload: true
}
/**
* Class Definition
* ====================================================
*/
2015-11-11 19:29:54 +00:00
2020-06-02 13:23:22 +00:00
class PushMenu {
constructor(element, options) {
this._element = element
this._options = $.extend({}, Default, options)
2015-11-11 19:29:54 +00:00
if ($(SELECTOR_OVERLAY).length === 0) {
2020-06-02 13:23:22 +00:00
this._addOverlay()
}
2018-03-17 17:07:55 +00:00
2020-06-02 13:23:22 +00:00
this._init()
}
// Public
2020-06-02 13:23:22 +00:00
expand() {
const $bodySelector = $(SELECTOR_BODY)
2020-06-04 18:06:38 +00:00
2020-06-02 13:23:22 +00:00
if (this._options.autoCollapseSize) {
if ($(window).width() <= this._options.autoCollapseSize) {
$bodySelector.addClass(CLASS_NAME_OPEN)
2020-06-02 13:23:22 +00:00
}
2015-11-11 19:29:54 +00:00
}
$bodySelector.addClass(CLASS_NAME_IS_OPENING).removeClass(`${CLASS_NAME_COLLAPSED} ${CLASS_NAME_CLOSED}`).delay(50).queue(function () {
$bodySelector.removeClass(CLASS_NAME_IS_OPENING)
$(this).dequeue()
})
2015-11-11 19:29:54 +00:00
2020-06-02 13:23:22 +00:00
if (this._options.enableRemember) {
localStorage.setItem(`remember${EVENT_KEY}`, CLASS_NAME_OPEN)
2020-06-02 13:23:22 +00:00
}
$(this._element).trigger($.Event(EVENT_SHOWN))
2020-06-02 13:23:22 +00:00
}
2015-11-11 19:29:54 +00:00
2020-06-02 13:23:22 +00:00
collapse() {
const $bodySelector = $(SELECTOR_BODY)
2020-06-04 18:06:38 +00:00
2020-06-02 13:23:22 +00:00
if (this._options.autoCollapseSize) {
if ($(window).width() <= this._options.autoCollapseSize) {
$bodySelector.removeClass(CLASS_NAME_OPEN).addClass(CLASS_NAME_CLOSED)
}
2015-11-11 19:29:54 +00:00
}
$bodySelector.addClass(CLASS_NAME_COLLAPSED)
2020-06-02 13:23:22 +00:00
if (this._options.enableRemember) {
localStorage.setItem(`remember${EVENT_KEY}`, CLASS_NAME_COLLAPSED)
2020-06-02 13:23:22 +00:00
}
2015-11-11 19:29:54 +00:00
$(this._element).trigger($.Event(EVENT_COLLAPSED))
2020-06-02 13:23:22 +00:00
}
2020-06-02 13:23:22 +00:00
toggle() {
if ($(SELECTOR_BODY).hasClass(CLASS_NAME_COLLAPSED)) {
2020-06-02 13:23:22 +00:00
this.expand()
} else {
this.collapse()
2015-11-11 19:29:54 +00:00
}
2020-06-02 13:23:22 +00:00
}
2015-11-11 19:29:54 +00:00
2020-06-02 13:23:22 +00:00
autoCollapse(resize = false) {
if (!this._options.autoCollapseSize) {
return
2015-11-11 19:29:54 +00:00
}
const $bodySelector = $(SELECTOR_BODY)
2020-06-04 18:06:38 +00:00
2020-06-02 13:23:22 +00:00
if ($(window).width() <= this._options.autoCollapseSize) {
if (!$bodySelector.hasClass(CLASS_NAME_OPEN)) {
2020-06-02 13:23:22 +00:00
this.collapse()
2020-05-31 13:45:54 +00:00
}
2020-06-02 13:23:22 +00:00
} else if (resize === true) {
if ($bodySelector.hasClass(CLASS_NAME_OPEN)) {
$bodySelector.removeClass(CLASS_NAME_OPEN)
} else if ($bodySelector.hasClass(CLASS_NAME_CLOSED)) {
2020-06-02 13:23:22 +00:00
this.expand()
}
}
2020-06-02 13:23:22 +00:00
}
2020-06-02 13:23:22 +00:00
remember() {
if (!this._options.enableRemember) {
return
}
2020-05-31 13:45:54 +00:00
2020-06-04 18:06:38 +00:00
const $body = $('body')
2020-06-02 13:23:22 +00:00
const toggleState = localStorage.getItem(`remember${EVENT_KEY}`)
2020-06-04 18:06:38 +00:00
if (toggleState === CLASS_NAME_COLLAPSED) {
2020-06-02 13:23:22 +00:00
if (this._options.noTransitionAfterReload) {
$body.addClass('hold-transition').addClass(CLASS_NAME_COLLAPSED).delay(50).queue(function () {
2020-05-31 13:45:54 +00:00
$(this).removeClass('hold-transition')
$(this).dequeue()
})
} else {
$body.addClass(CLASS_NAME_COLLAPSED)
}
2020-06-02 13:23:22 +00:00
} else if (this._options.noTransitionAfterReload) {
$body.addClass('hold-transition').removeClass(CLASS_NAME_COLLAPSED).delay(50).queue(function () {
2020-06-02 13:23:22 +00:00
$(this).removeClass('hold-transition')
$(this).dequeue()
})
} else {
$body.removeClass(CLASS_NAME_COLLAPSED)
}
2020-06-02 13:23:22 +00:00
}
2020-06-02 13:23:22 +00:00
// Private
2020-06-02 13:23:22 +00:00
_init() {
this.remember()
this.autoCollapse()
2020-06-02 13:23:22 +00:00
$(window).resize(() => {
this.autoCollapse(true)
})
}
2020-06-02 13:23:22 +00:00
_addOverlay() {
const overlay = $('<div />', {
id: 'sidebar-overlay'
})
2018-03-17 17:07:55 +00:00
2020-06-02 13:23:22 +00:00
overlay.on('click', () => {
this.collapse()
})
2018-03-17 17:07:55 +00:00
$(SELECTOR_WRAPPER).append(overlay)
2020-06-02 13:23:22 +00:00
}
2018-03-17 17:07:55 +00:00
2020-06-02 13:23:22 +00:00
// Static
2015-11-11 19:29:54 +00:00
2020-06-02 13:23:22 +00:00
static _jQueryInterface(operation) {
return this.each(function () {
let data = $(this).data(DATA_KEY)
const _options = $.extend({}, Default, $(this).data())
2015-11-11 19:29:54 +00:00
2020-06-02 13:23:22 +00:00
if (!data) {
data = new PushMenu(this, _options)
$(this).data(DATA_KEY, data)
}
2015-11-11 19:29:54 +00:00
2020-06-02 13:23:22 +00:00
if (typeof operation === 'string' && operation.match(/collapse|expand|toggle/)) {
data[operation]()
}
})
2015-11-11 19:29:54 +00:00
}
2020-06-02 13:23:22 +00:00
}
2015-11-11 19:29:54 +00:00
2020-06-02 13:23:22 +00:00
/**
* Data API
* ====================================================
*/
2015-11-11 19:29:54 +00:00
$(document).on('click', SELECTOR_TOGGLE_BUTTON, event => {
2020-06-02 13:23:22 +00:00
event.preventDefault()
2015-11-11 19:29:54 +00:00
2020-06-02 13:23:22 +00:00
let button = event.currentTarget
2015-11-11 19:29:54 +00:00
2020-06-02 13:23:22 +00:00
if ($(button).data('widget') !== 'pushmenu') {
button = $(button).closest(SELECTOR_TOGGLE_BUTTON)
2020-06-02 13:23:22 +00:00
}
2015-11-11 19:29:54 +00:00
2020-06-02 13:23:22 +00:00
PushMenu._jQueryInterface.call($(button), 'toggle')
})
2020-06-02 13:23:22 +00:00
$(window).on('load', () => {
PushMenu._jQueryInterface.call($(SELECTOR_TOGGLE_BUTTON))
2020-06-02 13:23:22 +00:00
})
2015-11-11 19:29:54 +00:00
2020-06-02 13:23:22 +00:00
/**
* jQuery API
* ====================================================
*/
2015-11-11 19:29:54 +00:00
2020-06-02 13:23:22 +00:00
$.fn[NAME] = PushMenu._jQueryInterface
$.fn[NAME].Constructor = PushMenu
$.fn[NAME].noConflict = function () {
$.fn[NAME] = JQUERY_NO_CONFLICT
return PushMenu._jQueryInterface
}
2018-02-03 23:45:19 +00:00
export default PushMenu