Skip to content

Commit 55a1c6b

Browse files
committed
Merge branch 'tz-improve-navigation-initialisation' into 'master'
Optimization of setup of Main Nav Layout, Contextual Sidebar and Fly Out Nav Closes #56257 See merge request gitlab-org/gitlab-ce!24362
2 parents 8b4b7ca + 8871dc9 commit 55a1c6b

File tree

3 files changed

+56
-38
lines changed

3 files changed

+56
-38
lines changed

app/assets/javascripts/contextual_sidebar.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ export default class ContextualSidebar {
1313
initDomElements() {
1414
this.$page = $('.layout-page');
1515
this.$sidebar = $('.nav-sidebar');
16+
17+
if (!this.$sidebar.length) return;
18+
1619
this.$innerScroll = $('.nav-sidebar-inner-scroll', this.$sidebar);
1720
this.$overlay = $('.mobile-overlay');
1821
this.$openSidebar = $('.toggle-mobile-nav');
@@ -21,20 +24,22 @@ export default class ContextualSidebar {
2124
}
2225

2326
bindEvents() {
27+
if (!this.$sidebar.length) return;
28+
2429
document.addEventListener('click', e => {
2530
if (
2631
!e.target.closest('.nav-sidebar') &&
2732
(bp.getBreakpointSize() === 'sm' || bp.getBreakpointSize() === 'md')
2833
) {
29-
this.toggleCollapsedSidebar(true);
34+
this.toggleCollapsedSidebar(true, true);
3035
}
3136
});
3237
this.$openSidebar.on('click', () => this.toggleSidebarNav(true));
3338
this.$closeSidebar.on('click', () => this.toggleSidebarNav(false));
3439
this.$overlay.on('click', () => this.toggleSidebarNav(false));
3540
this.$sidebarToggle.on('click', () => {
3641
const value = !this.$sidebar.hasClass('sidebar-collapsed-desktop');
37-
this.toggleCollapsedSidebar(value);
42+
this.toggleCollapsedSidebar(value, true);
3843
});
3944

4045
$(window).on('resize', () => _.debounce(this.render(), 100));
@@ -53,16 +58,19 @@ export default class ContextualSidebar {
5358
this.$sidebar.removeClass('sidebar-collapsed-desktop');
5459
}
5560

56-
toggleCollapsedSidebar(collapsed) {
61+
toggleCollapsedSidebar(collapsed, saveCookie) {
5762
const breakpoint = bp.getBreakpointSize();
5863

5964
if (this.$sidebar.length) {
6065
this.$sidebar.toggleClass('sidebar-collapsed-desktop', collapsed);
6166
this.$page.toggleClass('page-with-icon-sidebar', breakpoint === 'sm' ? true : collapsed);
6267
}
63-
ContextualSidebar.setCollapsedCookie(collapsed);
6468

65-
this.toggleSidebarOverflow();
69+
if (saveCookie) {
70+
ContextualSidebar.setCollapsedCookie(collapsed);
71+
}
72+
73+
requestIdleCallback(this.toggleSidebarOverflow);
6674
}
6775

6876
toggleSidebarOverflow() {
@@ -74,13 +82,15 @@ export default class ContextualSidebar {
7482
}
7583

7684
render() {
85+
if (!this.$sidebar.length) return;
86+
7787
const breakpoint = bp.getBreakpointSize();
7888

7989
if (breakpoint === 'sm' || breakpoint === 'md') {
80-
this.toggleCollapsedSidebar(true);
90+
this.toggleCollapsedSidebar(true, false);
8191
} else if (breakpoint === 'lg') {
8292
const collapse = parseBoolean(Cookies.get('sidebar_collapsed'));
83-
this.toggleCollapsedSidebar(collapse);
93+
this.toggleCollapsedSidebar(collapse, false);
8494
}
8595
}
8696
}

app/assets/javascripts/fly_out_nav.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ export const slope = (a, b) => (b.y - a.y) / (b.x - a.x);
2424
let headerHeight = 50;
2525

2626
export const getHeaderHeight = () => headerHeight;
27+
const setHeaderHeight = () => {
28+
headerHeight = sidebar.offsetTop;
29+
};
2730

2831
export const isSidebarCollapsed = () =>
2932
sidebar && sidebar.classList.contains('sidebar-collapsed-desktop');
@@ -186,7 +189,7 @@ export default () => {
186189
});
187190
}
188191

189-
headerHeight = document.querySelector('.nav-sidebar').offsetTop;
192+
requestIdleCallback(setHeaderHeight);
190193

191194
items.forEach(el => {
192195
const subItems = el.querySelector('.sidebar-sub-level-items');

app/assets/javascripts/layout_nav.js

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,53 @@ function hideEndFade($scrollingTabs) {
1111
});
1212
}
1313

14+
function initDeferred() {
15+
$(document).trigger('init.scrolling-tabs');
16+
}
17+
1418
export default function initLayoutNav() {
1519
const contextualSidebar = new ContextualSidebar();
1620
contextualSidebar.bindEvents();
1721

1822
initFlyOutNav();
1923

20-
$(document)
21-
.on('init.scrolling-tabs', () => {
22-
const $scrollingTabs = $('.scrolling-tabs').not('.is-initialized');
23-
$scrollingTabs.addClass('is-initialized');
24+
// We need to init it on DomContentLoaded as others could also call it
25+
$(document).on('init.scrolling-tabs', () => {
26+
const $scrollingTabs = $('.scrolling-tabs').not('.is-initialized');
27+
$scrollingTabs.addClass('is-initialized');
2428

25-
$(window)
26-
.on('resize.nav', () => {
27-
hideEndFade($scrollingTabs);
28-
})
29-
.trigger('resize.nav');
29+
$(window)
30+
.on('resize.nav', () => {
31+
hideEndFade($scrollingTabs);
32+
})
33+
.trigger('resize.nav');
3034

31-
$scrollingTabs.on('scroll', function tabsScrollEvent() {
32-
const $this = $(this);
33-
const currentPosition = $this.scrollLeft();
34-
const maxPosition = $this.prop('scrollWidth') - $this.outerWidth();
35+
$scrollingTabs.on('scroll', function tabsScrollEvent() {
36+
const $this = $(this);
37+
const currentPosition = $this.scrollLeft();
38+
const maxPosition = $this.prop('scrollWidth') - $this.outerWidth();
3539

36-
$this.siblings('.fade-left').toggleClass('scrolling', currentPosition > 0);
37-
$this.siblings('.fade-right').toggleClass('scrolling', currentPosition < maxPosition - 1);
38-
});
40+
$this.siblings('.fade-left').toggleClass('scrolling', currentPosition > 0);
41+
$this.siblings('.fade-right').toggleClass('scrolling', currentPosition < maxPosition - 1);
42+
});
3943

40-
$scrollingTabs.each(function scrollTabsEachLoop() {
41-
const $this = $(this);
42-
const scrollingTabWidth = $this.width();
43-
const $active = $this.find('.active');
44-
const activeWidth = $active.width();
44+
$scrollingTabs.each(function scrollTabsEachLoop() {
45+
const $this = $(this);
46+
const scrollingTabWidth = $this.width();
47+
const $active = $this.find('.active');
48+
const activeWidth = $active.width();
4549

46-
if ($active.length) {
47-
const offset = $active.offset().left + activeWidth;
50+
if ($active.length) {
51+
const offset = $active.offset().left + activeWidth;
4852

49-
if (offset > scrollingTabWidth - 30) {
50-
const scrollLeft = offset - scrollingTabWidth / 2 - activeWidth / 2;
53+
if (offset > scrollingTabWidth - 30) {
54+
const scrollLeft = offset - scrollingTabWidth / 2 - activeWidth / 2;
5155

52-
$this.scrollLeft(scrollLeft);
53-
}
56+
$this.scrollLeft(scrollLeft);
5457
}
55-
});
56-
})
57-
.trigger('init.scrolling-tabs');
58+
}
59+
});
60+
});
61+
62+
requestIdleCallback(initDeferred);
5863
}

0 commit comments

Comments
 (0)