2024-12-11 15:54:42 +00:00
|
|
|
import {queryElems} from '../utils/dom.ts';
|
2024-07-07 15:32:30 +00:00
|
|
|
import {POST} from '../modules/fetch.ts';
|
|
|
|
import {showErrorToast} from '../modules/toast.ts';
|
|
|
|
import {sleep} from '../utils.ts';
|
2024-10-28 20:15:05 +00:00
|
|
|
import RepoActivityTopAuthors from '../components/RepoActivityTopAuthors.vue';
|
|
|
|
import {createApp} from 'vue';
|
2024-12-11 13:54:30 +00:00
|
|
|
import {toOriginUrl} from '../utils/url.ts';
|
|
|
|
import {createTippy} from '../modules/tippy.ts';
|
2022-01-28 21:00:11 +00:00
|
|
|
|
2024-04-18 16:45:50 +00:00
|
|
|
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');
|
2024-02-25 16:53:44 +00:00
|
|
|
try {
|
2024-04-18 16:45:50 +00:00
|
|
|
for (let tryCount = 0; ;tryCount++) {
|
|
|
|
const response = await POST(el.href);
|
|
|
|
if (!response.ok) throw new Error(`Invalid server response: ${response.status}`);
|
2021-10-16 17:28:04 +00:00
|
|
|
|
2024-04-18 16:45:50 +00:00
|
|
|
const data = await response.json();
|
|
|
|
if (data.complete) break;
|
|
|
|
await sleep(Math.min((tryCount + 1) * 750, 2000));
|
2024-02-25 16:53:44 +00:00
|
|
|
}
|
2024-04-18 16:45:50 +00:00
|
|
|
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');
|
2024-02-25 16:53:44 +00:00
|
|
|
}
|
2021-10-16 17:28:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function initRepoArchiveLinks() {
|
2024-11-08 06:04:24 +00:00
|
|
|
queryElems(document, 'a.archive-link[href]', (el) => el.addEventListener('click', onDownloadArchive));
|
2021-10-16 17:28:04 +00:00
|
|
|
}
|
|
|
|
|
2024-10-28 20:15:05 +00:00
|
|
|
export function initRepoActivityTopAuthorsChart() {
|
|
|
|
const el = document.querySelector('#repo-activity-top-authors-chart');
|
|
|
|
if (el) {
|
|
|
|
createApp(RepoActivityTopAuthors).mount(el);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-11 13:54:30 +00:00
|
|
|
function initCloneSchemeUrlSelection(parent: Element) {
|
|
|
|
const elCloneUrlInput = parent.querySelector<HTMLInputElement>('.repo-clone-url');
|
2022-03-29 03:21:30 +00:00
|
|
|
|
2024-12-11 13:54:30 +00:00
|
|
|
const tabSsh = parent.querySelector('.repo-clone-ssh');
|
|
|
|
const tabHttps = parent.querySelector('.repo-clone-https');
|
|
|
|
const updateClonePanelUi = function() {
|
|
|
|
const scheme = localStorage.getItem('repo-clone-protocol') || 'https';
|
|
|
|
const isSSH = scheme === 'ssh' && Boolean(tabSsh) || scheme !== 'ssh' && !tabHttps;
|
|
|
|
if (tabHttps) {
|
|
|
|
tabHttps.textContent = window.origin.split(':')[0].toUpperCase(); // show "HTTP" or "HTTPS"
|
|
|
|
tabHttps.classList.toggle('active', !isSSH);
|
|
|
|
}
|
|
|
|
if (tabSsh) {
|
|
|
|
tabSsh.classList.toggle('active', isSSH);
|
|
|
|
}
|
|
|
|
|
|
|
|
const tab = isSSH ? tabSsh : tabHttps;
|
|
|
|
if (!tab) return;
|
|
|
|
const link = toOriginUrl(tab.getAttribute('data-link'));
|
|
|
|
|
|
|
|
for (const el of document.querySelectorAll('.js-clone-url')) {
|
|
|
|
if (el.nodeName === 'INPUT') {
|
|
|
|
(el as HTMLInputElement).value = link;
|
|
|
|
} else {
|
|
|
|
el.textContent = link;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const el of parent.querySelectorAll<HTMLAnchorElement>('.js-clone-url-editor')) {
|
|
|
|
el.href = el.getAttribute('data-href-template').replace('{url}', encodeURIComponent(link));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
updateClonePanelUi();
|
2024-12-12 02:01:20 +00:00
|
|
|
// tabSsh or tabHttps might not both exist, eg: guest view, or one is disabled by the server
|
|
|
|
tabSsh?.addEventListener('click', () => {
|
2021-10-16 17:28:04 +00:00
|
|
|
localStorage.setItem('repo-clone-protocol', 'ssh');
|
2024-12-11 13:54:30 +00:00
|
|
|
updateClonePanelUi();
|
2021-10-16 17:28:04 +00:00
|
|
|
});
|
2024-12-12 02:01:20 +00:00
|
|
|
tabHttps?.addEventListener('click', () => {
|
2022-03-29 03:21:30 +00:00
|
|
|
localStorage.setItem('repo-clone-protocol', 'https');
|
2024-12-11 13:54:30 +00:00
|
|
|
updateClonePanelUi();
|
|
|
|
});
|
|
|
|
elCloneUrlInput.addEventListener('focus', () => {
|
|
|
|
elCloneUrlInput.select();
|
2021-10-16 17:28:04 +00:00
|
|
|
});
|
2024-12-11 13:54:30 +00:00
|
|
|
}
|
2022-03-29 03:21:30 +00:00
|
|
|
|
2024-12-11 13:54:30 +00:00
|
|
|
function initClonePanelButton(btn: HTMLButtonElement) {
|
|
|
|
const elPanel = btn.nextElementSibling;
|
2024-12-11 15:54:42 +00:00
|
|
|
// "init" must be before the "createTippy" otherwise the "tippy-target" will be removed from the document
|
|
|
|
initCloneSchemeUrlSelection(elPanel);
|
2024-12-11 13:54:30 +00:00
|
|
|
createTippy(btn, {
|
|
|
|
content: elPanel,
|
|
|
|
trigger: 'click',
|
|
|
|
placement: 'bottom-end',
|
|
|
|
interactive: true,
|
|
|
|
hideOnClick: true,
|
2021-10-16 17:28:04 +00:00
|
|
|
});
|
2024-12-11 13:54:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function initRepoCloneButtons() {
|
|
|
|
queryElems(document, '.js-btn-clone-panel', initClonePanelButton);
|
|
|
|
queryElems(document, '.clone-buttons-combo', initCloneSchemeUrlSelection);
|
2021-10-16 17:28:04 +00:00
|
|
|
}
|
|
|
|
|
2024-11-03 11:00:12 +00:00
|
|
|
export async function updateIssuesMeta(url, action, issue_ids, id) {
|
|
|
|
try {
|
|
|
|
const response = await POST(url, {data: new URLSearchParams({action, issue_ids, id})});
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error('Failed to update issues meta');
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
}
|