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

227 lines
9.5 KiB
JavaScript
Raw Normal View History

2017-08-17 22:00:37 +05:30
/* eslint-disable func-names, space-before-function-paren, wrap-iife, no-var, 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 */
/* global Issuable */
/* global ListMilestone */
2017-09-10 17:25:29 +05:30
import _ from 'underscore';
2017-08-17 22:00:37 +05:30
2016-09-13 17:45:13 +05:30
(function() {
this.MilestoneSelect = (function() {
2017-08-17 22:00:37 +05:30
function MilestoneSelect(currentProject, els) {
var _this, $els;
2016-09-13 17:45:13 +05:30
if (currentProject != null) {
_this = this;
2017-09-10 17:25:29 +05:30
this.currentProject = typeof currentProject === 'string' ? JSON.parse(currentProject) : currentProject;
2016-09-13 17:45:13 +05:30
}
2017-08-17 22:00:37 +05:30
$els = $(els);
if (!els) {
$els = $('.js-milestone-select');
}
$els.each(function(i, dropdown) {
2017-09-10 17:25:29 +05:30
var $block, $dropdown, $loading, $selectbox, $sidebarCollapsedValue, $value, abilityName, collapsedSidebarLabelTemplate, defaultLabel, defaultNo, issuableId, issueUpdateURL, milestoneLinkNoneTemplate, milestoneLinkTemplate, milestonesUrl, projectId, selectedMilestone, selectedMilestoneDefault, showAny, showNo, showUpcoming, showStarted, useId, showMenuAbove;
2016-09-13 17:45:13 +05:30
$dropdown = $(dropdown);
projectId = $dropdown.data('project-id');
milestonesUrl = $dropdown.data('milestones');
issueUpdateURL = $dropdown.data('issueUpdate');
showNo = $dropdown.data('show-no');
showAny = $dropdown.data('show-any');
2016-11-03 12:29:30 +05:30
showMenuAbove = $dropdown.data('showMenuAbove');
2016-09-13 17:45:13 +05:30
showUpcoming = $dropdown.data('show-upcoming');
2017-08-17 22:00:37 +05:30
showStarted = $dropdown.data('show-started');
2016-09-13 17:45:13 +05:30
useId = $dropdown.data('use-id');
defaultLabel = $dropdown.data('default-label');
2017-09-10 17:25:29 +05:30
defaultNo = $dropdown.data('default-no');
2016-09-13 17:45:13 +05:30
issuableId = $dropdown.data('issuable-id');
abilityName = $dropdown.data('ability-name');
$selectbox = $dropdown.closest('.selectbox');
$block = $selectbox.closest('.block');
$sidebarCollapsedValue = $block.find('.sidebar-collapsed-icon');
$value = $block.find('.value');
$loading = $block.find('.block-loading').fadeOut();
2017-09-10 17:25:29 +05:30
selectedMilestoneDefault = (showAny ? '' : null);
selectedMilestoneDefault = (showNo && defaultNo ? 'No Milestone' : selectedMilestoneDefault);
selectedMilestone = $dropdown.data('selected') || selectedMilestoneDefault;
2016-09-13 17:45:13 +05:30
if (issueUpdateURL) {
2017-08-17 22:00:37 +05:30
milestoneLinkTemplate = _.template('<a href="/<%- full_path %>/milestones/<%- iid %>" class="bold has-tooltip" data-container="body" title="<%- remaining %>"><%- title %></a>');
2016-09-13 17:45:13 +05:30
milestoneLinkNoneTemplate = '<span class="no-value">None</span>';
collapsedSidebarLabelTemplate = _.template('<span class="has-tooltip" data-container="body" title="<%- remaining %>" data-placement="left"> <%- title %> </span>');
}
return $dropdown.glDropdown({
2016-11-03 12:29:30 +05:30
showMenuAbove: showMenuAbove,
2016-09-13 17:45:13 +05:30
data: function(term, callback) {
return $.ajax({
url: milestonesUrl
}).done(function(data) {
2016-11-03 12:29:30 +05:30
var extraOptions = [];
2016-09-13 17:45:13 +05:30
if (showAny) {
extraOptions.push({
id: 0,
name: '',
title: 'Any Milestone'
});
}
if (showNo) {
extraOptions.push({
id: -1,
name: 'No Milestone',
title: 'No Milestone'
});
}
if (showUpcoming) {
extraOptions.push({
id: -2,
name: '#upcoming',
title: 'Upcoming'
});
}
2017-08-17 22:00:37 +05:30
if (showStarted) {
extraOptions.push({
id: -3,
name: '#started',
title: 'Started'
});
}
2016-11-03 12:29:30 +05:30
if (extraOptions.length) {
2016-09-13 17:45:13 +05:30
extraOptions.push('divider');
}
2016-11-03 12:29:30 +05:30
callback(extraOptions.concat(data));
if (showMenuAbove) {
$dropdown.data('glDropdown').positionMenuAbove();
}
2017-09-10 17:25:29 +05:30
$(`[data-milestone-id="${selectedMilestone}"] > a`).addClass('is-active');
2016-09-13 17:45:13 +05:30
});
},
2017-09-10 17:25:29 +05:30
renderRow: function(milestone) {
return `
<li data-milestone-id="${milestone.name}">
<a href='#' class='dropdown-menu-milestone-link'>
${_.escape(milestone.title)}
</a>
</li>
`;
},
2016-09-13 17:45:13 +05:30
filterable: true,
search: {
fields: ['title']
},
selectable: true,
2016-11-03 12:29:30 +05:30
toggleLabel: function(selected, el, e) {
if (selected && 'id' in selected && $(el).hasClass('is-active')) {
2016-09-13 17:45:13 +05:30
return selected.title;
} else {
return defaultLabel;
}
},
2016-11-03 12:29:30 +05:30
defaultLabel: defaultLabel,
2016-09-13 17:45:13 +05:30
fieldName: $dropdown.data('field-name'),
text: function(milestone) {
return _.escape(milestone.title);
},
id: function(milestone) {
2016-11-03 12:29:30 +05:30
if (!useId && !$dropdown.is('.js-issuable-form-dropdown')) {
2016-09-13 17:45:13 +05:30
return milestone.name;
} else {
return milestone.id;
}
},
isSelected: function(milestone) {
return milestone.name === selectedMilestone;
},
hidden: function() {
$selectbox.hide();
2016-09-29 09:46:39 +05:30
// display:block overrides the hide-collapse rule
2016-09-13 17:45:13 +05:30
return $value.css('display', '');
},
2017-09-10 17:25:29 +05:30
opened: function(e) {
const $el = $(e.currentTarget);
if ($dropdown.hasClass('js-issue-board-sidebar')) {
selectedMilestone = $dropdown[0].dataset.selected || selectedMilestoneDefault;
}
$('a.is-active', $el).removeClass('is-active');
$(`[data-milestone-id="${selectedMilestone}"] > a`, $el).addClass('is-active');
},
2017-08-17 22:00:37 +05:30
vue: $dropdown.hasClass('js-issue-board-sidebar'),
clicked: function(options) {
const { $el, e } = options;
let selected = options.selectedObj;
2017-09-10 17:25:29 +05:30
var data, isIssueIndex, isMRIndex, isSelecting, page, boardsStore;
2016-09-13 17:45:13 +05:30
page = $('body').data('page');
isIssueIndex = page === 'projects:issues:index';
isMRIndex = (page === page && page === 'projects:merge_requests:index');
2017-09-10 17:25:29 +05:30
isSelecting = (selected.name !== selectedMilestone);
selectedMilestone = isSelecting ? selected.name : selectedMilestoneDefault;
2016-11-03 12:29:30 +05:30
if ($dropdown.hasClass('js-filter-bulk-update') || $dropdown.hasClass('js-issuable-form-dropdown')) {
e.preventDefault();
2016-09-13 17:45:13 +05:30
return;
}
2017-08-17 22:00:37 +05:30
if ($dropdown.closest('.add-issues-modal').length) {
boardsStore = gl.issueBoards.ModalStore.store.filter;
}
if (boardsStore) {
boardsStore[$dropdown.data('field-name')] = selected.name;
2016-09-13 17:45:13 +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();
2017-08-17 22:00:37 +05:30
} else if ($dropdown.hasClass('js-issue-board-sidebar')) {
2017-09-10 17:25:29 +05:30
if (selected.id !== -1 && isSelecting) {
2017-08-17 22:00:37 +05:30
gl.issueBoards.boardStoreIssueSet('milestone', new ListMilestone({
id: selected.id,
title: selected.name
}));
} else {
gl.issueBoards.boardStoreIssueDelete('milestone');
}
$dropdown.trigger('loading.gl.dropdown');
$loading.removeClass('hidden').fadeIn();
gl.issueBoards.BoardsStore.detail.issue.update($dropdown.attr('data-issue-update'))
.then(function () {
$dropdown.trigger('loaded.gl.dropdown');
$loading.fadeOut();
})
.catch(() => {
$loading.fadeOut();
});
2016-09-13 17:45:13 +05:30
} else {
selected = $selectbox.find('input[type="hidden"]').val();
data = {};
data[abilityName] = {};
data[abilityName].milestone_id = selected != null ? selected : null;
2017-08-17 22:00:37 +05:30
$loading.removeClass('hidden').fadeIn();
2016-09-13 17:45:13 +05:30
$dropdown.trigger('loading.gl.dropdown');
return $.ajax({
type: 'PUT',
url: issueUpdateURL,
data: data
}).done(function(data) {
$dropdown.trigger('loaded.gl.dropdown');
$loading.fadeOut();
$selectbox.hide();
$value.css('display', '');
if (data.milestone != null) {
2017-08-17 22:00:37 +05:30
data.milestone.full_path = _this.currentProject.full_path;
data.milestone.remaining = gl.utils.timeFor(data.milestone.due_date);
2016-09-13 17:45:13 +05:30
$value.html(milestoneLinkTemplate(data.milestone));
return $sidebarCollapsedValue.find('span').html(collapsedSidebarLabelTemplate(data.milestone));
} else {
$value.html(milestoneLinkNoneTemplate);
return $sidebarCollapsedValue.find('span').text('No');
}
});
}
}
});
});
}
return MilestoneSelect;
})();
2017-08-17 22:00:37 +05:30
}).call(window);