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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

138 lines
4.5 KiB
JavaScript
Raw Normal View History

import { GlBreakpointInstance as bp, breakpoints } from '@gitlab/ui/dist/utils';
import $ from 'jquery';
import { debounce } from 'lodash';
import { getCookie, setCookie, parseBoolean } from '~/lib/utils/common_utils';
2017-08-02 21:19:43 -04:00
export const SIDEBAR_COLLAPSED_CLASS = 'js-sidebar-collapsed';
2017-10-20 19:14:55 -04:00
export default class ContextualSidebar {
2017-07-17 18:33:12 -04:00
constructor() {
this.initDomElements();
2017-08-02 21:19:43 -04:00
this.render();
2017-07-17 18:33:12 -04:00
}
initDomElements() {
this.$page = $('.layout-page');
2017-07-17 18:33:12 -04:00
this.$sidebar = $('.nav-sidebar');
if (!this.$sidebar.length) return;
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() {
if (!this.$sidebar.length) return;
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', () => {
if (!ContextualSidebar.isDesktopBreakpoint()) {
this.toggleSidebarNav(!this.$sidebar.hasClass('sidebar-expanded-mobile'));
} else {
const value = !this.$sidebar.hasClass('sidebar-collapsed-desktop');
this.toggleCollapsedSidebar(value, true);
}
2017-08-02 21:19:43 -04:00
});
this.$page.on('transitionstart transitionend', () => {
$(document).trigger('content.resize');
});
2017-08-02 21:19:43 -04:00
$(window).on(
'resize',
debounce(() => this.render(), 100),
);
2017-08-02 21:19:43 -04:00
}
// See documentation: https://design.gitlab.com/regions/navigation#contextual-navigation
// NOTE: at 1200px nav sidebar should not overlap the content
// https://gitlab.com/gitlab-org/gitlab-foss/merge_requests/24555#note_134136110
static isDesktopBreakpoint = () => bp.windowWidth() >= breakpoints.xl;
2017-08-02 21:19:43 -04:00
static setCollapsedCookie(value) {
2019-03-04 00:57:40 -05:00
if (!ContextualSidebar.isDesktopBreakpoint()) {
2017-08-02 21:19:43 -04:00
return;
}
setCookie('sidebar_collapsed', value, { expires: 365 * 10 });
2017-07-17 18:33:12 -04:00
}
toggleSidebarNav(show) {
const breakpoint = bp.getBreakpointSize();
const dbp = ContextualSidebar.isDesktopBreakpoint();
const supportedSizes = ['xs', 'sm', 'md'];
2017-08-02 21:19:43 -04:00
this.$sidebar.toggleClass(SIDEBAR_COLLAPSED_CLASS, !show);
this.$sidebar.toggleClass('sidebar-expanded-mobile', !dbp ? show : false);
this.$overlay.toggleClass(
'mobile-nav-open',
supportedSizes.includes(breakpoint) ? show : false,
);
this.$sidebar.removeClass('sidebar-collapsed-desktop');
}
toggleCollapsedSidebar(collapsed, saveCookie) {
const breakpoint = bp.getBreakpointSize();
const dbp = ContextualSidebar.isDesktopBreakpoint();
const supportedSizes = ['xs', 'sm', 'md'];
if (this.$sidebar.length) {
this.$sidebar.toggleClass(`sidebar-collapsed-desktop ${SIDEBAR_COLLAPSED_CLASS}`, collapsed);
this.$sidebar.toggleClass('sidebar-expanded-mobile', !dbp ? !collapsed : false);
this.$page.toggleClass(
'page-with-icon-sidebar',
supportedSizes.includes(breakpoint) ? true : collapsed,
);
}
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-02 21:19:43 -04:00
}
render() {
if (!this.$sidebar.length) return;
if (!ContextualSidebar.isDesktopBreakpoint()) {
this.toggleSidebarNav(false);
2019-03-04 00:57:40 -05:00
} else {
const collapse = parseBoolean(getCookie('sidebar_collapsed'));
2019-03-04 00:57:40 -05:00
this.toggleCollapsedSidebar(collapse, true);
2017-08-02 21:19:43 -04:00
}
const modalEl = document.querySelector('.js-invite-members-modal');
if (modalEl) {
import(
/* webpackChunkName: 'initInviteMembersModal' */ '~/invite_members/init_invite_members_modal'
)
.then(({ default: initInviteMembersModal }) => {
initInviteMembersModal();
})
.catch(() => {});
const inviteTriggers = document.querySelectorAll('.js-invite-members-trigger');
if (inviteTriggers) {
import(
/* webpackChunkName: 'initInviteMembersTrigger' */ '~/invite_members/init_invite_members_trigger'
)
.then(({ default: initInviteMembersTrigger }) => {
initInviteMembersTrigger();
})
.catch(() => {});
}
}
2017-07-17 18:33:12 -04:00
}
}