2017-08-02 21:19:43 -04:00
|
|
|
import Cookies from 'js-cookie';
|
|
|
|
import _ from 'underscore';
|
2017-08-10 09:02:30 -04:00
|
|
|
import bp from './breakpoints';
|
2017-08-02 21:19:43 -04:00
|
|
|
|
2017-07-17 18:33:12 -04:00
|
|
|
export default class NewNavSidebar {
|
|
|
|
constructor() {
|
|
|
|
this.initDomElements();
|
2017-08-02 21:19:43 -04:00
|
|
|
this.render();
|
2017-07-17 18:33:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
initDomElements() {
|
2017-08-02 21:19:43 -04:00
|
|
|
this.$page = $('.page-with-sidebar');
|
2017-07-17 18:33:12 -04:00
|
|
|
this.$sidebar = $('.nav-sidebar');
|
2017-09-18 07:32:23 -04:00
|
|
|
this.$innerScroll = $('.nav-sidebar-inner-scroll', this.$sidebar);
|
2017-07-18 12:40:42 -04:00
|
|
|
this.$overlay = $('.mobile-overlay');
|
2017-07-17 18:33:12 -04:00
|
|
|
this.$openSidebar = $('.toggle-mobile-nav');
|
|
|
|
this.$closeSidebar = $('.close-nav-button');
|
2017-08-02 21:19:43 -04:00
|
|
|
this.$sidebarToggle = $('.js-toggle-sidebar');
|
2017-07-17 18:33:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bindEvents() {
|
2017-09-01 11:06:28 -04:00
|
|
|
document.addEventListener('click', (e) => {
|
|
|
|
if (!e.target.closest('.nav-sidebar') && (bp.getBreakpointSize() === 'sm' || bp.getBreakpointSize() === 'md')) {
|
|
|
|
this.toggleCollapsedSidebar(true);
|
|
|
|
}
|
|
|
|
});
|
2017-07-19 21:26:18 -04:00
|
|
|
this.$openSidebar.on('click', () => this.toggleSidebarNav(true));
|
|
|
|
this.$closeSidebar.on('click', () => this.toggleSidebarNav(false));
|
|
|
|
this.$overlay.on('click', () => this.toggleSidebarNav(false));
|
2017-08-02 21:19:43 -04:00
|
|
|
this.$sidebarToggle.on('click', () => {
|
|
|
|
const value = !this.$sidebar.hasClass('sidebar-icons-only');
|
|
|
|
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 });
|
2017-07-17 18:33:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
toggleSidebarNav(show) {
|
2017-07-18 12:40:42 -04:00
|
|
|
this.$sidebar.toggleClass('nav-sidebar-expanded', show);
|
|
|
|
this.$overlay.toggleClass('mobile-nav-open', show);
|
2017-08-02 21:19:43 -04:00
|
|
|
this.$sidebar.removeClass('sidebar-icons-only');
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleCollapsedSidebar(collapsed) {
|
2017-08-15 11:02:10 -04:00
|
|
|
const breakpoint = bp.getBreakpointSize();
|
|
|
|
|
2017-08-07 13:24:52 -04:00
|
|
|
if (this.$sidebar.length) {
|
2017-08-15 11:02:10 -04:00
|
|
|
this.$sidebar.toggleClass('sidebar-icons-only', collapsed);
|
|
|
|
this.$page.toggleClass('page-with-icon-sidebar', breakpoint === 'sm' ? true : collapsed);
|
2017-08-07 13:24:52 -04:00
|
|
|
}
|
2017-08-02 21:19:43 -04:00
|
|
|
NewNavSidebar.setCollapsedCookie(collapsed);
|
2017-09-18 07:32:23 -04:00
|
|
|
|
|
|
|
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-08-02 21:19:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const breakpoint = bp.getBreakpointSize();
|
|
|
|
|
|
|
|
if (breakpoint === 'sm' || breakpoint === 'md') {
|
|
|
|
this.toggleCollapsedSidebar(true);
|
|
|
|
} else if (breakpoint === 'lg') {
|
2017-09-11 03:30:43 -04:00
|
|
|
const collapse = Cookies.get('sidebar_collapsed') === 'true';
|
2017-08-02 21:19:43 -04:00
|
|
|
this.toggleCollapsedSidebar(collapse);
|
|
|
|
}
|
2017-07-17 18:33:12 -04:00
|
|
|
}
|
|
|
|
}
|