2018-05-09 12:01:36 +05:30
|
|
|
import $ from 'jquery';
|
2017-09-10 17:25:29 +05:30
|
|
|
import Cookies from 'js-cookie';
|
|
|
|
import _ from 'underscore';
|
|
|
|
import bp from './breakpoints';
|
2019-02-15 15:39:39 +05:30
|
|
|
import { parseBoolean } from '~/lib/utils/common_utils';
|
2017-09-10 17:25:29 +05:30
|
|
|
|
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');
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
if (!this.$sidebar.length) return;
|
|
|
|
|
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() {
|
2019-03-02 22:35:43 +05:30
|
|
|
if (!this.$sidebar.length) return;
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
document.addEventListener('click', e => {
|
|
|
|
if (
|
|
|
|
!e.target.closest('.nav-sidebar') &&
|
|
|
|
(bp.getBreakpointSize() === 'sm' || bp.getBreakpointSize() === 'md')
|
|
|
|
) {
|
2019-03-02 22:35:43 +05:30
|
|
|
this.toggleCollapsedSidebar(true, true);
|
2018-03-17 18:26:18 +05:30
|
|
|
}
|
|
|
|
});
|
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');
|
2019-03-02 22:35:43 +05:30
|
|
|
this.toggleCollapsedSidebar(value, true);
|
2017-09-10 17:25:29 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
$(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
|
|
|
}
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
toggleCollapsedSidebar(collapsed, saveCookie) {
|
2017-09-10 17:25:29 +05:30
|
|
|
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
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
if (saveCookie) {
|
|
|
|
ContextualSidebar.setCollapsedCookie(collapsed);
|
|
|
|
}
|
|
|
|
|
|
|
|
requestIdleCallback(() => this.toggleSidebarOverflow());
|
2018-03-17 18:26:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2019-03-02 22:35:43 +05:30
|
|
|
if (!this.$sidebar.length) return;
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
const breakpoint = bp.getBreakpointSize();
|
|
|
|
|
|
|
|
if (breakpoint === 'sm' || breakpoint === 'md') {
|
2019-03-02 22:35:43 +05:30
|
|
|
this.toggleCollapsedSidebar(true, false);
|
2017-09-10 17:25:29 +05:30
|
|
|
} else if (breakpoint === 'lg') {
|
2019-02-15 15:39:39 +05:30
|
|
|
const collapse = parseBoolean(Cookies.get('sidebar_collapsed'));
|
2019-03-02 22:35:43 +05:30
|
|
|
this.toggleCollapsedSidebar(collapse, false);
|
2017-09-10 17:25:29 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|