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-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-08-30 04:39:33 -04:00
|
|
|
this.$topLevelLinks = $('.sidebar-top-level-items > li > a');
|
2017-07-17 18:33:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bindEvents() {
|
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-08-30 04:39:33 -04:00
|
|
|
|
|
|
|
this.$topLevelLinks.attr('title', function updateTopLevelTitle() {
|
|
|
|
return collapsed ? this.getAttribute('aria-label') : '';
|
|
|
|
});
|
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') {
|
|
|
|
const collapse = Cookies.get('sidebar_collapsed') === 'true';
|
|
|
|
this.toggleCollapsedSidebar(collapse);
|
|
|
|
}
|
2017-07-17 18:33:12 -04:00
|
|
|
}
|
|
|
|
}
|