Remove duplicate logic in initListSubmits (#12660)

* Remove duplicate logic in initListSubmits

Using the same logic to handle Choosing reviewers and assignees as
choosing label. It's the first step of #10926.

Signed-off-by: a1012112796 <1012112796@qq.com>

* fix choose block

* fix nit

* try fix bug

* simple code

Co-authored-by: techknowlogick <techknowlogick@gitea.io>
This commit is contained in:
赵智超 2020-09-03 00:55:13 +08:00 committed by GitHub
parent ea775e67fb
commit 3981f1b127
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 67 deletions

View file

@ -157,7 +157,7 @@ function initLabelEdit() {
});
}
function updateIssuesMeta(url, action, issueIds, elementId, isAdd) {
function updateIssuesMeta(url, action, issueIds, elementId) {
return new Promise(((resolve) => {
$.ajax({
type: 'POST',
@ -167,7 +167,6 @@ function updateIssuesMeta(url, action, issueIds, elementId, isAdd) {
action,
issue_ids: issueIds,
id: elementId,
is_add: isAdd
},
success: resolve
});
@ -373,21 +372,20 @@ function initCommentForm() {
const $list = $(`.ui.${outerSelector}.list`);
const $noSelect = $list.find('.no-select');
const $listMenu = $(`.${selector} .menu`);
let hasLabelUpdateAction = $listMenu.data('action') === 'update';
const labels = {};
let hasUpdateAction = $listMenu.data('action') === 'update';
const items = {};
$(`.${selector}`).dropdown('setting', 'onHide', () => {
hasLabelUpdateAction = $listMenu.data('action') === 'update'; // Update the var
if (hasLabelUpdateAction) {
hasUpdateAction = $listMenu.data('action') === 'update'; // Update the var
if (hasUpdateAction) {
const promises = [];
Object.keys(labels).forEach((elementId) => {
const label = labels[elementId];
Object.keys(items).forEach((elementId) => {
const item = items[elementId];
const promise = updateIssuesMeta(
label['update-url'],
label.action,
label['issue-id'],
item['update-url'],
item.action,
item['issue-id'],
elementId,
label['is-checked']
);
promises.push(promise);
});
@ -395,67 +393,49 @@ function initCommentForm() {
}
});
$listMenu.find('.item:not(.no-select)').on('click', function () {
// we don't need the action attribute when updating assignees
if (selector === 'select-assignees-modify' || selector === 'select-reviewers-modify') {
// UI magic. We need to do this here, otherwise it would destroy the functionality of
// adding/removing labels
if ($(this).data('can-change') === 'block') {
return false;
}
if ($(this).hasClass('checked')) {
$(this).removeClass('checked');
$(this).find('.octicon-check').addClass('invisible');
$(this).data('is-checked', 'remove');
} else {
$(this).addClass('checked');
$(this).find('.octicon-check').removeClass('invisible');
$(this).data('is-checked', 'add');
}
updateIssuesMeta(
$listMenu.data('update-url'),
'',
$listMenu.data('issue-id'),
$(this).data('id'),
$(this).data('is-checked')
);
$listMenu.data('action', 'update'); // Update to reload the page when we updated items
$listMenu.find('.item:not(.no-select)').on('click', function (e) {
e.preventDefault();
if ($(this).hasClass('ban-change')) {
return false;
}
hasUpdateAction = $listMenu.data('action') === 'update'; // Update the var
if ($(this).hasClass('checked')) {
$(this).removeClass('checked');
$(this).find('.octicon-check').addClass('invisible');
if (hasLabelUpdateAction) {
if (!($(this).data('id') in labels)) {
labels[$(this).data('id')] = {
if (hasUpdateAction) {
if (!($(this).data('id') in items)) {
items[$(this).data('id')] = {
'update-url': $listMenu.data('update-url'),
action: 'detach',
'issue-id': $listMenu.data('issue-id'),
};
} else {
delete labels[$(this).data('id')];
delete items[$(this).data('id')];
}
}
} else {
$(this).addClass('checked');
$(this).find('.octicon-check').removeClass('invisible');
if (hasLabelUpdateAction) {
if (!($(this).data('id') in labels)) {
labels[$(this).data('id')] = {
if (hasUpdateAction) {
if (!($(this).data('id') in items)) {
items[$(this).data('id')] = {
'update-url': $listMenu.data('update-url'),
action: 'attach',
'issue-id': $listMenu.data('issue-id'),
};
} else {
delete labels[$(this).data('id')];
delete items[$(this).data('id')];
}
}
}
// TODO: Which thing should be done for choosing review requests
// to make choosed items be shown on time here?
if (selector === 'select-reviewers-modify' || selector === 'select-assignees-modify') {
return false;
}
const listIds = [];
$(this).parent().find('.item').each(function () {
if ($(this).hasClass('checked')) {
@ -473,23 +453,26 @@ function initCommentForm() {
$($(this).parent().data('id')).val(listIds.join(','));
return false;
});
$listMenu.find('.no-select.item').on('click', function () {
if (hasLabelUpdateAction || selector === 'select-assignees-modify') {
$listMenu.find('.no-select.item').on('click', function (e) {
e.preventDefault();
if (hasUpdateAction) {
updateIssuesMeta(
$listMenu.data('update-url'),
'clear',
$listMenu.data('issue-id'),
'',
''
).then(reload);
}
$(this).parent().find('.item').each(function () {
$(this).removeClass('checked');
$(this).find('.octicon').addClass('invisible');
$(this).data('is-checked', 'remove');
});
if (selector === 'select-reviewers-modify' || selector === 'select-assignees-modify') {
return false;
}
$list.find('.item').each(function () {
$(this).addClass('hide');
});
@ -521,7 +504,6 @@ function initCommentForm() {
'',
$menu.data('issue-id'),
$(this).data('id'),
$(this).data('is-checked')
).then(reload);
}
switch (input_id) {
@ -552,7 +534,6 @@ function initCommentForm() {
'',
$menu.data('issue-id'),
$(this).data('id'),
$(this).data('is-checked')
).then(reload);
}
@ -672,10 +653,9 @@ function initIssueComments() {
event.preventDefault();
updateIssuesMeta(
url,
'',
isChecked === 'true' ? 'attach' : 'detach',
issueId,
id,
isChecked
).then(reload);
});