forked from FrontendMatter/bootstrap-layout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsidebar-menu-collapse.js
More file actions
67 lines (59 loc) · 1.64 KB
/
Copy pathsidebar-menu-collapse.js
File metadata and controls
67 lines (59 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const COLLAPSE_SELECTOR = '[data-toggle="sidebar-collapse"]'
const COLLAPSE_DATA_KEY = 'bl.sidebar-collapse'
const COLLAPSE_DATA_INIT = `init.${ COLLAPSE_DATA_KEY }`
export class SidebarMenuCollapse {
/**
* SidebarMenuCollapse constructor
* @return {[type]} [description]
*/
constructor () {
jQuery(COLLAPSE_SELECTOR).each((index, button) => this.init(button))
}
/**
* Get a jQuery element
* @param {String|jQuery} elementOrSelector jQuery element or DOM selector
* @return {jQuery} A jQuery element
*/
_element (elementOrSelector) {
return elementOrSelector instanceof jQuery ? elementOrSelector : jQuery(elementOrSelector)
}
/**
* Click event listener
* @param {MouseEvent} e Mouse Event
*/
_onClick (e) {
e.preventDefault()
const button = jQuery(e.currentTarget)
const parent = button.parent()
if (parent.hasClass('open')) {
parent.removeClass('open')
}
else if (button.next('ul').html()) {
button.closest('ul').find('.open').removeClass('open')
parent.addClass('open')
}
}
/**
* Initialize a sidebar menu
* @param {String|jQuery} button jQuery element or DOM selector
*/
init (button) {
button = this._element(button)
if (!button.data(COLLAPSE_DATA_INIT)) {
button
.on(`click.${ COLLAPSE_DATA_KEY }`, this._onClick)
.data(COLLAPSE_DATA_INIT, true)
}
}
/**
* Destroy a sidebar menu
* @param {String|jQuery} button jQuery element or DOM selector
*/
destroy (button) {
this._element(button)
.off(`click.${ COLLAPSE_DATA_KEY }`)
.removeData(COLLAPSE_DATA_INIT)
}
}
// EXPORT INSTANCE
export let sidebarMenuCollapse = new SidebarMenuCollapse()