gitlab-org--gitlab-foss/app/assets/javascripts/milestones/milestone.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

50 lines
1.4 KiB
JavaScript
Raw Normal View History

import createFlash from '~/flash';
import { sanitize } from '~/lib/dompurify';
import axios from '~/lib/utils/axios_utils';
import { historyPushState } from '~/lib/utils/common_utils';
import { __ } from '~/locale';
import { GlTabsBehavior, TAB_SHOWN_EVENT } from '~/tabs';
2017-11-20 15:28:29 -05:00
export default class Milestone {
constructor() {
this.tabsEl = document.querySelector('.js-milestone-tabs');
this.glTabs = new GlTabsBehavior(this.tabsEl);
this.loadedTabs = new WeakSet();
2017-11-20 15:28:29 -05:00
this.bindTabsSwitching();
this.loadInitialTab();
}
bindTabsSwitching() {
this.tabsEl.addEventListener(TAB_SHOWN_EVENT, (event) => {
const tab = event.target;
const { activeTabPanel } = event.detail;
historyPushState(tab.getAttribute('href'));
this.loadTab(tab, activeTabPanel);
2017-11-20 15:28:29 -05:00
});
}
2017-11-20 15:28:29 -05:00
loadInitialTab() {
const tab = this.tabsEl.querySelector(`a[href="${window.location.hash}"]`);
this.glTabs.activateTab(tab || this.glTabs.activeTab);
2017-11-20 15:28:29 -05:00
}
loadTab(tab, tabPanel) {
const { endpoint } = tab.dataset;
2017-11-20 15:28:29 -05:00
if (endpoint && !this.loadedTabs.has(tab)) {
axios
.get(endpoint)
2018-01-31 04:29:29 -05:00
.then(({ data }) => {
// eslint-disable-next-line no-param-reassign
tabPanel.innerHTML = sanitize(data.html);
this.loadedTabs.add(tab);
2018-01-31 04:29:29 -05:00
})
.catch(() =>
createFlash({
message: __('Error loading milestone tab'),
}),
);
2017-11-20 15:28:29 -05:00
}
}
}