2021-12-01 13:15:19 -05:00
|
|
|
import createFlash from '~/flash';
|
2021-12-10 13:14:42 -05:00
|
|
|
import { sanitize } from '~/lib/dompurify';
|
2021-12-01 13:15:19 -05:00
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2021-12-10 13:14:42 -05:00
|
|
|
import { historyPushState } from '~/lib/utils/common_utils';
|
2021-12-01 13:15:19 -05:00
|
|
|
import { __ } from '~/locale';
|
2021-12-10 13:14:42 -05:00
|
|
|
import { GlTabsBehavior, TAB_SHOWN_EVENT } from '~/tabs';
|
2017-10-02 08:32:53 -04:00
|
|
|
|
2017-11-20 15:28:29 -05:00
|
|
|
export default class Milestone {
|
|
|
|
constructor() {
|
2021-12-10 13:14:42 -05:00
|
|
|
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() {
|
2021-12-10 13:14:42 -05:00
|
|
|
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
|
|
|
});
|
|
|
|
}
|
2020-09-18 14:10:26 -04:00
|
|
|
|
2017-11-20 15:28:29 -05:00
|
|
|
loadInitialTab() {
|
2021-12-10 13:14:42 -05:00
|
|
|
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
|
|
|
}
|
2021-12-10 13:14:42 -05:00
|
|
|
loadTab(tab, tabPanel) {
|
|
|
|
const { endpoint } = tab.dataset;
|
2017-11-20 15:28:29 -05:00
|
|
|
|
2021-12-10 13:14:42 -05:00
|
|
|
if (endpoint && !this.loadedTabs.has(tab)) {
|
2018-10-10 03:13:34 -04:00
|
|
|
axios
|
|
|
|
.get(endpoint)
|
2018-01-31 04:29:29 -05:00
|
|
|
.then(({ data }) => {
|
2021-12-10 13:14:42 -05:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
tabPanel.innerHTML = sanitize(data.html);
|
|
|
|
this.loadedTabs.add(tab);
|
2018-01-31 04:29:29 -05:00
|
|
|
})
|
2021-06-18 02:10:20 -04:00
|
|
|
.catch(() =>
|
|
|
|
createFlash({
|
|
|
|
message: __('Error loading milestone tab'),
|
|
|
|
}),
|
|
|
|
);
|
2017-11-20 15:28:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|