Skip to content

Commit 2b7b70b

Browse files
committed
more refactor
1 parent 667e26c commit 2b7b70b

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

web_src/js/features/repo-code.js

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ function changeHash(hash) {
1616
}
1717
}
1818

19+
function isBlame() {
20+
return Boolean(document.querySelector('div.blame'));
21+
}
22+
23+
function getLineEls() {
24+
return document.querySelectorAll(`.code-view td.lines-code${isBlame() ? '.blame-code' : ''}`);
25+
}
26+
1927
function selectRange($linesEls, $selectionEndEl, $selectionStartEls) {
2028
$linesEls.closest('tr').removeClass('active');
2129

@@ -25,7 +33,7 @@ function selectRange($linesEls, $selectionEndEl, $selectionStartEls) {
2533
const $viewGitBlame = $('a.view_git_blame');
2634

2735
const updateIssueHref = function (anchor) {
28-
if ($refInNewIssue.length === 0) {
36+
if (!$refInNewIssue.length) {
2937
return;
3038
}
3139
const urlIssueNew = $refInNewIssue.attr('data-url-issue-new');
@@ -35,7 +43,7 @@ function selectRange($linesEls, $selectionEndEl, $selectionStartEls) {
3543
};
3644

3745
const updateViewGitBlameFragment = function (anchor) {
38-
if ($viewGitBlame.length === 0) return;
46+
if (!$viewGitBlame.length) return;
3947
let href = $viewGitBlame.attr('href');
4048
href = `${href.replace(/#L\d+$|#L\d+-L\d+$/, '')}`;
4149
if (anchor.length !== 0) {
@@ -121,13 +129,7 @@ function showLineButton() {
121129
export function initRepoCodeView() {
122130
if ($('.code-view .lines-num').length > 0) {
123131
$(document).on('click', '.lines-num span', function (e) {
124-
let linesEls;
125-
if (document.querySelector('div.blame')) {
126-
linesEls = document.querySelectorAll('.code-view td.lines-code.blame-code');
127-
} else {
128-
linesEls = document.querySelectorAll('.code-view td.lines-code');
129-
}
130-
132+
const linesEls = getLineEls();
131133
const selectedEls = Array.from(linesEls).filter((el) => {
132134
return el.matches(`[rel=${this.getAttribute('id')}]`);
133135
});
@@ -147,27 +149,22 @@ export function initRepoCodeView() {
147149
}
148150

149151
// show code view menu marker (don't show in blame page)
150-
if ($('div.blame').length === 0) {
152+
if (!isBlame()) {
151153
showLineButton();
152154
}
153155
});
154156

155157
$(window).on('hashchange', () => {
156158
let m = window.location.hash.match(rangeAnchorRegex);
157-
let $linesEls;
158-
if ($('div.blame').length) {
159-
$linesEls = $('.code-view td.lines-code.blame-code');
160-
} else {
161-
$linesEls = $('.code-view td.lines-code');
162-
}
159+
const $linesEls = $(getLineEls());
163160
let $first;
164161
if (m) {
165162
$first = $linesEls.filter(`[rel=${m[1]}]`);
166163
if ($first.length) {
167164
selectRange($linesEls, $first, $linesEls.filter(`[rel=${m[2]}]`));
168165

169166
// show code view menu marker (don't show in blame page)
170-
if ($('div.blame').length === 0) {
167+
if (!isBlame()) {
171168
showLineButton();
172169
}
173170

@@ -182,7 +179,7 @@ export function initRepoCodeView() {
182179
selectRange($linesEls, $first);
183180

184181
// show code view menu marker (don't show in blame page)
185-
if ($('div.blame').length === 0) {
182+
if (!isBlame()) {
186183
showLineButton();
187184
}
188185

0 commit comments

Comments
 (0)