|
1 | 1 | import $ from 'jquery';
|
2 |
| -import {hideElem, showElem} from '../utils/dom.js'; |
| 2 | +import {hideElem, queryElems, showElem} from '../utils/dom.js'; |
3 | 3 | import {POST} from '../modules/fetch.js';
|
| 4 | +import {showErrorToast} from '../modules/toast.js'; |
| 5 | +import {sleep} from '../utils.js'; |
4 | 6 |
|
5 |
| -async function getArchive($target, url, first) { |
6 |
| - const dropdownBtn = $target[0].closest('.ui.dropdown.button') ?? $target[0].closest('.ui.dropdown.btn'); |
7 |
| - |
| 7 | +async function onDownloadArchive(e) { |
| 8 | + e.preventDefault(); |
| 9 | + // there are many places using the "archive-link", eg: the dropdown on the repo code page, the release list |
| 10 | + const el = e.target.closest('a.archive-link[href]'); |
| 11 | + const targetLoading = el.closest('.ui.dropdown') ?? el; |
| 12 | + targetLoading.classList.add('is-loading', 'loading-icon-2px'); |
8 | 13 | try {
|
9 |
| - dropdownBtn.classList.add('is-loading'); |
10 |
| - const response = await POST(url); |
11 |
| - if (response.status === 200) { |
12 |
| - const data = await response.json(); |
13 |
| - if (!data) { |
14 |
| - // XXX Shouldn't happen? |
15 |
| - dropdownBtn.classList.remove('is-loading'); |
16 |
| - return; |
17 |
| - } |
| 14 | + for (let tryCount = 0; ;tryCount++) { |
| 15 | + const response = await POST(el.href); |
| 16 | + if (!response.ok) throw new Error(`Invalid server response: ${response.status}`); |
18 | 17 |
|
19 |
| - if (!data.complete) { |
20 |
| - // Wait for only three quarters of a second initially, in case it's |
21 |
| - // quickly archived. |
22 |
| - setTimeout(() => { |
23 |
| - getArchive($target, url, false); |
24 |
| - }, first ? 750 : 2000); |
25 |
| - } else { |
26 |
| - // We don't need to continue checking. |
27 |
| - dropdownBtn.classList.remove('is-loading'); |
28 |
| - window.location.href = url; |
29 |
| - } |
| 18 | + const data = await response.json(); |
| 19 | + if (data.complete) break; |
| 20 | + await sleep(Math.min((tryCount + 1) * 750, 2000)); |
30 | 21 | }
|
31 |
| - } catch { |
32 |
| - dropdownBtn.classList.remove('is-loading'); |
| 22 | + window.location.href = el.href; // the archive is ready, start real downloading |
| 23 | + } catch (e) { |
| 24 | + console.error(e); |
| 25 | + showErrorToast(`Failed to download the archive: ${e}`, {duration: 2500}); |
| 26 | + } finally { |
| 27 | + targetLoading.classList.remove('is-loading', 'loading-icon-2px'); |
33 | 28 | }
|
34 | 29 | }
|
35 | 30 |
|
36 | 31 | export function initRepoArchiveLinks() {
|
37 |
| - $('.archive-link').on('click', function (event) { |
38 |
| - event.preventDefault(); |
39 |
| - const url = this.getAttribute('href'); |
40 |
| - if (!url) return; |
41 |
| - getArchive($(event.target), url, true); |
42 |
| - }); |
| 32 | + queryElems('a.archive-link[href]', (el) => el.addEventListener('click', onDownloadArchive)); |
43 | 33 | }
|
44 | 34 |
|
45 | 35 | export function initRepoCloneLink() {
|
|
0 commit comments