BigW Consortium Gitlab

wikis.js 1.94 KB
Newer Older
1 2
/* global Breakpoints */

3 4
import 'vendor/jquery.nicescroll';
import './breakpoints';
5

6
export default class Wikis {
7 8 9 10 11
  constructor() {
    this.bp = Breakpoints.get();
    this.sidebarEl = document.querySelector('.js-wiki-sidebar');
    this.sidebarExpanded = false;
    $(this.sidebarEl).niceScroll();
12

13 14 15 16
    const sidebarToggles = document.querySelectorAll('.js-sidebar-wiki-toggle');
    for (let i = 0; i < sidebarToggles.length; i += 1) {
      sidebarToggles[i].addEventListener('click', e => this.handleToggleSidebar(e));
    }
17

18 19 20
    this.newWikiForm = document.querySelector('form.new-wiki-page');
    if (this.newWikiForm) {
      this.newWikiForm.addEventListener('submit', e => this.handleNewWikiSubmit(e));
21 22
    }

23 24 25
    window.addEventListener('resize', () => this.renderSidebar());
    this.renderSidebar();
  }
26

27 28
  handleNewWikiSubmit(e) {
    if (!this.newWikiForm) return;
29

30 31
    const slugInput = this.newWikiForm.querySelector('#new_wiki_path');
    const slug = gl.text.slugify(slugInput.value);
32

33 34 35
    if (slug.length > 0) {
      const wikisPath = slugInput.getAttribute('data-wikis-path');
      window.location.href = `${wikisPath}/${slug}`;
36
      e.preventDefault();
37
    }
38
  }
39

40 41 42 43 44 45 46 47 48 49
  handleToggleSidebar(e) {
    e.preventDefault();
    this.sidebarExpanded = !this.sidebarExpanded;
    this.renderSidebar();
  }

  sidebarCanCollapse() {
    const bootstrapBreakpoint = this.bp.getBreakpointSize();
    return bootstrapBreakpoint === 'xs' || bootstrapBreakpoint === 'sm';
  }
50

51 52 53 54 55 56 57
  renderSidebar() {
    if (!this.sidebarEl) return;
    const { classList } = this.sidebarEl;
    if (this.sidebarExpanded || !this.sidebarCanCollapse()) {
      if (!classList.contains('right-sidebar-expanded')) {
        classList.remove('right-sidebar-collapsed');
        classList.add('right-sidebar-expanded');
58
      }
59 60 61
    } else if (classList.contains('right-sidebar-expanded')) {
      classList.add('right-sidebar-collapsed');
      classList.remove('right-sidebar-expanded');
62 63
    }
  }
64
}