2018-03-17 18:26:18 +05:30
|
|
|
import bp from '../../../breakpoints';
|
2018-11-08 19:23:39 +05:30
|
|
|
import { parseQueryStringIntoObject } from '../../../lib/utils/common_utils';
|
|
|
|
import { mergeUrlParams, redirectTo } from '../../../lib/utils/url_utility';
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
export default class Wikis {
|
|
|
|
constructor() {
|
|
|
|
this.sidebarEl = document.querySelector('.js-wiki-sidebar');
|
|
|
|
this.sidebarExpanded = false;
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
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));
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
this.newWikiForm = document.querySelector('form.new-wiki-page');
|
|
|
|
if (this.newWikiForm) {
|
|
|
|
this.newWikiForm.addEventListener('submit', e => this.handleNewWikiSubmit(e));
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
window.addEventListener('resize', () => this.renderSidebar());
|
|
|
|
this.renderSidebar();
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
handleNewWikiSubmit(e) {
|
|
|
|
if (!this.newWikiForm) return;
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
const slugInput = this.newWikiForm.querySelector('#new_wiki_path');
|
2019-02-13 22:33:31 +05:30
|
|
|
|
|
|
|
const slug = slugInput.value;
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
if (slug.length > 0) {
|
|
|
|
const wikisPath = slugInput.getAttribute('data-wikis-path');
|
2018-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
// If the wiki is empty, we need to merge the current URL params to keep the "create" view.
|
|
|
|
const params = parseQueryStringIntoObject(window.location.search.substr(1));
|
|
|
|
const url = mergeUrlParams(params, `${wikisPath}/${slug}`);
|
|
|
|
redirectTo(url);
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
e.preventDefault();
|
|
|
|
}
|
2017-09-10 17:25:29 +05:30
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
handleToggleSidebar(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.sidebarExpanded = !this.sidebarExpanded;
|
|
|
|
this.renderSidebar();
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
static sidebarCanCollapse() {
|
|
|
|
const bootstrapBreakpoint = bp.getBreakpointSize();
|
2018-11-08 19:23:39 +05:30
|
|
|
return bootstrapBreakpoint === 'xs';
|
2017-09-10 17:25:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
renderSidebar() {
|
|
|
|
if (!this.sidebarEl) return;
|
|
|
|
const { classList } = this.sidebarEl;
|
|
|
|
if (this.sidebarExpanded || !Wikis.sidebarCanCollapse()) {
|
|
|
|
if (!classList.contains('right-sidebar-expanded')) {
|
|
|
|
classList.remove('right-sidebar-collapsed');
|
|
|
|
classList.add('right-sidebar-expanded');
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
2017-09-10 17:25:29 +05:30
|
|
|
} else if (classList.contains('right-sidebar-expanded')) {
|
|
|
|
classList.add('right-sidebar-collapsed');
|
|
|
|
classList.remove('right-sidebar-expanded');
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
}
|
2017-09-10 17:25:29 +05:30
|
|
|
}
|