|
| 1 | +import $ from 'jquery'; |
| 2 | + |
| 3 | +import {svg} from '../svg.js'; |
| 4 | +import {strSubMatch} from '../utils.js'; |
| 5 | +const {csrf} = window.config; |
| 6 | + |
| 7 | +const threshold = 50; |
| 8 | +let files = []; |
| 9 | +let $repoFindFileInput, $repoFindFileTableBody, $repoFindFileNoResult; |
| 10 | + |
| 11 | +function filterRepoFiles(filter) { |
| 12 | + const treeLink = $repoFindFileInput.attr('data-url-tree-link'); |
| 13 | + $repoFindFileTableBody.empty(); |
| 14 | + |
| 15 | + const fileRes = []; |
| 16 | + if (filter) { |
| 17 | + for (let i = 0; i < files.length && fileRes.length < threshold; i++) { |
| 18 | + const subMatch = strSubMatch(files[i], filter); |
| 19 | + if (subMatch.length > 1) { |
| 20 | + fileRes.push(subMatch); |
| 21 | + } |
| 22 | + } |
| 23 | + } else { |
| 24 | + for (let i = 0; i < files.length && i < threshold; i++) { |
| 25 | + fileRes.push([files[i]]); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + const tmplRow = `<tr><td><a></a></td></tr>`; |
| 30 | + |
| 31 | + $repoFindFileNoResult.toggle(fileRes.length === 0); |
| 32 | + for (const matchRes of fileRes) { |
| 33 | + const $row = $(tmplRow); |
| 34 | + const $a = $row.find('a'); |
| 35 | + $a.attr('href', `${treeLink}/${matchRes.join('')}`); |
| 36 | + const $octiconFile = $(svg('octicon-file')).addClass('mr-3'); |
| 37 | + $a.append($octiconFile); |
| 38 | + // if the target file path is "abc/xyz", to search "bx", then the matchRes is ['a', 'b', 'c/', 'x', 'yz'] |
| 39 | + // the matchRes[odd] is matched and highlighted to red. |
| 40 | + for (let j = 0; j < matchRes.length; j++) { |
| 41 | + if (!matchRes[j]) continue; |
| 42 | + const $span = $('<span>').text(matchRes[j]); |
| 43 | + if (j % 2 === 1) $span.addClass('ui text red'); |
| 44 | + $a.append($span); |
| 45 | + } |
| 46 | + $repoFindFileTableBody.append($row); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +async function loadRepoFiles() { |
| 51 | + files = await $.ajax({ |
| 52 | + url: $repoFindFileInput.attr('data-url-data-link'), |
| 53 | + headers: {'X-Csrf-Token': csrf} |
| 54 | + }); |
| 55 | + filterRepoFiles($repoFindFileInput.val()); |
| 56 | +} |
| 57 | + |
| 58 | +export function initFindFileInRepo() { |
| 59 | + $repoFindFileInput = $('#repo-file-find-input'); |
| 60 | + if (!$repoFindFileInput.length) return; |
| 61 | + |
| 62 | + $repoFindFileTableBody = $('#repo-find-file-table tbody'); |
| 63 | + $repoFindFileNoResult = $('#repo-find-file-no-result'); |
| 64 | + $repoFindFileInput.on('input', () => filterRepoFiles($repoFindFileInput.val())); |
| 65 | + |
| 66 | + loadRepoFiles(); |
| 67 | +} |
0 commit comments