|
1 | 1 | import $ from 'jquery' |
2 | 2 | import PerfectScrollbar from 'perfect-scrollbar' |
| 3 | +import getStyle from './utilities/get-style' |
3 | 4 | import toggleClasses from './toggle-classes' |
4 | 5 |
|
5 | 6 | /** |
@@ -78,10 +79,14 @@ const Sidebar = (($) => { |
78 | 79 | class Sidebar { |
79 | 80 | constructor(element) { |
80 | 81 | this._element = element |
| 82 | + this.mobile = false |
81 | 83 | this.ps = null |
82 | 84 | this.perfectScrollbar(Event.INIT) |
83 | 85 | this.setActiveLink() |
| 86 | + this._breakpointTest = this._breakpointTest.bind(this) |
| 87 | + this._clickOutListener = this._clickOutListener.bind(this) |
84 | 88 | this._addEventListeners() |
| 89 | + this._addMediaQuery() |
85 | 90 | } |
86 | 91 |
|
87 | 92 | // Getters |
@@ -163,6 +168,50 @@ const Sidebar = (($) => { |
163 | 168 |
|
164 | 169 | // Private |
165 | 170 |
|
| 171 | + _addMediaQuery() { |
| 172 | + const sm = getStyle('--breakpoint-sm') |
| 173 | + if (!sm) { |
| 174 | + return |
| 175 | + } |
| 176 | + const smVal = parseInt(sm, 10) - 1 |
| 177 | + const mediaQueryList = window.matchMedia(`(max-width: ${smVal}px)`) |
| 178 | + |
| 179 | + this._breakpointTest(mediaQueryList) |
| 180 | + |
| 181 | + mediaQueryList.addListener(this._breakpointTest) |
| 182 | + } |
| 183 | + |
| 184 | + _breakpointTest(e) { |
| 185 | + this.mobile = Boolean(e.matches) |
| 186 | + this._toggleClickOut() |
| 187 | + } |
| 188 | + |
| 189 | + _clickOutListener(event) { |
| 190 | + if (!this._element.contains(event.target)) { // or use: event.target.closest(Selector.SIDEBAR) === null |
| 191 | + event.preventDefault() |
| 192 | + event.stopPropagation() |
| 193 | + this._removeClickOut() |
| 194 | + document.body.classList.remove('sidebar-show') |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + _addClickOut() { |
| 199 | + document.addEventListener(Event.CLICK, this._clickOutListener, true) |
| 200 | + } |
| 201 | + |
| 202 | + _removeClickOut() { |
| 203 | + document.removeEventListener(Event.CLICK, this._clickOutListener, true) |
| 204 | + } |
| 205 | + |
| 206 | + _toggleClickOut() { |
| 207 | + if (this.mobile && document.body.classList.contains('sidebar-show')) { |
| 208 | + document.body.classList.remove('aside-menu-show') |
| 209 | + this._addClickOut() |
| 210 | + } else { |
| 211 | + this._removeClickOut() |
| 212 | + } |
| 213 | + } |
| 214 | + |
166 | 215 | _addEventListeners() { |
167 | 216 | $(Selector.BRAND_MINIMIZER).on(Event.CLICK, (event) => { |
168 | 217 | event.preventDefault() |
@@ -190,9 +239,11 @@ const Sidebar = (($) => { |
190 | 239 | event.stopPropagation() |
191 | 240 | const toggle = event.currentTarget.dataset.toggle |
192 | 241 | toggleClasses(toggle, ShowClassNames) |
| 242 | + this._toggleClickOut() |
193 | 243 | }) |
194 | 244 |
|
195 | 245 | $(`${Selector.NAVIGATION} > ${Selector.NAV_ITEM} ${Selector.NAV_LINK}:not(${Selector.NAV_DROPDOWN_TOGGLE})`).on(Event.CLICK, () => { |
| 246 | + this._removeClickOut() |
196 | 247 | document.body.classList.remove('sidebar-show') |
197 | 248 | }) |
198 | 249 | } |
|
0 commit comments