2020-04-30 00:09:37 +00:00
|
|
|
/* eslint-disable one-var, no-self-compare, consistent-return, no-param-reassign, no-shadow */
|
2016-12-14 05:26:26 +00:00
|
|
|
/* global Issuable */
|
2018-03-09 20:18:59 +00:00
|
|
|
|
|
|
|
import $ from 'jquery';
|
2020-04-21 18:09:31 +00:00
|
|
|
import { template, escape } from 'lodash';
|
2020-08-21 15:10:03 +00:00
|
|
|
import Api from '~/api';
|
2021-02-14 18:09:20 +00:00
|
|
|
import initDeprecatedJQueryDropdown from '~/deprecated_jquery_dropdown';
|
|
|
|
import { __, sprintf } from '~/locale';
|
2021-12-09 09:11:03 +00:00
|
|
|
import { sortMilestonesByDueDate } from '~/milestones/utils';
|
2021-12-01 18:15:19 +00:00
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
import { timeFor, parsePikadayDate, dateInWords } from '~/lib/utils/datetime_utility';
|
2016-12-14 05:26:26 +00:00
|
|
|
|
2017-12-28 20:07:05 +00:00
|
|
|
export default class MilestoneSelect {
|
|
|
|
constructor(currentProject, els, options = {}) {
|
|
|
|
if (currentProject !== null) {
|
2018-04-26 19:08:47 +00:00
|
|
|
this.currentProject =
|
|
|
|
typeof currentProject === 'string' ? JSON.parse(currentProject) : currentProject;
|
2017-12-28 20:07:05 +00:00
|
|
|
}
|
2017-02-01 17:02:17 +00:00
|
|
|
|
2018-06-14 19:39:17 +00:00
|
|
|
MilestoneSelect.init(els, options);
|
2017-12-28 20:07:05 +00:00
|
|
|
}
|
2017-02-01 17:02:17 +00:00
|
|
|
|
2018-06-14 19:39:17 +00:00
|
|
|
static init(els, options) {
|
2017-12-28 20:07:05 +00:00
|
|
|
let $els = $(els);
|
2017-02-01 17:02:17 +00:00
|
|
|
|
2017-12-28 20:07:05 +00:00
|
|
|
if (!els) {
|
|
|
|
$els = $('.js-milestone-select');
|
|
|
|
}
|
2016-10-04 19:21:06 +00:00
|
|
|
|
2017-12-28 20:07:05 +00:00
|
|
|
$els.each((i, dropdown) => {
|
2018-04-26 19:08:47 +00:00
|
|
|
let milestoneLinkNoneTemplate,
|
|
|
|
milestoneLinkTemplate,
|
2020-08-21 15:10:03 +00:00
|
|
|
milestoneExpiredLinkTemplate,
|
2018-04-26 19:08:47 +00:00
|
|
|
selectedMilestone,
|
|
|
|
selectedMilestoneDefault;
|
2017-12-28 20:07:05 +00:00
|
|
|
const $dropdown = $(dropdown);
|
|
|
|
const issueUpdateURL = $dropdown.data('issueUpdate');
|
2018-02-20 22:20:48 +00:00
|
|
|
const showNo = $dropdown.data('showNo');
|
|
|
|
const showAny = $dropdown.data('showAny');
|
2017-12-28 20:07:05 +00:00
|
|
|
const showMenuAbove = $dropdown.data('showMenuAbove');
|
2018-02-20 22:20:48 +00:00
|
|
|
const showUpcoming = $dropdown.data('showUpcoming');
|
|
|
|
const showStarted = $dropdown.data('showStarted');
|
|
|
|
const useId = $dropdown.data('useId');
|
|
|
|
const defaultLabel = $dropdown.data('defaultLabel');
|
|
|
|
const defaultNo = $dropdown.data('defaultNo');
|
|
|
|
const abilityName = $dropdown.data('abilityName');
|
2017-12-28 20:07:05 +00:00
|
|
|
const $selectBox = $dropdown.closest('.selectbox');
|
|
|
|
const $block = $selectBox.closest('.block');
|
|
|
|
const $sidebarCollapsedValue = $block.find('.sidebar-collapsed-icon');
|
|
|
|
const $value = $block.find('.value');
|
2020-12-02 15:09:37 +00:00
|
|
|
const $loading = $block.find('.block-loading').addClass('gl-display-none');
|
2019-08-09 09:59:38 +00:00
|
|
|
selectedMilestoneDefault = showAny ? '' : null;
|
2019-05-03 13:00:44 +00:00
|
|
|
selectedMilestoneDefault =
|
2020-05-12 21:08:14 +00:00
|
|
|
showNo && defaultNo ? __('No milestone') : selectedMilestoneDefault;
|
2017-12-28 20:07:05 +00:00
|
|
|
selectedMilestone = $dropdown.data('selected') || selectedMilestoneDefault;
|
2017-10-13 16:07:10 +00:00
|
|
|
|
2017-12-28 20:07:05 +00:00
|
|
|
if (issueUpdateURL) {
|
2020-04-21 15:21:10 +00:00
|
|
|
milestoneLinkTemplate = template(
|
2018-06-12 22:40:16 +00:00
|
|
|
'<a href="<%- web_url %>" class="bold has-tooltip" data-container="body" title="<%- remaining %>"><%- title %></a>',
|
2018-04-26 19:08:47 +00:00
|
|
|
);
|
2020-08-21 15:10:03 +00:00
|
|
|
milestoneExpiredLinkTemplate = template(
|
|
|
|
'<a href="<%- web_url %>" class="bold has-tooltip" data-container="body" title="<%- remaining %>"><%- title %> (Past due)</a>',
|
|
|
|
);
|
2019-05-03 13:00:44 +00:00
|
|
|
milestoneLinkNoneTemplate = `<span class="no-value">${__('None')}</span>`;
|
2017-12-28 20:07:05 +00:00
|
|
|
}
|
2020-08-24 18:10:19 +00:00
|
|
|
return initDeprecatedJQueryDropdown($dropdown, {
|
2019-09-18 14:02:45 +00:00
|
|
|
showMenuAbove,
|
2020-08-21 15:10:03 +00:00
|
|
|
data: (term, callback) => {
|
2020-08-26 06:10:34 +00:00
|
|
|
let contextId = parseInt($dropdown.get(0).dataset.projectId, 10);
|
|
|
|
let getMilestones = Api.projectMilestones.bind(Api);
|
2020-08-21 15:10:03 +00:00
|
|
|
const reqParams = { state: 'active', include_parent_milestones: true };
|
2020-07-08 12:09:33 +00:00
|
|
|
|
2020-08-25 09:10:42 +00:00
|
|
|
if (term) {
|
|
|
|
reqParams.search = term.trim();
|
|
|
|
}
|
|
|
|
|
2020-08-21 15:10:03 +00:00
|
|
|
if (!contextId) {
|
|
|
|
contextId = $dropdown.get(0).dataset.groupId;
|
|
|
|
delete reqParams.include_parent_milestones;
|
2020-08-26 06:10:34 +00:00
|
|
|
getMilestones = Api.groupMilestones.bind(Api);
|
2020-08-21 15:10:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We don't use $.data() as it caches initial value and never updates!
|
|
|
|
return getMilestones(contextId, reqParams)
|
|
|
|
.then(({ data }) =>
|
|
|
|
data
|
2020-12-23 21:10:24 +00:00
|
|
|
.map((m) => ({
|
2020-08-21 15:10:03 +00:00
|
|
|
...m,
|
|
|
|
// Public API includes `title` instead of `name`.
|
|
|
|
name: m.title,
|
|
|
|
}))
|
2021-06-24 12:08:07 +00:00
|
|
|
.sort(sortMilestonesByDueDate),
|
2020-08-21 15:10:03 +00:00
|
|
|
)
|
2020-12-23 21:10:24 +00:00
|
|
|
.then((data) => {
|
2020-08-21 15:10:03 +00:00
|
|
|
const extraOptions = [];
|
|
|
|
if (showAny) {
|
|
|
|
extraOptions.push({
|
|
|
|
id: null,
|
|
|
|
name: null,
|
|
|
|
title: __('Any milestone'),
|
|
|
|
});
|
|
|
|
}
|
2021-02-23 18:10:40 +00:00
|
|
|
if (showNo && term.trim() === '') {
|
2020-08-21 15:10:03 +00:00
|
|
|
extraOptions.push({
|
|
|
|
id: -1,
|
|
|
|
name: __('No milestone'),
|
|
|
|
title: __('No milestone'),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (showUpcoming) {
|
|
|
|
extraOptions.push({
|
|
|
|
id: -2,
|
|
|
|
name: '#upcoming',
|
|
|
|
title: __('Upcoming'),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (showStarted) {
|
|
|
|
extraOptions.push({
|
|
|
|
id: -3,
|
|
|
|
name: '#started',
|
|
|
|
title: __('Started'),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (extraOptions.length) {
|
|
|
|
extraOptions.push({ type: 'divider' });
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(extraOptions.concat(data));
|
|
|
|
if (showMenuAbove) {
|
2020-08-24 18:10:19 +00:00
|
|
|
$dropdown.data('deprecatedJQueryDropdown').positionMenuAbove();
|
2020-08-21 15:10:03 +00:00
|
|
|
}
|
|
|
|
$(`[data-milestone-id="${selectedMilestone}"] > a`).addClass('is-active');
|
|
|
|
});
|
|
|
|
},
|
2020-12-23 21:10:24 +00:00
|
|
|
renderRow: (milestone) => {
|
2020-08-21 15:10:03 +00:00
|
|
|
const milestoneName = milestone.title || milestone.name;
|
|
|
|
let milestoneDisplayName = escape(milestoneName);
|
|
|
|
|
|
|
|
if (milestone.expired) {
|
|
|
|
milestoneDisplayName = sprintf(__('%{milestone} (expired)'), {
|
|
|
|
milestone: milestoneDisplayName,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return `
|
|
|
|
<li data-milestone-id="${escape(milestoneName)}">
|
2017-12-28 20:07:05 +00:00
|
|
|
<a href='#' class='dropdown-menu-milestone-link'>
|
2020-08-21 15:10:03 +00:00
|
|
|
${milestoneDisplayName}
|
2017-12-28 20:07:05 +00:00
|
|
|
</a>
|
|
|
|
</li>
|
2020-08-21 15:10:03 +00:00
|
|
|
`;
|
|
|
|
},
|
2017-12-28 20:07:05 +00:00
|
|
|
filterable: true,
|
2020-08-25 09:10:42 +00:00
|
|
|
filterRemote: true,
|
2017-12-28 20:07:05 +00:00
|
|
|
search: {
|
2018-04-26 19:08:47 +00:00
|
|
|
fields: ['title'],
|
2017-12-28 20:07:05 +00:00
|
|
|
},
|
|
|
|
selectable: true,
|
2019-09-16 12:06:26 +00:00
|
|
|
toggleLabel: (selected, el) => {
|
2017-12-28 20:07:05 +00:00
|
|
|
if (selected && 'id' in selected && $(el).hasClass('is-active')) {
|
|
|
|
return selected.title;
|
|
|
|
}
|
2020-04-30 00:09:37 +00:00
|
|
|
return defaultLabel;
|
2017-12-28 20:07:05 +00:00
|
|
|
},
|
2019-09-18 14:02:45 +00:00
|
|
|
defaultLabel,
|
2018-02-20 22:20:48 +00:00
|
|
|
fieldName: $dropdown.data('fieldName'),
|
2020-12-23 21:10:24 +00:00
|
|
|
text: (milestone) => escape(milestone.title),
|
|
|
|
id: (milestone) => {
|
2020-06-01 21:08:09 +00:00
|
|
|
if (milestone !== undefined) {
|
|
|
|
if (!useId && !$dropdown.is('.js-issuable-form-dropdown')) {
|
|
|
|
return milestone.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return milestone.id;
|
2017-12-28 20:07:05 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
hidden: () => {
|
|
|
|
$selectBox.hide();
|
|
|
|
// display:block overrides the hide-collapse rule
|
|
|
|
return $value.css('display', '');
|
|
|
|
},
|
2020-12-23 21:10:24 +00:00
|
|
|
opened: (e) => {
|
2017-12-28 20:07:05 +00:00
|
|
|
const $el = $(e.currentTarget);
|
2021-09-01 09:10:58 +00:00
|
|
|
if (options.handleClick) {
|
2017-12-28 20:07:05 +00:00
|
|
|
selectedMilestone = $dropdown[0].dataset.selected || selectedMilestoneDefault;
|
|
|
|
}
|
|
|
|
$('a.is-active', $el).removeClass('is-active');
|
2020-08-21 15:10:03 +00:00
|
|
|
$(`[data-milestone-id="${selectedMilestone}"] > a`, $el).addClass('is-active');
|
2017-12-28 20:07:05 +00:00
|
|
|
},
|
2021-09-01 09:10:58 +00:00
|
|
|
vue: false,
|
2020-12-23 21:10:24 +00:00
|
|
|
clicked: (clickEvent) => {
|
2019-09-16 12:06:26 +00:00
|
|
|
const { e } = clickEvent;
|
2017-12-28 20:07:05 +00:00
|
|
|
let selected = clickEvent.selectedObj;
|
2017-10-24 13:48:41 +00:00
|
|
|
|
2017-12-28 20:07:05 +00:00
|
|
|
if (!selected) return;
|
2017-02-01 17:02:17 +00:00
|
|
|
|
2017-12-28 20:07:05 +00:00
|
|
|
if (options.handleClick) {
|
|
|
|
e.preventDefault();
|
|
|
|
options.handleClick(selected);
|
|
|
|
return;
|
|
|
|
}
|
2017-02-01 17:02:17 +00:00
|
|
|
|
2017-12-28 20:07:05 +00:00
|
|
|
const page = $('body').attr('data-page');
|
|
|
|
const isIssueIndex = page === 'projects:issues:index';
|
2018-04-26 19:08:47 +00:00
|
|
|
const isMRIndex = page === page && page === 'projects:merge_requests:index';
|
|
|
|
const isSelecting = selected.name !== selectedMilestone;
|
2017-12-28 20:07:05 +00:00
|
|
|
selectedMilestone = isSelecting ? selected.name : selectedMilestoneDefault;
|
2018-03-20 10:09:38 +00:00
|
|
|
|
2018-04-26 19:08:47 +00:00
|
|
|
if (
|
|
|
|
$dropdown.hasClass('js-filter-bulk-update') ||
|
|
|
|
$dropdown.hasClass('js-issuable-form-dropdown')
|
|
|
|
) {
|
2017-12-28 20:07:05 +00:00
|
|
|
e.preventDefault();
|
|
|
|
return;
|
|
|
|
}
|
2016-10-05 11:20:59 +00:00
|
|
|
|
2021-03-29 00:09:36 +00:00
|
|
|
if ($dropdown.hasClass('js-filter-submit') && (isIssueIndex || isMRIndex)) {
|
2017-12-28 20:07:05 +00:00
|
|
|
return Issuable.filterResults($dropdown.closest('form'));
|
|
|
|
} else if ($dropdown.hasClass('js-filter-submit')) {
|
|
|
|
return $dropdown.closest('form').submit();
|
2021-09-01 09:10:58 +00:00
|
|
|
}
|
2017-12-28 20:07:05 +00:00
|
|
|
|
2021-09-01 09:10:58 +00:00
|
|
|
selected = $selectBox.find('input[type="hidden"]').val();
|
2017-12-28 20:07:05 +00:00
|
|
|
|
2021-09-01 09:10:58 +00:00
|
|
|
const data = {};
|
|
|
|
data[abilityName] = {};
|
|
|
|
data[abilityName].milestone_id = selected != null ? selected : null;
|
|
|
|
$loading.removeClass('gl-display-none');
|
|
|
|
$dropdown.trigger('loading.gl.dropdown');
|
|
|
|
return axios
|
|
|
|
.put(issueUpdateURL, data)
|
|
|
|
.then(({ data }) => {
|
|
|
|
$dropdown.trigger('loaded.gl.dropdown');
|
|
|
|
$loading.addClass('gl-display-none');
|
|
|
|
$selectBox.hide();
|
|
|
|
$value.css('display', '');
|
|
|
|
if (data.milestone != null) {
|
|
|
|
data.milestone.remaining = timeFor(data.milestone.due_date);
|
|
|
|
data.milestone.name = data.milestone.title;
|
|
|
|
$value.html(
|
|
|
|
data.milestone.expired
|
|
|
|
? milestoneExpiredLinkTemplate({
|
|
|
|
...data.milestone,
|
|
|
|
remaining: sprintf(__('%{due_date} (Past due)'), {
|
|
|
|
due_date: dateInWords(parsePikadayDate(data.milestone.due_date)),
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
: milestoneLinkTemplate(data.milestone),
|
|
|
|
);
|
2020-04-30 00:09:37 +00:00
|
|
|
return $sidebarCollapsedValue
|
2021-09-01 09:10:58 +00:00
|
|
|
.attr(
|
|
|
|
'data-original-title',
|
|
|
|
`${data.milestone.name}<br />${data.milestone.remaining}`,
|
|
|
|
)
|
2020-04-30 00:09:37 +00:00
|
|
|
.find('span')
|
2021-09-01 09:10:58 +00:00
|
|
|
.text(data.milestone.title);
|
|
|
|
}
|
|
|
|
$value.html(milestoneLinkNoneTemplate);
|
|
|
|
return $sidebarCollapsedValue
|
|
|
|
.attr('data-original-title', __('Milestone'))
|
|
|
|
.find('span')
|
|
|
|
.text(__('None'));
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
$loading.addClass('gl-display-none');
|
|
|
|
});
|
2018-04-26 19:08:47 +00:00
|
|
|
},
|
2016-07-24 20:45:11 +00:00
|
|
|
});
|
2017-12-28 20:07:05 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-07-03 21:19:29 +00:00
|
|
|
|
|
|
|
window.MilestoneSelect = MilestoneSelect;
|