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

255 lines
9.4 KiB
JavaScript
Raw Normal View History

2018-03-17 18:26:18 +05:30
/* eslint-disable func-names, space-before-function-paren, wrap-iife, no-underscore-dangle, prefer-arrow-callback, max-len, one-var, one-var-declaration-per-line, no-unused-vars, object-shorthand, comma-dangle, 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-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';
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-03-17 18:26:18 +05:30
this.init(els, options);
}
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
init(els, options) {
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);
2018-03-27 19:54:05 +05:30
const projectId = $dropdown.data('projectId');
2018-03-17 18:26:18 +05:30
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 issuableId = $dropdown.data('issuableId');
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;
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(
'<a href="/<%- full_path %>/milestones/<%- iid %>" class="bold has-tooltip" data-container="body" title="<%- remaining %>"><%- title %></a>',
);
2018-03-17 18:26:18 +05:30
milestoneLinkNoneTemplate = '<span class="no-value">None</span>';
}
return $dropdown.glDropdown({
showMenuAbove: 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,
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,
name: 'No Milestone',
2018-10-15 14:42:47 +05:30
title: 'No Milestone',
2018-03-17 18:26:18 +05:30
});
}
if (showUpcoming) {
extraOptions.push({
id: -2,
name: '#upcoming',
2018-10-15 14:42:47 +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',
2018-10-15 14:42:47 +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) {
extraOptions.push('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,
toggleLabel: (selected, el, e) => {
if (selected && 'id' in selected && $(el).hasClass('is-active')) {
return selected.title;
} else {
return defaultLabel;
}
},
defaultLabel: 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 => {
2018-03-17 18:26:18 +05:30
const { $el, e } = clickEvent;
let selected = clickEvent.selectedObj;
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
let data, boardsStore;
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) {
2018-10-15 14:42:47 +05:30
boardsStore = ModalStore.store.filter;
2018-03-17 18:26:18 +05:30
}
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
if (boardsStore) {
2018-03-27 19:54:05 +05:30
boardsStore[$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-10-15 14:42:47 +05:30
gl.issueBoards.boardStoreIssueSet(
'milestone',
new ListMilestone({
id: selected.id,
title: selected.name,
}),
);
2016-09-13 17:45:13 +05:30
} else {
2018-03-17 18:26:18 +05:30
gl.issueBoards.boardStoreIssueDelete('milestone');
}
$dropdown.trigger('loading.gl.dropdown');
$loading.removeClass('hidden').fadeIn();
2018-10-15 14:42:47 +05:30
gl.issueBoards.BoardsStore.detail.issue
.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.full_path = this.currentProject.full_path;
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
});
}
}