Skip to content

Commit 71e0f18

Browse files
Remove jQuery from the "find file" page (#29456)
- Switched to plain JavaScript - Tested the file searching functionality and it works as before # Demo using JavaScript without jQuery ![action](https://github.com/go-gitea/gitea/assets/20454870/8ceef0ed-ab87-448c-8b9b-9b5c0cd8bebd) --------- Signed-off-by: Yarden Shoham <[email protected]> Co-authored-by: Giteabot <[email protected]>
1 parent b5188cd commit 71e0f18

File tree

1 file changed

+29
-31
lines changed

1 file changed

+29
-31
lines changed

web_src/js/features/repo-findfile.js

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import $ from 'jquery';
21
import {svg} from '../svg.js';
32
import {toggleElem} from '../utils/dom.js';
43
import {pathEscapeSegments} from '../utils/url.js';
5-
6-
const {csrf} = window.config;
4+
import {GET} from '../modules/fetch.js';
75

86
const threshold = 50;
97
let files = [];
10-
let $repoFindFileInput, $repoFindFileTableBody, $repoFindFileNoResult;
8+
let repoFindFileInput, repoFindFileTableBody, repoFindFileNoResult;
119

1210
// return the case-insensitive sub-match result as an array: [unmatched, matched, unmatched, matched, ...]
1311
// res[even] is unmatched, res[odd] is matched, see unit tests for examples
@@ -74,46 +72,46 @@ export function filterRepoFilesWeighted(files, filter) {
7472
}
7573

7674
function filterRepoFiles(filter) {
77-
const treeLink = $repoFindFileInput.attr('data-url-tree-link');
78-
$repoFindFileTableBody.empty();
75+
const treeLink = repoFindFileInput.getAttribute('data-url-tree-link');
76+
repoFindFileTableBody.innerHTML = '';
7977

8078
const filterResult = filterRepoFilesWeighted(files, filter);
81-
const tmplRow = `<tr><td><a></a></td></tr>`;
8279

83-
toggleElem($repoFindFileNoResult, filterResult.length === 0);
80+
toggleElem(repoFindFileNoResult, filterResult.length === 0);
8481
for (const r of filterResult) {
85-
const $row = $(tmplRow);
86-
const $a = $row.find('a');
87-
$a.attr('href', `${treeLink}/${pathEscapeSegments(r.matchResult.join(''))}`);
88-
const $octiconFile = $(svg('octicon-file')).addClass('gt-mr-3');
89-
$a.append($octiconFile);
90-
// if the target file path is "abc/xyz", to search "bx", then the matchResult is ['a', 'b', 'c/', 'x', 'yz']
91-
// the matchResult[odd] is matched and highlighted to red.
92-
for (let j = 0; j < r.matchResult.length; j++) {
93-
if (!r.matchResult[j]) continue;
94-
const $span = $('<span>').text(r.matchResult[j]);
95-
if (j % 2 === 1) $span.addClass('ui text red');
96-
$a.append($span);
82+
const row = document.createElement('tr');
83+
const cell = document.createElement('td');
84+
const a = document.createElement('a');
85+
a.setAttribute('href', `${treeLink}/${pathEscapeSegments(r.matchResult.join(''))}`);
86+
a.innerHTML = svg('octicon-file', 16, 'gt-mr-3');
87+
row.append(cell);
88+
cell.append(a);
89+
for (const [index, part] of r.matchResult.entries()) {
90+
const span = document.createElement('span');
91+
// safely escape by using textContent
92+
span.textContent = part;
93+
// if the target file path is "abc/xyz", to search "bx", then the matchResult is ['a', 'b', 'c/', 'x', 'yz']
94+
// the matchResult[odd] is matched and highlighted to red.
95+
if (index % 2 === 1) span.classList.add('ui', 'text', 'red');
96+
a.append(span);
9797
}
98-
$repoFindFileTableBody.append($row);
98+
repoFindFileTableBody.append(row);
9999
}
100100
}
101101

102102
async function loadRepoFiles() {
103-
files = await $.ajax({
104-
url: $repoFindFileInput.attr('data-url-data-link'),
105-
headers: {'X-Csrf-Token': csrf}
106-
});
107-
filterRepoFiles($repoFindFileInput.val());
103+
const response = await GET(repoFindFileInput.getAttribute('data-url-data-link'));
104+
files = await response.json();
105+
filterRepoFiles(repoFindFileInput.value);
108106
}
109107

110108
export function initFindFileInRepo() {
111-
$repoFindFileInput = $('#repo-file-find-input');
112-
if (!$repoFindFileInput.length) return;
109+
repoFindFileInput = document.getElementById('repo-file-find-input');
110+
if (!repoFindFileInput) return;
113111

114-
$repoFindFileTableBody = $('#repo-find-file-table tbody');
115-
$repoFindFileNoResult = $('#repo-find-file-no-result');
116-
$repoFindFileInput.on('input', () => filterRepoFiles($repoFindFileInput.val()));
112+
repoFindFileTableBody = document.querySelector('#repo-find-file-table tbody');
113+
repoFindFileNoResult = document.getElementById('repo-find-file-no-result');
114+
repoFindFileInput.addEventListener('input', () => filterRepoFiles(repoFindFileInput.value));
117115

118116
loadRepoFiles();
119117
}

0 commit comments

Comments
 (0)