|
1 | | -import $ from 'jquery'; |
2 | 1 | import {svg} from '../svg.js'; |
3 | 2 | import {toggleElem} from '../utils/dom.js'; |
4 | 3 | import {pathEscapeSegments} from '../utils/url.js'; |
5 | | - |
6 | | -const {csrf} = window.config; |
| 4 | +import {GET} from '../modules/fetch.js'; |
7 | 5 |
|
8 | 6 | const threshold = 50; |
9 | 7 | let files = []; |
10 | | -let $repoFindFileInput, $repoFindFileTableBody, $repoFindFileNoResult; |
| 8 | +let repoFindFileInput, repoFindFileTableBody, repoFindFileNoResult; |
11 | 9 |
|
12 | 10 | // return the case-insensitive sub-match result as an array: [unmatched, matched, unmatched, matched, ...] |
13 | 11 | // res[even] is unmatched, res[odd] is matched, see unit tests for examples |
@@ -74,46 +72,46 @@ export function filterRepoFilesWeighted(files, filter) { |
74 | 72 | } |
75 | 73 |
|
76 | 74 | 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 = ''; |
79 | 77 |
|
80 | 78 | const filterResult = filterRepoFilesWeighted(files, filter); |
81 | | - const tmplRow = `<tr><td><a></a></td></tr>`; |
82 | 79 |
|
83 | | - toggleElem($repoFindFileNoResult, filterResult.length === 0); |
| 80 | + toggleElem(repoFindFileNoResult, filterResult.length === 0); |
84 | 81 | 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); |
97 | 97 | } |
98 | | - $repoFindFileTableBody.append($row); |
| 98 | + repoFindFileTableBody.append(row); |
99 | 99 | } |
100 | 100 | } |
101 | 101 |
|
102 | 102 | 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); |
108 | 106 | } |
109 | 107 |
|
110 | 108 | 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; |
113 | 111 |
|
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)); |
117 | 115 |
|
118 | 116 | loadRepoFiles(); |
119 | 117 | } |
0 commit comments