2020-04-29 20:09:37 -04:00
|
|
|
/* eslint-disable one-var, no-self-compare, consistent-return, no-param-reassign, no-shadow */
|
2016-12-14 00:26:26 -05:00
|
|
|
/* global Issuable */
|
|
|
|
/* global ListMilestone */
|
2018-03-09 15:18:59 -05:00
|
|
|
|
|
|
|
import $ from 'jquery';
|
2020-04-21 14:09:31 -04:00
|
|
|
import { template, escape } from 'lodash';
|
2018-04-19 10:43:20 -04:00
|
|
|
import { __ } from '~/locale';
|
2018-07-03 17:19:29 -04:00
|
|
|
import '~/gl_dropdown';
|
2018-01-31 04:34:25 -05:00
|
|
|
import axios from './lib/utils/axios_utils';
|
2017-12-07 06:09:17 -05:00
|
|
|
import { timeFor } from './lib/utils/datetime_utility';
|
2018-04-06 08:12:21 -04:00
|
|
|
import ModalStore from './boards/stores/modal_store';
|
2018-10-30 16:28:31 -04:00
|
|
|
import boardsStore, {
|
|
|
|
boardStoreIssueSet,
|
|
|
|
boardStoreIssueDelete,
|
|
|
|
} from './boards/stores/boards_store';
|
2016-12-14 00:26:26 -05:00
|
|
|
|
2017-12-28 15:07:05 -05:00
|
|
|
export default class MilestoneSelect {
|
|
|
|
constructor(currentProject, els, options = {}) {
|
|
|
|
if (currentProject !== null) {
|
2018-04-26 15:08:47 -04:00
|
|
|
this.currentProject =
|
|
|
|
typeof currentProject === 'string' ? JSON.parse(currentProject) : currentProject;
|
2017-12-28 15:07:05 -05:00
|
|
|
}
|
2017-02-01 12:02:17 -05:00
|
|
|
|
2018-06-14 15:39:17 -04:00
|
|
|
MilestoneSelect.init(els, options);
|
2017-12-28 15:07:05 -05:00
|
|
|
}
|
2017-02-01 12:02:17 -05:00
|
|
|
|
2018-06-14 15:39:17 -04:00
|
|
|
static init(els, options) {
|
2017-12-28 15:07:05 -05:00
|
|
|
let $els = $(els);
|
2017-02-01 12:02:17 -05:00
|
|
|
|
2017-12-28 15:07:05 -05:00
|
|
|
if (!els) {
|
|
|
|
$els = $('.js-milestone-select');
|
|
|
|
}
|
2016-10-04 15:21:06 -04:00
|
|
|
|
2017-12-28 15:07:05 -05:00
|
|
|
$els.each((i, dropdown) => {
|
2018-04-26 15:08:47 -04:00
|
|
|
let milestoneLinkNoneTemplate,
|
|
|
|
milestoneLinkTemplate,
|
|
|
|
selectedMilestone,
|
|
|
|
selectedMilestoneDefault;
|
2017-12-28 15:07:05 -05:00
|
|
|
const $dropdown = $(dropdown);
|
|
|
|
const milestonesUrl = $dropdown.data('milestones');
|
|
|
|
const issueUpdateURL = $dropdown.data('issueUpdate');
|
2018-02-20 17:20:48 -05:00
|
|
|
const showNo = $dropdown.data('showNo');
|
|
|
|
const showAny = $dropdown.data('showAny');
|
2017-12-28 15:07:05 -05:00
|
|
|
const showMenuAbove = $dropdown.data('showMenuAbove');
|
2018-02-20 17:20:48 -05: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 15:07:05 -05:00
|
|
|
const $selectBox = $dropdown.closest('.selectbox');
|
|
|
|
const $block = $selectBox.closest('.block');
|
|
|
|
const $sidebarCollapsedValue = $block.find('.sidebar-collapsed-icon');
|
|
|
|
const $value = $block.find('.value');
|
2019-12-18 07:07:48 -05:00
|
|
|
// eslint-disable-next-line no-jquery/no-fade
|
2017-12-28 15:07:05 -05:00
|
|
|
const $loading = $block.find('.block-loading').fadeOut();
|
2019-08-09 05:59:38 -04:00
|
|
|
selectedMilestoneDefault = showAny ? '' : null;
|
2019-05-03 09:00:44 -04:00
|
|
|
selectedMilestoneDefault =
|
2020-05-12 17:08:14 -04:00
|
|
|
showNo && defaultNo ? __('No milestone') : selectedMilestoneDefault;
|
2017-12-28 15:07:05 -05:00
|
|
|
selectedMilestone = $dropdown.data('selected') || selectedMilestoneDefault;
|
2017-10-13 12:07:10 -04:00
|
|
|
|
2017-12-28 15:07:05 -05:00
|
|
|
if (issueUpdateURL) {
|
2020-04-21 11:21:10 -04:00
|
|
|
milestoneLinkTemplate = template(
|
2018-06-12 18:40:16 -04:00
|
|
|
'<a href="<%- web_url %>" class="bold has-tooltip" data-container="body" title="<%- remaining %>"><%- title %></a>',
|
2018-04-26 15:08:47 -04:00
|
|
|
);
|
2019-05-03 09:00:44 -04:00
|
|
|
milestoneLinkNoneTemplate = `<span class="no-value">${__('None')}</span>`;
|
2017-12-28 15:07:05 -05:00
|
|
|
}
|
|
|
|
return $dropdown.glDropdown({
|
2019-09-18 10:02:45 -04:00
|
|
|
showMenuAbove,
|
2018-04-26 15:08:47 -04:00
|
|
|
data: (term, callback) =>
|
|
|
|
axios.get(milestonesUrl).then(({ data }) => {
|
2018-01-31 04:34:25 -05:00
|
|
|
const extraOptions = [];
|
|
|
|
if (showAny) {
|
|
|
|
extraOptions.push({
|
2018-04-26 15:08:47 -04:00
|
|
|
id: null,
|
2019-08-09 05:59:38 -04:00
|
|
|
name: null,
|
2020-05-12 17:08:14 -04:00
|
|
|
title: __('Any milestone'),
|
2018-01-31 04:34:25 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
if (showNo) {
|
|
|
|
extraOptions.push({
|
|
|
|
id: -1,
|
2020-05-12 17:08:14 -04:00
|
|
|
name: __('No milestone'),
|
|
|
|
title: __('No milestone'),
|
2018-01-31 04:34:25 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
if (showUpcoming) {
|
|
|
|
extraOptions.push({
|
|
|
|
id: -2,
|
|
|
|
name: '#upcoming',
|
2019-05-03 09:00:44 -04:00
|
|
|
title: __('Upcoming'),
|
2018-01-31 04:34:25 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
if (showStarted) {
|
|
|
|
extraOptions.push({
|
|
|
|
id: -3,
|
|
|
|
name: '#started',
|
2019-05-03 09:00:44 -04:00
|
|
|
title: __('Started'),
|
2018-01-31 04:34:25 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
if (extraOptions.length) {
|
2019-09-13 09:26:31 -04:00
|
|
|
extraOptions.push({ type: 'divider' });
|
2018-01-31 04:34:25 -05:00
|
|
|
}
|
2017-10-24 09:48:41 -04:00
|
|
|
|
2018-01-31 04:34:25 -05:00
|
|
|
callback(extraOptions.concat(data));
|
|
|
|
if (showMenuAbove) {
|
|
|
|
$dropdown.data('glDropdown').positionMenuAbove();
|
|
|
|
}
|
2020-04-21 14:09:31 -04:00
|
|
|
$(`[data-milestone-id="${escape(selectedMilestone)}"] > a`).addClass('is-active');
|
2018-01-31 04:34:25 -05:00
|
|
|
}),
|
2017-12-28 15:07:05 -05:00
|
|
|
renderRow: milestone => `
|
2020-04-21 14:09:31 -04:00
|
|
|
<li data-milestone-id="${escape(milestone.name)}">
|
2017-12-28 15:07:05 -05:00
|
|
|
<a href='#' class='dropdown-menu-milestone-link'>
|
2020-04-21 14:09:31 -04:00
|
|
|
${escape(milestone.title)}
|
2017-12-28 15:07:05 -05:00
|
|
|
</a>
|
|
|
|
</li>
|
|
|
|
`,
|
|
|
|
filterable: true,
|
|
|
|
search: {
|
2018-04-26 15:08:47 -04:00
|
|
|
fields: ['title'],
|
2017-12-28 15:07:05 -05:00
|
|
|
},
|
|
|
|
selectable: true,
|
2019-09-16 08:06:26 -04:00
|
|
|
toggleLabel: (selected, el) => {
|
2017-12-28 15:07:05 -05:00
|
|
|
if (selected && 'id' in selected && $(el).hasClass('is-active')) {
|
|
|
|
return selected.title;
|
|
|
|
}
|
2020-04-29 20:09:37 -04:00
|
|
|
return defaultLabel;
|
2017-12-28 15:07:05 -05:00
|
|
|
},
|
2019-09-18 10:02:45 -04:00
|
|
|
defaultLabel,
|
2018-02-20 17:20:48 -05:00
|
|
|
fieldName: $dropdown.data('fieldName'),
|
2020-04-21 14:09:31 -04:00
|
|
|
text: milestone => escape(milestone.title),
|
2018-04-26 15:08:47 -04:00
|
|
|
id: milestone => {
|
2017-12-28 15:07:05 -05:00
|
|
|
if (!useId && !$dropdown.is('.js-issuable-form-dropdown')) {
|
|
|
|
return milestone.name;
|
|
|
|
}
|
2020-04-29 20:09:37 -04:00
|
|
|
return milestone.id;
|
2017-12-28 15:07:05 -05:00
|
|
|
},
|
|
|
|
hidden: () => {
|
|
|
|
$selectBox.hide();
|
|
|
|
// display:block overrides the hide-collapse rule
|
|
|
|
return $value.css('display', '');
|
|
|
|
},
|
2018-04-26 15:08:47 -04:00
|
|
|
opened: e => {
|
2017-12-28 15:07:05 -05:00
|
|
|
const $el = $(e.currentTarget);
|
|
|
|
if ($dropdown.hasClass('js-issue-board-sidebar') || options.handleClick) {
|
|
|
|
selectedMilestone = $dropdown[0].dataset.selected || selectedMilestoneDefault;
|
|
|
|
}
|
|
|
|
$('a.is-active', $el).removeClass('is-active');
|
2020-04-21 14:09:31 -04:00
|
|
|
$(`[data-milestone-id="${escape(selectedMilestone)}"] > a`, $el).addClass('is-active');
|
2017-12-28 15:07:05 -05:00
|
|
|
},
|
|
|
|
vue: $dropdown.hasClass('js-issue-board-sidebar'),
|
2018-04-26 15:08:47 -04:00
|
|
|
clicked: clickEvent => {
|
2019-09-16 08:06:26 -04:00
|
|
|
const { e } = clickEvent;
|
2017-12-28 15:07:05 -05:00
|
|
|
let selected = clickEvent.selectedObj;
|
2017-10-24 09:48:41 -04:00
|
|
|
|
2018-12-06 09:33:14 -05:00
|
|
|
let data, modalStoreFilter;
|
2017-12-28 15:07:05 -05:00
|
|
|
if (!selected) return;
|
2017-02-01 12:02:17 -05:00
|
|
|
|
2017-12-28 15:07:05 -05:00
|
|
|
if (options.handleClick) {
|
|
|
|
e.preventDefault();
|
|
|
|
options.handleClick(selected);
|
|
|
|
return;
|
|
|
|
}
|
2017-02-01 12:02:17 -05:00
|
|
|
|
2017-12-28 15:07:05 -05:00
|
|
|
const page = $('body').attr('data-page');
|
|
|
|
const isIssueIndex = page === 'projects:issues:index';
|
2018-04-26 15:08:47 -04:00
|
|
|
const isMRIndex = page === page && page === 'projects:merge_requests:index';
|
|
|
|
const isSelecting = selected.name !== selectedMilestone;
|
2017-12-28 15:07:05 -05:00
|
|
|
selectedMilestone = isSelecting ? selected.name : selectedMilestoneDefault;
|
2018-03-20 06:09:38 -04:00
|
|
|
|
2018-04-26 15:08:47 -04:00
|
|
|
if (
|
|
|
|
$dropdown.hasClass('js-filter-bulk-update') ||
|
|
|
|
$dropdown.hasClass('js-issuable-form-dropdown')
|
|
|
|
) {
|
2017-12-28 15:07:05 -05:00
|
|
|
e.preventDefault();
|
|
|
|
return;
|
|
|
|
}
|
2016-10-05 07:20:59 -04:00
|
|
|
|
2017-12-28 15:07:05 -05:00
|
|
|
if ($dropdown.closest('.add-issues-modal').length) {
|
2018-12-06 09:33:14 -05:00
|
|
|
modalStoreFilter = ModalStore.store.filter;
|
2017-12-28 15:07:05 -05:00
|
|
|
}
|
2016-10-05 07:20:59 -04:00
|
|
|
|
2018-12-06 09:33:14 -05:00
|
|
|
if (modalStoreFilter) {
|
|
|
|
modalStoreFilter[$dropdown.data('fieldName')] = selected.name;
|
2017-12-28 15:07:05 -05:00
|
|
|
e.preventDefault();
|
|
|
|
} else if ($dropdown.hasClass('js-filter-submit') && (isIssueIndex || isMRIndex)) {
|
|
|
|
return Issuable.filterResults($dropdown.closest('form'));
|
|
|
|
} else if ($dropdown.hasClass('js-filter-submit')) {
|
|
|
|
return $dropdown.closest('form').submit();
|
|
|
|
} else if ($dropdown.hasClass('js-issue-board-sidebar')) {
|
|
|
|
if (selected.id !== -1 && isSelecting) {
|
2018-10-10 13:08:43 -04:00
|
|
|
boardStoreIssueSet(
|
2018-04-26 15:08:47 -04:00
|
|
|
'milestone',
|
|
|
|
new ListMilestone({
|
|
|
|
id: selected.id,
|
|
|
|
title: selected.name,
|
|
|
|
}),
|
|
|
|
);
|
2016-07-24 16:45:11 -04:00
|
|
|
} else {
|
2018-10-10 13:08:43 -04:00
|
|
|
boardStoreIssueDelete('milestone');
|
2017-12-28 15:07:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$dropdown.trigger('loading.gl.dropdown');
|
2019-12-18 07:07:48 -05:00
|
|
|
// eslint-disable-next-line no-jquery/no-fade
|
2017-12-28 15:07:05 -05:00
|
|
|
$loading.removeClass('hidden').fadeIn();
|
|
|
|
|
2018-10-10 13:08:43 -04:00
|
|
|
boardsStore.detail.issue
|
2018-04-26 15:08:47 -04:00
|
|
|
.update($dropdown.attr('data-issue-update'))
|
2017-12-28 15:07:05 -05:00
|
|
|
.then(() => {
|
2016-07-24 16:45:11 -04:00
|
|
|
$dropdown.trigger('loaded.gl.dropdown');
|
2019-12-18 07:07:48 -05:00
|
|
|
// eslint-disable-next-line no-jquery/no-fade
|
2016-07-24 16:45:11 -04:00
|
|
|
$loading.fadeOut();
|
2017-12-28 15:07:05 -05:00
|
|
|
})
|
|
|
|
.catch(() => {
|
2019-12-18 07:07:48 -05:00
|
|
|
// eslint-disable-next-line no-jquery/no-fade
|
2017-12-28 15:07:05 -05:00
|
|
|
$loading.fadeOut();
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
2017-12-28 15:07:05 -05:00
|
|
|
} else {
|
|
|
|
selected = $selectBox.find('input[type="hidden"]').val();
|
|
|
|
data = {};
|
|
|
|
data[abilityName] = {};
|
|
|
|
data[abilityName].milestone_id = selected != null ? selected : null;
|
2019-12-18 07:07:48 -05:00
|
|
|
// eslint-disable-next-line no-jquery/no-fade
|
2017-12-28 15:07:05 -05:00
|
|
|
$loading.removeClass('hidden').fadeIn();
|
|
|
|
$dropdown.trigger('loading.gl.dropdown');
|
2018-04-26 15:08:47 -04:00
|
|
|
return axios
|
|
|
|
.put(issueUpdateURL, data)
|
2018-01-31 04:34:25 -05:00
|
|
|
.then(({ data }) => {
|
|
|
|
$dropdown.trigger('loaded.gl.dropdown');
|
2019-12-18 07:07:48 -05:00
|
|
|
// eslint-disable-next-line no-jquery/no-fade
|
2018-01-31 04:34:25 -05:00
|
|
|
$loading.fadeOut();
|
|
|
|
$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(milestoneLinkTemplate(data.milestone));
|
2018-04-19 10:43:20 -04:00
|
|
|
return $sidebarCollapsedValue
|
2018-04-26 15:08:47 -04:00
|
|
|
.attr(
|
|
|
|
'data-original-title',
|
|
|
|
`${data.milestone.name}<br />${data.milestone.remaining}`,
|
|
|
|
)
|
2018-04-19 10:43:20 -04:00
|
|
|
.find('span')
|
|
|
|
.text(data.milestone.title);
|
2018-01-31 04:34:25 -05:00
|
|
|
}
|
2020-04-29 20:09:37 -04:00
|
|
|
$value.html(milestoneLinkNoneTemplate);
|
|
|
|
return $sidebarCollapsedValue
|
|
|
|
.attr('data-original-title', __('Milestone'))
|
|
|
|
.find('span')
|
|
|
|
.text(__('None'));
|
2018-03-04 06:15:43 -05:00
|
|
|
})
|
|
|
|
.catch(() => {
|
2019-12-18 07:07:48 -05:00
|
|
|
// eslint-disable-next-line no-jquery/no-fade
|
2018-03-04 06:15:43 -05:00
|
|
|
$loading.fadeOut();
|
2018-01-31 04:34:25 -05:00
|
|
|
});
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2018-04-26 15:08:47 -04:00
|
|
|
},
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
2017-12-28 15:07:05 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-07-03 17:19:29 -04:00
|
|
|
|
|
|
|
window.MilestoneSelect = MilestoneSelect;
|