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

53 lines
1.7 KiB
JavaScript
Raw Normal View History

import $ from 'jquery';
2017-10-20 23:14:55 +00:00
import ContextualSidebar from './contextual_sidebar';
import initFlyOutNav from './fly_out_nav';
2017-01-12 03:49:57 +00:00
2017-12-19 08:32:51 +00:00
function hideEndFade($scrollingTabs) {
$scrollingTabs.each(function scrollTabsLoop() {
const $this = $(this);
2018-02-20 22:20:48 +00:00
$this.siblings('.fade-right').toggleClass('scrolling', Math.round($this.width()) < $this.prop('scrollWidth'));
2017-12-19 08:32:51 +00:00
});
}
2016-07-24 20:45:11 +00:00
2017-12-19 08:32:51 +00:00
export default function initLayoutNav() {
const contextualSidebar = new ContextualSidebar();
contextualSidebar.bindEvents();
initFlyOutNav();
2016-07-24 20:45:11 +00:00
$(document).on('init.scrolling-tabs', () => {
const $scrollingTabs = $('.scrolling-tabs').not('.is-initialized');
$scrollingTabs.addClass('is-initialized');
2017-12-19 08:32:51 +00:00
$(window).on('resize.nav', () => {
hideEndFade($scrollingTabs);
}).trigger('resize.nav');
$scrollingTabs.on('scroll', function tabsScrollEvent() {
const $this = $(this);
const currentPosition = $this.scrollLeft();
const maxPosition = $this.prop('scrollWidth') - $this.outerWidth();
2016-07-24 20:45:11 +00:00
$this.siblings('.fade-left').toggleClass('scrolling', currentPosition > 0);
2017-12-19 08:32:51 +00:00
$this.siblings('.fade-right').toggleClass('scrolling', currentPosition < maxPosition - 1);
2016-07-24 20:45:11 +00:00
});
2017-12-19 08:32:51 +00:00
$scrollingTabs.each(function scrollTabsEachLoop() {
const $this = $(this);
const scrollingTabWidth = $this.width();
const $active = $this.find('.active');
const activeWidth = $active.width();
if ($active.length) {
2017-12-19 08:32:51 +00:00
const offset = $active.offset().left + activeWidth;
if (offset > scrollingTabWidth - 30) {
2017-12-19 08:32:51 +00:00
const scrollLeft = (offset - (scrollingTabWidth / 2)) - (activeWidth / 2);
$this.scrollLeft(scrollLeft);
}
}
});
2017-12-19 08:32:51 +00:00
}).trigger('init.scrolling-tabs');
}