2017-09-10 17:25:29 +05:30
|
|
|
import Cookies from 'js-cookie';
|
|
|
|
import _ from 'underscore';
|
|
|
|
import bp from './breakpoints';
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
export default class ContextualSidebar {
|
2017-09-10 17:25:29 +05:30
|
|
|
constructor() {
|
|
|
|
this.initDomElements();
|
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
|
|
|
|
initDomElements() {
|
2018-03-17 18:26:18 +05:30
|
|
|
this.$page = $('.layout-page');
|
2017-09-10 17:25:29 +05:30
|
|
|
this.$sidebar = $('.nav-sidebar');
|
2018-03-17 18:26:18 +05:30
|
|
|
this.$innerScroll = $('.nav-sidebar-inner-scroll', this.$sidebar);
|
2017-09-10 17:25:29 +05:30
|
|
|
this.$overlay = $('.mobile-overlay');
|
|
|
|
this.$openSidebar = $('.toggle-mobile-nav');
|
|
|
|
this.$closeSidebar = $('.close-nav-button');
|
|
|
|
this.$sidebarToggle = $('.js-toggle-sidebar');
|
|
|
|
}
|
|
|
|
|
|
|
|
bindEvents() {
|
2018-03-17 18:26:18 +05:30
|
|
|
document.addEventListener('click', (e) => {
|
|
|
|
if (!e.target.closest('.nav-sidebar') && (bp.getBreakpointSize() === 'sm' || bp.getBreakpointSize() === 'md')) {
|
|
|
|
this.toggleCollapsedSidebar(true);
|
|
|
|
}
|
|
|
|
});
|
2017-09-10 17:25:29 +05:30
|
|
|
this.$openSidebar.on('click', () => this.toggleSidebarNav(true));
|
|
|
|
this.$closeSidebar.on('click', () => this.toggleSidebarNav(false));
|
|
|
|
this.$overlay.on('click', () => this.toggleSidebarNav(false));
|
|
|
|
this.$sidebarToggle.on('click', () => {
|
2018-03-17 18:26:18 +05:30
|
|
|
const value = !this.$sidebar.hasClass('sidebar-collapsed-desktop');
|
2017-09-10 17:25:29 +05:30
|
|
|
this.toggleCollapsedSidebar(value);
|
|
|
|
});
|
|
|
|
|
|
|
|
$(window).on('resize', () => _.debounce(this.render(), 100));
|
|
|
|
}
|
|
|
|
|
|
|
|
static setCollapsedCookie(value) {
|
|
|
|
if (bp.getBreakpointSize() !== 'lg') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Cookies.set('sidebar_collapsed', value, { expires: 365 * 10 });
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleSidebarNav(show) {
|
2018-03-17 18:26:18 +05:30
|
|
|
this.$sidebar.toggleClass('sidebar-expanded-mobile', show);
|
2017-09-10 17:25:29 +05:30
|
|
|
this.$overlay.toggleClass('mobile-nav-open', show);
|
2018-03-17 18:26:18 +05:30
|
|
|
this.$sidebar.removeClass('sidebar-collapsed-desktop');
|
2017-09-10 17:25:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
toggleCollapsedSidebar(collapsed) {
|
|
|
|
const breakpoint = bp.getBreakpointSize();
|
|
|
|
|
|
|
|
if (this.$sidebar.length) {
|
2018-03-17 18:26:18 +05:30
|
|
|
this.$sidebar.toggleClass('sidebar-collapsed-desktop', collapsed);
|
2017-09-10 17:25:29 +05:30
|
|
|
this.$page.toggleClass('page-with-icon-sidebar', breakpoint === 'sm' ? true : collapsed);
|
|
|
|
}
|
2018-03-17 18:26:18 +05:30
|
|
|
ContextualSidebar.setCollapsedCookie(collapsed);
|
|
|
|
|
|
|
|
this.toggleSidebarOverflow();
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleSidebarOverflow() {
|
|
|
|
if (this.$innerScroll.prop('scrollHeight') > this.$innerScroll.prop('offsetHeight')) {
|
|
|
|
this.$innerScroll.css('overflow-y', 'scroll');
|
|
|
|
} else {
|
|
|
|
this.$innerScroll.css('overflow-y', '');
|
|
|
|
}
|
2017-09-10 17:25:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const breakpoint = bp.getBreakpointSize();
|
|
|
|
|
|
|
|
if (breakpoint === 'sm' || breakpoint === 'md') {
|
|
|
|
this.toggleCollapsedSidebar(true);
|
|
|
|
} else if (breakpoint === 'lg') {
|
|
|
|
const collapse = Cookies.get('sidebar_collapsed') === 'true';
|
|
|
|
this.toggleCollapsedSidebar(collapse);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|