mirror of
https://github.com/go-gitea/gitea.git
synced 2024-12-04 14:46:57 -05:00
Backport #30535 by wxiaoguang Regression of #29920 Fixes: https://github.com/go-gitea/gitea/issues/30569 Also this is a rewriting to eliminate the remaining jQuery usages from code. --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
parent
ea2ea8ef28
commit
fc376c863a
@ -1,45 +1,37 @@
|
|||||||
import $ from 'jquery';
|
import $ from 'jquery';
|
||||||
import {hideElem, showElem} from '../utils/dom.js';
|
import {hideElem, showElem} from '../utils/dom.js';
|
||||||
import {POST} from '../modules/fetch.js';
|
import {POST} from '../modules/fetch.js';
|
||||||
|
import {showErrorToast} from '../modules/toast.js';
|
||||||
|
import {sleep} from '../utils.js';
|
||||||
|
|
||||||
async function getArchive($target, url, first) {
|
async function onDownloadArchive(e) {
|
||||||
const dropdownBtn = $target[0].closest('.ui.dropdown.button') ?? $target[0].closest('.ui.dropdown.btn');
|
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', 'small-loading-icon');
|
||||||
try {
|
try {
|
||||||
dropdownBtn.classList.add('is-loading');
|
for (let tryCount = 0; ;tryCount++) {
|
||||||
const response = await POST(url);
|
const response = await POST(el.href);
|
||||||
if (response.status === 200) {
|
if (!response.ok) throw new Error(`Invalid server response: ${response.status}`);
|
||||||
const data = await response.json();
|
|
||||||
if (!data) {
|
|
||||||
// XXX Shouldn't happen?
|
|
||||||
dropdownBtn.classList.remove('is-loading');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!data.complete) {
|
const data = await response.json();
|
||||||
// Wait for only three quarters of a second initially, in case it's
|
if (data.complete) break;
|
||||||
// quickly archived.
|
await sleep(Math.min((tryCount + 1) * 750, 2000));
|
||||||
setTimeout(() => {
|
|
||||||
getArchive($target, url, false);
|
|
||||||
}, first ? 750 : 2000);
|
|
||||||
} else {
|
|
||||||
// We don't need to continue checking.
|
|
||||||
dropdownBtn.classList.remove('is-loading');
|
|
||||||
window.location.href = url;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch {
|
window.location.href = el.href; // the archive is ready, start real downloading
|
||||||
dropdownBtn.classList.remove('is-loading');
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
showErrorToast(`Failed to download the archive: ${e}`, {duration: 2500});
|
||||||
|
} finally {
|
||||||
|
targetLoading.classList.remove('is-loading', 'small-loading-icon');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function initRepoArchiveLinks() {
|
export function initRepoArchiveLinks() {
|
||||||
$('.archive-link').on('click', function (event) {
|
for (const el of document.querySelectorAll('a.archive-link[href]')) {
|
||||||
event.preventDefault();
|
el.addEventListener('click', onDownloadArchive);
|
||||||
const url = this.getAttribute('href');
|
}
|
||||||
if (!url) return;
|
|
||||||
getArchive($(event.target), url, true);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function initRepoCloneLink() {
|
export function initRepoCloneLink() {
|
||||||
|
Loading…
Reference in New Issue
Block a user