2016-11-14 16:33:21 -05:00
|
|
|
/* eslint-disable no-param-reassign */
|
|
|
|
/* global Breakpoints */
|
|
|
|
|
2016-11-03 17:38:54 -04:00
|
|
|
require('vendor/latinise');
|
|
|
|
require('./breakpoints');
|
|
|
|
require('vendor/jquery.nicescroll');
|
2016-11-14 16:33:21 -05:00
|
|
|
|
|
|
|
((global) => {
|
|
|
|
const dasherize = str => str.replace(/[_\s]+/g, '-');
|
|
|
|
const slugify = str => dasherize(str.trim().toLowerCase().latinise());
|
|
|
|
|
|
|
|
class Wikis {
|
|
|
|
constructor() {
|
|
|
|
this.bp = Breakpoints.get();
|
|
|
|
this.sidebarEl = document.querySelector('.js-wiki-sidebar');
|
|
|
|
this.sidebarExpanded = false;
|
2016-11-15 03:04:39 -05:00
|
|
|
$(this.sidebarEl).niceScroll();
|
2016-11-14 16:33:21 -05:00
|
|
|
|
|
|
|
const sidebarToggles = document.querySelectorAll('.js-sidebar-wiki-toggle');
|
2016-11-15 12:32:57 -05:00
|
|
|
for (let i = 0; i < sidebarToggles.length; i += 1) {
|
|
|
|
sidebarToggles[i].addEventListener('click', e => this.handleToggleSidebar(e));
|
2016-11-14 16:33:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
this.newWikiForm = document.querySelector('form.new-wiki-page');
|
|
|
|
if (this.newWikiForm) {
|
|
|
|
this.newWikiForm.addEventListener('submit', e => this.handleNewWikiSubmit(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener('resize', () => this.renderSidebar());
|
|
|
|
this.renderSidebar();
|
|
|
|
}
|
|
|
|
|
2016-11-30 14:32:26 -05:00
|
|
|
handleNewWikiSubmit(e) {
|
2016-11-14 16:33:21 -05:00
|
|
|
if (!this.newWikiForm) return;
|
|
|
|
|
|
|
|
const slugInput = this.newWikiForm.querySelector('#new_wiki_path');
|
|
|
|
const slug = slugify(slugInput.value);
|
|
|
|
|
|
|
|
if (slug.length > 0) {
|
|
|
|
const wikisPath = slugInput.getAttribute('data-wikis-path');
|
|
|
|
window.location.href = `${wikisPath}/${slug}`;
|
2016-11-30 14:32:26 -05:00
|
|
|
e.preventDefault();
|
2016-11-14 16:33:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-30 14:32:26 -05:00
|
|
|
handleToggleSidebar(e) {
|
|
|
|
e.preventDefault();
|
2016-11-14 16:33:21 -05:00
|
|
|
this.sidebarExpanded = !this.sidebarExpanded;
|
|
|
|
this.renderSidebar();
|
|
|
|
}
|
|
|
|
|
|
|
|
sidebarCanCollapse() {
|
|
|
|
const bootstrapBreakpoint = this.bp.getBreakpointSize();
|
|
|
|
return bootstrapBreakpoint === 'xs' || bootstrapBreakpoint === 'sm';
|
|
|
|
}
|
|
|
|
|
|
|
|
renderSidebar() {
|
2016-11-16 13:10:16 -05:00
|
|
|
if (!this.sidebarEl) return;
|
2016-11-14 16:33:21 -05:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
} else if (classList.contains('right-sidebar-expanded')) {
|
|
|
|
classList.add('right-sidebar-collapsed');
|
|
|
|
classList.remove('right-sidebar-expanded');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
global.Wikis = Wikis;
|
|
|
|
})(window.gl || (window.gl = {}));
|