summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/layout_nav.js
blob: a5f99bcdd8f5cd78de938b5afef7c397e3a57aa8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* 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 */

(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'));
    });
  };

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

    hideEndFade($scrollingTabs);
    $(window).off('resize.nav').on('resize.nav', function() {
      return hideEndFade($scrollingTabs);
    });
    $scrollingTabs.off('scroll').on('scroll', function(event) {
      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);
    });

    $scrollingTabs.each(function () {
      var $this = $(this);
      var scrollingTabWidth = $this.width();
      var $active = $this.find('.active');
      var activeWidth = $active.width();

      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);
        }
      }
    });
  });
}).call(window);