Browse Source

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>
for-closed-social
赵智超 4 years ago
committed by GitHub
parent
commit
3981f1b127
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 67 deletions
  1. +8
    -6
      routers/repo/issue.go
  2. +1
    -1
      templates/repo/issue/view_content/pull.tmpl
  3. +4
    -4
      templates/repo/issue/view_content/sidebar.tmpl
  4. +36
    -56
      web_src/js/index.js

+ 8
- 6
routers/repo/issue.go View File

@ -1516,10 +1516,11 @@ func updatePullReviewRequest(ctx *context.Context) {
}
reviewID := ctx.QueryInt64("id")
event := ctx.Query("is_add")
action := ctx.Query("action")
if event != "add" && event != "remove" {
ctx.ServerError("updatePullReviewRequest", fmt.Errorf("is_add should not be \"%s\"", event))
// TODO: Not support 'clear' now
if action != "attach" && action != "detach" {
ctx.Status(403)
return
}
@ -1532,19 +1533,20 @@ func updatePullReviewRequest(ctx *context.Context) {
return
}
err = isLegalReviewRequest(reviewer, ctx.User, event == "add", issue)
err = isLegalReviewRequest(reviewer, ctx.User, action == "attach", issue)
if err != nil {
ctx.ServerError("isLegalRequestReview", err)
return
}
err = issue_service.ReviewRequest(issue, ctx.User, reviewer, event == "add")
err = issue_service.ReviewRequest(issue, ctx.User, reviewer, action == "attach")
if err != nil {
ctx.ServerError("ReviewRequest", err)
return
}
} else {
ctx.ServerError("updatePullReviewRequest", fmt.Errorf("%d in %d is not Pull Request", issue.ID, issue.Repo.ID))
ctx.Status(403)
return
}
}

+ 1
- 1
templates/repo/issue/view_content/pull.tmpl View File

@ -49,7 +49,7 @@
{{end}}
{{if $canChoose }}
<a href="#" class="ui poping up icon re-request-review" data-is-checked="{{if eq .Type 4}}remove{{else}}add{{end}}" data-issue-id="{{$.Issue.ID}}" data-content="{{ if eq .Type 4 }} {{$.i18n.Tr "repo.issues.remove_request_review"}} {{else}} {{$.i18n.Tr "repo.issues.re_request_review"}} {{end}}" data-id="{{.ReviewerID}}" data-update-url="{{$.RepoLink}}/issues/request_review">
<a href="#" class="ui poping up icon re-request-review" data-is-checked="{{if eq .Type 4}}true{{else}}false{{end}}" data-issue-id="{{$.Issue.ID}}" data-content="{{ if eq .Type 4 }} {{$.i18n.Tr "repo.issues.remove_request_review"}} {{else}} {{$.i18n.Tr "repo.issues.re_request_review"}} {{end}}" data-id="{{.ReviewerID}}" data-update-url="{{$.RepoLink}}/issues/request_review">
{{svg "octicon-sync" 16}}
</a>
{{end}}

+ 4
- 4
templates/repo/issue/view_content/sidebar.tmpl View File

@ -12,7 +12,7 @@
{{svg "octicon-gear" 16}}
{{end}}
</span>
<div class="filter menu" data-action="" data-issue-id="{{$.Issue.ID}}" data-update-url="{{$.RepoLink}}/issues/request_review">
<div class="filter menu" data-action="update" data-issue-id="{{$.Issue.ID}}" data-update-url="{{$.RepoLink}}/issues/request_review">
<div class="header" style="text-transform: none;font-size:16px;">{{.i18n.Tr "repo.issues.new.add_reviewer_title"}}</div>
{{if .Reviewers}}
<div class="ui icon search input">
@ -44,7 +44,7 @@
{{$canChoose = true}}
{{end}}
<a class="{{if not $canChoose}}ui poping up{{end}} item {{if $checked}} checked {{end}}" href="#" data-id="{{.ID}}" data-id-selector="#review_request_{{.ID}}" data-can-change="{{if not $canChoose}}block{{end}}" {{if not $canChoose}} data-content="{{$.i18n.Tr "repo.issues.remove_request_review_block"}}"{{end}} data-is-checked="{{if $checked}}add{{else}}remove{{end}}">
<a class="{{if not $canChoose}}ui poping up{{end}} item {{if $checked}} checked {{end}} {{if not $canChoose}}ban-change{{end}}" href="#" data-id="{{.ID}}" data-id-selector="#review_request_{{.ID}}" {{if not $canChoose}} data-content="{{$.i18n.Tr "repo.issues.remove_request_review_block"}}"{{end}}>
<span class="octicon-check {{if not $checked}}invisible{{end}}">{{svg "octicon-check" 16}}</span>
<span class="text">
<img class="ui avatar image" src="{{.RelAvatarLink}}"> {{.GetDisplayName}}
@ -78,7 +78,7 @@
{{end}}
{{if $canChoose}}
<a href="#" class="ui poping up icon re-request-review" data-is-checked="{{if eq .Type 4}}remove{{else}}add{{end}}" data-content="{{ if eq .Type 4 }} {{$.i18n.Tr "repo.issues.remove_request_review"}} {{else}} {{$.i18n.Tr "repo.issues.re_request_review"}} {{end}}" data-issue-id="{{$.Issue.ID}}" data-id="{{.ReviewerID}}" data-update-url="{{$.RepoLink}}/issues/request_review">
<a href="#" class="ui poping up icon re-request-review" data-is-checked="{{if eq .Type 4}}true{{else}}false{{end}}" data-content="{{ if eq .Type 4 }} {{$.i18n.Tr "repo.issues.remove_request_review"}} {{else}} {{$.i18n.Tr "repo.issues.re_request_review"}} {{end}}" data-issue-id="{{$.Issue.ID}}" data-id="{{.ReviewerID}}" data-update-url="{{$.RepoLink}}/issues/request_review">
{{svg "octicon-sync" 16}}
</a>
{{end}}
@ -244,7 +244,7 @@
{{svg "octicon-gear" 16}}
{{end}}
</span>
<div class="filter menu" data-action="" data-issue-id="{{$.Issue.ID}}" data-update-url="{{$.RepoLink}}/issues/assignee">
<div class="filter menu" data-action="update" data-issue-id="{{$.Issue.ID}}" data-update-url="{{$.RepoLink}}/issues/assignee">
<div class="header" style="text-transform: none;font-size:16px;">{{.i18n.Tr "repo.issues.new.add_assignees_title"}}</div>
<div class="ui icon search input">
<i class="search icon"></i>

+ 36
- 56
web_src/js/index.js 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);
});

Loading…
Cancel
Save