AdminLTE/build/js/Treeview.js

163 lines
3.9 KiB
JavaScript
Raw Normal View History

2015-11-11 20:29:54 +01:00
/**
* --------------------------------------------
* AdminLTE Treeview.js
* License MIT
* --------------------------------------------
*/
const Treeview = (($) => {
/**
* Constants
* ====================================================
*/
const NAME = 'Treeview'
const DATA_KEY = 'lte.treeview'
const EVENT_KEY = `.${DATA_KEY}`
const JQUERY_NO_CONFLICT = $.fn[NAME]
2016-01-16 17:27:23 +01:00
const Event = {
2016-10-26 15:30:59 +02:00
SELECTED : `selected${EVENT_KEY}`,
EXPANDED : `expanded${EVENT_KEY}`,
COLLAPSED : `collapsed${EVENT_KEY}`,
2016-01-16 17:27:23 +01:00
LOAD_DATA_API: `load${EVENT_KEY}`
2015-11-11 20:29:54 +01:00
}
const Selector = {
2016-10-26 15:30:59 +02:00
LI : '.nav-item',
LINK : '.nav-link',
2016-01-16 17:27:23 +01:00
TREEVIEW_MENU: '.nav-treeview',
2016-10-26 15:30:59 +02:00
OPEN : '.menu-open',
DATA_WIDGET : '[data-widget="treeview"]'
2015-11-11 20:29:54 +01:00
}
2016-01-16 17:27:23 +01:00
const ClassName = {
2016-10-26 15:30:59 +02:00
LI : 'nav-item',
LINK : 'nav-link',
2016-01-16 17:27:23 +01:00
TREEVIEW_MENU: 'nav-treeview',
2016-10-26 15:30:59 +02:00
OPEN : 'menu-open'
2016-01-16 17:27:23 +01:00
}
const Default = {
2016-10-26 15:30:59 +02:00
trigger : `${Selector.DATA_WIDGET} ${Selector.LINK}`,
2016-01-16 17:27:23 +01:00
animationSpeed: 300,
2016-10-26 15:30:59 +02:00
accordion : true
2016-01-16 17:27:23 +01:00
}
2015-11-11 20:29:54 +01:00
/**
* Class Definition
* ====================================================
*/
class Treeview {
constructor(element, config) {
this._config = config
this._element = element
}
// Public
2016-01-16 17:27:23 +01:00
init() {
this._setupListeners()
}
expand(treeviewMenu, parentLi) {
2018-02-04 00:45:19 +01:00
const expandedEvent = $.Event(Event.EXPANDED)
2016-01-16 17:27:23 +01:00
if (this._config.accordion) {
2018-02-04 00:45:19 +01:00
const openMenuLi = parentLi.siblings(Selector.OPEN).first()
const openTreeview = openMenuLi.find(Selector.TREEVIEW_MENU).first()
2016-01-16 17:27:23 +01:00
this.collapse(openTreeview, openMenuLi)
}
treeviewMenu.slideDown(this._config.animationSpeed, () => {
parentLi.addClass(ClassName.OPEN)
$(this._element).trigger(expandedEvent)
})
}
collapse(treeviewMenu, parentLi) {
2018-02-04 00:45:19 +01:00
const collapsedEvent = $.Event(Event.COLLAPSED)
2016-01-16 17:27:23 +01:00
treeviewMenu.slideUp(this._config.animationSpeed, () => {
parentLi.removeClass(ClassName.OPEN)
$(this._element).trigger(collapsedEvent)
treeviewMenu.find(`${Selector.OPEN} > ${Selector.TREEVIEW_MENU}`).slideUp()
treeviewMenu.find(Selector.OPEN).removeClass(ClassName.OPEN)
})
}
toggle(event) {
2018-02-04 00:45:19 +01:00
const $relativeTarget = $(event.currentTarget)
const treeviewMenu = $relativeTarget.next()
2016-01-16 17:27:23 +01:00
if (!treeviewMenu.is(Selector.TREEVIEW_MENU)) {
return
}
event.preventDefault()
2018-02-04 00:45:19 +01:00
const parentLi = $relativeTarget.parents(Selector.LI).first()
const isOpen = parentLi.hasClass(ClassName.OPEN)
2016-01-16 17:27:23 +01:00
if (isOpen) {
this.collapse($(treeviewMenu), parentLi)
} else {
this.expand($(treeviewMenu), parentLi)
}
}
2015-11-11 20:29:54 +01:00
// Private
2016-01-16 17:27:23 +01:00
_setupListeners() {
$(document).on('click', this._config.trigger, (event) => {
this.toggle(event)
})
}
2015-11-11 20:29:54 +01:00
// Static
2016-01-16 17:27:23 +01:00
2015-11-11 20:29:54 +01:00
static _jQueryInterface(config) {
return this.each(function () {
2018-03-17 18:07:55 +01:00
let data = $(this).data(DATA_KEY)
2018-02-04 00:45:19 +01:00
const _config = $.extend({}, Default, $(this).data())
2015-11-11 20:29:54 +01:00
2016-01-16 17:27:23 +01:00
if (!data) {
data = new Treeview($(this), _config)
$(this).data(DATA_KEY, data)
}
if (config === 'init') {
data[config]()
}
2015-11-11 20:29:54 +01:00
})
}
}
2016-01-16 17:27:23 +01:00
/**
* Data API
* ====================================================
*/
$(window).on(Event.LOAD_DATA_API, () => {
$(Selector.DATA_WIDGET).each(function () {
2018-03-17 18:07:55 +01:00
Treeview._jQueryInterface.call($(this), 'init')
2016-01-16 17:27:23 +01:00
})
})
2015-11-11 20:29:54 +01:00
/**
* jQuery API
* ====================================================
*/
2016-01-16 17:27:23 +01:00
$.fn[NAME] = Treeview._jQueryInterface
2015-11-11 20:29:54 +01:00
$.fn[NAME].Constructor = Treeview
$.fn[NAME].noConflict = function () {
$.fn[NAME] = JQUERY_NO_CONFLICT
return Treeview._jQueryInterface
}
return Treeview
})(jQuery)
2018-02-04 00:45:19 +01:00
export default Treeview