Skip to content

Commit 456da24

Browse files
committed
refactor
1 parent 01f3f0f commit 456da24

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

web_src/js/features/common-issue.js

+24-15
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,34 @@ import $ from 'jquery';
22
import {updateIssuesMeta} from './repo-issue.js';
33

44
export function initCommonIssue() {
5-
const checkboxOperate = () => {
6-
if ($('.issue-checkbox input').is(':checked')) {
7-
$('#issue-filters').addClass('hide');
8-
$('#issue-actions').removeClass('hide');
9-
$('#issue-actions .six').prepend($('.issue-checkbox-all'));
5+
const $issueSelectAllWrapper = $('.issue-checkbox-all');
6+
const $issueSelectAll = $('.issue-checkbox-all input');
7+
const $issueCheckboxes = $('.issue-checkbox input');
8+
9+
const syncIssueSelectionState = () => {
10+
const $checked = $issueCheckboxes.filter(':checked');
11+
const anyChecked = $checked.length !== 0;
12+
const allChecked = anyChecked && $checked.length === $issueCheckboxes.length;
13+
14+
if (allChecked) {
15+
$issueSelectAll.prop({'checked': true, 'indeterminate': false});
16+
} else if (anyChecked) {
17+
$issueSelectAll.prop({'checked': false, 'indeterminate': true});
1018
} else {
11-
$('#issue-filters').removeClass('hide');
12-
$('#issue-actions').addClass('hide');
13-
$('#issue-filters .six').prepend($('.issue-checkbox-all'));
19+
$issueSelectAll.prop({'checked': false, 'indeterminate': false});
1420
}
21+
// if any issue is selected, show the action panel, otherwise show the filter panel
22+
$('#issue-filters').toggle(!anyChecked);
23+
$('#issue-actions').toggle(anyChecked);
24+
// there are two panels but only one select-all checkbox, so move the checkbox to the visible panel
25+
$('#issue-filters, #issue-actions').filter(':visible').find('.column:first').prepend($issueSelectAllWrapper);
1526
};
1627

17-
const checkboxpart = $('.issue-checkbox');
18-
checkboxpart.on('click', checkboxOperate);
28+
$issueCheckboxes.on('change', syncIssueSelectionState);
1929

20-
const checkboxall = $('.issue-checkbox-all');
21-
checkboxall.on('click', (e) => {
22-
checkboxpart.find('input').prop('checked', checkboxall.find('input').prop('checked'));
23-
checkboxOperate(e);
30+
$issueSelectAll.on('change', () => {
31+
$issueCheckboxes.prop('checked', $issueSelectAll.is(':checked'));
32+
syncIssueSelectionState();
2433
});
2534

2635
$('.issue-action').on('click', async function () {
@@ -51,7 +60,7 @@ export function initCommonIssue() {
5160
});
5261

5362
// NOTICE: This event trigger targets Firefox caching behaviour, as the checkboxes stay
54-
// checked after reload trigger ckecked event, if checkboxes are checked on load
63+
// checked after reload trigger checked event, if checkboxes are checked on load
5564
$('.issue-checkbox input[type="checkbox"]:checked').first().each((_, e) => {
5665
e.checked = false;
5766
$(e).trigger('click');

0 commit comments

Comments
 (0)