debian-mirror-gitlab/app/assets/javascripts/pages/shared/wikis/wikis.js

74 lines
2.3 KiB
JavaScript
Raw Normal View History

2020-03-13 15:44:24 +05:30
import { GlBreakpointInstance as bp } from '@gitlab/ui/dist/utils';
2020-07-28 23:09:34 +05:30
import Tracking from '~/tracking';
2021-01-29 00:20:46 +05:30
import showToast from '~/vue_shared/plugins/global_toast';
2021-11-18 22:05:49 +05:30
import ShortcutsWiki from '~/behaviors/shortcuts/shortcuts_wiki';
2016-09-13 17:45:13 +05:30
2020-07-28 23:09:34 +05:30
const TRACKING_EVENT_NAME = 'view_wiki_page';
2021-01-03 14:25:43 +05:30
const TRACKING_CONTEXT_SCHEMA = 'iglu:com.gitlab/wiki_page_context/jsonschema/1-0-1';
2020-07-28 23:09:34 +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) {
2021-03-08 18:12:59 +05:30
sidebarToggles[i].addEventListener('click', (e) => this.handleToggleSidebar(e));
2017-09-10 17:25:29 +05:30
}
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
window.addEventListener('resize', () => this.renderSidebar());
this.renderSidebar();
2020-04-08 14:13:33 +05:30
2020-07-28 23:09:34 +05:30
Wikis.trackPageView();
2021-01-29 00:20:46 +05:30
Wikis.showToasts();
2021-11-18 22:05:49 +05:30
Wikis.initShortcuts();
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();
2020-03-13 15:44:24 +05:30
return bootstrapBreakpoint === 'xs' || bootstrapBreakpoint === 'sm';
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
}
}
2020-07-28 23:09:34 +05:30
static trackPageView() {
const wikiPageContent = document.querySelector('.js-wiki-page-content[data-tracking-context]');
if (!wikiPageContent) return;
Tracking.event(document.body.dataset.page, TRACKING_EVENT_NAME, {
label: TRACKING_EVENT_NAME,
context: {
schema: TRACKING_CONTEXT_SCHEMA,
data: JSON.parse(wikiPageContent.dataset.trackingContext),
},
});
}
2021-01-29 00:20:46 +05:30
static showToasts() {
const toasts = document.querySelectorAll('.js-toast-message');
2021-03-08 18:12:59 +05:30
toasts.forEach((toast) => showToast(toast.dataset.message));
2021-01-29 00:20:46 +05:30
}
2021-11-18 22:05:49 +05:30
static initShortcuts() {
new ShortcutsWiki(); // eslint-disable-line no-new
}
2017-09-10 17:25:29 +05:30
}