gitlab-org--gitlab-foss/app/assets/javascripts/contextual_sidebar.js

113 lines
3.8 KiB
JavaScript
Raw Normal View History

import $ from 'jquery';
2017-08-03 01:19:43 +00:00
import Cookies from 'js-cookie';
import _ from 'underscore';
import bp from './breakpoints';
import { parseBoolean } from '~/lib/utils/common_utils';
2017-08-03 01:19:43 +00:00
const DESKTOP_BREAKPOINTS = ['xl', 'lg'];
2017-10-20 23:14:55 +00:00
export default class ContextualSidebar {
2017-07-17 22:33:12 +00:00
constructor() {
this.initDomElements();
2017-08-03 01:19:43 +00:00
this.render();
2017-07-17 22:33:12 +00:00
}
initDomElements() {
this.$page = $('.layout-page');
2017-07-17 22:33:12 +00:00
this.$sidebar = $('.nav-sidebar');
if (!this.$sidebar.length) return;
this.$innerScroll = $('.nav-sidebar-inner-scroll', this.$sidebar);
2017-07-18 16:40:42 +00:00
this.$overlay = $('.mobile-overlay');
2017-07-17 22:33:12 +00:00
this.$openSidebar = $('.toggle-mobile-nav');
this.$closeSidebar = $('.close-nav-button');
2017-08-03 01:19:43 +00:00
this.$sidebarToggle = $('.js-toggle-sidebar');
2017-07-17 22:33:12 +00:00
}
bindEvents() {
if (!this.$sidebar.length) return;
2018-10-24 19:17:03 +00:00
document.addEventListener('click', e => {
if (
!e.target.closest('.nav-sidebar') &&
(bp.getBreakpointSize() === 'sm' || bp.getBreakpointSize() === 'md')
2018-10-24 19:17:03 +00:00
) {
this.toggleCollapsedSidebar(true, true);
}
});
this.$openSidebar.on('click', () => this.toggleSidebarNav(true));
this.$closeSidebar.on('click', () => this.toggleSidebarNav(false));
this.$overlay.on('click', () => this.toggleSidebarNav(false));
2017-08-03 01:19:43 +00:00
this.$sidebarToggle.on('click', () => {
const breakpoint = bp.getBreakpointSize();
if (!ContextualSidebar.isDesktopBreakpoint(breakpoint)) {
this.toggleSidebarNav(!this.$sidebar.hasClass('sidebar-expanded-mobile'));
} else if (breakpoint === 'lg') {
const value = !this.$sidebar.hasClass('sidebar-collapsed-desktop');
this.toggleCollapsedSidebar(value, true);
}
2017-08-03 01:19:43 +00:00
});
$(window).on('resize', () => _.debounce(this.render(), 100));
}
// TODO: use the breakpoints from breakpoints.js once they have been updated for bootstrap 4
// See related issue and discussion: https://gitlab.com/gitlab-org/gitlab-ce/issues/56745
static isDesktopBreakpoint = (_bp = '') => DESKTOP_BREAKPOINTS.indexOf(_bp) > -1;
2017-08-03 01:19:43 +00:00
static setCollapsedCookie(value) {
if (bp.getBreakpointSize() !== 'lg') {
return;
}
Cookies.set('sidebar_collapsed', value, { expires: 365 * 10 });
2017-07-17 22:33:12 +00:00
}
toggleSidebarNav(show) {
const breakpoint = bp.getBreakpointSize();
const dbp = ContextualSidebar.isDesktopBreakpoint(breakpoint);
2017-08-03 01:19:43 +00:00
this.$sidebar.toggleClass('sidebar-expanded-mobile', !dbp ? show : false);
this.$overlay.toggleClass('mobile-nav-open', breakpoint === 'xs' ? show : false);
this.$sidebar.removeClass('sidebar-collapsed-desktop');
this.$page.toggleClass('page-with-contextual-sidebar', true);
}
toggleCollapsedSidebar(collapsed, saveCookie) {
const breakpoint = bp.getBreakpointSize();
const dbp = ContextualSidebar.isDesktopBreakpoint(breakpoint);
if (this.$sidebar.length) {
this.$sidebar.toggleClass('sidebar-collapsed-desktop', collapsed);
this.$sidebar.toggleClass('sidebar-expanded-mobile', !dbp ? !collapsed : false);
this.$page.toggleClass('page-with-icon-sidebar', breakpoint === 'sm' ? true : collapsed);
this.$page.toggleClass('page-with-contextual-sidebar', true);
}
if (saveCookie) {
ContextualSidebar.setCollapsedCookie(collapsed);
}
requestIdleCallback(() => 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-03 01:19:43 +00:00
}
render() {
if (!this.$sidebar.length) return;
2017-08-03 01:19:43 +00:00
const breakpoint = bp.getBreakpointSize();
if (!ContextualSidebar.isDesktopBreakpoint(breakpoint)) {
this.toggleSidebarNav(false);
2017-08-03 01:19:43 +00:00
} else if (breakpoint === 'lg') {
const collapse = parseBoolean(Cookies.get('sidebar_collapsed'));
this.toggleCollapsedSidebar(collapse, false);
2017-08-03 01:19:43 +00:00
}
2017-07-17 22:33:12 +00:00
}
}