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

69 lines
2.0 KiB
JavaScript
Raw Normal View History

import $ from 'jquery';
2018-01-31 09:29:29 +00:00
import axios from './lib/utils/axios_utils';
import flash from './flash';
2018-03-27 17:17:36 +00:00
import { mouseenter, debouncedMouseleave, togglePopover } from './shared/popover';
2017-11-20 20:28:29 +00:00
export default class Milestone {
constructor() {
this.bindTabsSwitching();
2017-11-20 20:28:29 +00:00
// Load merge request tab if it is active
// merge request tab is active based on different conditions in the backend
this.loadTab($('.js-milestone-tabs .active a'));
2017-11-20 20:28:29 +00:00
this.loadInitialTab();
}
bindTabsSwitching() {
return $('a[data-toggle="tab"]').on('show.bs.tab', (e) => {
const $target = $(e.target);
2016-07-24 20:45:11 +00:00
2017-11-20 20:28:29 +00:00
location.hash = $target.attr('href');
this.loadTab($target);
});
}
// eslint-disable-next-line class-methods-use-this
loadInitialTab() {
const $target = $(`.js-milestone-tabs a[href="${location.hash}"]`);
2017-04-25 14:25:20 +00:00
2017-11-20 20:28:29 +00:00
if ($target.length) {
$target.tab('show');
}
}
// 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')) {
2018-01-31 09:29:29 +00:00
axios.get(endpoint)
.then(({ data }) => {
$(tabElId).html(data.html);
$target.addClass('is-loaded');
})
.catch(() => flash('Error loading milestone tab'));
2017-11-20 20:28:29 +00:00
}
}
2018-03-13 14:44:06 +00:00
static initDeprecationMessage() {
2018-03-29 13:18:49 +00:00
const deprecationMesssageContainer = document.querySelector('.js-milestone-deprecation-message');
2018-03-13 14:44:06 +00:00
2018-03-26 17:46:42 +00:00
if (!deprecationMesssageContainer) return;
2018-03-13 14:44:06 +00:00
2018-03-29 13:18:49 +00:00
const deprecationMessage = deprecationMesssageContainer.querySelector('.js-milestone-deprecation-message-template').innerHTML;
const $popover = $('.js-popover-link', deprecationMesssageContainer);
2018-03-27 17:17:36 +00:00
const hideOnScroll = togglePopover.bind($popover, false);
2018-03-13 14:44:06 +00:00
2018-03-27 17:17:36 +00:00
$popover.popover({
2018-03-26 17:46:42 +00:00
content: deprecationMessage,
html: true,
placement: 'bottom',
})
.on('mouseenter', mouseenter)
2018-03-27 17:17:36 +00:00
.on('mouseleave', debouncedMouseleave())
2018-04-13 17:01:05 +00:00
.on('show.bs.popover', () => {
window.addEventListener('scroll', hideOnScroll, { once: true });
2018-03-27 17:17:36 +00:00
});
2018-03-13 14:44:06 +00:00
}
2017-11-20 20:28:29 +00:00
}