BigW Consortium Gitlab

layout_nav.js 2.26 KB
Newer Older
1
/* eslint-disable func-names, space-before-function-paren, no-var, prefer-arrow-callback, no-unused-vars, one-var, one-var-declaration-per-line, vars-on-top, max-len */
2
import _ from 'underscore';
3
import Cookies from 'js-cookie';
4
import NewNavSidebar from './new_sidebar';
5
import initFlyOutNav from './fly_out_nav';
6

Fatih Acet committed
7 8 9 10 11 12 13 14 15 16 17
(function() {
  var hideEndFade;

  hideEndFade = function($scrollingTabs) {
    return $scrollingTabs.each(function() {
      var $this;
      $this = $(this);
      return $this.siblings('.fade-right').toggleClass('scrolling', $this.width() < $this.prop('scrollWidth'));
    });
  };

18 19 20
  $(document).on('init.scrolling-tabs', () => {
    const $scrollingTabs = $('.scrolling-tabs').not('.is-initialized');
    $scrollingTabs.addClass('is-initialized');
21 22

    hideEndFade($scrollingTabs);
Fatih Acet committed
23
    $(window).off('resize.nav').on('resize.nav', function() {
24
      return hideEndFade($scrollingTabs);
Fatih Acet committed
25
    });
26
    $scrollingTabs.off('scroll').on('scroll', function(event) {
Fatih Acet committed
27 28 29 30 31 32 33
      var $this, currentPosition, maxPosition;
      $this = $(this);
      currentPosition = $this.scrollLeft();
      maxPosition = $this.prop('scrollWidth') - $this.outerWidth();
      $this.siblings('.fade-left').toggleClass('scrolling', currentPosition > 0);
      return $this.siblings('.fade-right').toggleClass('scrolling', currentPosition < maxPosition - 1);
    });
34 35

    $scrollingTabs.each(function () {
36 37 38 39
      var $this = $(this);
      var scrollingTabWidth = $this.width();
      var $active = $this.find('.active');
      var activeWidth = $active.width();
40

41 42 43 44 45 46 47 48
      if ($active.length) {
        var offset = $active.offset().left + activeWidth;

        if (offset > scrollingTabWidth - 30) {
          var scrollLeft = scrollingTabWidth / 2;
          scrollLeft = (offset - scrollLeft) - (activeWidth / 2);
          $this.scrollLeft(scrollLeft);
        }
49 50
      }
    });
Fatih Acet committed
51
  });
52 53

  function applyScrollNavClass() {
54 55
    const scrollOpacityHeight = 40;
    $('.navbar-border').css('opacity', Math.min($(window).scrollTop() / scrollOpacityHeight, 1));
56 57
  }

58
  $(() => {
59
    if (Cookies.get('new_nav') === 'true') {
60 61
      const newNavSidebar = new NewNavSidebar();
      newNavSidebar.bindEvents();
62

63 64
      initFlyOutNav();
    }
65

66 67
    $(window).on('scroll', _.throttle(applyScrollNavClass, 100));
  });
68
}).call(window);