2016-11-14 16:33:21 -05:00
|
|
|
/* global Breakpoints */
|
|
|
|
|
2017-05-16 16:52:40 -04:00
|
|
|
import 'vendor/jquery.nicescroll';
|
|
|
|
import './breakpoints';
|
2016-11-14 16:33:21 -05:00
|
|
|
|
2017-06-30 17:39:33 -04:00
|
|
|
export default class Wikis {
|
2017-07-05 13:20:41 -04:00
|
|
|
constructor() {
|
|
|
|
this.bp = Breakpoints.get();
|
|
|
|
this.sidebarEl = document.querySelector('.js-wiki-sidebar');
|
|
|
|
this.sidebarExpanded = false;
|
|
|
|
$(this.sidebarEl).niceScroll();
|
2016-11-14 16:33:21 -05:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
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));
|
|
|
|
}
|
2016-11-14 16:33:21 -05:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
this.newWikiForm = document.querySelector('form.new-wiki-page');
|
|
|
|
if (this.newWikiForm) {
|
|
|
|
this.newWikiForm.addEventListener('submit', e => this.handleNewWikiSubmit(e));
|
2016-11-14 16:33:21 -05:00
|
|
|
}
|
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
window.addEventListener('resize', () => this.renderSidebar());
|
|
|
|
this.renderSidebar();
|
|
|
|
}
|
2016-11-14 16:33:21 -05:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
handleNewWikiSubmit(e) {
|
|
|
|
if (!this.newWikiForm) return;
|
2016-11-14 16:33:21 -05:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
const slugInput = this.newWikiForm.querySelector('#new_wiki_path');
|
|
|
|
const slug = gl.text.slugify(slugInput.value);
|
2016-11-14 16:33:21 -05:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
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
|
|
|
}
|
2017-07-05 13:20:41 -04:00
|
|
|
}
|
2016-11-14 16:33:21 -05:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
handleToggleSidebar(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.sidebarExpanded = !this.sidebarExpanded;
|
|
|
|
this.renderSidebar();
|
|
|
|
}
|
|
|
|
|
|
|
|
sidebarCanCollapse() {
|
|
|
|
const bootstrapBreakpoint = this.bp.getBreakpointSize();
|
|
|
|
return bootstrapBreakpoint === 'xs' || bootstrapBreakpoint === 'sm';
|
|
|
|
}
|
2016-11-14 16:33:21 -05:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
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');
|
2016-11-14 16:33:21 -05:00
|
|
|
}
|
2017-07-05 13:20:41 -04:00
|
|
|
} else if (classList.contains('right-sidebar-expanded')) {
|
|
|
|
classList.add('right-sidebar-collapsed');
|
|
|
|
classList.remove('right-sidebar-expanded');
|
2016-11-14 16:33:21 -05:00
|
|
|
}
|
|
|
|
}
|
2017-07-05 13:20:41 -04:00
|
|
|
}
|