debian-mirror-gitlab/app/assets/javascripts/milestone_select.js

260 lines
9.2 KiB
JavaScript
Raw Normal View History

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