Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove jQuery from the "find file" page #29456

Merged
merged 6 commits into from
Feb 28, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 23 additions & 30 deletions web_src/js/features/repo-findfile.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import $ from 'jquery';
import {svg} from '../svg.js';
import {toggleElem} from '../utils/dom.js';
import {pathEscapeSegments} from '../utils/url.js';

const {csrf} = window.config;
import {GET} from '../modules/fetch.js';

const threshold = 50;
let files = [];
let $repoFindFileInput, $repoFindFileTableBody, $repoFindFileNoResult;
let repoFindFileInput, repoFindFileTableBody, repoFindFileNoResult;

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

function filterRepoFiles(filter) {
const treeLink = $repoFindFileInput.attr('data-url-tree-link');
$repoFindFileTableBody.empty();
const treeLink = repoFindFileInput.getAttribute('data-url-tree-link');
repoFindFileTableBody.innerHTML = '';

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

toggleElem($repoFindFileNoResult, filterResult.length === 0);
toggleElem(repoFindFileNoResult, filterResult.length === 0);
for (const r of filterResult) {
const $row = $(tmplRow);
const $a = $row.find('a');
$a.attr('href', `${treeLink}/${pathEscapeSegments(r.matchResult.join(''))}`);
const $octiconFile = $(svg('octicon-file')).addClass('gt-mr-3');
$a.append($octiconFile);
const row = document.createElement('tr');
const cell = document.createElement('td');
const a = document.createElement('a');
a.setAttribute('href', `${treeLink}/${pathEscapeSegments(r.matchResult.join(''))}`);
a.innerHTML = svg('octicon-file') + r.matchResult.map((part, index) =>
// if the target file path is "abc/xyz", to search "bx", then the matchResult is ['a', 'b', 'c/', 'x', 'yz']
// the matchResult[odd] is matched and highlighted to red.
for (let j = 0; j < r.matchResult.length; j++) {
if (!r.matchResult[j]) continue;
const $span = $('<span>').text(r.matchResult[j]);
if (j % 2 === 1) $span.addClass('ui text red');
$a.append($span);
}
$repoFindFileTableBody.append($row);
`<span class="${index % 2 === 1 ? 'ui text red' : ''}">${part}</span>`
).join('');
cell.append(a);
row.append(cell);
repoFindFileTableBody.append(row);
}
}

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

export function initFindFileInRepo() {
$repoFindFileInput = $('#repo-file-find-input');
if (!$repoFindFileInput.length) return;
repoFindFileInput = document.getElementById('repo-file-find-input');
if (!repoFindFileInput) return;

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

loadRepoFiles();
}
Loading