diff options
author | silverwind <me@silverwind.io> | 2024-07-07 17:32:30 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-07 15:32:30 +0000 |
commit | 5791a73e75b630db3cade3e606c45eb8d8a641cb (patch) | |
tree | 3042132ac6f486723ff2ae42245a5b8a09d98c72 /web_src/js/features/repo-common.ts | |
parent | 5115c278ff8f3f8beebb172ce20a939a10476dfd (diff) | |
download | gitea-5791a73e75b630db3cade3e606c45eb8d8a641cb.tar.gz gitea-5791a73e75b630db3cade3e606c45eb8d8a641cb.zip |
Convert frontend code to typescript (#31559)
None of the frontend js/ts files was touched besides these two commands
(edit: no longer true, I touched one file in
https://github.com/go-gitea/gitea/pull/31559/commits/61105d0618e285d97e95044bfb64415f364a4526
because of a deprecation that was not showing before the rename).
`tsc` currently reports 778 errors, so I have disabled it in CI as
planned.
Everything appears to work fine.
Diffstat (limited to 'web_src/js/features/repo-common.ts')
-rw-r--r-- | web_src/js/features/repo-common.ts | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/web_src/js/features/repo-common.ts b/web_src/js/features/repo-common.ts new file mode 100644 index 0000000000..ac63ef2145 --- /dev/null +++ b/web_src/js/features/repo-common.ts @@ -0,0 +1,83 @@ +import $ from 'jquery'; +import {hideElem, queryElems, showElem} from '../utils/dom.ts'; +import {POST} from '../modules/fetch.ts'; +import {showErrorToast} from '../modules/toast.ts'; +import {sleep} from '../utils.ts'; + +async function onDownloadArchive(e) { + e.preventDefault(); + // there are many places using the "archive-link", eg: the dropdown on the repo code page, the release list + const el = e.target.closest('a.archive-link[href]'); + const targetLoading = el.closest('.ui.dropdown') ?? el; + targetLoading.classList.add('is-loading', 'loading-icon-2px'); + try { + for (let tryCount = 0; ;tryCount++) { + const response = await POST(el.href); + if (!response.ok) throw new Error(`Invalid server response: ${response.status}`); + + const data = await response.json(); + if (data.complete) break; + await sleep(Math.min((tryCount + 1) * 750, 2000)); + } + window.location.href = el.href; // the archive is ready, start real downloading + } catch (e) { + console.error(e); + showErrorToast(`Failed to download the archive: ${e}`, {duration: 2500}); + } finally { + targetLoading.classList.remove('is-loading', 'loading-icon-2px'); + } +} + +export function initRepoArchiveLinks() { + queryElems('a.archive-link[href]', (el) => el.addEventListener('click', onDownloadArchive)); +} + +export function initRepoCloneLink() { + const $repoCloneSsh = $('#repo-clone-ssh'); + const $repoCloneHttps = $('#repo-clone-https'); + const $inputLink = $('#repo-clone-url'); + + if ((!$repoCloneSsh.length && !$repoCloneHttps.length) || !$inputLink.length) { + return; + } + + $repoCloneSsh.on('click', () => { + localStorage.setItem('repo-clone-protocol', 'ssh'); + window.updateCloneStates(); + }); + $repoCloneHttps.on('click', () => { + localStorage.setItem('repo-clone-protocol', 'https'); + window.updateCloneStates(); + }); + + $inputLink.on('focus', () => { + $inputLink.trigger('select'); + }); +} + +export function initRepoCommonBranchOrTagDropdown(selector) { + $(selector).each(function () { + const $dropdown = $(this); + $dropdown.find('.reference.column').on('click', function () { + hideElem($dropdown.find('.scrolling.reference-list-menu')); + showElem($($(this).data('target'))); + return false; + }); + }); +} + +export function initRepoCommonFilterSearchDropdown(selector) { + const $dropdown = $(selector); + if (!$dropdown.length) return; + + $dropdown.dropdown({ + fullTextSearch: 'exact', + selectOnKeydown: false, + onChange(_text, _value, $choice) { + if ($choice[0].getAttribute('data-url')) { + window.location.href = $choice[0].getAttribute('data-url'); + } + }, + message: {noResults: $dropdown[0].getAttribute('data-no-results')}, + }); +} |