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

46 lines
1.2 KiB
JavaScript
Raw Normal View History

import $ from 'jquery';
import { deprecatedCreateFlash as flash } from './flash';
import axios from './lib/utils/axios_utils';
import { __ } from './locale';
2017-11-20 15:28:29 -05:00
export default class Milestone {
constructor() {
this.bindTabsSwitching();
this.loadInitialTab();
}
bindTabsSwitching() {
return $('a[data-toggle="tab"]').on('show.bs.tab', (e) => {
2017-11-20 15:28:29 -05:00
const $target = $(e.target);
2016-07-24 16:45:11 -04:00
window.location.hash = $target.attr('href');
2017-11-20 15:28:29 -05:00
this.loadTab($target);
});
}
2017-11-20 15:28:29 -05:00
loadInitialTab() {
const $target = $(`.js-milestone-tabs a:not(.active)[href="${window.location.hash}"]`);
2017-04-25 10:25:20 -04:00
2017-11-20 15:28:29 -05:00
if ($target.length) {
$target.tab('show');
} else {
this.loadTab($('.js-milestone-tabs a.active'));
2017-11-20 15:28:29 -05:00
}
}
// eslint-disable-next-line class-methods-use-this
loadTab($target) {
const endpoint = $target.data('endpoint');
const tabElId = $target.attr('href');
if (endpoint && !$target.hasClass('is-loaded')) {
axios
.get(endpoint)
2018-01-31 04:29:29 -05:00
.then(({ data }) => {
$(tabElId).html(data.html);
$target.addClass('is-loaded');
})
.catch(() => flash(__('Error loading milestone tab')));
2017-11-20 15:28:29 -05:00
}
}
}