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

76 lines
2.4 KiB
JavaScript
Raw Normal View History

2017-08-03 01:19:43 +00:00
import Cookies from 'js-cookie';
import _ from 'underscore';
import bp from './breakpoints';
2017-08-03 01:19:43 +00:00
2017-07-17 22:33:12 +00:00
export default class NewNavSidebar {
constructor() {
this.initDomElements();
2017-08-03 01:19:43 +00:00
this.render();
2017-07-17 22:33:12 +00:00
}
initDomElements() {
2017-08-03 01:19:43 +00:00
this.$page = $('.page-with-sidebar');
2017-07-17 22:33:12 +00:00
this.$sidebar = $('.nav-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');
this.$topLevelLinks = $('.sidebar-top-level-items > li > a');
2017-07-17 22:33:12 +00:00
}
bindEvents() {
document.addEventListener('click', (e) => {
if (!e.target.closest('.nav-sidebar') && (bp.getBreakpointSize() === 'sm' || bp.getBreakpointSize() === 'md')) {
this.toggleCollapsedSidebar(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 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 22:33:12 +00:00
}
toggleSidebarNav(show) {
2017-07-18 16:40:42 +00:00
this.$sidebar.toggleClass('nav-sidebar-expanded', show);
this.$overlay.toggleClass('mobile-nav-open', show);
2017-08-03 01:19:43 +00:00
this.$sidebar.removeClass('sidebar-icons-only');
}
toggleCollapsedSidebar(collapsed) {
const breakpoint = bp.getBreakpointSize();
if (this.$sidebar.length) {
this.$sidebar.toggleClass('sidebar-icons-only', collapsed);
this.$page.toggleClass('page-with-icon-sidebar', breakpoint === 'sm' ? true : collapsed);
}
2017-08-03 01:19:43 +00:00
NewNavSidebar.setCollapsedCookie(collapsed);
this.$topLevelLinks.attr('title', function updateTopLevelTitle() {
return collapsed ? this.getAttribute('aria-label') : '';
});
2017-08-03 01:19:43 +00:00
}
render() {
const breakpoint = bp.getBreakpointSize();
if (breakpoint === 'sm' || breakpoint === 'md') {
this.toggleCollapsedSidebar(true);
} else if (breakpoint === 'lg') {
2017-09-06 10:58:24 +00:00
const collapse = this.$sidebar.hasClass('sidebar-icons-only');
2017-08-03 01:19:43 +00:00
this.toggleCollapsedSidebar(collapse);
}
2017-07-17 22:33:12 +00:00
}
}