2015-11-11 19:29:54 +00:00
|
|
|
/**
|
|
|
|
* --------------------------------------------
|
|
|
|
* AdminLTE PushMenu.js
|
|
|
|
* License MIT
|
|
|
|
* --------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
const PushMenu = (($) => {
|
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
* ====================================================
|
|
|
|
*/
|
|
|
|
|
2018-03-17 17:07:55 +00:00
|
|
|
const NAME = 'PushMenu'
|
|
|
|
const DATA_KEY = 'lte.pushmenu'
|
|
|
|
const EVENT_KEY = `.${DATA_KEY}`
|
2015-11-11 19:29:54 +00:00
|
|
|
const JQUERY_NO_CONFLICT = $.fn[NAME]
|
|
|
|
|
|
|
|
const Event = {
|
|
|
|
COLLAPSED: `collapsed${EVENT_KEY}`,
|
2018-03-17 17:07:55 +00:00
|
|
|
SHOWN : `shown${EVENT_KEY}`
|
|
|
|
}
|
|
|
|
|
|
|
|
const Default = {
|
|
|
|
screenCollapseSize: 768
|
2015-11-11 19:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const Selector = {
|
2018-03-17 17:07:55 +00:00
|
|
|
TOGGLE_BUTTON : '[data-widget="pushmenu"]',
|
|
|
|
SIDEBAR_MINI : '.sidebar-mini',
|
|
|
|
SIDEBAR_COLLAPSED: '.sidebar-collapse',
|
|
|
|
BODY : 'body',
|
|
|
|
OVERLAY : '#sidebar-overlay',
|
|
|
|
WRAPPER : '.wrapper'
|
|
|
|
}
|
|
|
|
|
|
|
|
const ClassName = {
|
|
|
|
SIDEBAR_OPEN: 'sidebar-open',
|
|
|
|
COLLAPSED : 'sidebar-collapse',
|
|
|
|
OPEN : 'sidebar-open',
|
|
|
|
SIDEBAR_MINI: 'sidebar-mini'
|
2015-11-11 19:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Definition
|
|
|
|
* ====================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
class PushMenu {
|
2018-03-17 17:07:55 +00:00
|
|
|
constructor(element, options) {
|
2015-11-11 19:29:54 +00:00
|
|
|
this._element = element
|
2018-03-17 17:07:55 +00:00
|
|
|
this._options = $.extend({}, Default, options)
|
|
|
|
|
|
|
|
if (!$(Selector.OVERLAY).length) {
|
|
|
|
this._addOverlay()
|
|
|
|
}
|
2015-11-11 19:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Public
|
|
|
|
|
|
|
|
show() {
|
2018-03-17 17:07:55 +00:00
|
|
|
$(Selector.BODY).addClass(ClassName.OPEN).removeClass(ClassName.COLLAPSED)
|
2015-11-11 19:29:54 +00:00
|
|
|
|
2018-02-03 23:45:19 +00:00
|
|
|
const shownEvent = $.Event(Event.SHOWN)
|
2015-11-11 19:29:54 +00:00
|
|
|
$(this._element).trigger(shownEvent)
|
|
|
|
}
|
|
|
|
|
|
|
|
collapse() {
|
2018-03-17 17:07:55 +00:00
|
|
|
$(Selector.BODY).removeClass(ClassName.OPEN).addClass(ClassName.COLLAPSED)
|
2015-11-11 19:29:54 +00:00
|
|
|
|
2018-02-03 23:45:19 +00:00
|
|
|
const collapsedEvent = $.Event(Event.COLLAPSED)
|
2015-11-11 19:29:54 +00:00
|
|
|
$(this._element).trigger(collapsedEvent)
|
|
|
|
}
|
|
|
|
|
|
|
|
toggle() {
|
2018-03-17 17:07:55 +00:00
|
|
|
let isShown
|
|
|
|
if ($(window).width() >= this._options.screenCollapseSize) {
|
|
|
|
isShown = !$(Selector.BODY).hasClass(ClassName.COLLAPSED)
|
|
|
|
} else {
|
|
|
|
isShown = $(Selector.BODY).hasClass(ClassName.OPEN)
|
2015-11-11 19:29:54 +00:00
|
|
|
}
|
|
|
|
|
2018-03-17 17:07:55 +00:00
|
|
|
if (isShown) {
|
2015-11-11 19:29:54 +00:00
|
|
|
this.collapse()
|
|
|
|
} else {
|
|
|
|
this.show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-17 17:07:55 +00:00
|
|
|
// Private
|
|
|
|
_addOverlay() {
|
|
|
|
const overlay = $('<div />', {
|
|
|
|
id: 'sidebar-overlay'
|
|
|
|
})
|
|
|
|
|
|
|
|
overlay.on('click', () => {
|
|
|
|
this.collapse()
|
|
|
|
})
|
|
|
|
|
|
|
|
$(Selector.WRAPPER).append(overlay)
|
|
|
|
}
|
|
|
|
|
2015-11-11 19:29:54 +00:00
|
|
|
// Static
|
|
|
|
|
|
|
|
static _jQueryInterface(operation) {
|
|
|
|
return this.each(function () {
|
|
|
|
let data = $(this).data(DATA_KEY)
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
data = new PushMenu(this)
|
|
|
|
$(this).data(DATA_KEY, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation) {
|
|
|
|
data[operation]()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data API
|
|
|
|
* ====================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
$(document).on('click', Selector.TOGGLE_BUTTON, (event) => {
|
|
|
|
event.preventDefault()
|
|
|
|
|
2016-01-16 16:27:23 +00:00
|
|
|
let button = event.currentTarget
|
2015-11-11 19:29:54 +00:00
|
|
|
|
|
|
|
if ($(button).data('widget') !== 'pushmenu') {
|
|
|
|
button = $(button).closest(Selector.TOGGLE_BUTTON)
|
|
|
|
}
|
|
|
|
|
|
|
|
PushMenu._jQueryInterface.call($(button), 'toggle')
|
|
|
|
})
|
|
|
|
|
|
|
|
/**
|
|
|
|
* jQuery API
|
|
|
|
* ====================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
$.fn[NAME] = PushMenu._jQueryInterface
|
|
|
|
$.fn[NAME].Constructor = PushMenu
|
2018-03-17 17:07:55 +00:00
|
|
|
$.fn[NAME].noConflict = function () {
|
2015-11-11 19:29:54 +00:00
|
|
|
$.fn[NAME] = JQUERY_NO_CONFLICT
|
|
|
|
return PushMenu._jQueryInterface
|
|
|
|
}
|
|
|
|
|
|
|
|
return PushMenu
|
|
|
|
})(jQuery)
|
2018-02-03 23:45:19 +00:00
|
|
|
|
|
|
|
export default PushMenu
|