BigW Consortium Gitlab

sidebar.js.es6 2.97 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
((global) => {
  let singleton;

  const pinnedStateCookie = 'pin_nav';
  const sidebarBreakpoint = 1024;

  const pageSelector = '.page-with-sidebar';
  const navbarSelector = '.navbar-fixed-top';
  const sidebarWrapperSelector = '.sidebar-wrapper';
  const sidebarContentSelector = '.nav-sidebar';

  const pinnedToggleSelector = '.js-nav-pin';
  const sidebarToggleSelector = '.toggle-nav-collapse, .side-nav-toggle';

  const pinnedPageClass = 'page-sidebar-pinned';
  const expandedPageClass = 'page-sidebar-expanded';

18 19
  const pinnedNavbarClass = 'header-sidebar-pinned';
  const expandedNavbarClass = 'header-sidebar-expanded';
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

  class Sidebar {
    constructor() {
      if (!singleton) {
        singleton = this;
        singleton.init();
      }
      return singleton;
    }

    init() {
      this.isPinned = $.cookie(pinnedStateCookie) === 'true';
      this.isExpanded = (
        window.innerWidth >= sidebarBreakpoint &&
        $(pageSelector).hasClass(expandedPageClass)
      );
      $(document)
Fatih Acet committed
37 38
        .on('click', sidebarToggleSelector, () => this.toggleSidebar())
        .on('click', pinnedToggleSelector, () => this.togglePinnedState())
39 40
        .on('click', 'html, body', (e) => this.handleClickEvent(e))
        .on('page:change', () => this.renderState());
41 42 43 44 45 46 47 48 49
      this.renderState();
    }

    handleClickEvent(e) {
      if (this.isExpanded && (!this.isPinned || window.innerWidth < sidebarBreakpoint)) {
        const $target = $(e.target);
        const targetIsToggle = $target.closest(sidebarToggleSelector).length > 0;
        const targetIsSidebar = $target.closest(sidebarWrapperSelector).length > 0;
        if (!targetIsToggle && (!targetIsSidebar || $target.closest('a'))) {
Fatih Acet committed
50
          this.toggleSidebar();
51 52 53 54
        }
      }
    }

Fatih Acet committed
55
    toggleSidebar() {
56 57 58 59
      this.isExpanded = !this.isExpanded;
      this.renderState();
    }

Fatih Acet committed
60
    togglePinnedState() {
61 62 63 64 65 66 67 68 69 70 71 72 73 74
      this.isPinned = !this.isPinned;
      if (!this.isPinned) {
        this.isExpanded = false;
      }
      $.cookie(pinnedStateCookie, this.isPinned ? 'true' : 'false', {
        path: gon.relative_url_root || '/',
        expires: 3650
      });
      this.renderState();
    }

    renderState() {
      $(pageSelector)
        .toggleClass(pinnedPageClass, this.isPinned && this.isExpanded)
75
        .toggleClass(expandedPageClass, this.isExpanded);
76 77
      $(navbarSelector)
        .toggleClass(pinnedNavbarClass, this.isPinned && this.isExpanded)
78
        .toggleClass(expandedNavbarClass, this.isExpanded);
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

      const $pinnedToggle = $(pinnedToggleSelector);
      const tooltipText = this.isPinned ? 'Unpin navigation' : 'Pin navigation';
      const tooltipState = $pinnedToggle.attr('aria-describedby') && this.isExpanded ? 'show' : 'hide';
      $pinnedToggle.attr('title', tooltipText).tooltip('fixTitle').tooltip(tooltipState);

      if (this.isExpanded) {
        setTimeout(() => $(sidebarContentSelector).niceScroll().updateScrollBar(), 200);
      }
    }
  }

  global.Sidebar = Sidebar;

})(window.gl || (window.gl = {}));